Skip to content
Browse files

[#21 status:accepted] enhanced matchers for body and table to deal wi…

…th variable amount of whitespace and added corresponding spec tests
  • Loading branch information...
1 parent 5af14b1 commit f9503ab5b5ff13856d698b373ce9011bdfbcaada @helabed helabed committed May 26, 2009
View
2 lib/matchers/table_body_matcher.rb
@@ -30,7 +30,7 @@ def negative_failure_message
def extract_html_content html
doc = Hpricot.XML(html)
elements = doc.search("table#{"##{@table_id}" if @table_id} tr").reject{|e| e.search('td').empty? }
- elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/\n \t\t/, "\n")}}
+ elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")}}
end
end
View
1 lib/matchers/table_header_matcher.rb
@@ -34,7 +34,6 @@ def extract_html_content html
elements.map do |node|
node.search('/th').map do |n|
n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")
- #n.inner_text.strip.gsub(/\n \t\t/, "\n")
end
end
end
View
6 lib/matchers/table_matcher.rb
@@ -32,12 +32,10 @@ def extract_html_content html
rows = doc.search("table#{"##{@table_id}" if @table_id} tr")
header_elements = rows.reject{|e| e.search('th').empty? }
- #header_content = header_elements.map{|n| n.search('/th').map{|n| n.inner_text.strip.gsub(/\n[\n \t]*/, "\n")}}
- header_content = header_elements.map{|n| n.search('/th').map{|n| n.inner_text.strip.gsub(/\n \t\t/, "\n")}}
+ header_content = header_elements.map{|n| n.search('/th').map{|n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")}}
body_elements = rows.reject{|e| e.search('td').empty? }
- #body_content = body_elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/\n[\n \t]*/, "\n")}}
- body_content = body_elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/\n \t\t/, "\n")}}
+ body_content = body_elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")}}
header_content + body_content
end
View
9 spec/table_body_matcher_spec.rb
@@ -14,6 +14,13 @@
response.should have_table_body('my_id', [['c1', 'c2']])
end
+ describe 'with extraneous multiline whitespace' do
+ it 'should remove extraneous whitespace' do
+ response = mock_model(Object, :body => "<table id='my_id'><tr><td>\n c1a\n \t<br/> \tc1b</td><td>c2</td></tr></table>")
+ response.should have_table_body('my_id', [["c1a\nc1b", 'c2']])
+ end
+ end
+
it 'should ignore header row' do
response = mock_model(Object, :body => '<table id="my_id"><tr><th>h1</th><th>h2</th></tr><tr><td>c1</td><td>c2</td></tr></table>')
response.should have_table_body('my_id', [['c1', 'c2']])
@@ -67,4 +74,4 @@
lambda{ response.should have_table_body('my_id', nil)}.should raise_error(RuntimeError, 'Invalid "expected" argument')
end
end
-end
+end
View
7 spec/table_matcher_spec.rb
@@ -13,6 +13,13 @@
response = mock_model(Object, :body => '<table id="my_id"><tr><th>h1</th><th>h2</th></tr><tr><td>c1</td><td>c2</td></tr></table>')
response.should have_table('my_id', [['h1', 'h2'], ['c1', 'c2']])
end
+
+ describe 'with extraneous multiline whitespace' do
+ it 'should remove extraneous whitespace' do
+ response = mock_model(Object, :body => "<table id=\"my_id\"><tr><th>h1</th><th>h2</th></tr><tr><td>\n c1a\n \t<br/> \tc1b</td><td>c2</td></tr></table>")
+ response.should have_table('my_id', [['h1', 'h2'], ["c1a\nc1b", 'c2']])
+ end
+ end
end
it 'can be called without a table_id' do

0 comments on commit f9503ab

Please sign in to comment.
Something went wrong with that request. Please try again.