Skip to content

Commit

Permalink
seperate out table tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jun 29, 2009
1 parent 07e79a9 commit 84d51a6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 95 deletions.
35 changes: 35 additions & 0 deletions test/active_record_table_test.rb
@@ -0,0 +1,35 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class Hirb::Helpers::ActiveRecordTableTest < Test::Unit::TestCase
context "activerecord table" do
test "with no select renders" do
expected_table = <<-TABLE.unindent
+-----+-------+
| age | name |
+-----+-------+
| 7 | rufus |
| 101 | alf |
+-----+-------+
2 rows in set
TABLE
@pets = [stub(:name=>'rufus', :age=>7, :attributes=>{"name"=>'rufus', 'age'=>7}, :class=>stub(:column_names=>%w{age name})),
stub(:name=>'alf', :age=>101)]
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
end

test "with select renders" do
expected_table = <<-TABLE.unindent
+-------+
| name |
+-------+
| rufus |
| alf |
+-------+
2 rows in set
TABLE
@pets = [stub(:name=>'rufus', :age=>7, :attributes=>{'name'=>'rufus'}, :class=>stub(:column_names=>%w{age name})),
stub(:name=>'alf', :age=>101)]
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
end
end
end
20 changes: 20 additions & 0 deletions test/auto_table_test.rb
@@ -0,0 +1,20 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class Hirb::Helpers::AutoTableTest < Test::Unit::TestCase
context "auto table" do
test "converts nonarrays to arrays and renders" do
require 'set'
expected_table = <<-TABLE.unindent
+-------+
| value |
+-------+
| 1 |
| 2 |
| 3 |
+-------+
3 rows in set
TABLE
Hirb::Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
end
end
end
49 changes: 49 additions & 0 deletions test/object_table_test.rb
@@ -0,0 +1,49 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class Hirb::Helpers::ObjectTableTest < Test::Unit::TestCase
context "object table" do
before(:all) {
@pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
}
test "renders" do
expected_table = <<-TABLE.unindent
+-------+-----+
| name | age |
+-------+-----+
| rufus | 7 |
| alf | 101 |
+-------+-----+
2 rows in set
TABLE
Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
end

test "with no options defaults to to_s field" do
expected_table = <<-TABLE.unindent
+-------+
| value |
+-------+
| rufus |
| alf |
+-------+
2 rows in set
TABLE
Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
end

test "renders simple arrays" do
expected_table = <<-TABLE.unindent
+-------+
| value |
+-------+
| 1 |
| 2 |
| 3 |
| 4 |
+-------+
4 rows in set
TABLE
Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
end
end
end
95 changes: 0 additions & 95 deletions test/table_test.rb
Expand Up @@ -250,101 +250,6 @@ def table(*args)
end
end

context "object table" do
before(:all) {
@pets = [stub(:name=>'rufus', :age=>7, :to_s=>'rufus'), stub(:name=>'alf', :age=>101, :to_s=>'alf')]
}
test "renders" do
expected_table = <<-TABLE.unindent
+-------+-----+
| name | age |
+-------+-----+
| rufus | 7 |
| alf | 101 |
+-------+-----+
2 rows in set
TABLE
Hirb::Helpers::ObjectTable.render(@pets, :fields=>[:name, :age]).should == expected_table
end

test "with no options defaults to to_s field" do
expected_table = <<-TABLE.unindent
+-------+
| value |
+-------+
| rufus |
| alf |
+-------+
2 rows in set
TABLE
Hirb::Helpers::ObjectTable.render(@pets).should == expected_table
end

test "renders simple arrays" do
expected_table = <<-TABLE.unindent
+-------+
| value |
+-------+
| 1 |
| 2 |
| 3 |
| 4 |
+-------+
4 rows in set
TABLE
Hirb::Helpers::ObjectTable.render([1,2,3,4]).should == expected_table
end
end

context "activerecord table" do
test "with no select renders" do
expected_table = <<-TABLE.unindent
+-----+-------+
| age | name |
+-----+-------+
| 7 | rufus |
| 101 | alf |
+-----+-------+
2 rows in set
TABLE
@pets = [stub(:name=>'rufus', :age=>7, :attributes=>{"name"=>'rufus', 'age'=>7}, :class=>stub(:column_names=>%w{age name})),
stub(:name=>'alf', :age=>101)]
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
end

test "with select renders" do
expected_table = <<-TABLE.unindent
+-------+
| name |
+-------+
| rufus |
| alf |
+-------+
2 rows in set
TABLE
@pets = [stub(:name=>'rufus', :age=>7, :attributes=>{'name'=>'rufus'}, :class=>stub(:column_names=>%w{age name})),
stub(:name=>'alf', :age=>101)]
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
end
end

context "auto table" do
test "converts nonarrays to arrays and renders" do
require 'set'
expected_table = <<-TABLE.unindent
+-------+
| value |
+-------+
| 1 |
| 2 |
| 3 |
+-------+
3 rows in set
TABLE
Hirb::Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
end
end

test "restrict_field_lengths ensures columns total doesn't exceed max width" do
@table = Hirb::Helpers::Table.new([{:f1=>'f1', :f2=>'2', :f3=>'3', :f4=>'4'}])
field_lengths = {:f1=>135, :f2=>45, :f3=>4, :f4=>55}
Expand Down

0 comments on commit 84d51a6

Please sign in to comment.