~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/test/controller/html-scanner/tag_node_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'abstract_unit'
2
 
 
3
 
class TagNodeTest < Test::Unit::TestCase
4
 
  def test_open_without_attributes
5
 
    node = tag("<tag>")
6
 
    assert_equal "tag", node.name
7
 
    assert_equal Hash.new, node.attributes
8
 
    assert_nil node.closing
9
 
  end
10
 
  
11
 
  def test_open_with_attributes
12
 
    node = tag("<TAG1 foo=hey_ho x:bar=\"blah blah\" BAZ='blah blah blah' >")
13
 
    assert_equal "tag1", node.name
14
 
    assert_equal "hey_ho", node["foo"]
15
 
    assert_equal "blah blah", node["x:bar"]
16
 
    assert_equal "blah blah blah", node["baz"]
17
 
  end
18
 
  
19
 
  def test_self_closing_without_attributes
20
 
    node = tag("<tag/>")
21
 
    assert_equal "tag", node.name
22
 
    assert_equal Hash.new, node.attributes
23
 
    assert_equal :self, node.closing
24
 
  end
25
 
  
26
 
  def test_self_closing_with_attributes
27
 
    node = tag("<tag a=b/>")
28
 
    assert_equal "tag", node.name
29
 
    assert_equal( { "a" => "b" }, node.attributes )
30
 
    assert_equal :self, node.closing
31
 
  end
32
 
  
33
 
  def test_closing_without_attributes
34
 
    node = tag("</tag>")
35
 
    assert_equal "tag", node.name
36
 
    assert_nil node.attributes
37
 
    assert_equal :close, node.closing
38
 
  end
39
 
  
40
 
  def test_bracket_op_when_no_attributes
41
 
    node = tag("</tag>")
42
 
    assert_nil node["foo"]
43
 
  end
44
 
 
45
 
  def test_bracket_op_when_attributes
46
 
    node = tag("<tag a=b/>")
47
 
    assert_equal "b", node["a"]
48
 
  end
49
 
  
50
 
  def test_attributes_with_escaped_quotes
51
 
    node = tag("<tag a='b\\'c' b=\"bob \\\"float\\\"\">")
52
 
    assert_equal "b\\'c", node["a"]
53
 
    assert_equal "bob \\\"float\\\"", node["b"]
54
 
  end
55
 
  
56
 
  def test_to_s
57
 
    node = tag("<a b=c d='f' g=\"h 'i'\" />")
58
 
    assert_equal %(<a b='c' d='f' g='h \\'i\\'' />), node.to_s
59
 
  end
60
 
  
61
 
  def test_tag
62
 
    assert tag("<tag>").tag?
63
 
  end
64
 
  
65
 
  def test_match_tag_as_string
66
 
    assert tag("<tag>").match(:tag => "tag")
67
 
    assert !tag("<tag>").match(:tag => "b")
68
 
  end
69
 
  
70
 
  def test_match_tag_as_regexp
71
 
    assert tag("<tag>").match(:tag => /t.g/)
72
 
    assert !tag("<tag>").match(:tag => /t[bqs]g/)
73
 
  end
74
 
 
75
 
  def test_match_attributes_as_string
76
 
    t = tag("<tag a=something b=else />")
77
 
    assert t.match(:attributes => {"a" => "something"})
78
 
    assert t.match(:attributes => {"b" => "else"})
79
 
  end
80
 
  
81
 
  def test_match_attributes_as_regexp
82
 
    t = tag("<tag a=something b=else />")
83
 
    assert t.match(:attributes => {"a" => /^something$/})
84
 
    assert t.match(:attributes => {"b" =>  /e.*e/})
85
 
    assert t.match(:attributes => {"a" => /me..i/, "b" => /.ls.$/})
86
 
  end
87
 
  
88
 
  def test_match_attributes_as_number
89
 
    t = tag("<tag a=15 b=3.1415 />")
90
 
    assert t.match(:attributes => {"a" => 15})
91
 
    assert t.match(:attributes => {"b" => 3.1415})
92
 
    assert t.match(:attributes => {"a" => 15, "b" => 3.1415})
93
 
  end
94
 
  
95
 
  def test_match_attributes_exist
96
 
    t = tag("<tag a=15 b=3.1415 />")
97
 
    assert t.match(:attributes => {"a" => true})
98
 
    assert t.match(:attributes => {"b" => true})
99
 
    assert t.match(:attributes => {"a" => true, "b" => true})
100
 
  end
101
 
  
102
 
  def test_match_attributes_not_exist
103
 
    t = tag("<tag a=15 b=3.1415 />")
104
 
    assert t.match(:attributes => {"c" => false})
105
 
    assert t.match(:attributes => {"c" => nil})
106
 
    assert t.match(:attributes => {"a" => true, "c" => false})
107
 
  end
108
 
  
109
 
  def test_match_parent_success
110
 
    t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
111
 
    assert t.match(:parent => {:tag => "foo", :attributes => {"k" => /v.l/, "j" => false}})
112
 
  end
113
 
  
114
 
  def test_match_parent_fail
115
 
    t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
116
 
    assert !t.match(:parent => {:tag => /kafka/})
117
 
  end
118
 
  
119
 
  def test_match_child_success
120
 
    t = tag("<tag x:k='something'>")
121
 
    tag("<child v=john a=kelly>", t)
122
 
    tag("<sib m=vaughn v=james>", t)
123
 
    assert t.match(:child => { :tag => "sib", :attributes => {"v" => /j/}})
124
 
    assert t.match(:child => { :attributes => {"a" => "kelly"}})
125
 
  end
126
 
  
127
 
  def test_match_child_fail
128
 
    t = tag("<tag x:k='something'>")
129
 
    tag("<child v=john a=kelly>", t)
130
 
    tag("<sib m=vaughn v=james>", t)
131
 
    assert !t.match(:child => { :tag => "sib", :attributes => {"v" => /r/}})
132
 
    assert !t.match(:child => { :attributes => {"v" => false}})
133
 
  end
134
 
  
135
 
  def test_match_ancestor_success
136
 
    t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
137
 
    assert t.match(:ancestor => {:tag => "parent", :attributes => {"a" => /ll/}})
138
 
    assert t.match(:ancestor => {:attributes => {"m" => "vaughn"}})
139
 
  end
140
 
  
141
 
  def test_match_ancestor_fail
142
 
    t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
143
 
    assert !t.match(:ancestor => {:tag => /^parent/, :attributes => {"v" => /m/}})
144
 
    assert !t.match(:ancestor => {:attributes => {"v" => false}})
145
 
  end
146
 
 
147
 
  def test_match_descendant_success
148
 
    tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
149
 
    assert t.match(:descendant => {:tag => "child", :attributes => {"a" => /ll/}})
150
 
    assert t.match(:descendant => {:attributes => {"m" => "vaughn"}})
151
 
  end
152
 
  
153
 
  def test_match_descendant_fail
154
 
    tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
155
 
    assert !t.match(:descendant => {:tag => /^child/, :attributes => {"v" => /m/}})
156
 
    assert !t.match(:descendant => {:attributes => {"v" => false}})
157
 
  end
158
 
  
159
 
  def test_match_child_count
160
 
    t = tag("<tag x:k='something'>")
161
 
    tag("hello", t)
162
 
    tag("<child v=john a=kelly>", t)
163
 
    tag("<sib m=vaughn v=james>", t)
164
 
    assert t.match(:children => { :count => 2 })
165
 
    assert t.match(:children => { :count => 2..4 })
166
 
    assert t.match(:children => { :less_than => 4 })
167
 
    assert t.match(:children => { :greater_than => 1 })
168
 
    assert !t.match(:children => { :count => 3 })
169
 
  end
170
 
 
171
 
  def test_conditions_as_strings
172
 
    t = tag("<tag x:k='something'>")
173
 
    assert t.match("tag" => "tag")
174
 
    assert t.match("attributes" => { "x:k" => "something" })
175
 
    assert !t.match("tag" => "gat")
176
 
    assert !t.match("attributes" => { "x:j" => "something" })
177
 
  end
178
 
 
179
 
  def test_attributes_as_symbols
180
 
    t = tag("<child v=john a=kelly>")
181
 
    assert t.match(:attributes => { :v => /oh/ })
182
 
    assert t.match(:attributes => { :a => /ll/ })
183
 
  end
184
 
 
185
 
  def test_match_sibling
186
 
    t = tag("<tag x:k='something'>")
187
 
    tag("hello", t)
188
 
    tag("<span a=b>", t)
189
 
    tag("world", t)
190
 
    m = tag("<span k=r>", t)
191
 
    tag("<span m=l>", t)
192
 
 
193
 
    assert m.match(:sibling => {:tag => "span", :attributes => {:a => true}})
194
 
    assert m.match(:sibling => {:tag => "span", :attributes => {:m => true}})
195
 
    assert !m.match(:sibling => {:tag => "span", :attributes => {:k => true}})
196
 
  end
197
 
 
198
 
  def test_match_sibling_before
199
 
    t = tag("<tag x:k='something'>")
200
 
    tag("hello", t)
201
 
    tag("<span a=b>", t)
202
 
    tag("world", t)
203
 
    m = tag("<span k=r>", t)
204
 
    tag("<span m=l>", t)
205
 
 
206
 
    assert m.match(:before => {:tag => "span", :attributes => {:m => true}})
207
 
    assert !m.match(:before => {:tag => "span", :attributes => {:a => true}})
208
 
    assert !m.match(:before => {:tag => "span", :attributes => {:k => true}})
209
 
  end
210
 
 
211
 
  def test_match_sibling_after
212
 
    t = tag("<tag x:k='something'>")
213
 
    tag("hello", t)
214
 
    tag("<span a=b>", t)
215
 
    tag("world", t)
216
 
    m = tag("<span k=r>", t)
217
 
    tag("<span m=l>", t)
218
 
 
219
 
    assert m.match(:after => {:tag => "span", :attributes => {:a => true}})
220
 
    assert !m.match(:after => {:tag => "span", :attributes => {:m => true}})
221
 
    assert !m.match(:after => {:tag => "span", :attributes => {:k => true}})
222
 
  end
223
 
 
224
 
  def test_to_s
225
 
    t = tag("<b x='foo'>")
226
 
    tag("hello", t)
227
 
    tag("<hr />", t)
228
 
    assert_equal %(<b x="foo">hello<hr /></b>), t.to_s
229
 
  end
230
 
 
231
 
  private
232
 
  
233
 
    def tag(content, parent=nil)
234
 
      node = HTML::Node.parse(parent,0,0,content)
235
 
      parent.children << node if parent
236
 
      node
237
 
    end
238
 
end