File:  [LON-CAPA] / loncom / xml / lontable.test
Revision 1.2: download - view: text, annotated - select for diffs
Tue Dec 9 11:50:08 2008 UTC (15 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
Many more tests.. not 100% handling the spans correctly yet.

    1: #!/usr/bin/perl
    2: 
    3: # The LearningOnline Network with CAPA
    4: #  Generating TeX tables.
    5: #
    6: # $Id: lontable.test,v 1.2 2008/12/09 11:50:08 foxr Exp $
    7: # 
    8: #
    9: # Copyright Michigan State University Board of Trustees
   10: #
   11: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   12: #
   13: # LON-CAPA is free software; you can redistribute it and/or modify
   14: # it under the terms of the GNU General Public License as published by
   15: # the Free Software Foundation; either version 2 of the License, or
   16: # (at your option) any later version.
   17: #
   18: # LON-CAPA is distributed in the hope that it will be useful,
   19: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   20: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   21: # GNU General Public License for more details.
   22: #
   23: # You should have received a copy of the GNU General Public License
   24: # along with LON-CAPA; if not, write to the Free Software
   25: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   26: #
   27: # /home/httpd/html/adm/gpl.txt
   28: #
   29: # http://www.lon-capa.org/
   30: ## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
   31: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
   32: # binary executable programs or libraries distributed by the 
   33: # Michigan State University (the "Licensee"), but any binaries so 
   34: # distributed are hereby licensed only for use in the context
   35: # of a program or computational system for which the Licensee is the 
   36: # primary author or distributor, and which performs substantial 
   37: # additional tasks beyond the translation of (La)TeX into HTML.
   38: # The C source of the Code may not be distributed by the Licensee
   39: # to any other parties under any circumstances.
   40: #
   41: 
   42: 
   43: # A TEST SUITE??? You've got to be kidding!!! LonCAPA don't have no
   44: # TEST SUITEs!!!
   45: #
   46: # Well lontable does and this is it.  The test suite ensures that
   47: # the lontable.pm module is able to create the data structures it
   48: # purports to create.  The suite also ensures that the correct
   49: # information is passed ot the LaTeX::Table class at generate
   50: # time.
   51: #
   52: 
   53: use strict;
   54: use lontable;
   55: use Test::More tests=>88;
   56: 
   57: #------------------- Default Construction tests: ---------------------------------
   58: #  Tests the getter forms of the configuration methods too:
   59: 
   60: my $testobject = new Apache::lontable();
   61: ok(($testobject->alignment() eq 'left'), "Default Construction - Correct alignment");
   62: ok(($testobject->table_border() == 0),   "Default Construction - Correct table border");
   63: ok(($testobject->cell_border()  == 0),   "Default Construction - Correct cell border");
   64: ok(($testobject->caption()  eq ''),      "Default Construction - Correct caption");
   65: ok(($testobject->theme() eq "Zurich"),   "Default Construction - Correct theme");
   66: ok(($testobject->get_object_attribute('column_count') == 0),
   67:                                          "Default Construction - zero columncount");
   68: ok((!$testobject->get_object_attribute('row_open')), 
   69:                                          "Default Construction - Row not open");
   70: my $rows = $testobject->get_object_attribute('rows');
   71: ok((scalar(@$rows) == 0),                'Default Construction - empty row array');
   72: 
   73: 
   74: #--------------- Configured construction tests -----------------------------------
   75: #
   76: 
   77: $testobject = new Apache::lontable({alignment    => 'right',
   78: 				       outer_border => 1,
   79: 				       inner_border => 1,
   80: 				       caption      => 'Test caption',
   81: 				       theme        => 'Houston'});
   82: ok($testobject->alignment() eq 'right',  'Configured Construction - Correct alignment');
   83: ok($testobject->table_border() == 1,     'Configured Construction - correct border');
   84: ok($testobject->cell_border() == 1,      'Configured Construction - correct cell border');
   85: ok($testobject->caption eq 'Test caption', 'Configured Construction - Correct caption');
   86: ok($testobject->theme() eq 'Houston',    'Confiugred construction - correct theme');
   87: 
   88: 
   89: #-------------- Test global configuration setters ----------------------------
   90: 
   91: # Each test starts with a fresh object:
   92: 
   93: # Table of methods to test...
   94: 
   95: $testobject = new Apache::lontable();
   96: 
   97: my @configmethods = ('alignment', 'table_border', 'cell_border', 'caption', 'theme');
   98: 
   99: # Table of parameters for each method and expected results from the getter
  100: 
  101: my @values        = ('center', '1', '1', "Testing", 'Miami');
  102: my $i = 0;
  103: foreach my $method (@configmethods) {
  104:     $testobject = new Apache::lontable();
  105:     $testobject->$method($values[$i]);
  106:     ok($testobject->$method() eq $values[$i], "Global Config: Testing $method as a setter");
  107:     $i++;
  108: }
  109: 
  110: #--------------- Test row management --------------------------------------
  111: 
  112: # start row adds a row to the table; in default config unless overridden.
  113: 
  114: $testobject = new Apache::lontable();
  115: 
  116: $testobject->start_row();	# Unconfigured row.
  117: ok($testobject->get_object_attribute('row_open') == 1, "One row added");
  118: $rows = $testobject->get_object_attribute('rows');
  119: ok(scalar(@$rows)  == 1,                      ' only one row');
  120: my $row   = $rows->[0];
  121: my $cells = $row->{'cells'};
  122: ok($row->{'default_halign'} eq 'left',         "default row halign");
  123: ok($row->{'default_valign'} eq 'top',          'default row valign');
  124: ok(scalar(@$cells) == 0,              'Initial cell count');
  125: 
  126: #  Start row with row open makes a second row:
  127: 
  128: $testobject->start_row();
  129: ok($testobject->get_object_attribute('row_open') == 1, "Two rows.. still open");
  130: $rows = $testobject->get_object_attribute('rows');
  131: ok(scalar(@$rows) == 2, 'two rows now');
  132: 
  133: # Creating row with configuration:
  134: 
  135: $testobject->start_row({'default_halign'  => "center",
  136: 			'default_valign'  => 'bottom'});
  137: $rows = $testobject->get_object_attribute('rows');
  138: $row  = $rows->[-1];		# Last row should be configured:
  139: ok($row->{'default_halign'} eq 'center',   "Overridden horiz align");
  140: ok($row->{'default_valign'} eq 'bottom',   'Overridden vert. align');
  141: 
  142: # end row says no row is open..we'll need to look at it again when we 
  143: # start adding cells, as it also manages the max cell count.
  144: 
  145: $testobject->end_row();
  146: ok($testobject->get_object_attribute('row_open') == 0, "Row closed");
  147: 
  148: 
  149: #-------------------- Cell management -------------------------------
  150: 
  151: $testobject = new Apache::lontable();
  152: $testobject->start_row();
  153: $testobject->add_cell("cell 0");
  154: $testobject->add_cell("cell 1");
  155: $testobject->end_row();
  156: 
  157: #  At this point we should have one row (that's already been tested).
  158: #  we should have max cell count of 2
  159: #  we should be able to see the cells we added with default values.
  160: 
  161: ok($testobject->get_object_attribute('column_count') == 2, 'max cells ok');
  162: $rows = $testobject->get_object_attribute('rows');
  163: $row  = $rows->[0];
  164: my $cols = $row->{'cells'};	# Reference to cell array
  165: ok(scalar(@$cols) == 2,   ' correct cell count');
  166: my $cell = $cols->[0];
  167: ok($cell->{'contents'} eq 'cell 0', 'Correct cell 0 contents');
  168: ok($cell->{'rowspan'}  == 1,        'Correct cell 0 rowspan');
  169: ok($cell->{'colspan'}  == 1,        'Correct cell 0 colspan');
  170: $cell = $cols->[1];
  171: ok($cell->{'contents'} eq 'cell 1', 'correct cell 1 contents');
  172: ok($cell->{'rowspan'} == 1,         'correct cell 1 rowspan');
  173: ok($cell->{'colspan'} == 1,         'correct cell 1 column span');
  174: 
  175: # Add a second row that has 3 columns and some rowspans.
  176: # - column_count -> 3
  177: # - the cells should have the correct rowspans/colspans.
  178: 
  179: $testobject->start_row();
  180: $testobject->add_cell("row2 cell 0", 
  181: 		      {'rowspan'  => 2});
  182: $testobject->add_cell('row2 cell 1',
  183: 		      {'rowspan'  => 3});
  184: $testobject->add_cell('row 2 cell 3');
  185: $testobject->end_row();
  186: 
  187: $row  = $rows->[1];
  188: $cols = $row->{'cells'};
  189: ok(scalar(@$cols) == 3, 'correct columnm count');
  190: ok($testobject->get_object_attribute('column_count') == 3, 'max cols ok');
  191: 
  192: $cell = $cols->[0];
  193: ok($cell->{'contents'} eq 'row2 cell 0',  'Contents 2,0');
  194: ok($cell->{'rowspan'} == 2, 'rowspand 2,0');
  195: ok($cell->{'colspan'} == 1, 'colspan 2,0');
  196: 
  197: $cell = $cols->[1];
  198: ok($cell->{'contents'} eq 'row2 cell 1',  'Contents 2,1');
  199: ok($cell->{'rowspan'} == 3, 'rowspand 2,1');
  200: ok($cell->{'colspan'} == 1, 'colspan 2,2');
  201: 
  202: $cell = $cols->[2];
  203: ok($cell->{'contents'} eq 'row 2 cell 3', "Contents 2,2");
  204: ok($cell->{'rowspan'} == 1, "Rowspan 2,2");
  205: ok($cell->{'colspan'} == 1, 'Colspan 2,2');
  206: 
  207: #--------------------------- Test colspans with row spans. ----------------------
  208: #
  209: # Make a table that looks like:  
  210: #
  211: #  +-------------------------+---------------------+
  212: #  |     Spans 2 cols, 3 rows| spans 2 cols 1 row  |
  213: #  |                         +-----------+---------+
  214: #  |                         |  span 1,1 | span 1 1|
  215: #  |                         +-----------+---------+
  216: #  |                         |2rows 1col | span 1 1|
  217: #  +----------+--------------+           +---------+
  218: #  | Span 1 1 | span 1 1     |           |span 1 1 |
  219: #  +----------+---------+----+-----------+---------+
  220: 
  221: 
  222: $testobject = new Apache::lontable();
  223: 
  224: $testobject->start_row();
  225: $testobject->add_cell('2 cols 3 rows', {rowspan => 3, colspan => 2});
  226: $testobject->add_cell('2 cols 1 row', {colspan => 2});
  227: $testobject->end_row();
  228: 
  229: $testobject->start_row();
  230: $testobject->add_cell('ordinary cell');
  231: $testobject->add_cell('ordinary cell');
  232: $testobject->end_row();
  233: 
  234: $testobject->start_row();
  235: $testobject->add_cell('2 rows 1 col', {rowspan => 2});
  236: $testobject->add_cell('ordinary cell');
  237: $testobject->end_row();
  238: 
  239: $testobject->start_row();
  240: $testobject->add_cell('ordinary cell');
  241: $testobject->add_cell('ordinary cell');
  242: $testobject->add_cell('ordinary cell');
  243: $testobject->end_row();
  244: 
  245: #  First of all the table should have figured out tere are 4 cols and 4 rows:
  246: 
  247: ok($testobject->get_object_attribute('column_count') == 4, 'col count with spans');
  248: 
  249: #  First row should be trivial 2 cells with the spans described.
  250: 
  251: my $row   = $testobject->get_row(0);
  252: my $cells = $row->{'cells'};           # Refs an array of cells.
  253: ok(scalar(@$cells) == 2,          ' 2 cell hashes in the row 0');
  254: ok($cells->[0]->{'rowspan'} == 3, '1,1 rowspan');
  255: ok($cells->[0]->{'colspan'} == 2, '1,1 colspan');
  256: ok($cells->[0]->{'contents'} eq '2 cols 3 rows', '1,1 contents');
  257: ok($cells->[1]->{'rowspan'} == 1,  '1 2 rowspan');
  258: ok($cells->[1]->{'colspan'} == 2,  '1 2 colspan');
  259: ok($cells->[1]->{'contents'} eq '2 cols 1 row', '1 2 contents');
  260: 
  261: # Second row, the first cell should carry down an empty cell from the row
  262: # above it (first cell), same colspan, rowspan of 2 now, then there should
  263: # be two ordinary cells:
  264: 
  265: $row   = $testobject->get_row(1);
  266: $cells = $row->{'cells'};
  267: ok(scalar(@$cells) == 3,  ' 3 cell hashes in row 1');
  268: ok($cells->[0]->{'rowspan'} == 2, '2,1 rowspan carried from above');
  269: ok($cells->[0]->{'colspan'} == 2, '2,1 colspan carried from above');
  270: ok($cells->[0]->{'contents'} eq '', '2,1 should be empty');
  271: ok($cells->[1]->{'rowspan'} == 1, '2,2 rowspan');
  272: ok($cells->[1]->{'colspan'} == 1, '2,2 colspan');
  273: ok($cells->[1]->{'contents'} eq 'ordinary cell', '2,2 contents');
  274: ok($cells->[2]->{'rowspan'} == 1, '2,3 rowspan');
  275: ok($cells->[2]->{'colspan'} == 1, '2,3 colspan');
  276: ok($cells->[2]->{'contents'} eq 'ordinary cell','2,3 contents');
  277: 
  278: # 3'd row.  Shoould look a lot like the second row, but the second cell
  279: # has a rowspan of 2.
  280: 
  281: $row   = $testobject->get_row(2);
  282: $cells = $row->{'cells'};
  283: ok(scalar(@$cells) == 3, '3 cell hashes in row 3');
  284: ok($cells->[0]->{'rowspan'} == 1, '3,1 rowspan carried from above, down -> 1');
  285: ok($cells->[0]->{'colspan'} == 2, '3,1 colspan carried from above.');
  286: ok($cells->[0]->{'contents'} eq "" , '3,1 contetns empty');
  287: 
  288: ok($cells->[1]->{'rowspan'} == 2, '3,2, rowspan');
  289: ok($cells->[1]->{'colspan'} == 1, '3,2 colspan');
  290: ok($cells->[1]->{'contents'} eq '2 rows 1 col', '3,2 contents');
  291: 
  292: ok($cells->[2]->{'rowspan'} == 1, '3,3 rowspan');
  293: ok($cells->[2]->{'colspan'} == 1, '3,3 colspan');
  294: ok($cells->[2]->{'contents'} eq 'ordinary cell', '3,3 contents');
  295: 
  296: # last row, should have cell 3 carried down from above. all other cells
  297: # are ordinary.
  298: 
  299: $row   = $testobject->get_row(3);
  300: $cells = $row->{'cells'};
  301: ok(scalar(@$cells) == 4, '4 cell hashes in row 4');
  302: ok($cells->[0]->{'rowspan'} == 1, "4,1 rowsspan");
  303: ok($cells->[0]->{'colspan'} == 1, "4,1 colspan");
  304: ok($cells->[0]->{'contents'} eq 'ordinary cell', '4,1 contents');
  305: 
  306: ok($cells->[1]->{'rowspan'} == 1, '4,2 rowspan');
  307: ok($cells->[1]->{'colspan'} == 1, '4,2 colspan');
  308: ok($cells->[1]->{'contents'} eq 'ordinary cell', '4,2, contents');
  309: 
  310: ok($cells->[2]->{'rowspan'} == 1, "4,3 rowspan carried down");
  311: ok($cells->[2]->{'colspan'} == 1, '4,3 colspan carried down');
  312: ok($cells->[2]->{'contents'} eq '', '4,3 contents empty');
  313: 
  314: ok($cells->[3]->{'rowspan'} == 1, "4,4 rowspan");
  315: ok($cells->[3]->{'colspan'} == 2, '4,4 colspan');
  316: ok($cells->[3]->{'contents'} eq 'ordinary cell', '4,4 contents');

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