1 require File.dirname(__FILE__) + '/../spec_helper'
2 require 'mspec/expectations/expectations'
3 require 'mspec/matchers/equal_element'
5 describe EqualElementMatcher do
6 it "matches if it finds an element with the passed name, no matter what attributes/content" do
7 EqualElementMatcher.new("A").matches?('<A></A>').should be_true
8 EqualElementMatcher.new("A").matches?('<A HREF="http://example.com"></A>').should be_true
9 EqualElementMatcher.new("A").matches?('<A HREF="http://example.com"></A>').should be_true
11 EqualElementMatcher.new("BASE").matches?('<BASE></A>').should be_false
12 EqualElementMatcher.new("BASE").matches?('<A></BASE>').should be_false
13 EqualElementMatcher.new("BASE").matches?('<A></A>').should be_false
14 EqualElementMatcher.new("BASE").matches?('<A HREF="http://example.com"></A>').should be_false
15 EqualElementMatcher.new("BASE").matches?('<A HREF="http://example.com"></A>').should be_false
18 it "matches if it finds an element with the passed name and the passed attributes" do
19 EqualElementMatcher.new("A", {}).matches?('<A></A>').should be_true
20 EqualElementMatcher.new("A", nil).matches?('<A HREF="http://example.com"></A>').should be_true
21 EqualElementMatcher.new("A", "HREF" => "http://example.com").matches?('<A HREF="http://example.com"></A>').should be_true
23 EqualElementMatcher.new("A", {}).matches?('<A HREF="http://example.com"></A>').should be_false
24 EqualElementMatcher.new("A", "HREF" => "http://example.com").matches?('<A></A>').should be_false
25 EqualElementMatcher.new("A", "HREF" => "http://example.com").matches?('<A HREF="http://test.com"></A>').should be_false
26 EqualElementMatcher.new("A", "HREF" => "http://example.com").matches?('<A HREF="http://example.com" HREF="http://example.com"></A>').should be_false
29 it "matches if it finds an element with the passed name, the passed attributes and the passed content" do
30 EqualElementMatcher.new("A", {}, "").matches?('<A></A>').should be_true
31 EqualElementMatcher.new("A", {"HREF" => "http://example.com"}, "Example").matches?('<A HREF="http://example.com">Example</A>').should be_true
33 EqualElementMatcher.new("A", {}, "Test").matches?('<A></A>').should be_false
34 EqualElementMatcher.new("A", {"HREF" => "http://example.com"}, "Example").matches?('<A HREF="http://example.com"></A>').should be_false
35 EqualElementMatcher.new("A", {"HREF" => "http://example.com"}, "Example").matches?('<A HREF="http://example.com">Test</A>').should be_false
38 it "can match unclosed elements" do
39 EqualElementMatcher.new("BASE", nil, nil, :not_closed => true).matches?('<BASE>').should be_true
40 EqualElementMatcher.new("BASE", {"HREF" => "http://example.com"}, nil, :not_closed => true).matches?('<BASE HREF="http://example.com">').should be_true
41 EqualElementMatcher.new("BASE", {"HREF" => "http://example.com"}, "Example", :not_closed => true).matches?('<BASE HREF="http://example.com">Example').should be_true
43 EqualElementMatcher.new("BASE", {}, nil, :not_closed => true).matches?('<BASE HREF="http://example.com">').should be_false
44 EqualElementMatcher.new("BASE", {"HREF" => "http://example.com"}, "", :not_closed => true).matches?('<BASE HREF="http://example.com">Example').should be_false
45 EqualElementMatcher.new("BASE", {"HREF" => "http://example.com"}, "Test", :not_closed => true).matches?('<BASE HREF="http://example.com">Example').should be_false
48 it "provides a useful failure message" do
49 equal_element = EqualElementMatcher.new("A", {}, "Test")
50 equal_element.matches?('<A></A>').should be_false
51 equal_element.failure_message.should == [%{Expected "<A></A>"\n}, %{to be a 'A' element with no attributes and "Test" as content}]
53 equal_element = EqualElementMatcher.new("A", {}, "")
54 equal_element.matches?('<A>Test</A>').should be_false
55 equal_element.failure_message.should == [%{Expected "<A>Test</A>"\n}, %{to be a 'A' element with no attributes and no content}]
57 equal_element = EqualElementMatcher.new("A", "HREF" => "http://www.example.com")
58 equal_element.matches?('<A>Test</A>').should be_false
59 equal_element.failure_message.should == [%{Expected "<A>Test</A>"\n}, %{to be a 'A' element with HREF="http://www.example.com" and any content}]
62 it "provides a useful negative failure message" do
63 equal_element = EqualElementMatcher.new("A", {}, "Test")
64 equal_element.matches?('<A></A>').should be_false
65 equal_element.negative_failure_message.should == [%{Expected "<A></A>"\n}, %{not to be a 'A' element with no attributes and "Test" as content}]
67 equal_element = EqualElementMatcher.new("A", {}, "")
68 equal_element.matches?('<A>Test</A>').should be_false
69 equal_element.negative_failure_message.should == [%{Expected "<A>Test</A>"\n}, %{not to be a 'A' element with no attributes and no content}]
71 equal_element = EqualElementMatcher.new("A", "HREF" => "http://www.example.com")
72 equal_element.matches?('<A>Test</A>').should be_false
73 equal_element.negative_failure_message.should == [%{Expected "<A>Test</A>"\n}, %{not to be a 'A' element with HREF="http://www.example.com" and any content}]