test/unit/row_test.rb in table_helper-0.1.0 vs test/unit/row_test.rb in table_helper-0.2.0

- old
+ new

@@ -1,13 +1,22 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper') +class RowParent < TableHelper::HtmlElement + attr_reader :table + + def initialize(table = TableHelper::CollectionTable.new([])) + @table = table + end +end + class RowByDefaultTest < Test::Unit::TestCase def setup - @row = TableHelper::Row.new + @parent = RowParent.new + @row = TableHelper::Row.new(@parent) end - def test_should_not_set_any_html_options + def test_should_not_have_class assert_nil @row[:class] end def test_should_not_have_any_cells assert @row.cells.empty? @@ -18,74 +27,71 @@ end def test_should_have_a_builder assert_not_nil @row.builder end -end - -class RowTest < Test::Unit::TestCase - def setup - @row = TableHelper::Row.new - end - def test_should_create_cell_reader_after_building_cell - @row.cell :name - assert_nothing_raised {@row.builder.name} - assert_instance_of TableHelper::Cell, @row.builder.name + def test_should_have_a_parent + assert_equal @parent, @row.parent end - def test_should_use_cell_name_for_class_name - @row.cell :name - assert_equal 'name', @row.cells['name'][:class] + def test_should_have_a_table + assert_equal @parent.table, @row.table end - def test_should_sanitize_cell_name_for_reader_method - @row.cell 'the-name' - - @row.builder.the_name - assert_nothing_raised {@row.builder.the_name} - assert_instance_of TableHelper::Cell, @row.builder.the_name + def test_should_be_empty + assert @row.empty? end - - def test_should_use_unsanitized_cell_name_for_cell_class - @row.cell 'the-name' - assert_equal ['the-name'], @row.cell_names - assert_equal 'the-name', @row.cells['the-name'][:class] - end - - def test_should_allow_html_options_when_building_cell - @row.cell :name, 'Name', :class => 'pretty' - - assert_equal 'name pretty', @row.cells['name'][:class] - end end class RowWithoutCellsTest < Test::Unit::TestCase def setup - @row = TableHelper::Row.new + @row = TableHelper::Row.new(RowParent.new) end def test_should_be_able_to_clear_cells @row.clear assert_equal [], @row.cell_names end + def test_should_be_empty + assert @row.empty? + end + def test_should_build_html assert_equal '<tr></tr>', @row.html end end class RowWithCellsTest < Test::Unit::TestCase def setup - @row = TableHelper::Row.new - @row.cell :name + @row = TableHelper::Row.new(RowParent.new) + @cell = @row.cell :name end + def test_should_create_cell_reader + assert_nothing_raised {@row.builder.name} + assert_equal @cell, @row.builder.name + end + + def test_should_use_cell_name_for_class_name + assert_equal 'name', @cell[:class] + end + + def test_should_have_cells + expected = {'name' => @cell} + assert_equal expected, @row.cells + end + def test_should_have_cell_names assert_equal ['name'], @row.cell_names end + def test_should_not_be_empty + assert !@row.empty? + end + def test_should_be_able_to_clear_existing_cells @row.clear assert_equal [], @row.cell_names end @@ -93,40 +99,79 @@ def test_should_build_html assert_equal '<tr><td class="name">Name</td></tr>', @row.html end def test_should_create_new_cell_if_cell_already_exists - old_cell = @row.cells['name'] - - @row.cell :name - assert_not_same old_cell, @row.cells['name'] + new_cell = @row.cell :name + assert_not_same new_cell, @cell end - def test_should_be_able_to_read_cell_after_clearing + def test_should_be_able_to_recreate_cell_after_clearing @row.clear @row.cell :name assert_equal ['name'], @row.cell_names assert_nothing_raised {@row.builder.name} end + + def test_should_allow_html_options + @row.clear + cell = @row.cell :name, 'Name', :class => 'pretty' + + assert_equal 'pretty name', cell[:class] + end end class RowWithMultipleCellsTest < Test::Unit::TestCase def setup - @row = TableHelper::Row.new - @row.cell :name - @row.cell :location + @row = TableHelper::Row.new(RowParent.new) + @name = @row.cell :name + @location = @row.cell :location end + def test_should_have_cells + expected = {'name' => @name, 'location' => @location} + assert_equal expected, @row.cells + end + + def test_should_have_cell_names + assert_equal ['location', 'name'], @row.cell_names.sort + end + def test_should_build_html assert_equal '<tr><td class="name">Name</td><td class="location">Location</td></tr>', @row.html end end +class RowWithUnconventionalCellNamesTest < Test::Unit::TestCase + def setup + @row = TableHelper::Row.new(RowParent.new) + @cell = @row.cell 'the-name' + end + + def test_should_have_cells + expected = {'the-name' => @cell} + assert_equal expected, @row.cells + end + + def test_should_have_cell_names + assert_equal ['the-name'], @row.cell_names + end + + def test_should_use_sanitized_name_for_reader_method + assert_nothing_raised {@row.builder.the_name} + assert_equal @cell, @row.builder.the_name + end + + def test_should_use_unsanitized_name_for_class + assert_equal 'the-name', @cell[:class] + end +end + class RowWithConflictingCellNamesTest < Test::Unit::TestCase def setup - @row = TableHelper::Row.new + @row = TableHelper::Row.new(RowParent.new) @row.cell :id end def test_should_be_able_to_read_cell assert_instance_of TableHelper::Cell, @row.cells['id'] @@ -137,7 +182,23 @@ assert_instance_of TableHelper::Cell, @row.cells['id'] end def test_should_be_able_to_clear assert_nothing_raised {@row.clear} + end +end + +class RowWithTableObjectNameTest < Test::Unit::TestCase + def setup + table = TableHelper::CollectionTable.new([], Object) + @row = TableHelper::Row.new(RowParent.new(table)) + @cell = @row.cell :id + end + + def test_should_not_have_class + assert_nil @row[:class] + end + + def test_should_namespace_cell + assert_equal 'object-id', @cell[:class] end end