html2fortitude 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,424 @@
1
+ require 'test_helper'
2
+
3
+ class Html2FortitudeTest < MiniTest::Unit::TestCase
4
+ def test_empty_render_should_remain_empty
5
+ assert_equal '', render('')
6
+ end
7
+
8
+ def test_doctype
9
+ assert_equal '!!!', render("<!DOCTYPE html>")
10
+ assert_equal '!!! 1.1', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">')
11
+ assert_equal '!!! Strict', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
12
+ assert_equal '!!! Frameset', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">')
13
+ assert_equal '!!! Mobile 1.2', render('<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">')
14
+ assert_equal '!!! Basic 1.1', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">')
15
+ assert_equal '!!!', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
16
+ assert_equal '!!! Strict', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">')
17
+ assert_equal '!!! Frameset', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">')
18
+ assert_equal '!!!', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">')
19
+ end
20
+
21
+ def test_id_and_class_should_be_removed_from_hash
22
+ assert_equal '%span#foo.bar', render('<span id="foo" class="bar"> </span>')
23
+ end
24
+
25
+ def test_no_tag_name_for_div_if_class_or_id_is_present
26
+ assert_equal '#foo', render('<div id="foo"> </div>')
27
+ assert_equal '.foo', render('<div class="foo"> </div>')
28
+ end
29
+
30
+ def test_multiple_class_names
31
+ assert_equal '.foo.bar.baz', render('<div class=" foo bar baz "> </div>')
32
+ end
33
+
34
+ def test_should_have_pretty_attributes
35
+ assert_equal('%input{:name => "login", :type => "text"}/',
36
+ render('<input type="text" name="login" />'))
37
+ assert_equal('%meta{:content => "text/html", "http-equiv" => "Content-Type"}/',
38
+ render('<meta http-equiv="Content-Type" content="text/html" />'))
39
+ end
40
+
41
+ def test_should_have_html_style_attributes
42
+ assert_equal('%input(name="login" type="text")/',
43
+ render('<input type="text" name="login" />', :html_style_attributes => true))
44
+ assert_equal('%meta(content="text/html" http-equiv="Content-Type")/',
45
+ render('<meta http-equiv="Content-Type" content="text/html" />', :html_style_attributes => true))
46
+ end
47
+
48
+ def test_should_have_ruby_19_hash_style_attributes
49
+ assert_equal('%input{name: "login", type: "text"}/',
50
+ render('<input type="text" name="login" />', :ruby19_style_attributes => true))
51
+ assert_equal('%meta{content: "text/html", "http-equiv" => "Content-Type"}/',
52
+ render('<meta http-equiv="Content-Type" content="text/html" />', :ruby19_style_attributes => true))
53
+ end
54
+
55
+ def test_should_have_attributes_without_values
56
+ assert_equal('%input{:disabled => "disabled"}/', render('<input disabled>'))
57
+ end
58
+
59
+ def test_class_with_dot_and_hash
60
+ assert_equal('%div{:class => "foo.bar"}', render("<div class='foo.bar'></div>"))
61
+ assert_equal('%div{:class => "foo#bar"}', render("<div class='foo#bar'></div>"))
62
+ assert_equal('.foo.bar{:class => "foo#bar foo.bar"}', render("<div class='foo foo#bar bar foo.bar'></div>"))
63
+ end
64
+
65
+ def test_id_with_dot_and_hash
66
+ assert_equal('%div{:id => "foo.bar"}', render("<div id='foo.bar'></div>"))
67
+ assert_equal('%div{:id => "foo#bar"}', render("<div id='foo#bar'></div>"))
68
+ end
69
+
70
+ def test_interpolation
71
+ assert_equal('Foo \#{bar} baz', render('Foo #{bar} baz'))
72
+ end
73
+
74
+ def test_interpolation_in_attrs
75
+ assert_equal('%p{:foo => "\#{bar} baz"}', render('<p foo="#{bar} baz"></p>'))
76
+ end
77
+
78
+ def test_cdata
79
+ assert_equal(<<FORTITUDE.strip, render(<<HTML))
80
+ %p
81
+ :cdata
82
+ <a foo="bar" baz="bang">
83
+ <div id="foo">flop</div>
84
+ </a>
85
+ FORTITUDE
86
+ <p><![CDATA[
87
+ <a foo="bar" baz="bang">
88
+ <div id="foo">flop</div>
89
+ </a>
90
+ ]]></p>
91
+ HTML
92
+ end
93
+
94
+ def test_self_closing_tag
95
+ assert_equal("%img/", render("<img />"))
96
+ end
97
+
98
+ def test_inline_text
99
+ assert_equal("%p foo", render("<p>foo</p>"))
100
+ end
101
+
102
+ def test_inline_comment
103
+ assert_equal("/ foo", render("<!-- foo -->"))
104
+ assert_equal(<<FORTITUDE.strip, render(<<HTML))
105
+ / foo
106
+ %p bar
107
+ FORTITUDE
108
+ <!-- foo -->
109
+ <p>bar</p>
110
+ HTML
111
+ end
112
+
113
+ def test_non_inline_comment
114
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
115
+ /
116
+ Foo
117
+ Bar
118
+ FORTITUDE
119
+ <!-- Foo
120
+ Bar -->
121
+ HTML
122
+ end
123
+
124
+ def test_non_inline_text
125
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
126
+ %p
127
+ foo
128
+ FORTITUDE
129
+ <p>
130
+ foo
131
+ </p>
132
+ HTML
133
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
134
+ %p
135
+ foo
136
+ FORTITUDE
137
+ <p>
138
+ foo</p>
139
+ HTML
140
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
141
+ %p
142
+ foo
143
+ FORTITUDE
144
+ <p>foo
145
+ </p>
146
+ HTML
147
+ end
148
+
149
+ def test_script_tag
150
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
151
+ :javascript
152
+ function foo() {
153
+ return "12" & "13";
154
+ }
155
+ FORTITUDE
156
+ <script type="text/javascript">
157
+ function foo() {
158
+ return "12" & "13";
159
+ }
160
+ </script>
161
+ HTML
162
+ end
163
+
164
+ def test_script_tag_with_html_escaped_javascript
165
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
166
+ :javascript
167
+ function foo() {
168
+ return "12" & "13";
169
+ }
170
+ FORTITUDE
171
+ <script type="text/javascript">
172
+ function foo() {
173
+ return "12" &amp; "13";
174
+ }
175
+ </script>
176
+ HTML
177
+ end
178
+
179
+ def test_script_tag_with_cdata
180
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
181
+ :javascript
182
+ function foo() {
183
+ return "&amp;";
184
+ }
185
+ FORTITUDE
186
+ <script type="text/javascript">
187
+ <![CDATA[
188
+ function foo() {
189
+ return "&amp;";
190
+ }
191
+ ]]>
192
+ </script>
193
+ HTML
194
+ end
195
+
196
+ def test_pre
197
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
198
+ %pre
199
+ :preserve
200
+ foo
201
+ bar
202
+ baz
203
+ FORTITUDE
204
+ <pre>foo
205
+ bar
206
+ baz</pre>
207
+ HTML
208
+ end
209
+
210
+ def test_pre_code
211
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
212
+ %pre
213
+ %code
214
+ :preserve
215
+ foo
216
+ bar
217
+ baz
218
+ FORTITUDE
219
+ <pre><code>foo
220
+ bar
221
+ baz</code></pre>
222
+ HTML
223
+ end
224
+
225
+ def test_code_without_pre
226
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
227
+ %code
228
+ foo
229
+ bar
230
+ baz
231
+ FORTITUDE
232
+ <code>foo
233
+ bar
234
+ baz</code>
235
+ HTML
236
+ end
237
+
238
+ def test_conditional_comment
239
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
240
+ /[if foo]
241
+ bar
242
+ baz
243
+ FORTITUDE
244
+ <!--[if foo]>
245
+ bar
246
+ baz
247
+ <![endif]-->
248
+ HTML
249
+ end
250
+
251
+ def test_style_to_css_filter
252
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
253
+ :css
254
+ foo {
255
+ bar: baz;
256
+ }
257
+ FORTITUDE
258
+ <style type="text/css">
259
+ foo {
260
+ bar: baz;
261
+ }
262
+ </style>
263
+ HTML
264
+ end
265
+
266
+ def test_style_to_css_filter_with_following_content
267
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
268
+ %head
269
+ :css
270
+ foo {
271
+ bar: baz;
272
+ }
273
+ %body Hello
274
+ FORTITUDE
275
+ <head>
276
+ <style type="text/css">
277
+ foo {
278
+ bar: baz;
279
+ }
280
+ </style>
281
+ </head>
282
+ <body>Hello</body>
283
+ HTML
284
+ end
285
+
286
+ def test_style_to_css_filter_with_no_content
287
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
288
+ :css
289
+ FORTITUDE
290
+ <style type="text/css"> </style>
291
+ HTML
292
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
293
+ :css
294
+ FORTITUDE
295
+ <style type="text/css"></style>
296
+ HTML
297
+ end
298
+
299
+ def test_filter_with_inconsistent_indentation
300
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
301
+ :css
302
+ foo {
303
+ badly: indented;
304
+ }
305
+ FORTITUDE
306
+ <style type="text/css">
307
+ foo {
308
+ badly: indented;
309
+ }
310
+ </style>
311
+ HTML
312
+ end
313
+
314
+ def test_inline_conditional_comment
315
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
316
+ /[if foo] bar baz
317
+ FORTITUDE
318
+ <!--[if foo]> bar baz <![endif]-->
319
+ HTML
320
+ end
321
+
322
+ def test_minus_in_tag
323
+ assert_equal("%p - foo bar -", render("<p>- foo bar -</p>"))
324
+ end
325
+
326
+ def test_equals_in_tag
327
+ assert_equal("%p = foo bar =", render("<p>= foo bar =</p>"))
328
+ end
329
+
330
+ def test_hash_in_tag
331
+ assert_equal("%p # foo bar #", render("<p># foo bar #</p>"))
332
+ end
333
+
334
+ def test_comma_post_tag
335
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
336
+ #foo
337
+ %span> Foo
338
+ ,
339
+ %span bar
340
+ Foo
341
+ %span> bar
342
+ ,
343
+ %span baz
344
+ FORTITUDE
345
+ <div id="foo">
346
+ <span>Foo</span>, <span>bar</span>
347
+ Foo<span>bar</span>, <span>baz</span>
348
+ </div>
349
+ HTML
350
+ end
351
+
352
+ def test_comma_post_tag_with_text_before
353
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
354
+ #foo
355
+ Batch
356
+ = succeed "," do
357
+ %span Foo
358
+ %span Bar
359
+ FORTITUDE
360
+ <div id="foo">
361
+ Batch
362
+ <span>Foo</span>, <span>Bar</span>
363
+ </div>
364
+ HTML
365
+ end
366
+
367
+ def test_fortitude_tags_should_be_on_new_line_after_tag_with_blank_content
368
+ xml = "<weight> </weight>\n<pages>102</pages>"
369
+ fortitude = "%weight\n%pages 102"
370
+ assert_equal fortitude, render(xml)
371
+ end
372
+
373
+ # Encodings
374
+
375
+ def test_encoding_error
376
+ render("foo\nbar\nb\xFEaz".force_encoding("utf-8"))
377
+ assert(false, "Expected exception")
378
+ # TODO ageweke
379
+ # rescue Haml::Error => e
380
+ # assert_equal(3, e.line)
381
+ # assert_match(/Invalid UTF-8 character/, e.message)
382
+ end
383
+
384
+ def test_ascii_incompatible_encoding_error
385
+ template = "foo\nbar\nb_z".encode("utf-16le")
386
+ template[9] = "\xFE".force_encoding("utf-16le")
387
+ render(template)
388
+ assert(false, "Expected exception")
389
+ # TODO ageweke
390
+ # rescue Haml::Error => e
391
+ # assert_equal(3, e.line)
392
+ # assert_match(/Invalid UTF-16LE character/, e.message)
393
+ end
394
+
395
+ # Regression Tests
396
+
397
+ def test_xhtml_strict_doctype
398
+ assert_equal('!!! Strict', render(<<HTML))
399
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
400
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
401
+ HTML
402
+ end
403
+
404
+ def test_html_document_without_doctype
405
+ assert_equal(<<FORTITUDE.rstrip, render(<<HTML))
406
+ !!!
407
+ %html
408
+ %head
409
+ %title Hello
410
+ %body
411
+ %p Hello
412
+ FORTITUDE
413
+ <html>
414
+ <head>
415
+ <title>Hello</title>
416
+ </head>
417
+ <body>
418
+ <p>Hello</p>
419
+ </body>
420
+ </html>
421
+ HTML
422
+ end
423
+
424
+ end
@@ -0,0 +1,39 @@
1
+ #reopen classes that need to be modified for JRuby specific behavior
2
+ class ErbTest
3
+ def test_inline_erb
4
+ assert_equal("%p= foo", render_erb("<p><%= foo %></p>"))
5
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<HTML))
6
+ %p
7
+ = foo
8
+ FORTITUDE
9
+ <p><%= foo %>
10
+ </p>
11
+ HTML
12
+ end
13
+
14
+ def test_two_multiline_erb_loud_scripts
15
+ assert_equal(<<FORTITUDE.rstrip, render_erb(<<ERB))
16
+ .blah
17
+ = foo + |
18
+ bar.baz.bang + |
19
+ baz |
20
+ = foo.bar do |
21
+ bang |
22
+ end |
23
+ %p foo
24
+ FORTITUDE
25
+ <div class="blah">
26
+ <%=
27
+ foo +
28
+ bar.baz.bang +
29
+ baz
30
+ %>
31
+ <%= foo.bar do
32
+ bang
33
+ end %>
34
+ <p>foo</p>
35
+ </div>
36
+ ERB
37
+ end
38
+
39
+ end