diff --git a/spec/public/matchers/contain_spec.rb b/spec/public/matchers/contain_spec.rb new file mode 100644 index 00000000..78ebae11 --- /dev/null +++ b/spec/public/matchers/contain_spec.rb @@ -0,0 +1,114 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe "contain" do + include Webrat::Matchers + + before(:each) do + @body = <<-HTML +
+
hello, world!
+

Welcome "Bryan"

+

Welcome 'Bryan'

+

Welcome 'Bryan"

+ +
+ HTML + end + + before(:each) do + @body = <<-EOF +
+
hello, world!
+
+ EOF + end + + describe "#matches?" do + it "should call element#contains? when the argument is a string" do + @body.should contain("hello, world!") + end + + it "should call element#matches? when the argument is a regular expression" do + @body.should contain(/hello, world/) + end + end + + describe "asserts for contains," do + include Test::Unit::Assertions + + before(:each) do + should_receive(:response_body).and_return @body + require 'test/unit' + end + + describe "assert_contain" do + it "should pass when containing the text" do + assert_contain("hello, world") + end + + it "should pass when containing the regexp" do + assert_contain(/hello, world/) + end + + it "should throw an exception when the body doesnt contain the text" do + lambda { + assert_contain("monkeys") + }.should raise_error(Test::Unit::AssertionFailedError) + end + + it "should throw an exception when the body doesnt contain the regexp" do + lambda { + assert_contain(/monkeys/) + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + + describe "assert_not_contain" do + it "should pass when not containing the text" do + assert_not_contain("monkeys") + end + + it "should pass when not containing the regexp" do + assert_not_contain(/monkeys/) + end + + it "should throw an exception when the body does contain the text" do + lambda { + assert_not_contain("hello, world") + }.should raise_error(Test::Unit::AssertionFailedError) + end + + it "should throw an exception when the body does contain the regexp" do + lambda { + assert_not_contain(/hello, world/) + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + end + + describe "#failure_message" do + it "should include the content string" do + hc = Webrat::Matchers::HasContent.new("hello, world!") + hc.matches?(@body) + + hc.failure_message.should include("\"hello, world!\"") + end + + it "should include the content regular expresson" do + hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/) + hc.matches?(@body) + + hc.failure_message.should include("/hello,\\sworld!/") + end + + it "should include the element's inner content" do + hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/) + hc.matches?(@body) + + hc.failure_message.should include("hello, world!") + end + end +end diff --git a/spec/public/matchers/have_selector_spec.rb b/spec/public/matchers/have_selector_spec.rb new file mode 100644 index 00000000..24d9de9b --- /dev/null +++ b/spec/public/matchers/have_selector_spec.rb @@ -0,0 +1,90 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe "have_selector" do + include Webrat::Matchers + + before(:each) do + @body = <<-HTML +
+
hello, world!
+

Welcome "Bryan"

+

Welcome 'Bryan'

+

Welcome 'Bryan"

+ +
+ HTML + end + + it "should be able to match a CSS selector" do + @body.should have_selector("div") + end + + it "should not match a CSS selector that does not exist" do + @body.should_not have_selector("p") + end + + it "should be able to loop over all the matched elements" do + @body.should have_selector("div") do |node| + node.first.name.should == "div" + end + end + + it "should not match of any of the matchers in the block fail" do + lambda { + @body.should have_selector("div") do |node| + node.first.name.should == "p" + end + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + it "should be able to use #have_selector in the block" do + @body.should have_selector("#main") do |node| + node.should have_selector(".inner") + end + end + + it "should not match any parent tags in the block" do + lambda { + @body.should have_selector(".inner") do |node| + node.should have_selector("#main") + end + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + describe "asserts for selector," do + include Test::Unit::Assertions + + before(:each) do + should_receive(:response_body).and_return @body + require 'test/unit' + end + + describe "assert_have_selector" do + it "should pass when body contains the selection" do + assert_have_selector("div") + end + + it "should throw an exception when the body doesnt have matching selection" do + lambda { + assert_have_selector("p") + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + + describe "assert_have_not_selector" do + it "should pass when the body doesn't contan the selection" do + assert_have_no_selector("p") + end + + it "should throw an exception when the body does contain the selection" do + lambda { + assert_have_no_selector("div") + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + end + +end diff --git a/spec/public/matchers/have_tag_spec.rb b/spec/public/matchers/have_tag_spec.rb new file mode 100644 index 00000000..671d5e0f --- /dev/null +++ b/spec/public/matchers/have_tag_spec.rb @@ -0,0 +1,148 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe "have_tag" do + include Webrat::Matchers + include Webrat::HaveTagMatcher + + before(:each) do + @body = <<-HTML +
+
hello, world!
+

Welcome "Bryan"

+

Welcome 'Bryan'

+

Welcome 'Bryan"

+ +
+ HTML + end + + it "should be able to match a tag" do + @body.should have_tag("div") + end + + it "should not match the tag when it should not match" do + @body.should_not have_tag("p") + end + + it "should be able to specify the content of the tag" do + @body.should have_tag("div", :content => "hello, world!") + end + + it "should be able to specify the content of the tag with double quotes in it" do + @body.should have_tag("h2", :content => 'Welcome "Bryan"') + end + + it "should be able to specify the content of the tag with single quotes in it" do + @body.should have_tag("h3", :content => "Welcome 'Bryan'") + end + + it "should be able to specify the content of the tag with both kinds of quotes" do + @body.should have_tag("h4", :content => "Welcome 'Bryan\"") + end + + it "should be able to specify the number of occurences of the tag" do + @body.should have_tag("li", :count => 2) + end + + it "should not match if the count is wrong" do + lambda { + @body.should have_tag("li", :count => 3) + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + it "should be able to specify the attributes of the tag" do + @body.should have_tag("div", :class => "inner") + end + + it "should be able to loop over all the matched elements" do + @body.should have_tag("div") do |node| + node.first.name.should == "div" + end + end + + it "should not match of any of the matchers in the block fail" do + lambda { + @body.should have_tag("div") do |node| + node.first.name.should == "p" + end + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + it "should be able to use #have_tag in the block" do + @body.should have_tag("div", :id => "main") do |node| + node.should have_tag("div", :class => "inner") + end + end + + it "should not match any parent tags in the block" do + lambda { + @body.should have_tag("div", :class => "inner") do |node| + node.should have_tag("div", :id => "main") + end + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + it "should work with items that have multiple child nodes" do + @body.should have_tag("ul") do |n| + n.should have_tag("li", :content => "First") + n.should have_tag("li", :content => "Second") + end + end + + describe "asserts for tags" do + include Test::Unit::Assertions + + before(:each) do + should_receive(:response_body).and_return @body + require 'test/unit' + end + + describe "assert_have_tag" do + it "should pass when body contains the tag" do + assert_have_tag("div") + end + + it "should pass when finding with additional selectors" do + assert_have_tag("div", :class => "inner") + end + + + it "should throw an exception when the body doesn't have matching tag" do + lambda { + assert_have_tag("p") + }.should raise_error(Test::Unit::AssertionFailedError) + end + + it "should throw an exception when the body doesn't have a tag matching the attributes" do + lambda { + assert_have_tag("div", :class => "nope") + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + + describe "assert_have_no_tag" do + it "should pass when the body doesn't contan the tag" do + assert_have_no_tag("p") + end + + it "should pass when the body doesn't contain the tag due to additional selectors missing" do + assert_have_no_tag("div", :class => "nope") + end + + it "should throw an exception when the body does contain the tag" do + lambda { + assert_have_no_tag("div") + }.should raise_error(Test::Unit::AssertionFailedError) + end + + it "should throw an exception when the body contains the tag with additional selectors" do + lambda { + assert_have_no_tag("div", :class => "inner") + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + end +end diff --git a/spec/public/matchers/have_xpath_spec.rb b/spec/public/matchers/have_xpath_spec.rb new file mode 100644 index 00000000..7f06205f --- /dev/null +++ b/spec/public/matchers/have_xpath_spec.rb @@ -0,0 +1,95 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe "have_xpath" do + include Webrat::Matchers + + before(:each) do + @body = <<-HTML +
+
hello, world!
+

Welcome "Bryan"

+

Welcome 'Bryan'

+

Welcome 'Bryan"

+ +
+ HTML + end + + it "should be able to match an XPATH" do + @body.should have_xpath("//div") + end + + it "should not match a XPATH that does not exist" do + @body.should_not have_xpath("//p") + end + + it "should be able to loop over all the matched elements" do + @body.should have_xpath("//div") do |node| + node.first.name.should == "div" + end + end + + it "should not match if any of the matchers in the block fail" do + lambda { + @body.should have_xpath("//div") do |node| + node.first.name.should == "p" + end + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + it "should be able to use #have_xpath in the block" do + @body.should have_xpath("//div[@id='main']") do |node| + node.should have_xpath("./div[@class='inner']") + end + end + + it "should convert absolute paths to relative in the block" do + @body.should have_xpath("//div[@id='main']") do |node| + node.should have_xpath("//div[@class='inner']") + end + end + + it "should not match any parent tags in the block" do + lambda { + @body.should have_xpath("//div[@class='inner']") do |node| + node.should have_xpath("//div[@id='main']") + end + }.should raise_error(Spec::Expectations::ExpectationNotMetError) + end + + describe 'asserts for xpath' do + include Test::Unit::Assertions + + before(:each) do + should_receive(:response_body).and_return @body + require 'test/unit' + end + + describe "assert_have_xpath" do + it "should pass when body contains the selection" do + assert_have_xpath("//div") + end + + it "should throw an exception when the body doesnt have matching xpath" do + lambda { + assert_have_xpath("//p") + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + + describe "assert_have_no_xpath" do + it "should pass when the body doesn't contan the xpath" do + assert_have_no_xpath("//p") + end + + it "should throw an exception when the body does contain the xpath" do + lambda { + assert_have_no_xpath("//div") + }.should raise_error(Test::Unit::AssertionFailedError) + end + end + end +end diff --git a/spec/public/matchers_spec.rb b/spec/public/matchers_spec.rb deleted file mode 100644 index 4361c5e6..00000000 --- a/spec/public/matchers_spec.rb +++ /dev/null @@ -1,396 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") - -describe Webrat::Matchers do - include Webrat::Matchers - include Webrat::HaveTagMatcher - - before(:each) do - @body = <<-HTML -
-
hello, world!
-

Welcome "Bryan"

-

Welcome 'Bryan'

-

Welcome 'Bryan"

- -
- HTML - end - - describe "#have_xpath" do - it "should be able to match an XPATH" do - @body.should have_xpath("//div") - end - - it "should not match a XPATH that does not exist" do - @body.should_not have_xpath("//p") - end - - it "should be able to loop over all the matched elements" do - @body.should have_xpath("//div") do |node| - node.first.name.should == "div" - end - end - - it "should not match if any of the matchers in the block fail" do - lambda { - @body.should have_xpath("//div") do |node| - node.first.name.should == "p" - end - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - it "should be able to use #have_xpath in the block" do - @body.should have_xpath("//div[@id='main']") do |node| - node.should have_xpath("./div[@class='inner']") - end - end - - it "should convert absolute paths to relative in the block" do - @body.should have_xpath("//div[@id='main']") do |node| - node.should have_xpath("//div[@class='inner']") - end - end - - it "should not match any parent tags in the block" do - lambda { - @body.should have_xpath("//div[@class='inner']") do |node| - node.should have_xpath("//div[@id='main']") - end - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - describe 'asserts for xpath' do - include Test::Unit::Assertions - - before(:each) do - should_receive(:response_body).and_return @body - require 'test/unit' - end - - describe "assert_have_xpath" do - it "should pass when body contains the selection" do - assert_have_xpath("//div") - end - - it "should throw an exception when the body doesnt have matching xpath" do - lambda { - assert_have_xpath("//p") - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - - describe "assert_have_no_xpath" do - it "should pass when the body doesn't contan the xpath" do - assert_have_no_xpath("//p") - end - - it "should throw an exception when the body does contain the xpath" do - lambda { - assert_have_no_xpath("//div") - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - end - end - - describe "#have_selector" do - - it "should be able to match a CSS selector" do - @body.should have_selector("div") - end - - it "should not match a CSS selector that does not exist" do - @body.should_not have_selector("p") - end - - it "should be able to loop over all the matched elements" do - @body.should have_selector("div") do |node| - node.first.name.should == "div" - end - end - - it "should not match of any of the matchers in the block fail" do - lambda { - @body.should have_selector("div") do |node| - node.first.name.should == "p" - end - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - it "should be able to use #have_selector in the block" do - @body.should have_selector("#main") do |node| - node.should have_selector(".inner") - end - end - - it "should not match any parent tags in the block" do - lambda { - @body.should have_selector(".inner") do |node| - node.should have_selector("#main") - end - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - describe "asserts for selector," do - include Test::Unit::Assertions - - before(:each) do - should_receive(:response_body).and_return @body - require 'test/unit' - end - - describe "assert_have_selector" do - it "should pass when body contains the selection" do - assert_have_selector("div") - end - - it "should throw an exception when the body doesnt have matching selection" do - lambda { - assert_have_selector("p") - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - - describe "assert_have_not_selector" do - it "should pass when the body doesn't contan the selection" do - assert_have_no_selector("p") - end - - it "should throw an exception when the body does contain the selection" do - lambda { - assert_have_no_selector("div") - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - end - end - - describe "#have_tag" do - it "should be able to match a tag" do - @body.should have_tag("div") - end - - it "should not match the tag when it should not match" do - @body.should_not have_tag("p") - end - - it "should be able to specify the content of the tag" do - @body.should have_tag("div", :content => "hello, world!") - end - - it "should be able to specify the content of the tag with double quotes in it" do - @body.should have_tag("h2", :content => 'Welcome "Bryan"') - end - - it "should be able to specify the content of the tag with single quotes in it" do - @body.should have_tag("h3", :content => "Welcome 'Bryan'") - end - - it "should be able to specify the content of the tag with both kinds of quotes" do - @body.should have_tag("h4", :content => "Welcome 'Bryan\"") - end - - it "should be able to specify the number of occurences of the tag" do - @body.should have_tag("li", :count => 2) - end - - it "should not match if the count is wrong" do - lambda { - @body.should have_tag("li", :count => 3) - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - it "should be able to specify the attributes of the tag" do - @body.should have_tag("div", :class => "inner") - end - - it "should be able to loop over all the matched elements" do - @body.should have_tag("div") do |node| - node.first.name.should == "div" - end - end - - it "should not match of any of the matchers in the block fail" do - lambda { - @body.should have_tag("div") do |node| - node.first.name.should == "p" - end - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - it "should be able to use #have_tag in the block" do - @body.should have_tag("div", :id => "main") do |node| - node.should have_tag("div", :class => "inner") - end - end - - it "should not match any parent tags in the block" do - lambda { - @body.should have_tag("div", :class => "inner") do |node| - node.should have_tag("div", :id => "main") - end - }.should raise_error(Spec::Expectations::ExpectationNotMetError) - end - - it "should work with items that have multiple child nodes" do - @body.should have_tag("ul") do |n| - n.should have_tag("li", :content => "First") - n.should have_tag("li", :content => "Second") - end - end - - describe "asserts for tags," do - include Test::Unit::Assertions - before(:each) do - should_receive(:response_body).and_return @body - require 'test/unit' - end - describe "assert_have_tag" do - it "should pass when body contains the tag" do - assert_have_tag("div") - end - - it "should pass when finding with additional selectors" do - assert_have_tag("div", :class => "inner") - end - - - it "should throw an exception when the body doesn't have matching tag" do - lambda { - assert_have_tag("p") - }.should raise_error(Test::Unit::AssertionFailedError) - end - - it "should throw an exception when the body doesn't have a tag matching the attributes" do - lambda { - assert_have_tag("div", :class => "nope") - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - - describe "assert_have_no_tag" do - it "should pass when the body doesn't contan the tag" do - assert_have_no_tag("p") - end - - it "should pass when the body doesn't contain the tag due to additional selectors missing" do - assert_have_no_tag("div", :class => "nope") - end - - it "should throw an exception when the body does contain the tag" do - lambda { - assert_have_no_tag("div") - }.should raise_error(Test::Unit::AssertionFailedError) - end - - it "should throw an exception when the body contains the tag with additional selectors" do - lambda { - assert_have_no_tag("div", :class => "inner") - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - end - - end - - describe Webrat::Matchers::HasContent do - include Webrat::Matchers - - before(:each) do - @body = <<-EOF -
-
hello, world!
-
- EOF - end - - describe "#matches?" do - it "should call element#contains? when the argument is a string" do - @body.should contain("hello, world!") - end - - it "should call element#matches? when the argument is a regular expression" do - @body.should contain(/hello, world/) - end - end - - describe "asserts for contains," do - include Test::Unit::Assertions - - before(:each) do - should_receive(:response_body).and_return @body - require 'test/unit' - end - - describe "assert_contain" do - it "should pass when containing the text" do - assert_contain("hello, world") - end - - it "should pass when containing the regexp" do - assert_contain(/hello, world/) - end - - it "should throw an exception when the body doesnt contain the text" do - lambda { - assert_contain("monkeys") - }.should raise_error(Test::Unit::AssertionFailedError) - end - - it "should throw an exception when the body doesnt contain the regexp" do - lambda { - assert_contain(/monkeys/) - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - - describe "assert_not_contain" do - it "should pass when not containing the text" do - assert_not_contain("monkeys") - end - - it "should pass when not containing the regexp" do - assert_not_contain(/monkeys/) - end - - it "should throw an exception when the body does contain the text" do - lambda { - assert_not_contain("hello, world") - }.should raise_error(Test::Unit::AssertionFailedError) - end - - it "should throw an exception when the body does contain the regexp" do - lambda { - assert_not_contain(/hello, world/) - }.should raise_error(Test::Unit::AssertionFailedError) - end - end - end - - describe "#failure_message" do - it "should include the content string" do - hc = Webrat::Matchers::HasContent.new("hello, world!") - hc.matches?(@body) - - hc.failure_message.should include("\"hello, world!\"") - end - - it "should include the content regular expresson" do - hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/) - hc.matches?(@body) - - hc.failure_message.should include("/hello,\\sworld!/") - end - - it "should include the element's inner content" do - hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/) - hc.matches?(@body) - - hc.failure_message.should include("hello, world!") - end - end - end -end