ghazel-erubis_rails_helper 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/README.rdoc +66 -0
  2. data/lib/erubis_rails_helper/erubis/generator.rb +100 -0
  3. data/lib/erubis_rails_helper/template_handlers/erubis.rb +260 -0
  4. data/lib/erubis_rails_helper.rb +20 -0
  5. data/test/abstract_unit.rb +51 -0
  6. data/test/controller/action_pack_assertions_test.rb +516 -0
  7. data/test/controller/addresses_render_test.rb +37 -0
  8. data/test/controller/assert_select_test.rb +734 -0
  9. data/test/controller/base_test.rb +217 -0
  10. data/test/controller/benchmark_test.rb +32 -0
  11. data/test/controller/caching_test.rb +690 -0
  12. data/test/controller/capture_test.rb +66 -0
  13. data/test/controller/content_type_test.rb +168 -0
  14. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  15. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  16. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  17. data/test/controller/cookie_test.rb +111 -0
  18. data/test/controller/deprecation/deprecated_base_methods_test.rb +32 -0
  19. data/test/controller/dispatcher_test.rb +103 -0
  20. data/test/controller/fake_controllers.rb +33 -0
  21. data/test/controller/fake_models.rb +19 -0
  22. data/test/controller/filter_params_test.rb +49 -0
  23. data/test/controller/filters_test.rb +885 -0
  24. data/test/controller/flash_test.rb +142 -0
  25. data/test/controller/header_test.rb +14 -0
  26. data/test/controller/helper_test.rb +224 -0
  27. data/test/controller/html-scanner/cdata_node_test.rb +15 -0
  28. data/test/controller/html-scanner/document_test.rb +148 -0
  29. data/test/controller/html-scanner/node_test.rb +89 -0
  30. data/test/controller/html-scanner/sanitizer_test.rb +273 -0
  31. data/test/controller/html-scanner/tag_node_test.rb +238 -0
  32. data/test/controller/html-scanner/text_node_test.rb +50 -0
  33. data/test/controller/html-scanner/tokenizer_test.rb +131 -0
  34. data/test/controller/http_basic_authentication_test.rb +88 -0
  35. data/test/controller/http_digest_authentication_test.rb +178 -0
  36. data/test/controller/integration_test.rb +417 -0
  37. data/test/controller/layout_test.rb +204 -0
  38. data/test/controller/logging_test.rb +46 -0
  39. data/test/controller/middleware_stack_test.rb +90 -0
  40. data/test/controller/mime_responds_test.rb +536 -0
  41. data/test/controller/mime_type_test.rb +93 -0
  42. data/test/controller/polymorphic_routes_test.rb +293 -0
  43. data/test/controller/rack_test.rb +294 -0
  44. data/test/controller/record_identifier_test.rb +139 -0
  45. data/test/controller/redirect_test.rb +282 -0
  46. data/test/controller/render_test.rb +1752 -0
  47. data/test/controller/request/json_params_parsing_test.rb +45 -0
  48. data/test/controller/request/multipart_params_parsing_test.rb +223 -0
  49. data/test/controller/request/query_string_parsing_test.rb +120 -0
  50. data/test/controller/request/url_encoded_params_parsing_test.rb +184 -0
  51. data/test/controller/request/xml_params_parsing_test.rb +88 -0
  52. data/test/controller/request_forgery_protection_test.rb +246 -0
  53. data/test/controller/request_test.rb +407 -0
  54. data/test/controller/rescue_test.rb +536 -0
  55. data/test/controller/resources_test.rb +1341 -0
  56. data/test/controller/routing_test.rb +2557 -0
  57. data/test/controller/selector_test.rb +628 -0
  58. data/test/controller/send_file_test.rb +161 -0
  59. data/test/controller/session/cookie_store_test.rb +239 -0
  60. data/test/controller/session/mem_cache_store_test.rb +127 -0
  61. data/test/controller/session/test_session_test.rb +58 -0
  62. data/test/controller/test_test.rb +692 -0
  63. data/test/controller/translation_test.rb +26 -0
  64. data/test/controller/url_rewriter_test.rb +359 -0
  65. data/test/controller/verification_test.rb +270 -0
  66. data/test/controller/view_paths_test.rb +141 -0
  67. data/test/controller/webservice_test.rb +260 -0
  68. data/test/fixtures/alternate_helpers/foo_helper.rb +3 -0
  69. data/test/fixtures/company.rb +10 -0
  70. data/test/fixtures/developer.rb +9 -0
  71. data/test/fixtures/helpers/abc_helper.rb +5 -0
  72. data/test/fixtures/helpers/fun/games_helper.rb +3 -0
  73. data/test/fixtures/helpers/fun/pdf_helper.rb +3 -0
  74. data/test/fixtures/mascot.rb +3 -0
  75. data/test/fixtures/project.rb +3 -0
  76. data/test/fixtures/reply.rb +7 -0
  77. data/test/fixtures/topic.rb +3 -0
  78. data/test/template/asset_tag_helper_test.rb +713 -0
  79. data/test/template/atom_feed_helper_test.rb +286 -0
  80. data/test/template/benchmark_helper_test.rb +86 -0
  81. data/test/template/compiled_templates_test.rb +203 -0
  82. data/test/template/date_helper_i18n_test.rb +120 -0
  83. data/test/template/date_helper_test.rb +2469 -0
  84. data/test/template/erb_util_test.rb +24 -0
  85. data/test/template/form_helper_test.rb +1176 -0
  86. data/test/template/form_options_helper_test.rb +751 -0
  87. data/test/template/form_tag_helper_test.rb +327 -0
  88. data/test/template/javascript_helper_test.rb +106 -0
  89. data/test/template/number_helper_i18n_test.rb +69 -0
  90. data/test/template/number_helper_test.rb +128 -0
  91. data/test/template/prototype_helper_test.rb +639 -0
  92. data/test/template/record_tag_helper_test.rb +58 -0
  93. data/test/template/render_test.rb +291 -0
  94. data/test/template/sanitize_helper_test.rb +48 -0
  95. data/test/template/scriptaculous_helper_test.rb +90 -0
  96. data/test/template/tag_helper_test.rb +97 -0
  97. data/test/template/test_test.rb +54 -0
  98. data/test/template/text_helper_test.rb +520 -0
  99. data/test/template/translation_helper_test.rb +32 -0
  100. data/test/template/url_helper_test.rb +614 -0
  101. data/test/test_erubis_rails_helper.rb +4 -0
  102. data/test/testing_sandbox.rb +15 -0
  103. data/test/view/test_case_test.rb +8 -0
  104. metadata +174 -0
@@ -0,0 +1,273 @@
1
+ require 'abstract_unit'
2
+
3
+ class SanitizerTest < ActionController::TestCase
4
+ def setup
5
+ @sanitizer = nil # used by assert_sanitizer
6
+ end
7
+
8
+ def test_strip_tags
9
+ sanitizer = HTML::FullSanitizer.new
10
+ assert_equal("<<<bad html", sanitizer.sanitize("<<<bad html"))
11
+ assert_equal("<<", sanitizer.sanitize("<<<bad html>"))
12
+ assert_equal("Dont touch me", sanitizer.sanitize("Dont touch me"))
13
+ assert_equal("This is a test.", sanitizer.sanitize("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
14
+ assert_equal("Weirdos", sanitizer.sanitize("Wei<<a>a onclick='alert(document.cookie);'</a>/>rdos"))
15
+ assert_equal("This is a test.", sanitizer.sanitize("This is a test."))
16
+ assert_equal(
17
+ %{This is a test.\n\n\nIt no longer contains any HTML.\n}, sanitizer.sanitize(
18
+ %{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n}))
19
+ assert_equal "This has a here.", sanitizer.sanitize("This has a <!-- comment --> here.")
20
+ assert_equal "This has a here.", sanitizer.sanitize("This has a <![CDATA[<section>]]> here.")
21
+ assert_equal "This has an unclosed ", sanitizer.sanitize("This has an unclosed <![CDATA[<section>]] here...")
22
+ [nil, '', ' '].each { |blank| assert_equal blank, sanitizer.sanitize(blank) }
23
+ end
24
+
25
+ def test_strip_links
26
+ sanitizer = HTML::LinkSanitizer.new
27
+ assert_equal "Dont touch me", sanitizer.sanitize("Dont touch me")
28
+ assert_equal "on my mind\nall day long", sanitizer.sanitize("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
29
+ assert_equal "0wn3d", sanitizer.sanitize("<a href='http://www.rubyonrails.com/'><a href='http://www.rubyonrails.com/' onlclick='steal()'>0wn3d</a></a>")
30
+ assert_equal "Magic", sanitizer.sanitize("<a href='http://www.rubyonrails.com/'>Mag<a href='http://www.ruby-lang.org/'>ic")
31
+ assert_equal "FrrFox", sanitizer.sanitize("<href onlclick='steal()'>FrrFox</a></href>")
32
+ assert_equal "My mind\nall <b>day</b> long", sanitizer.sanitize("<a href='almost'>My mind</a>\n<A href='almost'>all <b>day</b> long</A>")
33
+ assert_equal "all <b>day</b> long", sanitizer.sanitize("<<a>a href='hello'>all <b>day</b> long<</A>/a>")
34
+
35
+ assert_equal "<a<a", sanitizer.sanitize("<a<a")
36
+ end
37
+
38
+ def test_sanitize_form
39
+ assert_sanitized "<form action=\"/foo/bar\" method=\"post\"><input></form>", ''
40
+ end
41
+
42
+ def test_sanitize_plaintext
43
+ raw = "<plaintext><span>foo</span></plaintext>"
44
+ assert_sanitized raw, "<span>foo</span>"
45
+ end
46
+
47
+ def test_sanitize_script
48
+ assert_sanitized "a b c<script language=\"Javascript\">blah blah blah</script>d e f", "a b cd e f"
49
+ end
50
+
51
+ # fucked
52
+ def test_sanitize_js_handlers
53
+ raw = %{onthis="do that" <a href="#" onclick="hello" name="foo" onbogus="remove me">hello</a>}
54
+ assert_sanitized raw, %{onthis="do that" <a name="foo" href="#">hello</a>}
55
+ end
56
+
57
+ def test_sanitize_javascript_href
58
+ raw = %{href="javascript:bang" <a href="javascript:bang" name="hello">foo</a>, <span href="javascript:bang">bar</span>}
59
+ assert_sanitized raw, %{href="javascript:bang" <a name="hello">foo</a>, <span>bar</span>}
60
+ end
61
+
62
+ def test_sanitize_image_src
63
+ raw = %{src="javascript:bang" <img src="javascript:bang" width="5">foo</img>, <span src="javascript:bang">bar</span>}
64
+ assert_sanitized raw, %{src="javascript:bang" <img width="5">foo</img>, <span>bar</span>}
65
+ end
66
+
67
+ HTML::WhiteListSanitizer.allowed_tags.each do |tag_name|
68
+ define_method "test_should_allow_#{tag_name}_tag" do
69
+ assert_sanitized "start <#{tag_name} title=\"1\" onclick=\"foo\">foo <bad>bar</bad> baz</#{tag_name}> end", %(start <#{tag_name} title="1">foo bar baz</#{tag_name}> end)
70
+ end
71
+ end
72
+
73
+ def test_should_allow_anchors
74
+ assert_sanitized %(<a href="foo" onclick="bar"><script>baz</script></a>), %(<a href="foo"></a>)
75
+ end
76
+
77
+ # RFC 3986, sec 4.2
78
+ def test_allow_colons_in_path_component
79
+ assert_sanitized("<a href=\"./this:that\">foo</a>")
80
+ end
81
+
82
+ %w(src width height alt).each do |img_attr|
83
+ define_method "test_should_allow_image_#{img_attr}_attribute" do
84
+ assert_sanitized %(<img #{img_attr}="foo" onclick="bar" />), %(<img #{img_attr}="foo" />)
85
+ end
86
+ end
87
+
88
+ def test_should_handle_non_html
89
+ assert_sanitized 'abc'
90
+ end
91
+
92
+ def test_should_handle_blank_text
93
+ assert_sanitized nil
94
+ assert_sanitized ''
95
+ end
96
+
97
+ def test_should_allow_custom_tags
98
+ text = "<u>foo</u>"
99
+ sanitizer = HTML::WhiteListSanitizer.new
100
+ assert_equal(text, sanitizer.sanitize(text, :tags => %w(u)))
101
+ end
102
+
103
+ def test_should_allow_only_custom_tags
104
+ text = "<u>foo</u> with <i>bar</i>"
105
+ sanitizer = HTML::WhiteListSanitizer.new
106
+ assert_equal("<u>foo</u> with bar", sanitizer.sanitize(text, :tags => %w(u)))
107
+ end
108
+
109
+ def test_should_allow_custom_tags_with_attributes
110
+ text = %(<blockquote cite="http://example.com/">foo</blockquote>)
111
+ sanitizer = HTML::WhiteListSanitizer.new
112
+ assert_equal(text, sanitizer.sanitize(text))
113
+ end
114
+
115
+ def test_should_allow_custom_tags_with_custom_attributes
116
+ text = %(<blockquote foo="bar">Lorem ipsum</blockquote>)
117
+ sanitizer = HTML::WhiteListSanitizer.new
118
+ assert_equal(text, sanitizer.sanitize(text, :attributes => ['foo']))
119
+ end
120
+
121
+ [%w(img src), %w(a href)].each do |(tag, attr)|
122
+ define_method "test_should_strip_#{attr}_attribute_in_#{tag}_with_bad_protocols" do
123
+ assert_sanitized %(<#{tag} #{attr}="javascript:bang" title="1">boo</#{tag}>), %(<#{tag} title="1">boo</#{tag}>)
124
+ end
125
+ end
126
+
127
+ def test_should_flag_bad_protocols
128
+ sanitizer = HTML::WhiteListSanitizer.new
129
+ %w(about chrome data disk hcp help javascript livescript lynxcgi lynxexec ms-help ms-its mhtml mocha opera res resource shell vbscript view-source vnd.ms.radio wysiwyg).each do |proto|
130
+ assert sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://bad")
131
+ end
132
+ end
133
+
134
+ def test_should_accept_good_protocols
135
+ sanitizer = HTML::WhiteListSanitizer.new
136
+ HTML::WhiteListSanitizer.allowed_protocols.each do |proto|
137
+ assert !sanitizer.send(:contains_bad_protocols?, 'src', "#{proto}://good")
138
+ end
139
+ end
140
+
141
+ def test_should_reject_hex_codes_in_protocol
142
+ assert_sanitized %(<a href="&#37;6A&#37;61&#37;76&#37;61&#37;73&#37;63&#37;72&#37;69&#37;70&#37;74&#37;3A&#37;61&#37;6C&#37;65&#37;72&#37;74&#37;28&#37;22&#37;58&#37;53&#37;53&#37;22&#37;29">1</a>), "<a>1</a>"
143
+ assert @sanitizer.send(:contains_bad_protocols?, 'src', "%6A%61%76%61%73%63%72%69%70%74%3A%61%6C%65%72%74%28%22%58%53%53%22%29")
144
+ end
145
+
146
+ def test_should_block_script_tag
147
+ assert_sanitized %(<SCRIPT\nSRC=http://ha.ckers.org/xss.js></SCRIPT>), ""
148
+ end
149
+
150
+ [%(<IMG SRC="javascript:alert('XSS');">),
151
+ %(<IMG SRC=javascript:alert('XSS')>),
152
+ %(<IMG SRC=JaVaScRiPt:alert('XSS')>),
153
+ %(<IMG """><SCRIPT>alert("XSS")</SCRIPT>">),
154
+ %(<IMG SRC=javascript:alert(&quot;XSS&quot;)>),
155
+ %(<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>),
156
+ %(<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>),
157
+ %(<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>),
158
+ %(<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>),
159
+ %(<IMG SRC="jav\tascript:alert('XSS');">),
160
+ %(<IMG SRC="jav&#x09;ascript:alert('XSS');">),
161
+ %(<IMG SRC="jav&#x0A;ascript:alert('XSS');">),
162
+ %(<IMG SRC="jav&#x0D;ascript:alert('XSS');">),
163
+ %(<IMG SRC=" &#14; javascript:alert('XSS');">),
164
+ %(<IMG SRC=`javascript:alert("RSnake says, 'XSS'")`>)].each_with_index do |img_hack, i|
165
+ define_method "test_should_not_fall_for_xss_image_hack_#{i+1}" do
166
+ assert_sanitized img_hack, "<img>"
167
+ end
168
+ end
169
+
170
+ def test_should_sanitize_tag_broken_up_by_null
171
+ assert_sanitized %(<SCR\0IPT>alert(\"XSS\")</SCR\0IPT>), "alert(\"XSS\")"
172
+ end
173
+
174
+ def test_should_sanitize_invalid_script_tag
175
+ assert_sanitized %(<SCRIPT/XSS SRC="http://ha.ckers.org/xss.js"></SCRIPT>), ""
176
+ end
177
+
178
+ def test_should_sanitize_script_tag_with_multiple_open_brackets
179
+ assert_sanitized %(<<SCRIPT>alert("XSS");//<</SCRIPT>), "&lt;"
180
+ assert_sanitized %(<iframe src=http://ha.ckers.org/scriptlet.html\n<a), %(&lt;a)
181
+ end
182
+
183
+ def test_should_sanitize_unclosed_script
184
+ assert_sanitized %(<SCRIPT SRC=http://ha.ckers.org/xss.js?<B>), "<b>"
185
+ end
186
+
187
+ def test_should_sanitize_half_open_scripts
188
+ assert_sanitized %(<IMG SRC="javascript:alert('XSS')"), "<img>"
189
+ end
190
+
191
+ def test_should_not_fall_for_ridiculous_hack
192
+ img_hack = %(<IMG\nSRC\n=\n"\nj\na\nv\na\ns\nc\nr\ni\np\nt\n:\na\nl\ne\nr\nt\n(\n'\nX\nS\nS\n'\n)\n"\n>)
193
+ assert_sanitized img_hack, "<img>"
194
+ end
195
+
196
+ # fucked
197
+ def test_should_sanitize_attributes
198
+ assert_sanitized %(<SPAN title="'><script>alert()</script>">blah</SPAN>), %(<span title="'&gt;&lt;script&gt;alert()&lt;/script&gt;">blah</span>)
199
+ end
200
+
201
+ def test_should_sanitize_illegal_style_properties
202
+ raw = %(display:block; position:absolute; left:0; top:0; width:100%; height:100%; z-index:1; background-color:black; background-image:url(http://www.ragingplatypus.com/i/cam-full.jpg); background-x:center; background-y:center; background-repeat:repeat;)
203
+ expected = %(display: block; width: 100%; height: 100%; background-color: black; background-image: ; background-x: center; background-y: center;)
204
+ assert_equal expected, sanitize_css(raw)
205
+ end
206
+
207
+ def test_should_sanitize_with_trailing_space
208
+ raw = "display:block; "
209
+ expected = "display: block;"
210
+ assert_equal expected, sanitize_css(raw)
211
+ end
212
+
213
+ def test_should_sanitize_xul_style_attributes
214
+ raw = %(-moz-binding:url('http://ha.ckers.org/xssmoz.xml#xss'))
215
+ assert_equal '', sanitize_css(raw)
216
+ end
217
+
218
+ def test_should_sanitize_invalid_tag_names
219
+ assert_sanitized(%(a b c<script/XSS src="http://ha.ckers.org/xss.js"></script>d e f), "a b cd e f")
220
+ end
221
+
222
+ def test_should_sanitize_non_alpha_and_non_digit_characters_in_tags
223
+ assert_sanitized('<a onclick!#$%&()*~+-_.,:;?@[/|\]^`=alert("XSS")>foo</a>', "<a>foo</a>")
224
+ end
225
+
226
+ def test_should_sanitize_invalid_tag_names_in_single_tags
227
+ assert_sanitized('<img/src="http://ha.ckers.org/xss.js"/>', "<img />")
228
+ end
229
+
230
+ def test_should_sanitize_img_dynsrc_lowsrc
231
+ assert_sanitized(%(<img lowsrc="javascript:alert('XSS')" />), "<img />")
232
+ end
233
+
234
+ def test_should_sanitize_div_background_image_unicode_encoded
235
+ raw = %(background-image:\0075\0072\006C\0028'\006a\0061\0076\0061\0073\0063\0072\0069\0070\0074\003a\0061\006c\0065\0072\0074\0028.1027\0058.1053\0053\0027\0029'\0029)
236
+ assert_equal '', sanitize_css(raw)
237
+ end
238
+
239
+ def test_should_sanitize_div_style_expression
240
+ raw = %(width: expression(alert('XSS'));)
241
+ assert_equal '', sanitize_css(raw)
242
+ end
243
+
244
+ def test_should_sanitize_img_vbscript
245
+ assert_sanitized %(<img src='vbscript:msgbox("XSS")' />), '<img />'
246
+ end
247
+
248
+ def test_should_sanitize_cdata_section
249
+ assert_sanitized "<![CDATA[<span>section</span>]]>", "&lt;![CDATA[&lt;span>section&lt;/span>]]>"
250
+ end
251
+
252
+ def test_should_sanitize_unterminated_cdata_section
253
+ assert_sanitized "<![CDATA[<span>neverending...", "&lt;![CDATA[&lt;span>neverending...]]>"
254
+ end
255
+
256
+ def test_should_not_mangle_urls_with_ampersand
257
+ assert_sanitized %{<a href=\"http://www.domain.com?var1=1&amp;var2=2\">my link</a>}
258
+ end
259
+
260
+ protected
261
+ def assert_sanitized(input, expected = nil)
262
+ @sanitizer ||= HTML::WhiteListSanitizer.new
263
+ if input
264
+ assert_dom_equal expected || input, @sanitizer.sanitize(input)
265
+ else
266
+ assert_nil @sanitizer.sanitize(input)
267
+ end
268
+ end
269
+
270
+ def sanitize_css(input)
271
+ (@sanitizer ||= HTML::WhiteListSanitizer.new).sanitize_css(input)
272
+ end
273
+ end
@@ -0,0 +1,238 @@
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
@@ -0,0 +1,50 @@
1
+ require 'abstract_unit'
2
+
3
+ class TextNodeTest < Test::Unit::TestCase
4
+ def setup
5
+ @node = HTML::Text.new(nil, 0, 0, "hello, howdy, aloha, annyeong")
6
+ end
7
+
8
+ def test_to_s
9
+ assert_equal "hello, howdy, aloha, annyeong", @node.to_s
10
+ end
11
+
12
+ def test_find_string
13
+ assert_equal @node, @node.find("hello, howdy, aloha, annyeong")
14
+ assert_equal false, @node.find("bogus")
15
+ end
16
+
17
+ def test_find_regexp
18
+ assert_equal @node, @node.find(/an+y/)
19
+ assert_nil @node.find(/b/)
20
+ end
21
+
22
+ def test_find_hash
23
+ assert_equal @node, @node.find(:content => /howdy/)
24
+ assert_nil @node.find(:content => /^howdy$/)
25
+ assert_equal false, @node.find(:content => "howdy")
26
+ end
27
+
28
+ def test_find_other
29
+ assert_nil @node.find(:hello)
30
+ end
31
+
32
+ def test_match_string
33
+ assert @node.match("hello, howdy, aloha, annyeong")
34
+ assert_equal false, @node.match("bogus")
35
+ end
36
+
37
+ def test_match_regexp
38
+ assert_not_nil @node, @node.match(/an+y/)
39
+ assert_nil @node.match(/b/)
40
+ end
41
+
42
+ def test_match_hash
43
+ assert_not_nil @node, @node.match(:content => "howdy")
44
+ assert_nil @node.match(:content => /^howdy$/)
45
+ end
46
+
47
+ def test_match_other
48
+ assert_nil @node.match(:hello)
49
+ end
50
+ end