80249e0e8342bc1c8d8637e8c181ed6733e9460a
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / assertions / tag_assertions.rb
1 module ActionController
2 module Assertions
3 # Pair of assertions to testing elements in the HTML output of the response.
4 module TagAssertions
5 # Asserts that there is a tag/node/element in the body of the response
6 # that meets all of the given conditions. The +conditions+ parameter must
7 # be a hash of any of the following keys (all are optional):
8 #
9 # * <tt>:tag</tt>: the node type must match the corresponding value
10 # * <tt>:attributes</tt>: a hash. The node's attributes must match the
11 # corresponding values in the hash.
12 # * <tt>:parent</tt>: a hash. The node's parent must match the
13 # corresponding hash.
14 # * <tt>:child</tt>: a hash. At least one of the node's immediate children
15 # must meet the criteria described by the hash.
16 # * <tt>:ancestor</tt>: a hash. At least one of the node's ancestors must
17 # meet the criteria described by the hash.
18 # * <tt>:descendant</tt>: a hash. At least one of the node's descendants
19 # must meet the criteria described by the hash.
20 # * <tt>:sibling</tt>: a hash. At least one of the node's siblings must
21 # meet the criteria described by the hash.
22 # * <tt>:after</tt>: a hash. The node must be after any sibling meeting
23 # the criteria described by the hash, and at least one sibling must match.
24 # * <tt>:before</tt>: a hash. The node must be before any sibling meeting
25 # the criteria described by the hash, and at least one sibling must match.
26 # * <tt>:children</tt>: a hash, for counting children of a node. Accepts
27 # the keys:
28 # * <tt>:count</tt>: either a number or a range which must equal (or
29 # include) the number of children that match.
30 # * <tt>:less_than</tt>: the number of matching children must be less
31 # than this number.
32 # * <tt>:greater_than</tt>: the number of matching children must be
33 # greater than this number.
34 # * <tt>:only</tt>: another hash consisting of the keys to use
35 # to match on the children, and only matching children will be
36 # counted.
37 # * <tt>:content</tt>: the textual content of the node must match the
38 # given value. This will not match HTML tags in the body of a
39 # tag--only text.
40 #
41 # Conditions are matched using the following algorithm:
42 #
43 # * if the condition is a string, it must be a substring of the value.
44 # * if the condition is a regexp, it must match the value.
45 # * if the condition is a number, the value must match number.to_s.
46 # * if the condition is +true+, the value must not be +nil+.
47 # * if the condition is +false+ or +nil+, the value must be +nil+.
48 #
49 # === Examples
50 #
51 # # Assert that there is a "span" tag
52 # assert_tag :tag => "span"
53 #
54 # # Assert that there is a "span" tag with id="x"
55 # assert_tag :tag => "span", :attributes => { :id => "x" }
56 #
57 # # Assert that there is a "span" tag using the short-hand
58 # assert_tag :span
59 #
60 # # Assert that there is a "span" tag with id="x" using the short-hand
61 # assert_tag :span, :attributes => { :id => "x" }
62 #
63 # # Assert that there is a "span" inside of a "div"
64 # assert_tag :tag => "span", :parent => { :tag => "div" }
65 #
66 # # Assert that there is a "span" somewhere inside a table
67 # assert_tag :tag => "span", :ancestor => { :tag => "table" }
68 #
69 # # Assert that there is a "span" with at least one "em" child
70 # assert_tag :tag => "span", :child => { :tag => "em" }
71 #
72 # # Assert that there is a "span" containing a (possibly nested)
73 # # "strong" tag.
74 # assert_tag :tag => "span", :descendant => { :tag => "strong" }
75 #
76 # # Assert that there is a "span" containing between 2 and 4 "em" tags
77 # # as immediate children
78 # assert_tag :tag => "span",
79 # :children => { :count => 2..4, :only => { :tag => "em" } }
80 #
81 # # Get funky: assert that there is a "div", with an "ul" ancestor
82 # # and an "li" parent (with "class" = "enum"), and containing a
83 # # "span" descendant that contains text matching /hello world/
84 # assert_tag :tag => "div",
85 # :ancestor => { :tag => "ul" },
86 # :parent => { :tag => "li",
87 # :attributes => { :class => "enum" } },
88 # :descendant => { :tag => "span",
89 # :child => /hello world/ }
90 #
91 # <b>Please note</b>: +assert_tag+ and +assert_no_tag+ only work
92 # with well-formed XHTML. They recognize a few tags as implicitly self-closing
93 # (like br and hr and such) but will not work correctly with tags
94 # that allow optional closing tags (p, li, td). <em>You must explicitly
95 # close all of your tags to use these assertions.</em>
96 def assert_tag(*opts)
97 clean_backtrace do
98 opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
99 tag = find_tag(opts)
100 assert tag, "expected tag, but no tag found matching #{opts.inspect} in:\n#{@response.body.inspect}"
101 end
102 end
103
104 # Identical to +assert_tag+, but asserts that a matching tag does _not_
105 # exist. (See +assert_tag+ for a full discussion of the syntax.)
106 #
107 # === Examples
108 # # Assert that there is not a "div" containing a "p"
109 # assert_no_tag :tag => "div", :descendant => { :tag => "p" }
110 #
111 # # Assert that an unordered list is empty
112 # assert_no_tag :tag => "ul", :descendant => { :tag => "li" }
113 #
114 # # Assert that there is not a "p" tag with between 1 to 3 "img" tags
115 # # as immediate children
116 # assert_no_tag :tag => "p",
117 # :children => { :count => 1..3, :only => { :tag => "img" } }
118 def assert_no_tag(*opts)
119 clean_backtrace do
120 opts = opts.size > 1 ? opts.last.merge({ :tag => opts.first.to_s }) : opts.first
121 tag = find_tag(opts)
122 assert !tag, "expected no tag, but found tag matching #{opts.inspect} in:\n#{@response.body.inspect}"
123 end
124 end
125 end
126 end
127 end