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, 4 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.

    1: #!/usr/bin/perl
    2: 
    3: # The LearningOnline Network with CAPA
    4: #  Generating TeX tables.
    5: #
    6: # $Id: lontable.test,v 1.4 2008/12/29 11:57:37 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({theme => "Dresden",
  223: 				    caption => "This is the table caption",
  224: 				    outer_border => 1,
  225: 				    inner_border => 1,
  226: 				    width        => '1.0\textwidth', 
  227: 				    alignment    => 'left'});
  228: 
  229: $testobject->start_row();
  230: $testobject->add_cell('2 cols 3 rows', {rowspan => 3, colspan => 2});
  231: $testobject->add_cell('2 cols 1 row', {colspan => 2});
  232: $testobject->end_row();
  233: 
  234: $testobject->start_row({default_halign => 'left'});
  235: $testobject->add_cell('ordinary cell');
  236: $testobject->add_cell('ordinary cell', {halign => 'center'});
  237: $testobject->end_row();
  238: 
  239: $testobject->start_row({default_halign => 'right'});
  240: $testobject->add_cell('2 rows 1 col', {rowspan => 2, halign => 'right'});
  241: $testobject->add_cell('ordinary cell');
  242: $testobject->end_row();
  243: 
  244: $testobject->start_row({default_halign => 'center'});
  245: $testobject->add_cell('ordinary cell');
  246: $testobject->add_cell('ordinary cell');
  247: $testobject->add_cell('ordinary cell', {halign => 'left'});
  248: $testobject->end_row();
  249: 
  250: #  First of all the table should have figured out there are 4 cols and 4 rows:
  251: 
  252: ok($testobject->get_object_attribute('column_count') == 4, 'col count with spans');
  253: 
  254: #  First row should be trivial 2 cells with the spans described.
  255: 
  256: my $row   = $testobject->get_row(0);
  257: my $cells = $row->{'cells'};           # Refs an array of cells.
  258: ok(scalar(@$cells) == 2,          ' 2 cell hashes in the row 0');
  259: ok($cells->[0]->{'rowspan'} == 3, '1,1 rowspan');
  260: ok($cells->[0]->{'colspan'} == 2, '1,1 colspan');
  261: ok($cells->[0]->{'contents'} eq '2 cols 3 rows', '1,1 contents');
  262: ok($cells->[1]->{'rowspan'} == 1,  '1 2 rowspan');
  263: ok($cells->[1]->{'colspan'} == 2,  '1 2 colspan');
  264: ok($cells->[1]->{'contents'} eq '2 cols 1 row', '1 2 contents');
  265: 
  266: # Second row, the first cell should carry down an empty cell from the row
  267: # above it (first cell), same colspan, rowspan of 2 now, then there should
  268: # be two ordinary cells:
  269: 
  270: $row   = $testobject->get_row(1);
  271: $cells = $row->{'cells'};
  272: 
  273: ok(scalar(@$cells) == 3,  ' 3 cell hashes in row 1');
  274: ok($cells->[0]->{'rowspan'} == 2, '2,1 rowspan carried from above');
  275: ok($cells->[0]->{'colspan'} == 2, '2,1 colspan carried from above');
  276: ok($cells->[0]->{'contents'} eq '', '2,1 should be empty');
  277: ok($cells->[1]->{'rowspan'} == 1, '2,2 rowspan');
  278: ok($cells->[1]->{'colspan'} == 1, '2,2 colspan');
  279: ok($cells->[1]->{'contents'} eq 'ordinary cell', '2,2 contents');
  280: ok($cells->[2]->{'rowspan'} == 1, '2,3 rowspan');
  281: ok($cells->[2]->{'colspan'} == 1, '2,3 colspan');
  282: ok($cells->[2]->{'contents'} eq 'ordinary cell','2,3 contents');
  283: 
  284: # 3'd row.  Shoould look a lot like the second row, but the second cell
  285: # has a rowspan of 2.
  286: 
  287: $row   = $testobject->get_row(2);
  288: $cells = $row->{'cells'};
  289: ok(scalar(@$cells) == 3, '3 cell hashes in row 3');
  290: ok($cells->[0]->{'rowspan'} == 1, '3,1 rowspan carried from above, down -> 1');
  291: ok($cells->[0]->{'colspan'} == 2, '3,1 colspan carried from above.');
  292: ok($cells->[0]->{'contents'} eq "" , '3,1 contetns empty');
  293: 
  294: ok($cells->[1]->{'rowspan'} == 2, '3,2, rowspan');
  295: ok($cells->[1]->{'colspan'} == 1, '3,2 colspan');
  296: ok($cells->[1]->{'contents'} eq '2 rows 1 col', '3,2 contents');
  297: 
  298: ok($cells->[2]->{'rowspan'} == 1, '3,3 rowspan');
  299: ok($cells->[2]->{'colspan'} == 1, '3,3 colspan');
  300: ok($cells->[2]->{'contents'} eq 'ordinary cell', '3,3 contents');
  301: 
  302: # last row, should have cell 3 carried down from above. all other cells
  303: # are ordinary.
  304: 
  305: $row   = $testobject->get_row(3);
  306: $cells = $row->{'cells'};
  307: ok(scalar(@$cells) == 4, '4 cell hashes in row 4');
  308: ok($cells->[0]->{'rowspan'} == 1, "4,1 rowsspan");
  309: ok($cells->[0]->{'colspan'} == 1, "4,1 colspan");
  310: ok($cells->[0]->{'contents'} eq 'ordinary cell', '4,1 contents');
  311: 
  312: ok($cells->[1]->{'rowspan'} == 1, '4,2 rowspan');
  313: ok($cells->[1]->{'colspan'} == 1, '4,2 colspan');
  314: my $contents = $cells->[1]->{'contents'};
  315: ok($cells->[1]->{'contents'} eq 'ordinary cell', '4,2, contents');
  316: 
  317: ok($cells->[2]->{'rowspan'} == 1, "4,3 rowspan carried down");
  318: ok($cells->[2]->{'colspan'} == 1, '4,3 colspan carried down');
  319: ok($cells->[2]->{'contents'} eq '', '4,3 contents empty');
  320: 
  321: ok($cells->[3]->{'rowspan'} == 1, "4,4 rowspan");
  322: ok($cells->[3]->{'colspan'} == 1, '4,4 colspan');
  323: ok($cells->[3]->{'contents'} eq 'ordinary cell', '4,4 contents');
  324: 
  325: 
  326: 
  327: my $table = $testobject->generate();
  328: $table->set_filename('table.tex');
  329: $table->generate();
  330: 
  331: 
  332: 

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