Permalink
Browse files

[#21] allowed variable white space for table_header_matcher, and pull…

…ed stuff from master
  • Loading branch information...
2 parents 0a20df3 + c75a15f commit 6164bbe67caf9c16aefd75001e41d5b75e081ce4 @helabed helabed committed May 26, 2009
View
@@ -1,52 +1,60 @@
require 'hpricot'
# %w(check_box_group drop_down radio_group span_text table_body table_header td_link).each do |element|
- # require File.join(File.dirname(__FILE__), 'matchers', "#{element}_matcher")
+ # require File.join(File.dirname(__FILE__), 'matchers', "#{element}_matcher")
# end
Dir.glob(File.dirname(__FILE__) + '/matchers/*.rb').each do |rb|
- require rb
+ require rb
end
module Spec # :nodoc:
module Rails
module Matchers
- def have_check_box_group target_name, expected
- CheckBoxGroupMatcher.new target_name, expected
- end
+ def have_button expected
+ ButtonMatcher.new expected
+ end
+
+ def have_check_box_group target_name, expected
+ CheckBoxGroupMatcher.new target_name, expected
+ end
- def have_dropdown target_id, expected_options
- DropDownMatcher.new target_id, expected_options
- end
+ def have_dropdown target_id, expected_options
+ DropDownMatcher.new target_id, expected_options
+ end
+
+ def have_image expected_path
+ ImageMatcher.new expected_path
+ end
- def have_options expected_options
- OptionsMatcher.new expected_options
- end
+ def have_options expected_options
+ OptionsMatcher.new expected_options
+ end
- def have_radio_group target_name, expected_radio_choices
- RadioGroupMatcher.new target_name, expected_radio_choices
- end
+ def have_radio_group target_name, expected_radio_choices
+ RadioGroupMatcher.new target_name, expected_radio_choices
+ end
- def have_span_text target_id, expected_text
- SpanTextMatcher.new target_id, expected_text
- end
+ def have_span_text target_id, expected_text
+ SpanTextMatcher.new target_id, expected_text
+ end
def have_table table_id_or_expected, expected = nil
TableMatcher.new table_id_or_expected, expected
end
- def have_table_header table_id_or_expected, expected = nil
- TableHeaderMatcher.new table_id_or_expected, expected
- end
+ def have_table_header table_id_or_expected, expected = nil
+ TableHeaderMatcher.new table_id_or_expected, expected
+ end
- def have_table_body table_id_or_expected, expected = nil
- TableBodyMatcher.new table_id_or_expected, expected
- end
+ def have_table_body table_id_or_expected, expected = nil
+ TableBodyMatcher.new table_id_or_expected, expected
+ end
- def have_td_link target_id, expected_link, expected_text
- TdLinkMatcher.new target_id, expected_link, expected_text
- end
- end
- end
+ def have_td_link target_id, expected_link, expected_text
+ TdLinkMatcher.new target_id, expected_link, expected_text
+ end
+ end
+ end
end
@@ -0,0 +1,33 @@
+module Spec # :nodoc:
+ module Rails
+ module Matchers
+ class ButtonMatcher
+ def initialize expected_value
+ # @target_id = target_id
+ @expected = expected_value
+ end
+
+ def matches? response
+ @actual = extract_html_content response.body
+ @actual == @expected
+ end
+
+ def failure_message
+ "\nWrong button value.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ end
+
+ def negative_failure_message
+ "\nShould not have matched button: #{@target_id}, with text: '#{@expected}'\n\n"
+ end
+
+ def extract_html_content html
+ doc = Hpricot.XML(html)
+ # elements = doc.search("//input[@value='#{@expected}' & @type='button|submit']")
+ # elements = doc.search("input[@value~='#{@expected}'][@type]")
+ elements = doc.search("//input[@value='#{@expected}']").select{|e| ['submit', 'button'].include?(e['type'])}
+ elements.map{|n| n['value']}.first
+ end
+ end
+ end
+ end
+end
@@ -1,42 +1,42 @@
module Spec # :nodoc:
- module Rails
- module Matchers
- class CheckBoxGroupMatcher
-
- def initialize target_name, expected
- @expected = expected
- @target_name = target_name
- end
-
- def matches? response
- @actual = extract_html_content response.body
- @actual == @expected
- end
-
- def failure_message
- "\nWrong check box group contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
- end
-
- def negative_failure_message
- "\nShould not have matched check box group: name: #{@target_name}, with: #{@expected.inspect}\n\n"
- end
-
- def extract_html_content html
- doc = Hpricot.XML(html)
- # elements = doc.search('div input[type="checkbox"]')
- elements = doc.search('div/input')
- elements = elements.select{|n| n.elem? && n.get_attribute('type') == 'checkbox' && n.get_attribute('name') == @target_name}
- elements.map{|n| value = n.get_attribute('value'); [value, find_label(n)]}
- end
-
- private
-
- def find_label node
- label_node = node.next_sibling
- label_node.inner_text
- end
-
- end
- end
- end
+ module Rails
+ module Matchers
+ class CheckBoxGroupMatcher
+
+ def initialize target_name, expected
+ @expected = expected
+ @target_name = target_name
+ end
+
+ def matches? response
+ @actual = extract_html_content response.body
+ @actual == @expected
+ end
+
+ def failure_message
+ "\nWrong check box group contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ end
+
+ def negative_failure_message
+ "\nShould not have matched check box group: name: #{@target_name}, with: #{@expected.inspect}\n\n"
+ end
+
+ def extract_html_content html
+ doc = Hpricot.XML(html)
+ # elements = doc.search('div input[type="checkbox"]')
+ elements = doc.search('div/input')
+ elements = elements.select{|n| n.elem? && n.get_attribute('type') == 'checkbox' && n.get_attribute('name') == @target_name}
+ elements.map{|n| value = n.get_attribute('value'); [value, find_label(n)]}
+ end
+
+ private
+
+ def find_label node
+ label_node = node.next_sibling
+ label_node.inner_text
+ end
+
+ end
+ end
+ end
end
@@ -1,43 +1,43 @@
module Spec # :nodoc:
- module Rails
- module Matchers
- class DropDownMatcher
+ module Rails
+ module Matchers
+ class DropDownMatcher
- def initialize target_id, expected
- @target_id = target_id
- @expected = expected
- end
+ def initialize target_id, expected
+ @target_id = target_id
+ @expected = expected
+ end
- def matches? response
- @actual = extract_html_content response.body
- @actual == @expected
- end
+ def matches? response
+ @actual = extract_html_content response.body
+ @actual == @expected
+ end
- def failure_message
- if @found_select
- "\nWrong '#{@target_id}' drop down contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
- else
- "\nCould not find a select element with id: '#{@target_id}'\n\n"
- end
- end
+ def failure_message
+ if @found_select
+ "\nWrong '#{@target_id}' drop down contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ else
+ "\nCould not find a select element with id: '#{@target_id}'\n\n"
+ end
+ end
- def negative_failure_message
- "\nShould not have matched dropdown with id: #{@target_id}\n\tand contents: #{@expected.inspect}\n\n"
- end
+ def negative_failure_message
+ "\nShould not have matched dropdown with id: #{@target_id}\n\tand contents: #{@expected.inspect}\n\n"
+ end
- def extract_html_content html
- doc = Hpricot.XML(html)
- select = doc.search("select##{@target_id}")
- @found_select = ! select.empty?
+ def extract_html_content html
+ doc = Hpricot.XML(html)
+ select = doc.search("select##{@target_id}")
+ @found_select = ! select.empty?
- if @found_select
- select.search("/option").map{|n| n.inner_text.strip}
- else
- []
- end
- end
+ if @found_select
+ select.search("/option").map{|n| n.inner_text.strip}
+ else
+ []
+ end
+ end
- end
- end
- end
+ end
+ end
+ end
end
@@ -0,0 +1,32 @@
+module Spec # :nodoc:
+ module Rails
+ module Matchers
+ class ImageMatcher
+ def initialize expected_path
+ @expected = expected_path
+ end
+
+ def matches? response
+ @actual = extract_html_content response.body
+ @actual =~ /#{@expected}/
+ end
+
+ def failure_message
+ "\nWrong image path.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ end
+
+ def negative_failure_message
+ "\nShould not have matched image with path: '#{@expected}'\n\n"
+ end
+
+ def extract_html_content html
+ doc = Hpricot.XML(html)
+ elements = doc.search("img[@src*=\"#{@expected}\"]")
+ # elements.each {|e| puts e.inspect}
+ # elements.first.to_html
+ elements.map{|n| n.to_html}.first
+ end
+ end
+ end
+ end
+end
@@ -2,7 +2,6 @@ module Spec # :nodoc:
module Rails
module Matchers
class OptionsMatcher
-
def initialize expected
@expected = expected
end
@@ -24,7 +23,6 @@ def extract_html_content html
doc = Hpricot.XML(html)
doc.search("/option").map{|n| n.inner_text.strip}
end
-
end
end
end
@@ -1,34 +1,32 @@
module Spec # :nodoc:
- module Rails
- module Matchers
- class RadioGroupMatcher
+ module Rails
+ module Matchers
+ class RadioGroupMatcher
+ def initialize target_name, expected
+ @expected = expected
+ @target_name = target_name
+ end
- def initialize target_name, expected
- @expected = expected
- @target_name = target_name
- end
+ def matches? response
+ @actual = extract_html_content response.body
+ @actual == @expected
+ end
- def matches? response
- @actual = extract_html_content response.body
- @actual == @expected
- end
+ def failure_message
+ "\nWrong radio group contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ end
- def failure_message
- "\nWrong radio group contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
- end
+ def negative_failure_message
+ "\nShould not have matched radio group: name: #{@target_name}, with: #{@expected.inspect}\n\n"
+ end
- def negative_failure_message
- "\nShould not have matched radio group: name: #{@target_name}, with: #{@expected.inspect}\n\n"
- end
-
- def extract_html_content html
- doc = Hpricot.XML(html)
- elements = doc.search('input')
- elements = elements.select{|n| n.elem? && n.get_attribute('name') == @target_name}
- elements.map{|n| n.get_attribute('value')}
- end
-
- end
- end
- end
+ def extract_html_content html
+ doc = Hpricot.XML(html)
+ elements = doc.search('input')
+ elements = elements.select{|n| n.elem? && n.get_attribute('name') == @target_name}
+ elements.map{|n| n.get_attribute('value')}
+ end
+ end
+ end
+ end
end
Oops, something went wrong.

0 comments on commit 6164bbe

Please sign in to comment.