Updated to latest rspec
[gitorious:georgyos-clone.git] / vendor / plugins / rspec-rails / lib / spec / rails / matchers / assert_select.rb
1 # This is a wrapper of assert_select for rspec.
2
3 module Spec # :nodoc:
4   module Rails
5     module Matchers
6
7       class AssertSelect #:nodoc:
8
9         def initialize(assertion, spec_scope, *args, &block)
10           @assertion = assertion
11           @spec_scope = spec_scope
12           @args = args
13           @block = block
14         end
15         
16         def matches?(response_or_text, &block)
17           if ActionController::TestResponse === response_or_text and
18                    response_or_text.headers.key?('Content-Type') and
19                    response_or_text.headers['Content-Type'].to_sym == :xml
20             @args.unshift(HTML::Document.new(response_or_text.body, false, true).root)           
21           elsif String === response_or_text
22             @args.unshift(HTML::Document.new(response_or_text).root)
23           end
24           @block = block if block
25           begin
26             @spec_scope.send(@assertion, *@args, &@block)
27           rescue ::Test::Unit::AssertionFailedError => @error
28           end
29           
30           @error.nil?
31         end
32
33         def failure_message; @error.message; end
34         def negative_failure_message; "should not #{description}, but did"; end
35
36         def description
37           {
38             :assert_select => "have tag#{format_args(*@args)}",
39             :assert_select_email => "send email#{format_args(*@args)}",
40           }[@assertion]
41         end
42
43       private
44
45         def format_args(*args)
46           return "" if args.empty?
47           return "(#{arg_list(*args)})"
48         end
49
50         def arg_list(*args)
51           args.collect do |arg|
52             arg.respond_to?(:description) ? arg.description : arg.inspect
53           end.join(", ")
54         end
55         
56       end
57       
58       # :call-seq:
59       #   response.should have_tag(*args, &block)
60       #   string.should have_tag(*args, &block)
61       #
62       # wrapper for assert_select with additional support for using
63       # css selectors to set expectation on Strings. Use this in
64       # helper specs, for example, to set expectations on the results
65       # of helper methods.
66       #
67       # == Examples
68       #
69       #   # in a controller spec
70       #   response.should have_tag("div", "some text")
71       #
72       #   # in a helper spec (person_address_tag is a method in the helper)
73       #   person_address_tag.should have_tag("input#person_address")
74       #
75       # see documentation for assert_select at http://api.rubyonrails.org/
76       def have_tag(*args, &block)
77         AssertSelect.new(:assert_select, self, *args, &block)
78       end
79     
80       # wrapper for a nested assert_select
81       #
82       #   response.should have_tag("div#form") do
83       #     with_tag("input#person_name[name=?]", "person[name]")
84       #   end
85       #
86       # see documentation for assert_select at http://api.rubyonrails.org/
87       def with_tag(*args, &block)
88         should have_tag(*args, &block)
89       end
90     
91       # wrapper for a nested assert_select with false
92       #
93       #   response.should have_tag("div#1") do
94       #     without_tag("span", "some text that shouldn't be there")
95       #   end
96       #
97       # see documentation for assert_select at http://api.rubyonrails.org/
98       def without_tag(*args, &block)
99         should_not have_tag(*args, &block)
100       end
101     
102       # :call-seq:
103       #   response.should have_rjs(*args, &block)
104       #
105       # wrapper for assert_select_rjs
106       #
107       # see documentation for assert_select_rjs at http://api.rubyonrails.org/
108       def have_rjs(*args, &block)
109         AssertSelect.new(:assert_select_rjs, self, *args, &block)
110       end
111       
112       # :call-seq:
113       #   response.should send_email(*args, &block)
114       #
115       # wrapper for assert_select_email
116       #
117       # see documentation for assert_select_email at http://api.rubyonrails.org/
118       def send_email(*args, &block)
119         AssertSelect.new(:assert_select_email, self, *args, &block)
120       end
121       
122       # wrapper for assert_select_encoded
123       #
124       # see documentation for assert_select_encoded at http://api.rubyonrails.org/
125       def with_encoded(*args, &block)
126         should AssertSelect.new(:assert_select_encoded, self, *args, &block)
127       end
128     end
129   end
130 end