html-table 1.6.3 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,101 @@
1
+ ###############################################################################
2
+ # head_spec.rb
3
+ #
4
+ # Test suite for the described_class class. The Table::Head class is a singleton
5
+ # class, so we have to take extra measures to ensure that a fresh instance
6
+ # is created between tests.
7
+ ###############################################################################
8
+ require 'rspec'
9
+ require 'html/table'
10
+
11
+ #####################################################################
12
+ # Ensure that a fresh instance of described_class is used between tests
13
+ # by calling 'refresh' in the 'teardown' method.
14
+ #####################################################################
15
+ class HTML::Table::Head
16
+ private
17
+ def refresh
18
+ @@head = nil
19
+ end
20
+ end
21
+
22
+ RSpec.describe HTML::Table::Head do
23
+ before do
24
+ @table = HTML::Table.new
25
+ @thead = described_class.create
26
+ end
27
+
28
+ example "constructor" do
29
+ expect{ described_class.create }.not_to raise_error
30
+ expect{ described_class.create("foo") }.not_to raise_error
31
+ expect{ described_class.create(1) }.not_to raise_error
32
+ expect{ described_class.create(%w/foo bar baz/) }.not_to raise_error
33
+ expect{ described_class.create([1,2,3]) }.not_to raise_error
34
+ expect{ described_class.create([[1,2,3],["foo","bar"]]) }.not_to raise_error
35
+ end
36
+
37
+ example "basic" do
38
+ html = "<thead></thead>"
39
+ expect(@thead.html.gsub(/\s{2,}|\n/, '')).to eq(html)
40
+ end
41
+
42
+ example "end_tags" do
43
+ expect(described_class).to respond_to(:end_tags?)
44
+ expect(described_class).to respond_to(:end_tags=)
45
+ expect{ described_class.end_tags? }.not_to raise_error
46
+ expect{ described_class.end_tags = true }.not_to raise_error
47
+ expect{ described_class.end_tags = "foo" }.to raise_error(HTML::Mixin::StrongTyping::ArgumentTypeError)
48
+ end
49
+
50
+ example "with_attributes" do
51
+ html = "<thead align='left' char='x'></thead>"
52
+ @thead.align = "left"
53
+ @thead.char = 'x'
54
+ expect(@thead.html.gsub(/\s{2,}|\n/, '')).to eq(html)
55
+ end
56
+
57
+ example "push_single_row" do
58
+ html = "<thead><tr><td>test</td></tr></thead>"
59
+ @thead.push HTML::Table::Row.new{|r| r.content = "test"}
60
+ expect(@thead.html.gsub(/\s{2,}|\n/, '')).to eq(html)
61
+ end
62
+
63
+ example "push_multiple_rows" do
64
+ html = "<thead><tr><td>test</td></tr><tr><td>foo</td></tr></thead>"
65
+ r1 = HTML::Table::Row.new("test")
66
+ r2 = HTML::Table::Row.new("foo")
67
+ @thead.push(r1, r2)
68
+ expect(@thead.html.gsub(/\s{2,}|\n/, '')).to eq(html)
69
+ end
70
+
71
+ example "add_content_directly" do
72
+ html = "<thead><tr><td>hello</td><td>world</td></tr></thead>"
73
+ @thead.content = "hello","world"
74
+ expect(@thead.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
75
+ end
76
+
77
+ example "add_content_in_constructor" do
78
+ html = "<thead><tr><td>hello</td><td>world</td></tr></thead>"
79
+ @thead.send(:refresh)
80
+ @thead = described_class.create(["hello","world"])
81
+ expect(@thead.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
82
+ end
83
+
84
+ example "configure_column" do
85
+ html = "<thead><tr><td>hello</td><td abbr='test' width=3 nowrap>world"
86
+ html += "</td></tr></thead>"
87
+ @thead.content = "hello","world"
88
+ @thead.configure(0,1){ |d|
89
+ d.abbr = 'test'
90
+ d.width = 3
91
+ d.nowrap = true
92
+ }
93
+ expect(@thead.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
94
+ end
95
+
96
+ after do
97
+ @table = nil
98
+ @thead.send(:refresh)
99
+ @thead = nil
100
+ end
101
+ end
@@ -0,0 +1,72 @@
1
+ #####################################################
2
+ # header_spec.rb
3
+ #
4
+ # Test suite for the HTML::Table::Row::Header class.
5
+ #####################################################
6
+ require 'rspec'
7
+ require 'html/table'
8
+
9
+ RSpec.describe HTML::Table::Row::Header do
10
+ before do
11
+ @theader = described_class.new
12
+ end
13
+
14
+ example "basic" do
15
+ html = "<th></th>"
16
+ expect(@theader.html.gsub(/\s+/, '')).to eq(html)
17
+ end
18
+
19
+ example "constructor" do
20
+ expect{ described_class.new }.not_to raise_error
21
+ expect{ described_class.new("foo") }.not_to raise_error
22
+ expect{ described_class.new(1) }.not_to raise_error
23
+ expect{ described_class.new(%w/foo bar baz/) }.not_to raise_error
24
+ expect{ described_class.new([1,2,3]) }.not_to raise_error
25
+ expect{ described_class.new([[1,2,3],["foo","bar"]]) }.not_to raise_error
26
+ end
27
+
28
+ example "with_attributes" do
29
+ html = "<th align='left' colspan=3 nowrap></th>"
30
+ @theader.align = 'left'
31
+ @theader.colspan = 3
32
+ @theader.nowrap = true
33
+ expect(@theader.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
34
+ end
35
+
36
+ example "configure_not_allowed" do
37
+ expect{ @theader.configure }.to raise_error(NoMethodError)
38
+ end
39
+
40
+ example "add_content" do
41
+ html = "<th>hello world</th>"
42
+ @theader.content = "hello world"
43
+ expect(@theader.html.gsub(/\s{2,}/, '')).to eq(html)
44
+ end
45
+
46
+ example "add_multiple_content_items" do
47
+ html = "<th>hello world</th>"
48
+ @theader.content = "hello"," world"
49
+ expect(@theader.html.gsub(/\s{2,}/, '')).to eq(html)
50
+ end
51
+
52
+ example "add_content_in_constructor" do
53
+ html = "<th>hello world</th>"
54
+ @theader = described_class.new("hello world")
55
+ expect(@theader.html.gsub(/\s{2,}/, '')).to eq(html)
56
+ end
57
+
58
+ example "indent_level" do
59
+ expect(described_class).to respond_to(:indent_level)
60
+ expect(described_class).to respond_to(:indent_level=)
61
+ expect{ described_class.indent_level = "foo" }.to raise_error(ArgumentTypeError)
62
+ expect{ described_class.indent_level = 6 }.not_to raise_error
63
+ end
64
+
65
+ example "end_tags" do
66
+ expect(described_class).to respond_to(:end_tags?)
67
+ expect(described_class).to respond_to(:end_tags=)
68
+ expect{ described_class.end_tags = "foo" }.to raise_error(ArgumentTypeError)
69
+ expect{ described_class.end_tags = 1 }.to raise_error(ArgumentTypeError)
70
+ expect{ described_class.end_tags = true }.not_to raise_error
71
+ end
72
+ end
@@ -0,0 +1,32 @@
1
+ ############################################################################
2
+ # html_handler_spec.rb
3
+ #
4
+ # Test suite for the HtmlHandler module. For these tests, we'll use an
5
+ # instance of the Table class where the module has been mixed in.
6
+ ############################################################################
7
+ require 'rspec'
8
+ require 'html/table'
9
+
10
+ RSpec.describe HTML::Mixin::HtmlHandler do
11
+ before do
12
+ @table = HTML::Table.new(["foo",1,"bar"])
13
+ end
14
+
15
+ example "html" do
16
+ expect(@table).to respond_to(:html)
17
+ expect{ @table.html }.not_to raise_error
18
+ expect{ @table.html = "foo" }.to raise_error(NoMethodError)
19
+ expect( @table.html).to be_kind_of(String)
20
+ expect( @table.html.length > 0).to eq(true)
21
+ end
22
+
23
+ example "modify_html" do
24
+ expect{ @table.send(:modify_html) }.to raise_error(ArgumentError)
25
+ expect{ @table.send(:modify_html,"nowrap") }.not_to raise_error
26
+ expect{ @table.send(:modify_html,"align","top") }.not_to raise_error
27
+ expect{
28
+ @table.send(:modify_html,"align","top")
29
+ @table.send(:modify_html,"align","top")
30
+ }.not_to raise_error
31
+ end
32
+ end
@@ -0,0 +1,136 @@
1
+ ############################################
2
+ # row_spec.rb
3
+ #
4
+ # Specs for the Table::Row class.
5
+ ############################################
6
+ require 'rspec'
7
+ require 'html/table'
8
+ include HTML
9
+
10
+ RSpec.describe HTML::Table::Row do
11
+ before do
12
+ @trow = described_class.new
13
+ end
14
+
15
+ example "constructor" do
16
+ expect{ Table::Row.new }.not_to raise_error
17
+ expect{ Table::Row.new("foo") }.not_to raise_error
18
+ expect{ Table::Row.new(1) }.not_to raise_error
19
+ expect{ Table::Row.new([1,2,3]) }.not_to raise_error
20
+ expect{ Table::Row.new([[1,2,3],["foo","bar"]]) }.not_to raise_error
21
+ end
22
+
23
+ example "basic" do
24
+ html = "<tr></tr>"
25
+ expect(@trow.html.gsub(/\s+/, '')).to eq(html)
26
+ end
27
+
28
+ example "header" do
29
+ expect(@trow).to respond_to(:header?)
30
+ expect(@trow).to respond_to(:header=)
31
+ expect{ @trow.header? }.not_to raise_error
32
+ expect{ @trow.header = true }.not_to raise_error
33
+ end
34
+
35
+ example "with_attributes" do
36
+ html = "<tr align='center'></tr>"
37
+ @trow.align = "center"
38
+ expect(@trow.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
39
+ end
40
+
41
+ example "index_assignment_constraints" do
42
+ expect{ @trow[0] = "foo" }.to raise_error(ArgumentTypeError)
43
+ expect{ @trow[0] = 1 }.to raise_error(ArgumentTypeError)
44
+ expect{ @trow[0] = Table::Caption.new }.to raise_error(ArgumentTypeError)
45
+ expect{ @trow[0] = Table::Row::Data.new }.not_to raise_error
46
+ expect{ @trow[0] = Table::Row::Header.new }.not_to raise_error
47
+ end
48
+
49
+ example "push_constraints" do
50
+ expect{ @trow.push(Table::Caption.new) }.to raise_error(ArgumentTypeError)
51
+ expect{ @trow.push(nil) }.to raise_error(ArgumentTypeError)
52
+ expect{ @trow.push("test") }.not_to raise_error
53
+ expect{ @trow.push(7) }.not_to raise_error
54
+ expect{ @trow.push(Table::Row::Data.new) }.not_to raise_error
55
+ expect{ @trow.push(Table::Row::Header.new) }.not_to raise_error
56
+ end
57
+
58
+ example "double_arrow_constraints" do
59
+ expect{ @trow << Table::Caption.new }.to raise_error(ArgumentTypeError)
60
+ expect{ @trow << "test" }.not_to raise_error
61
+ expect{ @trow << "test" << "foo" }.not_to raise_error
62
+ expect{ @trow << Table::Row::Data.new }.not_to raise_error
63
+ expect{ @trow << Table::Row::Header.new }.not_to raise_error
64
+ end
65
+
66
+ example "header_in_constructor" do
67
+ expect{ @trow = Table::Row.new('test', true) }.not_to raise_error
68
+ html = "<tr><th>test</th></tr>"
69
+ expect(@trow.html.gsub(/\s+/, '')).to eq(html)
70
+ end
71
+
72
+ example "push_single_data_element" do
73
+ html = "<tr><td>hello</td></tr>"
74
+ @trow.push Table::Row::Data.new{ |d| d.content = "hello" }
75
+ expect(@trow.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
76
+ end
77
+
78
+ example "push_multiple_data_element" do
79
+ html = "<tr><td>hello</td><td>world</td></tr>"
80
+ d1 = Table::Row::Data.new{ |d| d.content = "hello" }
81
+ d2 = Table::Row::Data.new{ |d| d.content = "world" }
82
+ @trow.push d1, d2
83
+ expect(@trow.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
84
+ end
85
+
86
+ example "add_content_directly" do
87
+ html = "<tr><td>hello</td><td>world</td></tr>"
88
+ @trow.content = "hello","world"
89
+ expect(@trow.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
90
+ end
91
+
92
+ example "add_content_in_constructor" do
93
+ html = "<tr><td>hello</td><td>world</td></tr>"
94
+ @trow = Table::Row.new(%w/hello world/)
95
+ expect(@trow.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
96
+ end
97
+
98
+ example "configure_column" do
99
+ html = "<tr><td>hello</td><td abbr='test' width=3 nowrap>world</td></tr>"
100
+ @trow.content = "hello","world"
101
+ @trow.configure(1){ |d|
102
+ d.abbr = 'test'
103
+ d.width = 3
104
+ d.nowrap = true
105
+ }
106
+ expect(@trow.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
107
+ end
108
+
109
+ example "unshift_constraints" do
110
+ expect{ @trow.unshift(Table::Caption.new) }.to raise_error(ArgumentTypeError)
111
+ expect{ @trow.unshift(nil) }.to raise_error(ArgumentTypeError)
112
+ expect{ @trow.unshift("test") }.not_to raise_error
113
+ expect{ @trow.unshift(7) }.not_to raise_error
114
+ expect{ @trow.unshift(Table::Row::Data.new) }.not_to raise_error
115
+ expect{ @trow.unshift(Table::Row::Header.new) }.not_to raise_error
116
+ end
117
+
118
+ example "configure_error" do
119
+ expect{ @trow.configure(0,0){ }.to raise_error(ArgumentError) }
120
+ end
121
+
122
+ example "indent_level" do
123
+ expect(Table::Row).to respond_to(:indent_level)
124
+ expect(Table::Row).to respond_to(:indent_level=)
125
+ expect{ Table::Row.indent_level = "foo" }.to raise_error(ArgumentTypeError)
126
+ expect{ Table::Row.indent_level = 3 }.not_to raise_error
127
+ end
128
+
129
+ example "end_tags" do
130
+ expect(Table::Row).to respond_to(:end_tags?)
131
+ expect(Table::Row).to respond_to(:end_tags=)
132
+ expect{ Table::Row.end_tags = "foo" }.to raise_error(ArgumentTypeError)
133
+ expect{ Table::Row.end_tags = 1 }.to raise_error(ArgumentTypeError)
134
+ expect{ Table::Row.end_tags = true }.not_to raise_error
135
+ end
136
+ end
@@ -0,0 +1,152 @@
1
+ #######################################################################
2
+ # table_spec.rb
3
+ #
4
+ # Specs for the HTML::Table class. These specs should be run via the
5
+ # 'rake spec' or 'rake spec:table' task.
6
+ #######################################################################
7
+ require 'rspec'
8
+ require 'html/table'
9
+
10
+ RSpec.describe HTML::Table do
11
+ before do
12
+ @table = described_class.new
13
+ end
14
+
15
+ example "version" do
16
+ expect(described_class::VERSION).to eq('1.7.0')
17
+ expect(described_class::VERSION).to be_frozen
18
+ end
19
+
20
+ example "constructor" do
21
+ expect{ described_class.new }.not_to raise_error
22
+ expect{ described_class.new('foo') }.not_to raise_error
23
+ expect{ described_class.new(1) }.not_to raise_error
24
+ expect{ described_class.new(%w/foo bar baz/) }.not_to raise_error
25
+ expect{ described_class.new([1,2,3]) }.not_to raise_error
26
+ expect{ described_class.new([[1,2,3],['foo','bar']]) }.not_to raise_error
27
+ end
28
+
29
+ example "constructor_with_attributes" do
30
+ expect{ described_class.new(%w[foo bar baz], :border => 1) }.not_to raise_error
31
+ end
32
+
33
+ example "html_case" do
34
+ expect(described_class).to respond_to(:html_case)
35
+ expect(described_class).to respond_to(:html_case=)
36
+ expect{ described_class.html_case = 'upper' }.not_to raise_error
37
+ expect{ described_class.html_case = 'lower' }.not_to raise_error
38
+ expect{ described_class.html_case = 'foo' }.to raise_error(ArgumentError)
39
+ expect{ described_class.html_case = 7 }.to raise_error(ArgumentTypeError)
40
+ end
41
+
42
+ example "indent_level" do
43
+ expect(described_class).to respond_to(:indent_level)
44
+ expect(described_class).to respond_to(:indent_level=)
45
+ expect{ described_class.indent_level = 0 }.not_to raise_error
46
+ expect{ described_class.indent_level = 'foo' }.to raise_error(ArgumentTypeError)
47
+ end
48
+
49
+ example "index" do
50
+ expect{ @table[0] = 'foo' }.to raise_error(ArgumentTypeError)
51
+ end
52
+
53
+ example "caption_index_constraints" do
54
+ expect{ @table[0] = described_class::Caption.new }.not_to raise_error
55
+ expect{ @table[1] = described_class::Caption.new }.to raise_error(ArgumentError)
56
+ end
57
+
58
+ example "head_index_constraints" do
59
+ expect{ @table[0] = described_class::Head.create }.not_to raise_error
60
+ expect{ @table[1] = described_class::Head.create }.to raise_error(ArgumentError)
61
+ expect{ @table[2] = described_class::Head.create }.to raise_error(ArgumentError)
62
+ end
63
+
64
+ example "foot_index_constraints" do
65
+ expect {
66
+ @table[0] = described_class::Caption.new
67
+ @table[-1] = described_class::Foot.create
68
+ }.not_to raise_error
69
+ expect{ @table[0] = described_class::Foot.create }.to raise_error(ArgumentError)
70
+ end
71
+
72
+ example "unshift_constraints" do
73
+ expect{ @table.unshift described_class::Row.new }.not_to raise_error
74
+ expect{ @table.unshift described_class::Row::Data.new }.to raise_error(ArgumentTypeError)
75
+ expect{ @table.unshift 'foo' }.to raise_error(ArgumentTypeError)
76
+ end
77
+
78
+ example "push_constraints" do
79
+ expect{ @table.push described_class::Row.new }.not_to raise_error
80
+ expect{ @table.push('foo') }.to raise_error(ArgumentTypeError)
81
+ expect{ @table.push(7) }.to raise_error(ArgumentTypeError)
82
+ expect{ @table.push(nil) }.to raise_error(ArgumentTypeError)
83
+ end
84
+
85
+ example "double_arrow_constraints" do
86
+ expect{ @table << HTML::Table::Row.new }.not_to raise_error
87
+ expect{ @table << HTML::Table::Row.new << HTML::Table::Row.new }.not_to raise_error
88
+ expect{ @table << 'foo' }.to raise_error(ArgumentTypeError)
89
+ expect{ @table << 7 }.to raise_error(ArgumentTypeError)
90
+ expect{ @table << nil }.to raise_error(ArgumentTypeError)
91
+ end
92
+
93
+ example "basic" do
94
+ html = "<table>\n</table>"
95
+ expect(@table.html).to eq(html)
96
+ end
97
+
98
+ example "with_attributes" do
99
+ html = "<table border=1 align='left' nowrap>\n</table>"
100
+ @table.border = 1
101
+ @table.align = 'left'
102
+ @table.nowrap = true
103
+ expect(@table.html).to eq(html)
104
+ end
105
+
106
+ example "add_row_push" do
107
+ html = '<table><tr></tr></table>'
108
+ @table.push(described_class::Row.new)
109
+ expect(@table.html.gsub(/\s+/, '')).to eq(html)
110
+ end
111
+
112
+ example "add_row_by_index" do
113
+ html = '<table><tr></tr></table>'
114
+ @table[0] = described_class::Row.new
115
+ expect(@table.html.gsub(/\s+/, '')).to eq(html)
116
+ end
117
+
118
+ example "add_multiple_rows" do
119
+ html = '<table><tr></tr><tr></tr></table>'
120
+ @table.push HTML::Table::Row.new, HTML::Table::Row.new
121
+ expect(@table.html.gsub(/\s+/, '')).to eq(html)
122
+ end
123
+
124
+ example "add_single_data_element" do
125
+ html = '<table><tr><td>hello</td></tr></table>'
126
+ @table.content = 'hello'
127
+ expect(@table.html.gsub(/\s+/, '')).to eq(html)
128
+ end
129
+
130
+ example "add_multiple_data_elements" do
131
+ html = '<table><tr><td>hello</td></tr><tr><td>world</td></tr></table>'
132
+ @table.content = 'hello','world'
133
+ expect(@table.html.gsub(/\s+/, '')).to eq(html)
134
+ end
135
+
136
+ example "configure_row" do
137
+ html = "<table><tr align='center'><td bgcolor='red'>hello</td></tr>"
138
+ html << '</table>'
139
+ @table.push HTML::Table::Row::Data.new{ |d| d.content = 'hello' }
140
+ @table.configure(0){ |t| t.align = 'center' }
141
+ @table.configure(0,0){ |d| d.bgcolor = 'red' }
142
+ expect(@table.html.gsub(/\s{2,}|\n+/, '')).to eq(html)
143
+ end
144
+
145
+ example "global_end_tags" do
146
+ expect(described_class).to respond_to(:global_end_tags?)
147
+ expect(described_class).to respond_to(:global_end_tags=)
148
+ expect{ described_class.global_end_tags = false }.not_to raise_error
149
+ expect{ described_class.global_end_tags = true }.not_to raise_error
150
+ expect{ described_class.global_end_tags = 'foo' }.to raise_error(ArgumentTypeError)
151
+ end
152
+ end