forked from erector/erector
/
rhtml_parser_spec.rb
320 lines (262 loc) · 8.87 KB
/
rhtml_parser_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
require "erector/erect"
module ParserTestHelper
def assert_evals_to_self(input)
assert_evals_to(input, input)
end
def parse(input)
result = @parser.parse(input)
if result
result.set_indent(0) if result.respond_to? :set_indent
else
puts @parser.failure_reason
puts @parser.terminal_failures.join("\n")
result.should_not be_nil
end
result
end
end
describe RhtmlParser do
include ParserTestHelper
before :each do
@parser = RhtmlParser.new
end
it "converts text" do
parse("hello").convert.should == "text 'hello'\n"
parse("hello maude!").convert.should == "text 'hello maude!'\n"
parse(" hello ").convert.should == "text 'hello'\n"
end
it "unescapes HTML entities in text" do
parse("<").convert.should == "text '<'\n"
parse("5 > 2").convert.should == "text '5 > 2'\n"
end
it "converts self-closing tags" do
parse("<br/>").convert.should == "br\n"
parse("<br />").convert.should == "br\n"
end
it "converts open tag" do
parse("<div>").convert.should == "div do\n"
parse("<h1>").convert.should == "h1 do\n"
end
it "converts close tag" do
parse("</div>").convert.should == "end\n"
parse("</h1>").convert.should == "end\n"
end
it "converts two nested divs" do
parse("<div><div></div></div>").convert.should ==
"div do\n" +
" div do\n" +
" end\n" +
"end\n"
end
it "converts two nested divs with whitespace" do
parse("<div> <div> </div> </div>").convert.should ==
"div do\n" +
" div do\n" +
" end\n" +
"end\n"
end
it "converts no open, text, and no close tag" do
parse("hello</div>").convert.should == "text 'hello'\nend\n"
end
it "converts open, text, and no close tag" do
parse("<div>hello").convert.should == "div do\n text 'hello'\n"
end
it "converts open, text, close" do
parse("<div>hello</div>").convert.should == "div do\n text 'hello'\nend\n"
end
it "autocloses an img tag" do
parse("<img src='foo'>").convert.should == "img :src => 'foo'\n"
end
it "converts a scriptlet" do
parse("<% foo %>").convert.should == "foo\n"
end
it "converts open, text, scriptlet, text, close" do
parse("<div>hello <% 5.times do %> very <% end %> much</div>").convert.should ==
"div do\n" +
" text 'hello'\n" +
" 5.times do\n" +
" text 'very'\n" +
" end\n" +
" text 'much'\n" +
"end\n"
end
it "converts open, scriptlet, text, close" do
parse("<div><% 5.times do %> very <% end %> much</div>").convert.should ==
"div do\n" +
" 5.times do\n" +
" text 'very'\n" +
" end\n" +
" text 'much'\n" +
"end\n"
end
it "converts open, text, scriptlet, close" do
parse("<div>hello <% 5.times do %> very <% end %></div>").convert.should ==
"div do\n" +
" text 'hello'\n" +
" 5.times do\n" +
" text 'very'\n" +
" end\n" +
"end\n"
end
it "converts printlets into rawtext statements" do
parse("<%= 1+1 %>").convert.should == "rawtext 1+1\n"
parse("<%= link_to \"mom\" %>").convert.should == "rawtext link_to(\"mom\")\n"
end
it "converts h-printlets into text statements" do
parse("<%=h foo %>").convert.should == "text foo\n"
parse("<%= h \"mom\" %>").convert.should == "text \"mom\"\n"
end
it "allows naked percent signs inside scriptlets" do
parse("<% x = 10 % 5 %>").convert.should == "x = 10 % 5\n"
end
it "indents" do
i = Erector::Indenting.new(nil, nil)
i.line("foo").should == "foo\n"
i.line_in("bar").should == "bar\n"
i.line_in("baz").should == " baz\n"
i.line("baf").should == " baf\n"
i.line_out("end").should == " end\n"
i.line_out("end").should == "end\n"
end
it "indents extra when told to" do
parse("<div>hello</div>").set_indent(2).convert.should ==
" div do\n" +
" text 'hello'\n" +
" end\n"
end
it "indents scriptlets ending with do and end" do
parse("<% form_for :foo do |x,y| %><% 5.times do %>hello<% end %><% end %>bye").convert.should ==
"form_for :foo do |x,y|\n" +
" 5.times do\n" +
" text 'hello'\n" +
" end\n" +
"end\n" +
"text 'bye'\n"
end
it "converts HTML attributes" do
parse("<div id='foo'/>").convert.should == "div :id => 'foo'\n"
parse("<div id='foo' class='bar'/>").convert.should == "div :id => 'foo', :class => 'bar'\n"
parse("<div id='foo'>bar</div>").convert.should == "div :id => 'foo' do\n text 'bar'\nend\n"
end
it "escapes single quotes inside attribute values" do
@parser.root = :attribute
parse("a=\"don't worry\"").convert.should == ":a => 'don\\'t worry'"
end
it "allows newlines where whitespace is allowed" do
parse("<img src='foo' \nalt='bar' />").convert.should == "img :src => 'foo', :alt => 'bar'\n"
end
it "treats tab characters the same as spaces" do
parse("<div \t />").convert.should == "div\n"
end
it "deals with HTML entities in text" do
parse("<").convert.should == "text '<'\n"
end
it "deals with a naked less-than or greater-than sign inside text" do
parse("if x > 2 or x< 5 then").convert.should == "text 'if x > 2 or x< 5 then'\n"
end
it "wraps printlets in parens if necessary, to avoid warning: parenthesize argument(s) for future version" do
parse("<%= h \"mom\" %>").convert.should == "text \"mom\"\n"
parse("<%= h hi \"mom\" %>").convert.should == "text hi(\"mom\")\n"
parse("<%= \"mom\" %>").convert.should == "rawtext \"mom\"\n"
parse("<%= \"hi mom\" %>").convert.should == "rawtext \"hi mom\"\n"
parse("<%= hi \"mom\" %>").convert.should == "rawtext hi(\"mom\")\n"
parse("<%= link_to blah %>").convert.should == "rawtext link_to(blah)\n"
parse("<%= link_to blah blah %>").convert.should == "rawtext link_to(blah blah)\n"
parse("<%= link_to blah(blah) %>").convert.should == "rawtext link_to(blah(blah))\n"
parse("<%= link_to(blah) %>").convert.should == "rawtext link_to(blah)\n"
end
it "won't parenthesize expressions" do
parse("<%= h foo / bar %>").convert.should == "text foo / bar\n"
end
it "converts yield so layouts work" do
pending("easy enough to make this pass, but the result doesn't seem to work as a layout")
parse("<%= yield %>").convert.should == "rawtext @content\n"
parse("<%= \"yield\" %>").convert.should == "rawtext \"yield\"\n"
parse("<%= \"the yield is good\" %>").convert.should == "rawtext \"the yield is good\"\n"
end
it "parses quoted strings" do
@parser.root = :quoted
parse("'foo'").value.should == "foo"
parse("\"foo\"").value.should == "foo"
end
it "converts attributes in isolation" do
@parser.root = :attribute
parse("a='foo'").convert.should == ":a => 'foo'"
parse("a=\"foo\"").convert.should == ":a => 'foo'"
end
it "parses a set of attributes" do
@parser.root = :attributes
parse("a='foo' b='bar'").convert.should == " :a => 'foo', :b => 'bar'"
end
it "works with namespaced attributes" do
@parser.root = :attribute
parse('xml:lang="en"').convert.should == "'xml:lang' => 'en'"
end
it "deals with HTML entities in attribute values" do
@parser.root = :attribute
parse("foo='b<r'").convert.should == ":foo => 'b<r'"
parse("foo='b<r'").convert.should == ":foo => 'b<r'"
end
it "converts DOCTYPEs" do
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
parse(html).convert.should == "rawtext '#{html}'\n"
end
## More functional-type specs below here
it "ignores spaces, tabs and newlines" do
parse(" <div>\t\n" + "\thello !" + "\n\t</div>").convert.should ==
"div do\n" +
" text 'hello !'\n" +
"end\n"
end
it "parses some scaffolding" do
parse("<p>
<b>Name:</b>
<%=h @foo.name %>
</p>").convert.should ==
"p do\n" +
" b do\n" +
" text 'Name:'\n" +
" end\n" +
" text @foo.name\n" +
"end\n"
end
it "parses edit.erb.html" do
parse("<h1>Editing foo</h1>
<%= error_messages_for :foo %>
<% form_for(@foo) do |f| %>
<p>
<b>Name</b><br />
<%= f.text_field :name %>
</p>
<p>
<b>Age</b><br />
<%= f.text_field :age %>
</p>
<p>
<%= f.submit \"Update\" %>
</p>
<% end %>
<%= link_to 'Show', @foo %> |
<%= link_to 'Back', foos_path %>
")
end
it "parses show.html.erb" do
parse("<p>
<b>Name:</b>
<%=h @foo.name %>
</p>
<p>
<b>Age:</b>
<%=h @foo.age %>
</p>
<%= link_to 'Edit', edit_foo_path(@foo) %> |
<%= link_to 'Back', foos_path %>
")
end
it "does meta" do
parse('<meta http-equiv="content-type" content="text/html;charset=UTF-8" />').convert.should ==
"meta 'http-equiv' => 'content-type', :content => 'text/html;charset=UTF-8'\n"
end
end