File:  [LON-CAPA] / loncom / xml / lontable.test
Revision 1.4: download - view: text, annotated - select for diffs
Mon Dec 29 11:57:37 2008 UTC (15 years, 3 months ago) by foxr
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Add code to support horizontal alignment, correct bordering, and
table width.

#!/usr/bin/perl

# The LearningOnline Network with CAPA
#  Generating TeX tables.
#
# $Id: lontable.test,v 1.4 2008/12/29 11:57:37 foxr Exp $
# 
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
# TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
# binary executable programs or libraries distributed by the 
# Michigan State University (the "Licensee"), but any binaries so 
# distributed are hereby licensed only for use in the context
# of a program or computational system for which the Licensee is the 
# primary author or distributor, and which performs substantial 
# additional tasks beyond the translation of (La)TeX into HTML.
# The C source of the Code may not be distributed by the Licensee
# to any other parties under any circumstances.
#


# A TEST SUITE??? You've got to be kidding!!! LonCAPA don't have no
# TEST SUITEs!!!
#
# Well lontable does and this is it.  The test suite ensures that
# the lontable.pm module is able to create the data structures it
# purports to create.  The suite also ensures that the correct
# information is passed ot the LaTeX::Table class at generate
# time.
#

use strict;
use lontable;
use Test::More tests=>88;

#------------------- Default Construction tests: ---------------------------------
#  Tests the getter forms of the configuration methods too:

my $testobject = new Apache::lontable();
ok(($testobject->alignment() eq 'left'), "Default Construction - Correct alignment");
ok(($testobject->table_border() == 0),   "Default Construction - Correct table border");
ok(($testobject->cell_border()  == 0),   "Default Construction - Correct cell border");
ok(($testobject->caption()  eq ''),      "Default Construction - Correct caption");
ok(($testobject->theme() eq "Zurich"),   "Default Construction - Correct theme");
ok(($testobject->get_object_attribute('column_count') == 0),
                                         "Default Construction - zero columncount");
ok((!$testobject->get_object_attribute('row_open')), 
                                         "Default Construction - Row not open");
my $rows = $testobject->get_object_attribute('rows');
ok((scalar(@$rows) == 0),                'Default Construction - empty row array');


#--------------- Configured construction tests -----------------------------------
#

$testobject = new Apache::lontable({alignment    => 'right',
				       outer_border => 1,
				       inner_border => 1,
				       caption      => 'Test caption',
				       theme        => 'Houston'});
ok($testobject->alignment() eq 'right',  'Configured Construction - Correct alignment');
ok($testobject->table_border() == 1,     'Configured Construction - correct border');
ok($testobject->cell_border() == 1,      'Configured Construction - correct cell border');
ok($testobject->caption eq 'Test caption', 'Configured Construction - Correct caption');
ok($testobject->theme() eq 'Houston',    'Confiugred construction - correct theme');


#-------------- Test global configuration setters ----------------------------

# Each test starts with a fresh object:

# Table of methods to test...

$testobject = new Apache::lontable();

my @configmethods = ('alignment', 'table_border', 'cell_border', 'caption', 'theme');

# Table of parameters for each method and expected results from the getter

my @values        = ('center', '1', '1', "Testing", 'Miami');
my $i = 0;
foreach my $method (@configmethods) {
    $testobject = new Apache::lontable();
    $testobject->$method($values[$i]);
    ok($testobject->$method() eq $values[$i], "Global Config: Testing $method as a setter");
    $i++;
}

#--------------- Test row management --------------------------------------

# start row adds a row to the table; in default config unless overridden.

$testobject = new Apache::lontable();

$testobject->start_row();	# Unconfigured row.
ok($testobject->get_object_attribute('row_open') == 1, "One row added");
$rows = $testobject->get_object_attribute('rows');
ok(scalar(@$rows)  == 1,                      ' only one row');
my $row   = $rows->[0];
my $cells = $row->{'cells'};
ok($row->{'default_halign'} eq 'left',         "default row halign");
ok($row->{'default_valign'} eq 'top',          'default row valign');
ok(scalar(@$cells) == 0,              'Initial cell count');

#  Start row with row open makes a second row:

$testobject->start_row();
ok($testobject->get_object_attribute('row_open') == 1, "Two rows.. still open");
$rows = $testobject->get_object_attribute('rows');
ok(scalar(@$rows) == 2, 'two rows now');

# Creating row with configuration:

$testobject->start_row({'default_halign'  => "center",
			'default_valign'  => 'bottom'});
$rows = $testobject->get_object_attribute('rows');
$row  = $rows->[-1];		# Last row should be configured:
ok($row->{'default_halign'} eq 'center',   "Overridden horiz align");
ok($row->{'default_valign'} eq 'bottom',   'Overridden vert. align');

# end row says no row is open..we'll need to look at it again when we 
# start adding cells, as it also manages the max cell count.

$testobject->end_row();
ok($testobject->get_object_attribute('row_open') == 0, "Row closed");


#-------------------- Cell management -------------------------------

$testobject = new Apache::lontable();
$testobject->start_row();
$testobject->add_cell("cell 0");
$testobject->add_cell("cell 1");
$testobject->end_row();

#  At this point we should have one row (that's already been tested).
#  we should have max cell count of 2
#  we should be able to see the cells we added with default values.

ok($testobject->get_object_attribute('column_count') == 2, 'max cells ok');
$rows = $testobject->get_object_attribute('rows');
$row  = $rows->[0];
my $cols = $row->{'cells'};	# Reference to cell array
ok(scalar(@$cols) == 2,   ' correct cell count');
my $cell = $cols->[0];
ok($cell->{'contents'} eq 'cell 0', 'Correct cell 0 contents');
ok($cell->{'rowspan'}  == 1,        'Correct cell 0 rowspan');
ok($cell->{'colspan'}  == 1,        'Correct cell 0 colspan');
$cell = $cols->[1];
ok($cell->{'contents'} eq 'cell 1', 'correct cell 1 contents');
ok($cell->{'rowspan'} == 1,         'correct cell 1 rowspan');
ok($cell->{'colspan'} == 1,         'correct cell 1 column span');

# Add a second row that has 3 columns and some rowspans.
# - column_count -> 3
# - the cells should have the correct rowspans/colspans.

$testobject->start_row();
$testobject->add_cell("row2 cell 0", 
		      {'rowspan'  => 2});
$testobject->add_cell('row2 cell 1',
		      {'rowspan'  => 3});
$testobject->add_cell('row 2 cell 3');
$testobject->end_row();

$row  = $rows->[1];
$cols = $row->{'cells'};
ok(scalar(@$cols) == 3, 'correct columnm count');
ok($testobject->get_object_attribute('column_count') == 3, 'max cols ok');

$cell = $cols->[0];
ok($cell->{'contents'} eq 'row2 cell 0',  'Contents 2,0');
ok($cell->{'rowspan'} == 2, 'rowspand 2,0');
ok($cell->{'colspan'} == 1, 'colspan 2,0');

$cell = $cols->[1];
ok($cell->{'contents'} eq 'row2 cell 1',  'Contents 2,1');
ok($cell->{'rowspan'} == 3, 'rowspand 2,1');
ok($cell->{'colspan'} == 1, 'colspan 2,2');

$cell = $cols->[2];
ok($cell->{'contents'} eq 'row 2 cell 3', "Contents 2,2");
ok($cell->{'rowspan'} == 1, "Rowspan 2,2");
ok($cell->{'colspan'} == 1, 'Colspan 2,2');

#--------------------------- Test colspans with row spans. ----------------------
#
# Make a table that looks like:  
#
#  +-------------------------+---------------------+
#  |     Spans 2 cols, 3 rows| spans 2 cols 1 row  |
#  |                         +-----------+---------+
#  |                         |  span 1,1 | span 1 1|
#  |                         +-----------+---------+
#  |                         |2rows 1col | span 1 1|
#  +----------+--------------+           +---------+
#  | Span 1 1 | span 1 1     |           |span 1 1 |
#  +----------+---------+----+-----------+---------+


$testobject = new Apache::lontable({theme => "Dresden",
				    caption => "This is the table caption",
				    outer_border => 1,
				    inner_border => 1,
				    width        => '1.0\textwidth', 
				    alignment    => 'left'});

$testobject->start_row();
$testobject->add_cell('2 cols 3 rows', {rowspan => 3, colspan => 2});
$testobject->add_cell('2 cols 1 row', {colspan => 2});
$testobject->end_row();

$testobject->start_row({default_halign => 'left'});
$testobject->add_cell('ordinary cell');
$testobject->add_cell('ordinary cell', {halign => 'center'});
$testobject->end_row();

$testobject->start_row({default_halign => 'right'});
$testobject->add_cell('2 rows 1 col', {rowspan => 2, halign => 'right'});
$testobject->add_cell('ordinary cell');
$testobject->end_row();

$testobject->start_row({default_halign => 'center'});
$testobject->add_cell('ordinary cell');
$testobject->add_cell('ordinary cell');
$testobject->add_cell('ordinary cell', {halign => 'left'});
$testobject->end_row();

#  First of all the table should have figured out there are 4 cols and 4 rows:

ok($testobject->get_object_attribute('column_count') == 4, 'col count with spans');

#  First row should be trivial 2 cells with the spans described.

my $row   = $testobject->get_row(0);
my $cells = $row->{'cells'};           # Refs an array of cells.
ok(scalar(@$cells) == 2,          ' 2 cell hashes in the row 0');
ok($cells->[0]->{'rowspan'} == 3, '1,1 rowspan');
ok($cells->[0]->{'colspan'} == 2, '1,1 colspan');
ok($cells->[0]->{'contents'} eq '2 cols 3 rows', '1,1 contents');
ok($cells->[1]->{'rowspan'} == 1,  '1 2 rowspan');
ok($cells->[1]->{'colspan'} == 2,  '1 2 colspan');
ok($cells->[1]->{'contents'} eq '2 cols 1 row', '1 2 contents');

# Second row, the first cell should carry down an empty cell from the row
# above it (first cell), same colspan, rowspan of 2 now, then there should
# be two ordinary cells:

$row   = $testobject->get_row(1);
$cells = $row->{'cells'};

ok(scalar(@$cells) == 3,  ' 3 cell hashes in row 1');
ok($cells->[0]->{'rowspan'} == 2, '2,1 rowspan carried from above');
ok($cells->[0]->{'colspan'} == 2, '2,1 colspan carried from above');
ok($cells->[0]->{'contents'} eq '', '2,1 should be empty');
ok($cells->[1]->{'rowspan'} == 1, '2,2 rowspan');
ok($cells->[1]->{'colspan'} == 1, '2,2 colspan');
ok($cells->[1]->{'contents'} eq 'ordinary cell', '2,2 contents');
ok($cells->[2]->{'rowspan'} == 1, '2,3 rowspan');
ok($cells->[2]->{'colspan'} == 1, '2,3 colspan');
ok($cells->[2]->{'contents'} eq 'ordinary cell','2,3 contents');

# 3'd row.  Shoould look a lot like the second row, but the second cell
# has a rowspan of 2.

$row   = $testobject->get_row(2);
$cells = $row->{'cells'};
ok(scalar(@$cells) == 3, '3 cell hashes in row 3');
ok($cells->[0]->{'rowspan'} == 1, '3,1 rowspan carried from above, down -> 1');
ok($cells->[0]->{'colspan'} == 2, '3,1 colspan carried from above.');
ok($cells->[0]->{'contents'} eq "" , '3,1 contetns empty');

ok($cells->[1]->{'rowspan'} == 2, '3,2, rowspan');
ok($cells->[1]->{'colspan'} == 1, '3,2 colspan');
ok($cells->[1]->{'contents'} eq '2 rows 1 col', '3,2 contents');

ok($cells->[2]->{'rowspan'} == 1, '3,3 rowspan');
ok($cells->[2]->{'colspan'} == 1, '3,3 colspan');
ok($cells->[2]->{'contents'} eq 'ordinary cell', '3,3 contents');

# last row, should have cell 3 carried down from above. all other cells
# are ordinary.

$row   = $testobject->get_row(3);
$cells = $row->{'cells'};
ok(scalar(@$cells) == 4, '4 cell hashes in row 4');
ok($cells->[0]->{'rowspan'} == 1, "4,1 rowsspan");
ok($cells->[0]->{'colspan'} == 1, "4,1 colspan");
ok($cells->[0]->{'contents'} eq 'ordinary cell', '4,1 contents');

ok($cells->[1]->{'rowspan'} == 1, '4,2 rowspan');
ok($cells->[1]->{'colspan'} == 1, '4,2 colspan');
my $contents = $cells->[1]->{'contents'};
ok($cells->[1]->{'contents'} eq 'ordinary cell', '4,2, contents');

ok($cells->[2]->{'rowspan'} == 1, "4,3 rowspan carried down");
ok($cells->[2]->{'colspan'} == 1, '4,3 colspan carried down');
ok($cells->[2]->{'contents'} eq '', '4,3 contents empty');

ok($cells->[3]->{'rowspan'} == 1, "4,4 rowspan");
ok($cells->[3]->{'colspan'} == 1, '4,4 colspan');
ok($cells->[3]->{'contents'} eq 'ordinary cell', '4,4 contents');



my $table = $testobject->generate();
$table->set_filename('table.tex');
$table->generate();




FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>