Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First pass at check_box_group_matcher. [#16 state:open]

  • Loading branch information...
commit 0aa8f5c50045dcd1514da89261210dddb6bfc2ae 1 parent 404391a
Stephen Anderson authored
View
6 lib/html_matchers.rb
@@ -1,5 +1,5 @@
require 'hpricot'
-%w(drop_down radio_group span_text table_body table_header td_link).each do |element|
+%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")
end
@@ -7,6 +7,10 @@ module Spec # :nodoc:
module Rails
module Matchers
+ 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
View
42 lib/matchers/check_box_group_matcher.rb
@@ -0,0 +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
+end
View
4 lib/matchers/drop_down_matcher.rb
@@ -1,6 +1,6 @@
module Spec # :nodoc:
- module Rails
- module Matchers
+ module Rails
+ module Matchers
class DropDownMatcher
def initialize target_id, expected
View
4 lib/matchers/radio_group_matcher.rb
@@ -1,6 +1,6 @@
module Spec # :nodoc:
- module Rails
- module Matchers
+ module Rails
+ module Matchers
class RadioGroupMatcher
def initialize target_name, expected
View
4 lib/matchers/span_text_matcher.rb
@@ -1,6 +1,6 @@
module Spec # :nodoc:
- module Rails
- module Matchers
+ module Rails
+ module Matchers
class SpanTextMatcher
def initialize target_id, expected_text
View
4 lib/matchers/table_body_matcher.rb
@@ -1,6 +1,6 @@
module Spec # :nodoc:
- module Rails
- module Matchers
+ module Rails
+ module Matchers
class TableBodyMatcher
def initialize table_id_or_expected, expected
View
4 lib/matchers/table_header_matcher.rb
@@ -1,6 +1,6 @@
module Spec # :nodoc:
- module Rails
- module Matchers
+ module Rails
+ module Matchers
class TableHeaderMatcher
def initialize table_id_or_expected, expected
View
4 lib/matchers/td_link_matcher.rb
@@ -1,6 +1,6 @@
module Spec # :nodoc:
- module Rails
- module Matchers
+ module Rails
+ module Matchers
class TdLinkMatcher
def initialize target_id, expected_text, expected_link
View
27 spec/check_box_group_matcher_spec.rb
@@ -0,0 +1,27 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe 'check_box_group_matcher' do
+ it 'should find matching check box group' do
+ response = mock_model(Object, :body => check_box_html)
+ response.should have_check_box_group('user[genre_ids][]', [['1', 'Rock/Pop General'], ['2', 'Modern "Alternative"'], ['4', 'Metal']])
+ end
+
+ private
+
+ def check_box_html
+ %{<div>
+ <input id="genre_1" name="user[genre_ids][]" type="checkbox" value="1" />
+ <label for="genre_1">Rock/Pop General</label>
+ </div>
+
+ <div>
+ <input id="genre_2" name="user[genre_ids][]" type="checkbox" value="2" />
+ <label for="genre_2">Modern "Alternative"</label>
+ </div>
+
+ <div>
+ <input id="genre_4" name="user[genre_ids][]" type="checkbox" value="4" />
+ <label for="genre_4">Metal</label>
+ </div>}
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.