--- loncom/xml/lontable.test 2008/12/02 11:57:25 1.1 +++ loncom/xml/lontable.test 2008/12/09 11:50:08 1.2 @@ -3,7 +3,7 @@ # The LearningOnline Network with CAPA # Generating TeX tables. # -# $Id: lontable.test,v 1.1 2008/12/02 11:57:25 foxr Exp $ +# $Id: lontable.test,v 1.2 2008/12/09 11:50:08 foxr Exp $ # # # Copyright Michigan State University Board of Trustees @@ -52,7 +52,7 @@ use strict; use lontable; -use Test::More tests=>18; +use Test::More tests=>88; #------------------- Default Construction tests: --------------------------------- # Tests the getter forms of the configuration methods too: @@ -74,7 +74,7 @@ ok((scalar(@$rows) == 0), #--------------- Configured construction tests ----------------------------------- # -my $testobject = new Apache::lontable({alignment => 'right', +$testobject = new Apache::lontable({alignment => 'right', outer_border => 1, inner_border => 1, caption => 'Test caption', @@ -92,6 +92,8 @@ ok($testobject->theme() eq 'Houston', # 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 @@ -104,3 +106,211 @@ foreach my $method (@configmethods) { 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(); + +$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(); +$testobject->add_cell('ordinary cell'); +$testobject->add_cell('ordinary cell'); +$testobject->end_row(); + +$testobject->start_row(); +$testobject->add_cell('2 rows 1 col', {rowspan => 2}); +$testobject->add_cell('ordinary cell'); +$testobject->end_row(); + +$testobject->start_row(); +$testobject->add_cell('ordinary cell'); +$testobject->add_cell('ordinary cell'); +$testobject->add_cell('ordinary cell'); +$testobject->end_row(); + +# First of all the table should have figured out tere 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'); +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'} == 2, '4,4 colspan'); +ok($cells->[3]->{'contents'} eq 'ordinary cell', '4,4 contents');