html2fortitude 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ describe "html2fortitude basics" do
2
+ it "should render simple text with #text" do
3
+ expect(h2f_content("hello, world")).to eq(%{text "hello, world"})
4
+ end
5
+
6
+ it "should not skip newlines in the source" do
7
+ expect(h2f_content(%{<p foo="bar"/>
8
+
9
+ <p bar="baz"/>})).to eq(%{p(:foo => "bar")
10
+
11
+ p(:bar => "baz")})
12
+ end
13
+ end
@@ -0,0 +1,411 @@
1
+ describe "html2fortitude command-line usage" do
2
+ it "should return help if passed --help" do
3
+ with_temp_directory("help") do
4
+ result = invoke("--help")
5
+ expect(result).to match(/Fortitude/mi)
6
+ expect(result).to match(/html2fortitude/mi)
7
+ expect(result).to match(/\-\-output/mi)
8
+ end
9
+ end
10
+
11
+ it "should transform a simple file to the same location by default" do
12
+ with_temp_directory("simple_file") do
13
+ splat! "one.html.erb", <<-EOF
14
+ hello, world
15
+ EOF
16
+
17
+ output = invoke("-c MyWidget", "one.html.erb")
18
+ expect(output).to match(/one\.html\.erb\s*\-\>\s*.*one\.rb/)
19
+
20
+ result = h2f_from("one.rb")
21
+ expect(result.class_name).to eq("MyWidget")
22
+ expect(result.superclass).to eq("Fortitude::Widget::Html5")
23
+ expect(result.content_text).to eq(%{text "hello, world"})
24
+ expect(result.method_name).to eq("content")
25
+ expect(result.needs).to eq({ })
26
+ end
27
+ end
28
+
29
+ it "should let you select the output file with -o" do
30
+ with_temp_directory("output_file") do
31
+ splat! "one.html.erb", <<-EOF
32
+ hello, world
33
+ EOF
34
+
35
+ output = invoke("-c MyWidget", "-o foo.bar.xxx", "one.html.erb")
36
+ expect(output).to match(/one\.html\.erb\s*\-\>\s*.*foo\.bar\.xxx/)
37
+
38
+ result = h2f_from("foo.bar.xxx")
39
+ expect(result.content_text).to eq(%{text "hello, world"})
40
+ end
41
+ end
42
+
43
+ it "should let you select the class name with -c" do
44
+ with_temp_directory("output_file") do
45
+ splat! "one.html.erb", <<-EOF
46
+ hello, world
47
+ EOF
48
+
49
+ invoke("-c SomeThingYo", "one.html.erb")
50
+
51
+ result = h2f_from("one\.rb")
52
+ expect(result.class_name).to eq("SomeThingYo")
53
+ expect(result.content_text).to eq(%{text "hello, world"})
54
+ end
55
+ end
56
+
57
+ it "should automatically infer the base directory, and the class name from that" do
58
+ with_temp_directory("inferred_base") do
59
+ splat! "app/views/foo/one.html.erb", <<-EOF
60
+ hello, world
61
+ EOF
62
+
63
+ invoke("app/views/foo/one.html.erb")
64
+
65
+ result = h2f_from("app/views/foo/one.rb")
66
+ expect(result.class_name).to eq("Views::Foo::One")
67
+ expect(result.content_text).to eq(%{text "hello, world"})
68
+ end
69
+ end
70
+
71
+ it "should allow overriding an inferred class name with -c" do
72
+ with_temp_directory("inferred_base_with_override") do
73
+ splat! "app/views/foo/one.html.erb", <<-EOF
74
+ hello, world
75
+ EOF
76
+
77
+ invoke("-c SomeWidget", "app/views/foo/one.html.erb")
78
+
79
+ result = h2f_from("app/views/foo/one.rb")
80
+ expect(result.class_name).to eq("SomeWidget")
81
+ expect(result.content_text).to eq(%{text "hello, world"})
82
+ end
83
+ end
84
+
85
+ it "should allow manually specifying a base directory with -b" do
86
+ with_temp_directory("explicit_base") do
87
+ splat! "foo/bar/baz/one.html.erb", <<-EOF
88
+ hello, world
89
+ EOF
90
+
91
+ invoke("-b foo/bar", "foo/bar/baz/one.html.erb")
92
+
93
+ result = h2f_from("foo/bar/baz/one.rb")
94
+ expect(result.class_name).to eq("Baz::One")
95
+ expect(result.content_text).to eq(%{text "hello, world"})
96
+ end
97
+ end
98
+
99
+ it "should allow manually specifying a base directory with -b, and overriding the class name with -c" do
100
+ with_temp_directory("explicit_base_with_override") do
101
+ splat! "foo/bar/baz/one.html.erb", <<-EOF
102
+ hello, world
103
+ EOF
104
+
105
+ invoke("-b foo/bar", "-c MyWidget", "foo/bar/baz/one.html.erb")
106
+
107
+ result = h2f_from("foo/bar/baz/one.rb")
108
+ expect(result.class_name).to eq("MyWidget")
109
+ expect(result.content_text).to eq(%{text "hello, world"})
110
+ end
111
+ end
112
+
113
+ it "should infer the class base properly, even if you output the file elsewhere" do
114
+ with_temp_directory("inferred_base_with_other_output") do
115
+ splat! "app/views/foo/one.html.erb", <<-EOF
116
+ hello, world
117
+ EOF
118
+
119
+ FileUtils.mkdir_p("bar/baz")
120
+ invoke("app/views/foo/one.html.erb", "-o bar/baz/two.rb")
121
+
122
+ result = h2f_from("bar/baz/two.rb")
123
+ expect(result.class_name).to eq("Views::Foo::One")
124
+ expect(result.content_text).to eq(%{text "hello, world"})
125
+ end
126
+ end
127
+
128
+ it "should let you override the superclass with -s" do
129
+ with_temp_directory("set_superclass") do
130
+ splat! "one.html.erb", <<-EOF
131
+ hello, world
132
+ EOF
133
+
134
+ invoke("one.html.erb", "-c MyWidget", "-s MyBaseWidget")
135
+
136
+ result = h2f_from("one.rb")
137
+ expect(result.class_name).to eq("MyWidget")
138
+ expect(result.superclass).to eq("MyBaseWidget")
139
+ expect(result.content_text).to eq(%{text "hello, world"})
140
+ end
141
+ end
142
+
143
+ it "should let you change the method name using --method" do
144
+ with_temp_directory("method_name") do
145
+ splat! "one.html.erb", <<-EOF
146
+ hello, world
147
+ EOF
148
+
149
+ invoke("one.html.erb", "-c MyWidget", "-m foobar")
150
+
151
+ result = h2f_from("one.rb")
152
+ expect(result.class_name).to eq("MyWidget")
153
+ expect(result.method_name).to eq("foobar")
154
+ expect(result.content_text).to eq(%{text "hello, world"})
155
+ end
156
+ end
157
+
158
+ it "should, by default, map needs to nil" do
159
+ with_temp_directory("default_needs") do
160
+ splat! "one.html.erb", <<-EOF
161
+ hello, <%= @first_name %> <%= @last_name %>
162
+ EOF
163
+
164
+ invoke("one.html.erb", "-c MyWidget")
165
+
166
+ result = h2f_from("one.rb")
167
+ expect(result.class_name).to eq("MyWidget")
168
+ expect(result.needs).to eq({ ":first_name" => "nil", ":last_name" => "nil" })
169
+ expect(result.content_text).to eq(%{text "hello, "
170
+ text(first_name)
171
+ text " "
172
+ text(last_name)})
173
+ end
174
+ end
175
+
176
+ it "should make needs required, if asked to" do
177
+ with_temp_directory("required_needs") do
178
+ splat! "one.html.erb", <<-EOF
179
+ hello, <%= @first_name %> <%= @last_name %>
180
+ EOF
181
+
182
+ invoke("one.html.erb", "-c MyWidget", "--assigns required_needs")
183
+
184
+ result = h2f_from("one.rb")
185
+ expect(result.class_name).to eq("MyWidget")
186
+ expect(result.needs).to eq({ ":first_name" => nil, ":last_name" => nil })
187
+ expect(result.content_text).to eq(%{text "hello, "
188
+ text(first_name)
189
+ text " "
190
+ text(last_name)})
191
+ end
192
+ end
193
+
194
+ it "should make needs use instance variables, if asked to" do
195
+ with_temp_directory("instance_variable_needs") do
196
+ splat! "one.html.erb", <<-EOF
197
+ hello, <%= @first_name %> <%= @last_name %>
198
+ EOF
199
+
200
+ invoke("one.html.erb", "-c MyWidget", "--assigns instance_variables")
201
+
202
+ result = h2f_from("one.rb")
203
+ expect(result.class_name).to eq("MyWidget")
204
+ expect(result.needs).to eq({ ":first_name" => "nil", ":last_name" => "nil" })
205
+ expect(result.content_text).to eq(%{text "hello, "
206
+ text(@first_name)
207
+ text " "
208
+ text(@last_name)})
209
+ end
210
+ end
211
+
212
+ it "should emit no needs, if asked to" do
213
+ with_temp_directory("no_needs") do
214
+ splat! "one.html.erb", <<-EOF
215
+ hello, <%= @first_name %> <%= @last_name %>
216
+ EOF
217
+
218
+ invoke("one.html.erb", "-c MyWidget", "--assigns no_needs")
219
+
220
+ result = h2f_from("one.rb")
221
+ expect(result.class_name).to eq("MyWidget")
222
+ expect(result.needs).to eq({ })
223
+ expect(result.content_text).to eq(%{text "hello, "
224
+ text(first_name)
225
+ text " "
226
+ text(last_name)})
227
+ end
228
+ end
229
+
230
+ it "should use braces for blocks, by default" do
231
+ with_temp_directory("default_braces") do
232
+ splat! "one.html.erb", <<-EOF
233
+ <p>
234
+ <span>hello, world</span>
235
+ </p>
236
+ EOF
237
+
238
+ invoke("one.html.erb", "-c MyWidget")
239
+
240
+ result = h2f_from("one.rb")
241
+ expect(result.content_text).to eq(%{p {
242
+ span("hello, world")
243
+ }})
244
+ end
245
+ end
246
+
247
+ it "should use do/end for blocks, if asked to" do
248
+ with_temp_directory("do_end") do
249
+ splat! "one.html.erb", <<-EOF
250
+ <p>
251
+ <span>hello, world</span>
252
+ </p>
253
+ EOF
254
+
255
+ invoke("one.html.erb", "-c MyWidget", "--do-end")
256
+
257
+ result = h2f_from("one.rb")
258
+ expect(result.content_text).to eq(%{p do
259
+ span("hello, world")
260
+ end})
261
+ end
262
+ end
263
+
264
+ it "should use old-style hashes, by default" do
265
+ with_temp_directory("old_hashes") do
266
+ splat! "one.html.erb", <<-EOF
267
+ <p class="foo"/>
268
+ EOF
269
+
270
+ invoke("one.html.erb", "-c MyWidget")
271
+
272
+ result = h2f_from("one.rb")
273
+ expect(result.content_text).to eq(%{p(:class => "foo")})
274
+ end
275
+ end
276
+
277
+ it "should use new-style hashes, if asked to" do
278
+ with_temp_directory("new_hashes") do
279
+ splat! "one.html.erb", <<-EOF
280
+ <p class="foo"/>
281
+ EOF
282
+
283
+ invoke("one.html.erb", "-c MyWidget", "--new-style-hashes")
284
+
285
+ result = h2f_from("one.rb")
286
+ expect(result.content_text).to eq(%{p(class: "foo")})
287
+ end
288
+ end
289
+
290
+ it "should process stdin to stdout, if asked to" do
291
+ with_temp_directory("stdin_processing") do
292
+ splat! "one.html.erb", <<-EOF
293
+ hello, world
294
+ EOF
295
+
296
+ output = invoke("-", "-c MyWidget", "< one.html.erb")
297
+ result = Html2FortitudeResult.new(output)
298
+
299
+ expect(result.class_name).to eq("MyWidget")
300
+ expect(result.content_text).to eq(%{text "hello, world"})
301
+ end
302
+ end
303
+
304
+ it "should process multiple files from the command line properly" do
305
+ with_temp_directory("multiple_files") do
306
+ splat! "one.html.erb", <<-EOF
307
+ hello, world
308
+ EOF
309
+
310
+ splat! "two.html.erb", <<-EOF
311
+ hello, universe
312
+ EOF
313
+
314
+ invoke("one.html.erb", "two.html.erb", "-c MyWidget")
315
+
316
+ result1 = h2f_from("one.rb")
317
+ expect(result1.class_name).to eq("MyWidget")
318
+ expect(result1.content_text).to eq(%{text "hello, world"})
319
+
320
+ result2 = h2f_from("two.rb")
321
+ expect(result2.class_name).to eq("MyWidget")
322
+ expect(result2.content_text).to eq(%{text "hello, universe"})
323
+ end
324
+ end
325
+
326
+ it "should process multiple files from the command line with an inferred base directory" do
327
+ with_temp_directory("multiple_files_with_inferred_base") do
328
+ splat! "app/views/foo/one.html.erb", <<-EOF
329
+ hello, world
330
+ EOF
331
+
332
+ splat! "app/views/bar/two.html.erb", <<-EOF
333
+ hello, universe
334
+ EOF
335
+
336
+ invoke("app/views/foo/one.html.erb", "app/views/bar/two.html.erb")
337
+
338
+ result1 = h2f_from("app/views/foo/one.rb")
339
+ expect(result1.class_name).to eq("Views::Foo::One")
340
+ expect(result1.content_text).to eq(%{text "hello, world"})
341
+
342
+ result2 = h2f_from("app/views/bar/two.rb")
343
+ expect(result2.class_name).to eq("Views::Bar::Two")
344
+ expect(result2.content_text).to eq(%{text "hello, universe"})
345
+ end
346
+ end
347
+
348
+ it "should process an entire directory if asked to" do
349
+ with_temp_directory("entire_directory") do
350
+ splat! "app/views/foo/one.html.erb", <<-EOF
351
+ hello, world
352
+ EOF
353
+
354
+ splat! "app/views/bar/two.html.erb", <<-EOF
355
+ hello, universe
356
+ EOF
357
+
358
+ invoke("app")
359
+
360
+ result1 = h2f_from("app/views/foo/one.rb")
361
+ expect(result1.class_name).to eq("Views::Foo::One")
362
+ expect(result1.content_text).to eq(%{text "hello, world"})
363
+
364
+ result2 = h2f_from("app/views/bar/two.rb")
365
+ expect(result2.class_name).to eq("Views::Bar::Two")
366
+ expect(result2.content_text).to eq(%{text "hello, universe"})
367
+ end
368
+ end
369
+
370
+ it "should process multiple directories at once if asked to" do
371
+ with_temp_directory("multiple_directories_and_files") do
372
+ splat! "app/views/foo/one.html.erb", <<-EOF
373
+ hello, world
374
+ EOF
375
+
376
+ splat! "app/views/bar/two.html.erb", <<-EOF
377
+ hello, universe
378
+ EOF
379
+
380
+ splat! "other/app/views/baz/something.html.erb", <<-EOF
381
+ something 1
382
+ EOF
383
+
384
+ splat! "other/app/views/quux/other_thing.html.erb", <<-EOF
385
+ other_thing 1
386
+ EOF
387
+
388
+ invoke("app", "other")
389
+
390
+ result1 = h2f_from("app/views/foo/one.rb")
391
+ expect(result1.class_name).to eq("Views::Foo::One")
392
+ expect(result1.content_text).to eq(%{text "hello, world"})
393
+
394
+ result2 = h2f_from("app/views/bar/two.rb")
395
+ expect(result2.class_name).to eq("Views::Bar::Two")
396
+ expect(result2.content_text).to eq(%{text "hello, universe"})
397
+
398
+ result3 = h2f_from("other/app/views/baz/something.rb")
399
+ expect(result3.class_name).to eq("Views::Baz::Something")
400
+ expect(result3.content_text).to eq(%{text "something 1"})
401
+
402
+ result4 = h2f_from("other/app/views/quux/other_thing.rb")
403
+ expect(result4.class_name).to eq("Views::Quux::OtherThing")
404
+ expect(result4.content_text).to eq(%{text "other_thing 1"})
405
+
406
+ result4 = h2f_from("other/app/views/quux/other_thing.rb")
407
+ expect(result4.class_name).to eq("Views::Quux::OtherThing")
408
+ expect(result4.content_text).to eq(%{text "other_thing 1"})
409
+ end
410
+ end
411
+ end
@@ -0,0 +1,208 @@
1
+ describe "html2fortitude ERb support" do
2
+ it "should work with ERb blocks around HTML" do
3
+ expect(h2f_content(%{<% foo do %>
4
+ bar
5
+ <p class="baz"/>
6
+ <% end %>})).to eq(%{foo do
7
+ text "bar"
8
+
9
+ p(:class => "baz")
10
+ end})
11
+ end
12
+
13
+ it "should maintain space between ERb outputting blocks" do
14
+ expect(h2f_content(%{hello, <%= @first_name %> <%= @last_name %>})).to eq(%{text "hello, "
15
+ text(first_name)
16
+ text " "
17
+ text(last_name)})
18
+ end
19
+
20
+ it "should maintain space between ERb outputting blocks, even if it's a newline" do
21
+ expect(h2f_content(%{hello, <%= @first_name %>
22
+ <%= @last_name %>})).to eq(%{text "hello, "
23
+ text(first_name)
24
+ text " "
25
+ text(last_name)})
26
+ end
27
+
28
+ it "should leave space after an ERb block" do
29
+ expect(h2f_content(%{<% foo do %>
30
+ bar
31
+ <p class="baz"/>
32
+ <% end %>
33
+ hello, world})).to eq(%{foo do
34
+ text "bar"
35
+
36
+ p(:class => "baz")
37
+ end
38
+ text "hello, world"})
39
+ end
40
+
41
+ it "should handle ERb blocks that are loud, like form_for" do
42
+ expect(h2f_content(%{<%= form_for do |f| %>
43
+ <%= f.text_field :name %>
44
+ <p class="baz"/>
45
+ <% end %>})).to eq(%{text(form_for do |f|
46
+ text(f.text_field :name)
47
+ p(:class => "baz")
48
+ end)})
49
+ end
50
+
51
+ it "should handle multiline ERb blocks that are loud, like form_for" do
52
+ expect(h2f_content(%{<%= foo + bar
53
+ form_for do |f| %>
54
+ <%= f.text_field :name %>
55
+ <p class="baz"/>
56
+ <% end %>})).to eq(%{foo + bar
57
+ text(form_for do |f|
58
+ text(f.text_field :name)
59
+ p(:class => "baz")
60
+ end)})
61
+ end
62
+
63
+ it "should interpolate ERb inside a <script> block when possible" do
64
+ expect(h2f_content(%{<script type="text/javascript">
65
+ foo
66
+ <%= bar %>
67
+ baz
68
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT
69
+ foo
70
+ \#{bar}
71
+ baz
72
+ END_OF_JAVASCRIPT_CONTENT})
73
+ end
74
+
75
+ it "should interpolate ERb inside a <style> block when possible" do
76
+ expect(h2f_content(%{<style type="text/css">
77
+ foo
78
+ <%= bar %>
79
+ baz
80
+ </style>})).to eq(%{style <<-END_OF_STYLE_CONTENT, :type => "text/css"
81
+ foo
82
+ \#{bar}
83
+ baz
84
+ END_OF_STYLE_CONTENT})
85
+ end
86
+
87
+ it "should render ERb silent inside <script> blocks as big warnings" do
88
+ expect(h2f_content(%{<script type="text/javascript">
89
+ bar
90
+ <% baz %>
91
+ quux
92
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT
93
+ bar
94
+
95
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
96
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
97
+ # way of accomplishing the same result here:
98
+ # <%
99
+ # baz
100
+ # %>
101
+ quux
102
+ END_OF_JAVASCRIPT_CONTENT})
103
+ end
104
+
105
+ it "should render ERb silent inside <style> blocks as big warnings" do
106
+ expect(h2f_content(%{<style type="text/css">
107
+ bar
108
+ <% baz %>
109
+ quux
110
+ </style>})).to eq(%{style <<-END_OF_STYLE_CONTENT, :type => "text/css"
111
+ bar
112
+
113
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
114
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
115
+ # way of accomplishing the same result here:
116
+ # <%
117
+ # baz
118
+ # %>
119
+ quux
120
+ END_OF_STYLE_CONTENT})
121
+ end
122
+
123
+ it "should render ERb blocks inside <script> blocks as big warnings" do
124
+ expect(h2f_content(%{<script type="text/javascript">
125
+ bar
126
+ <% if foo %>
127
+ quux
128
+ <% else %>
129
+ bar
130
+ <% end %>
131
+ quux
132
+ </script>})).to eq(%{javascript <<-END_OF_JAVASCRIPT_CONTENT
133
+ bar
134
+
135
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
136
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
137
+ # way of accomplishing the same result here:
138
+ # <%
139
+ # if foo
140
+ # %>
141
+
142
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
143
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
144
+ # way of accomplishing the same result here:
145
+ # <%
146
+ # quux
147
+ # %>
148
+
149
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
150
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
151
+ # way of accomplishing the same result here:
152
+ # <%
153
+ # else
154
+ # %>
155
+
156
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
157
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
158
+ # way of accomplishing the same result here:
159
+ # <%
160
+ # bar
161
+ # %>
162
+ quux
163
+ END_OF_JAVASCRIPT_CONTENT})
164
+ end
165
+
166
+ it "should render ERb blocks inside <style> blocks as big warnings" do
167
+ expect(h2f_content(%{<style type="text/css">
168
+ bar
169
+ <% if foo %>
170
+ quux
171
+ <% else %>
172
+ bar
173
+ <% end %>
174
+ quux
175
+ </style>})).to eq(%{style <<-END_OF_STYLE_CONTENT, :type => "text/css"
176
+ bar
177
+
178
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
179
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
180
+ # way of accomplishing the same result here:
181
+ # <%
182
+ # if foo
183
+ # %>
184
+
185
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
186
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
187
+ # way of accomplishing the same result here:
188
+ # <%
189
+ # quux
190
+ # %>
191
+
192
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
193
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
194
+ # way of accomplishing the same result here:
195
+ # <%
196
+ # else
197
+ # %>
198
+
199
+ # HTML2FORTITUDE_FIXME_BEGIN: The following code was interpolated into this block using ERb;
200
+ # Fortitude isn't a simple string-manipulation engine, so you will have to find another
201
+ # way of accomplishing the same result here:
202
+ # <%
203
+ # bar
204
+ # %>
205
+ quux
206
+ END_OF_STYLE_CONTENT})
207
+ end
208
+ end