manatee 0.0.1.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +23 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.mdown +55 -0
  5. data/Rakefile +7 -0
  6. data/app/assets/javascripts/manatee.js +1 -0
  7. data/app/assets/javascripts/manatee/helpers.js +4 -0
  8. data/app/assets/javascripts/manatee/helpers/asset_tag.jsh.coffee +138 -0
  9. data/app/assets/javascripts/manatee/helpers/asset_url.jsh.coffee +90 -0
  10. data/app/assets/javascripts/manatee/helpers/csrf.jsh.coffee +5 -0
  11. data/app/assets/javascripts/manatee/helpers/date.jsh.coffee +54 -0
  12. data/app/assets/javascripts/manatee/helpers/debug.jsh.coffee +2 -0
  13. data/app/assets/javascripts/manatee/helpers/form.jsh.coffee +25 -0
  14. data/app/assets/javascripts/manatee/helpers/form_builder.jsh.coffee +24 -0
  15. data/app/assets/javascripts/manatee/helpers/form_options.jsh.coffee +267 -0
  16. data/app/assets/javascripts/manatee/helpers/form_tag.jsh.coffee +204 -0
  17. data/app/assets/javascripts/manatee/helpers/javascript.jsh.coffee +15 -0
  18. data/app/assets/javascripts/manatee/helpers/number.jsh.coffee +58 -0
  19. data/app/assets/javascripts/manatee/helpers/sanitize.jsh.coffee +5 -0
  20. data/app/assets/javascripts/manatee/helpers/tag.jsh.coffee +58 -0
  21. data/app/assets/javascripts/manatee/helpers/text.jsh.coffee +12 -0
  22. data/app/assets/javascripts/manatee/helpers/translation.jsh.coffee +7 -0
  23. data/app/assets/javascripts/manatee/helpers/url.jsh.coffee +36 -0
  24. data/app/assets/javascripts/manatee/helpers/util.jsh.coffee +41 -0
  25. data/app/assets/javascripts/manatee/rails_helpers.js +2 -0
  26. data/app/assets/javascripts/manatee/rails_helpers/routes.js.erb +195 -0
  27. data/app/assets/javascripts/manatee/rails_routes.js +1 -0
  28. data/app/assets/javascripts/manatee/renderer.js.erb +53 -0
  29. data/app/assets/javascripts/manatee_railsless.js +1 -0
  30. data/lib/manatee.rb +87 -0
  31. data/lib/manatee/config.rb +51 -0
  32. data/lib/manatee/handler.rb +45 -0
  33. data/lib/manatee/rails.rb +6 -0
  34. data/lib/manatee/rails/extensions.rb +23 -0
  35. data/lib/manatee/rails/handler.rb +16 -0
  36. data/lib/manatee/rails/hash_visitor.rb +35 -0
  37. data/lib/manatee/rails/helper.rb +9 -0
  38. data/lib/manatee/rails/resolver.rb +70 -0
  39. data/lib/manatee/rails/routes_compiler.rb +34 -0
  40. data/lib/manatee/sprockets.rb +8 -0
  41. data/lib/manatee/sprockets/jsh_processor_2x.rb +32 -0
  42. data/lib/manatee/sprockets/jsh_processor_3x.rb +31 -0
  43. data/lib/manatee/version.rb +3 -0
  44. data/manatee.gemspec +32 -0
  45. data/test/example/renderer.js.erb +8 -0
  46. data/test/example/translations.en.yml +410 -0
  47. data/test/example/views/index.jst.eco +12 -0
  48. data/test/helpers/asset_tag_test.rb +175 -0
  49. data/test/helpers/asset_url_test.rb +349 -0
  50. data/test/helpers/form_options_test.rb +718 -0
  51. data/test/helpers/form_tag_test.rb +387 -0
  52. data/test/helpers/javascript_test.rb +39 -0
  53. data/test/helpers/number_test.rb +111 -0
  54. data/test/helpers/tag_test.rb +71 -0
  55. data/test/support/dom_assertion.rb +49 -0
  56. data/test/support/view_test.rb +91 -0
  57. data/test/test_helper.rb +17 -0
  58. metadata +213 -0
@@ -0,0 +1,387 @@
1
+ require_relative '../test_helper'
2
+
3
+ class FormTagTest < Manatee::ViewTest
4
+
5
+ def setup
6
+ config_renderer do |config|
7
+ config.fonts_path = '/fonts'
8
+ config.audios_path = '/audios'
9
+ config.videos_path = '/videos'
10
+ config.images_path = '/images'
11
+ config.javascript_path = ''
12
+ config.stylesheet_path = ''
13
+ config.protect_from_forgery = true
14
+ end
15
+ end
16
+
17
+ FieldTypes = {
18
+ color: :color,
19
+ date: :date,
20
+ datetime: :datetime,
21
+ email: :email,
22
+ file: :file,
23
+ hidden: :hidden,
24
+ month: :month,
25
+ number: :number,
26
+ password: :password,
27
+ range: :range,
28
+ search: :search,
29
+ text: :text,
30
+ time: :time,
31
+ url: :url,
32
+ week: :week,
33
+ datetimeLocal: :'datetime-local',
34
+ telephone: :tel,
35
+ phone: :tel
36
+ }
37
+
38
+ FieldTypes.each do |field, type|
39
+ define_method :"test_optionsless_#{field}" do
40
+ html_code = %(<input type="#{type}" id="field_name" name="field[name]" value="field_value"/>)
41
+ assert_dom_helper html_code, :"#{field}FieldTag", 'field[name]', 'field_value'
42
+
43
+ html_code = %(<input type="#{type}" id="field_name" name="field[name]"/>)
44
+ assert_dom_helper html_code, :"#{field}FieldTag", 'field[name]', nil
45
+ end
46
+
47
+ define_method :"test_#{field}_with_options" do
48
+ html_code = %(<input type="#{type}" id="field_name" name="field_name" class="#{field}_class" data-attribute="this is a #{type} field"/>)
49
+ assert_dom_helper html_code, :"#{field}FieldTag", 'field_name', nil, class: "#{field}_class", data: { attribute: "this is a #{type} field" }
50
+
51
+ html_code = %(<input type="#{type}" id="field_name" name="field_name" value="field_value" class="#{field}_class" data-attribute="this is a #{type} field"/>)
52
+ assert_dom_helper html_code, :"#{field}FieldTag", 'field_name', 'field_value', class: "#{field}_class", data: { attribute: "this is a #{type} field" }
53
+ end
54
+ end
55
+
56
+ def test_button_tag
57
+ assert_dom_helper %(<button name="button" type="submit">Button</button>), :buttonTag
58
+ end
59
+
60
+ def test_button_tag_with_submit_type
61
+ assert_dom_helper %(<button name="button" type="submit">Save</button>), :buttonTag, 'Save', type: 'submit'
62
+ end
63
+
64
+ def test_button_tag_with_button_type
65
+ assert_dom_helper %(<button name="button" type="button">Button</button>), :buttonTag, 'Button', type: 'button'
66
+ end
67
+
68
+ def test_button_tag_with_reset_type
69
+ assert_dom_helper %(<button name="button" type="reset">Reset</button>), :buttonTag, 'Reset', type: 'reset'
70
+ end
71
+
72
+ def test_button_tag_with_disabled_option
73
+ assert_dom_helper %(<button name="button" type="reset" disabled="disabled">Reset</button>), :buttonTag, 'Reset', type: 'reset', disabled: true
74
+ end
75
+
76
+ def test_button_tag_escape_content
77
+ assert_dom_helper %(<button name="button" type="reset" disabled="disabled"><b>Reset</b></button>), :buttonTag, '<b>Reset</b>', type: 'reset', disabled: true
78
+ end
79
+
80
+ def test_button_tag_with_block
81
+ assert_dom_helper('<button name="button" type="submit">Content</button>', :buttonTag){ 'Content' }
82
+ end
83
+
84
+ def test_button_tag_with_block_and_options
85
+ html_code = '<button name="temptation" type="button"><strong>Do not press me</strong></button>'
86
+ assert_dom_helper(html_code, :buttonTag, name: 'temptation', type: 'button'){ helper_call :contentTag, :strong, 'Do not press me' }
87
+ end
88
+
89
+ def test_button_tag_with_confirmation
90
+ html_code = %(<button name="button" type="submit" data-confirm="Are you sure?">Save</button>)
91
+ assert_dom_helper html_code, :buttonTag, 'Save', type: 'submit', data: { confirm: 'Are you sure?' }
92
+ end
93
+
94
+ def test_checkbox_tag
95
+ html_code = %(<input type="checkbox" id="field_name" name="field[name]" value="field_value"/>)
96
+ assert_dom_helper html_code, :checkBoxTag, 'field[name]', 'field_value'
97
+
98
+ html_code = %(<input type="checkbox" id="field_name" name="field[name]" value="field_value" checked="checked"/>)
99
+ assert_dom_helper html_code, :checkboxTag, 'field[name]', 'field_value', checked: true
100
+
101
+ html_code = %(<input type="checkbox" id="field_name" name="field[name]" value="1"/>)
102
+ assert_dom_helper html_code, :checkBoxTag, 'field[name]'
103
+
104
+ html_code = %(<input type="checkbox" id="field_name" value="1" name="field_name" class="checkbox_class" data-attribute="this is a checkbox field"/>)
105
+ assert_dom_helper html_code, :checkboxTag, 'field_name', nil, false, class: "checkbox_class", data: { attribute: "this is a checkbox field" }
106
+
107
+ html_code = %(<input type="checkbox" id="field_name" name="field_name" value="field_value" class="checkbox_class" data-attribute="this is a checkbox field"/>)
108
+ assert_dom_helper html_code, :checkBoxTag, 'field_name', 'field_value', false, class: "checkbox_class", data: { attribute: "this is a checkbox field" }
109
+
110
+ html_code = %(<input type="checkbox" id="field_name" name="field_name" checked="checked" value="field_value" class="checkbox_class" data-attribute="this is a checkbox field"/>)
111
+ assert_dom_helper html_code, :checkBoxTag, 'field_name', 'field_value', true, class: "checkbox_class", data: { attribute: "this is a checkbox field" }
112
+ end
113
+
114
+ def test_fieldset_tag
115
+ html_code = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
116
+ assert_dom_helper( html_code, :fieldsetTag, 'Your details' ){ 'Hello world!' }
117
+
118
+ html_code = %(<fieldset>Hello world!</fieldset>)
119
+ assert_dom_helper( html_code, :fieldSetTag ){ 'Hello world!' }
120
+
121
+ html_code = %(<fieldset>Hello world!</fieldset>)
122
+ assert_dom_helper( html_code, :fieldsetTag, '' ){ 'Hello world!' }
123
+
124
+ html_code = %(<fieldset class="format">Hello world!</fieldset>)
125
+ assert_dom_helper( html_code, :fieldSetTag, '', class: 'format' ){ 'Hello world!' }
126
+
127
+ html_code = %(<fieldset></fieldset>)
128
+ assert_dom_helper html_code, :fieldSetTag
129
+
130
+ html_code = %(<fieldset><legend>You legend!</legend></fieldset>)
131
+ assert_dom_helper html_code, :fieldsetTag, 'You legend!'
132
+ end
133
+
134
+ def test_form_tag
135
+ assert_dom_helper whole_form, :formTag
136
+ end
137
+
138
+ def test_form_tag_multipart
139
+ expected = whole_form 'http://www.example.com', enctype: true
140
+ assert_dom_helper expected, :formTag, 'http://www.example.com', multipart: true
141
+ end
142
+
143
+ def test_form_tag_with_method_patch
144
+ expected = whole_form 'http://www.example.com', method: :patch
145
+ assert_dom_helper expected, :formTag, 'http://www.example.com', method: :patch
146
+ end
147
+
148
+ def test_form_tag_with_method_put
149
+ expected = whole_form'http://www.example.com', method: :put
150
+ assert_dom_helper expected, :formTag, 'http://www.example.com', method: :put
151
+ end
152
+
153
+ def test_form_tag_with_method_delete
154
+ expected = whole_form 'http://www.example.com', method: :delete
155
+ assert_dom_helper expected, :formTag, 'http://www.example.com', method: :delete
156
+ end
157
+
158
+ def test_form_tag_with_remote
159
+ expected = whole_form 'http://www.example.com', remote: true
160
+ assert_dom_helper expected, :formTag, 'http://www.example.com', remote: true
161
+ end
162
+
163
+ def test_form_tag_with_remote_false
164
+ expected = whole_form '/', remote: false
165
+ assert_dom_helper expected, :formTag, nil, remote: false
166
+ end
167
+
168
+ def test_form_tag_enforce_utf8_true
169
+ expected = whole_form 'http://www.example.com', enforce_utf8: true
170
+ assert_dom_helper expected, :formTag, 'http://www.example.com', enforce_utf8: true
171
+ end
172
+
173
+ def test_form_tag_enforce_utf8_false
174
+ expected = whole_form 'http://www.example.com', enforce_utf8: false
175
+ assert_dom_helper expected, :formTag, 'http://www.example.com', enforce_utf8: false
176
+ end
177
+
178
+ def test_form_tag_with_block
179
+ expected = whole_form{ 'Hello world!' }
180
+ assert_dom_helper(expected, :formTag){ 'Hello world!' }
181
+ assert_dom_js_helper expected, :formTag, %( function(){ return 'Hello world!';} )
182
+ end
183
+
184
+ def test_form_tag_with_block_and_method
185
+ expected = whole_form('http://www.example.com', method: :put){ 'Hello world!' }
186
+ assert_dom_helper(expected, :formTag, 'http://www.example.com', method: :put){ 'Hello world!' }
187
+ assert_dom_js_helper expected, :formTag, %( 'http://www.example.com', { method: 'put' }, function(){ return 'Hello world!';} )
188
+ end
189
+
190
+ def test_image_submit_tag
191
+ html_code = %(<input alt="Save" type="image" src="/images/save.gif"/>)
192
+ assert_dom_helper html_code, :imageSubmitTag, 'save.gif'
193
+
194
+ html_code = %(<input alt="Save" type="image" src="/images/save.gif" data-confirm="Are you sure?" />)
195
+ assert_dom_helper html_code, :imageSubmitTag, 'save.gif', data: { confirm: 'Are you sure?' }
196
+ end
197
+
198
+ def test_label_tag_without_text
199
+ expected = %(<label for="title">Title</label>)
200
+ assert_dom_helper expected, :labelTag, 'title'
201
+
202
+ expected = %(<label for="title">Title</label>)
203
+ assert_dom_helper expected, :labelTag, :title
204
+ end
205
+
206
+ def test_label_tag_with_text
207
+ expected = %(<label for="title">My Title</label>)
208
+ assert_dom_helper expected, :labelTag, 'title', 'My Title'
209
+ end
210
+
211
+ def test_label_tag_class_string
212
+ expected = %(<label for="title" class="small_label">My Title</label>)
213
+ assert_dom_helper expected, :labelTag, 'title', 'My Title', class: 'small_label'
214
+ end
215
+
216
+ def test_label_tag_id_sanitized
217
+ expected = %(<label for="item_title">Item title</label>)
218
+ assert_dom_helper expected, :labelTag, 'item[title]'
219
+ end
220
+
221
+ def test_label_tag_with_block
222
+ assert_dom_helper('<label>Blocked</label>', :labelTag){ 'Blocked' }
223
+ end
224
+
225
+ def test_label_tag_with_block_and_argument
226
+ assert_dom_helper('<label for="clock">Grandfather</label>', :labelTag, 'clock'){ 'Grandfather' }
227
+ end
228
+
229
+ def test_label_tag_with_block_and_argument_and_options
230
+ assert_dom_helper('<label for="clock" id="label_clock">Grandfather</label>', :labelTag, 'clock', id: 'label_clock'){ 'Grandfather' }
231
+ end
232
+
233
+ def test_radio_button_tag
234
+ expected = %(<input id="people_david" name="people" type="radio" value="david" />)
235
+ assert_dom_helper expected, :radioButtonTag, 'people', 'david'
236
+
237
+ expected = %(<input id="num_people_5" name="num_people" type="radio" value="5" />)
238
+ assert_dom_helper expected, :radioButtonTag, 'num_people', 5
239
+
240
+ expected = %(<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />)
241
+ assert_dom_template expected, %( this.radioButtonTag('gender', 'm') + this.radioButtonTag('gender', 'f') )
242
+
243
+ expected = %(<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />)
244
+ assert_dom_template expected, %( this.radioButtonTag('opinion', '-1') + this.radioButtonTag('opinion', '1') )
245
+
246
+ expected = %(<input id="person_gender_m" name="person[gender]" type="radio" value="m" />)
247
+ assert_dom_helper expected, :radioButtonTag, 'person[gender]', 'm'
248
+
249
+ expected = %(<input id="ctrlname_apache2.2" name="ctrlname" type="radio" value="apache2.2" />)
250
+ assert_dom_helper expected, :radioButtonTag, 'ctrlname', 'apache2.2'
251
+ end
252
+
253
+ def test_select_tag
254
+ expected = %(<select id="people" name="people"><option>david</option></select>)
255
+ assert_dom_helper expected, :selectTag, 'people', '<option>david</option>'
256
+ end
257
+
258
+ def test_select_tag_with_multiple
259
+ expected = %(<select id="colors" multiple="multiple" name="colors"><option>Red</option><option>Blue</option><option>Green</option></select>)
260
+ assert_dom_helper expected, :selectTag, 'colors', '<option>Red</option><option>Blue</option><option>Green</option>', multiple: true
261
+ end
262
+
263
+ def test_select_tag_disabled
264
+ expected = %(<select id="places" disabled="disabled" name="places"><option>Home</option><option>Work</option><option>Pub</option></select>)
265
+ assert_dom_helper expected, :selectTag, 'places', '<option>Home</option><option>Work</option><option>Pub</option>', disabled: true
266
+ end
267
+
268
+ def test_select_tag_id_sanitized
269
+ expected = %(<select id="project_1people" name="project[1]people"><option>david</option></select>)
270
+ assert_dom_helper expected, :selectTag, 'project[1]people', '<option>david</option>'
271
+ end
272
+
273
+ def test_select_tag_with_include_blank
274
+ expected = %(<select id="places" name="places"><option value=""></option><option>Home</option><option>Work</option><option>Pub</option></select>)
275
+ assert_dom_helper expected, :selectTag, 'places', '<option>Home</option><option>Work</option><option>Pub</option>', include_blank: true
276
+ end
277
+
278
+ def test_select_tag_with_prompt
279
+ expected = %(<select id="places" name="places"><option value="">string</option><option>Home</option><option>Work</option><option>Pub</option></select>)
280
+ assert_dom_helper expected, :selectTag, 'places', '<option>Home</option><option>Work</option><option>Pub</option>', prompt: 'string'
281
+ end
282
+
283
+ def test_select_tag_escapes_prompt
284
+ expected = %(<select id="places" name="places"><option value="">&lt;script&gt;alert(1337)&lt;/script&gt;</option><option>Home</option><option>Work</option><option>Pub</option></select>)
285
+ assert_dom_helper expected, :selectTag, 'places', '<option>Home</option><option>Work</option><option>Pub</option>', prompt: '<script>alert(1337)</script>'
286
+ end
287
+
288
+ def test_select_tag_with_prompt_and_include_blank
289
+ expected = %(<select name="places" id="places"><option value="">string</option><option value=""></option><option>Home</option><option>Work</option><option>Pub</option></select>)
290
+ assert_dom_helper expected, :selectTag, 'places', '<option>Home</option><option>Work</option><option>Pub</option>', prompt: 'string', include_blank: true
291
+ end
292
+
293
+ def test_select_tag_with_nil_option_tags_and_include_blank
294
+ expected = %(<select id="places" name="places"><option value=""></option></select>)
295
+ assert_dom_helper expected, :selectTag, 'places', nil, include_blank: true
296
+ end
297
+
298
+ def test_select_tag_with_nil_option_tags_and_prompt
299
+ expected = %(<select id="places" name="places"><option value="">string</option></select>)
300
+ assert_dom_helper expected, :selectTag, 'places', nil, prompt: 'string'
301
+ end
302
+
303
+ def test_submit_tag
304
+ expected = %(<input name='commit' data-disable-with="Saving..." onclick="alert(&#39;hello!&#39;)" type="submit" value="Save" />)
305
+ assert_dom_helper expected, :submitTag, 'Save', onclick: "alert('hello!')", data: { disable_with: 'Saving...' }
306
+ end
307
+
308
+ def test_submit_tag_with_no_onclick_options
309
+ expected = %(<input name='commit' data-disable-with="Saving..." type="submit" value="Save" />)
310
+ assert_dom_helper expected, :submitTag, 'Save', data: { disable_with: 'Saving...' }
311
+ end
312
+
313
+ def test_submit_tag_with_confirmation
314
+ expected = %(<input name='commit' type='submit' value='Save' data-confirm="Are you sure?" />)
315
+ assert_dom_helper expected, :submitTag, 'Save', data: { confirm: 'Are you sure?' }
316
+ end
317
+
318
+ def test_text_area_tag_size
319
+ expected = %(<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>)
320
+ assert_dom_helper expected, :textAreaTag, 'body', 'hello world', size: '20x40'
321
+ end
322
+
323
+ def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer
324
+ expected = %(<textarea id="body" name="body">\nhello world</textarea>)
325
+ assert_dom_helper expected, :textareaTag, 'body', 'hello world', size: 20
326
+ end
327
+
328
+ def test_text_area_tag_id_sanitized
329
+ expected = %(<textarea id="item__description" name="item[][description]">\n</textarea>)
330
+ assert_dom_helper expected, :textAreaTag, 'item[][description]'
331
+ end
332
+
333
+ def test_text_area_tag_escape_content
334
+ expected = %(<textarea cols="20" id="body" name="body" rows="40">\n&lt;b&gt;hello\n world&lt;/b&gt;</textarea>)
335
+ assert_dom_helper expected, :textareaTag, 'body', "<b>hello\n world</b>", size: '20x40'
336
+ end
337
+
338
+ def test_text_area_tag_unescaped_content
339
+ expected = %(<textarea cols="20" id="body" name="body" rows="40">\n<b>hello world</b></textarea>)
340
+ assert_dom_helper expected, :textareaTag, 'body', '<b>hello world</b>', size: '20x40', escape: false
341
+ end
342
+
343
+ def test_text_area_tag_unescaped_nil_content
344
+ expected = %(<textarea id="body" name="body">\n</textarea>)
345
+ assert_dom_helper expected, :textareaTag, 'body', nil, escape: false
346
+ end
347
+
348
+ def test_utf8_enforcer_tag
349
+ expected = '<input name="utf8" type="hidden" value="&#x2713;" />'
350
+ assert_dom_helper expected, :utf8EnforcerTag
351
+ end
352
+
353
+ protected
354
+ def whole_form(action = '/', options = {})
355
+ output = form_text(action, options) + hidden_fields(options)
356
+ output << yield if block_given?
357
+ output << '</form>'
358
+ end
359
+
360
+ def form_text(action = '/', options = {})
361
+ remote, enctype, html_class, id, method = options.values_at(:remote, :enctype, :html_class, :id, :method)
362
+
363
+ txt = %{<form accept-charset="UTF-8" action="#{action}"}
364
+
365
+ txt << %{ enctype="multipart/form-data"} if enctype
366
+ txt << %{ data-remote="true"} if remote
367
+ txt << %{ class="#{html_class}"} if html_class
368
+ txt << %{ id="#{id}"} if id
369
+
370
+ txt << %{ method="#{method.to_s == "get" ? "get" : "post"}">}
371
+ end
372
+
373
+ def hidden_fields(options = {})
374
+ method = options[:method]
375
+ enforce_utf8 = options.fetch :enforce_utf8, true
376
+ csrf_protection = options.fetch :authenticity_token, true
377
+
378
+ txt = %{<div style="display:none">}
379
+ txt << %{<input name="utf8" type="hidden" value="&#x2713;" />} if enforce_utf8
380
+ if method && !%w(get post).include?(method.to_s)
381
+ txt << %{<input name="_method" type="hidden" value="#{method}" />}
382
+ end
383
+ txt << %(<input type="hidden" name="authenticity_token" value="#{CSRF_TOKEN}"/>) if csrf_protection
384
+ txt << %{</div>}
385
+ end
386
+
387
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../test_helper'
2
+
3
+ class JavascriptTest < Manatee::ViewTest
4
+
5
+ def test_escape_javascript
6
+ assert_helper '', :escapeJavascript, nil
7
+
8
+ assert_helper %(This \\"thing\\" is really\\n netos\\'), :escapeJavascript, %(This "thing" is really\n netos')
9
+ assert_helper %(backslash\\\\test), :escapeJavascript, %(backslash\\test)
10
+ assert_helper %(dont <\\/close> tags), :escapeJavascript, %(dont </close> tags)
11
+ assert_helper %(unicode &#x2028; newline), :escapeJavascript, %(unicode \342\200\250 newline).force_encoding(Encoding::UTF_8).encode!
12
+ assert_helper %(unicode &#x2029; newline), :escapeJavascript, %(unicode \342\200\251 newline).force_encoding(Encoding::UTF_8).encode!
13
+
14
+ given = %('quoted' "double-quoted" new-line:\n </closed>)
15
+ expect = %(\\'quoted\\' \\"double-quoted\\" new-line:\\n <\\/closed>)
16
+ assert_helper expect, :escapeJavascript, given
17
+
18
+ assert_helper %(dont <\\/close> tags), :j, %(dont </close> tags)
19
+ end
20
+
21
+ def test_javascript_tag
22
+ assert_dom_helper "<script>\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, "alert('hello')"
23
+ end
24
+
25
+ def test_javascript_tag_with_options
26
+ assert_dom_helper "<script id=\"the_js_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, "alert('hello')", id: "the_js_tag"
27
+ end
28
+
29
+ def test_javascript_tag_with_block
30
+ assert_dom_helper "<script>\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, lambda{ "alert('hello')" }
31
+ assert_dom_helper "<script id=\"js_script_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, lambda{ "alert('hello')" }, { id: 'js_script_tag' }
32
+ assert_dom_helper "<script id=\"js_script_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, { id: 'js_script_tag' }, lambda{ "alert('hello')" }
33
+
34
+ assert_dom_js_helper "<script>\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, %(function(){ return "alert('hello')"; })
35
+ assert_dom_js_helper "<script id=\"js_script_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, %( {id: 'js_script_tag'}, function(){ return "alert('hello')"; } )
36
+ assert_dom_js_helper "<script id=\"js_script_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", :javascriptTag, %( function(){ return "alert('hello')"; }, {id: 'js_script_tag'} )
37
+ end
38
+
39
+ end
@@ -0,0 +1,111 @@
1
+ require_relative '../test_helper'
2
+
3
+ class NumberTest < Manatee::ViewTest
4
+
5
+ def test_number_to_currency
6
+ assert_helper nil, :numberToCurrency, nil
7
+ assert_helper '$1,234,567,890.50', :numberToCurrency, 1234567890.50
8
+ assert_helper '$1,234,567,892', :numberToCurrency, 1234567891.50, precision: 0
9
+ assert_helper '1,234,567,890.50 - K&#269;', :numberToCurrency, '-1234567890.50', unit: 'K&#269;', format: '%n %u', negative_format: '%n - %u', escape: false
10
+ assert_helper '&amp;pound;1,234,567,890.50', :numberToCurrency, '1234567890.50', unit: '&pound;'
11
+ assert_helper '&lt;b&gt;1,234,567,890.50&lt;/b&gt; $', :numberToCurrency, '1234567890.50', format: '<b>%n</b> %u'
12
+ assert_helper '&lt;b&gt;1,234,567,890.50&lt;/b&gt; $', :numberToCurrency, '-1234567890.50', negative_format: '<b>%n</b> %u'
13
+ assert_helper '&lt;b&gt;1,234,567,890.50&lt;/b&gt; $', :numberToCurrency, '-1234567890.50', negative_format: '<b>%n</b> %u'
14
+ end
15
+
16
+ # def test_number_to_phone
17
+ # assert_helper nil, :numberToPhone, nil
18
+ # assert_helper '555-1234', :numberToPhone, 5551234
19
+ # assert_helper '(800) 555-1212 x 123', :numberToPhone, 8005551212, area_code: true, extension: 123
20
+ # assert_helper '+18005551212', :numberToPhone, 8005551212, delimiter: '', country_code: 1
21
+ # assert_helper '+&lt;script&gt;&lt;/script&gt;8005551212', :numberToPhone, 8005551212, delimiter: '', country_code: '<script></script>'
22
+ # assert_helper '8005551212 x &lt;script&gt;&lt;/script&gt;', :numberToPhone, 8005551212, delimiter: '', extension: '<script></script>'
23
+ # end
24
+
25
+ # def test_number_to_percentage
26
+ # assert_helper nil, :numberToPercentage, nil
27
+ # assert_helper '100.000%', :numberToPercentage, 100
28
+ # assert_helper '100.000 %', :numberToPercentage, 100, format: '%n %'
29
+ # assert_helper '&lt;b&gt;100.000&lt;/b&gt; %', :numberToPercentage, 100, format: '<b>%n</b> %'
30
+ # assert_helper '<b>100.000</b> %', :numberToPercentage, 100, format: raw('<b>%n</b> %')
31
+ # assert_helper '100%', :numberToPercentage, 100, precision: 0
32
+ # assert_helper '123.4%', :numberToPercentage, 123.400, precision: 3, strip_insignificant_zeros: true
33
+ # assert_helper '1.000,000%', :numberToPercentage, 1000, delimiter: '.', separator: ','
34
+ # assert_helper '98a%', :numberToPercentage, '98a'
35
+ # assert_helper 'NaN%', :numberToPercentage, Float::NAN
36
+ # assert_helper 'Inf%', :numberToPercentage, Float::INFINITY
37
+ # end
38
+
39
+ # def test_number_with_delimiter
40
+ # assert_helper nil, :numberWithDelimiter, nil
41
+ # assert_helper '12,345,678', :numberWithDelimiter, 12345678
42
+ # assert_helper '0', :numberWithDelimiter, 0
43
+ # end
44
+
45
+ # def test_number_with_precision
46
+ # assert_helper nil, :numberWithPrecision, nil
47
+ # assert_helper '-111.235', :numberWithPrecision, -111.2346
48
+ # assert_helper '111.00', :numberWithPrecision, 111, precision: 2
49
+ # assert_helper '0.00100', :numberWithPrecision, 0.001, precision: 5
50
+ # assert_helper '3.33', :numberWithPrecision, Rational(10, 3), precision: 2
51
+ # end
52
+
53
+ # def test_number_to_human_size
54
+ # assert_helper nil, :numberToHumanSize, nil
55
+ # assert_helper '3 Bytes', :numberToHumanSize, 3.14159265
56
+ # assert_helper '1.2 MB', :numberToHumanSize, 1234567, precision: 2
57
+ # end
58
+
59
+ # def test_number_to_human
60
+ # assert_helper nil, :numberToHuman, nil
61
+ # assert_helper '0', :numberToHuman, 0
62
+ # assert_helper '1.23 Thousand', :numberToHuman, 1234
63
+ # assert_helper '489.0 Thousand', :numberToHuman, 489000, precision: 4, strip_insignificant_zeros: false
64
+ # end
65
+
66
+ # def test_number_to_human_escape_units
67
+ # volume = { unit: '<b>ml</b>', thousand: '<b>lt</b>', million: '<b>m3</b>', trillion: '<b>km3</b>', quadrillion: '<b>Pl</b>' }
68
+ # assert_helper '123 &lt;b&gt;lt&lt;/b&gt;', :numberToHuman, 123456, units: volume
69
+ # assert_helper '12 &lt;b&gt;ml&lt;/b&gt;', :numberToHuman, 12, units: volume
70
+ # assert_helper '1.23 &lt;b&gt;m3&lt;/b&gt;', :numberToHuman, 1234567, units: volume
71
+ # assert_helper '1.23 &lt;b&gt;km3&lt;/b&gt;', :numberToHuman, 1_234_567_000_000, units: volume
72
+ # assert_helper '1.23 &lt;b&gt;Pl&lt;/b&gt;', :numberToHuman, 1_234_567_000_000_000, units: volume
73
+ #
74
+ # #Including fractionals
75
+ # distance = { mili: '<b>mm</b>', centi: '<b>cm</b>', deci: '<b>dm</b>', unit: '<b>m</b>',
76
+ # ten: '<b>dam</b>', hundred: '<b>hm</b>', thousand: '<b>km</b>',
77
+ # micro: '<b>um</b>', nano: '<b>nm</b>', pico: '<b>pm</b>', femto: '<b>fm</b>'}
78
+ # assert_helper '1.23 &lt;b&gt;mm&lt;/b&gt;', :numberToHuman, 0.00123, units: distance
79
+ # assert_helper '1.23 &lt;b&gt;cm&lt;/b&gt;', :numberToHuman, 0.0123, units: distance
80
+ # assert_helper '1.23 &lt;b&gt;dm&lt;/b&gt;', :numberToHuman, 0.123, units: distance
81
+ # assert_helper '1.23 &lt;b&gt;m&lt;/b&gt;', :numberToHuman, 1.23, units: distance
82
+ # assert_helper '1.23 &lt;b&gt;dam&lt;/b&gt;', :numberToHuman, 12.3, units: distance
83
+ # assert_helper '1.23 &lt;b&gt;hm&lt;/b&gt;', :numberToHuman, 123, units: distance
84
+ # assert_helper '1.23 &lt;b&gt;km&lt;/b&gt;', :numberToHuman, 1230, units: distance
85
+ # assert_helper '1.23 &lt;b&gt;um&lt;/b&gt;', :numberToHuman, 0.00000123, units: distance
86
+ # assert_helper '1.23 &lt;b&gt;nm&lt;/b&gt;', :numberToHuman, 0.00000000123, units: distance
87
+ # assert_helper '1.23 &lt;b&gt;pm&lt;/b&gt;', :numberToHuman, 0.00000000000123, units: distance
88
+ # assert_helper '1.23 &lt;b&gt;fm&lt;/b&gt;', :numberToHuman, 0.00000000000000123, units: distance
89
+ # end
90
+
91
+ # def test_number_helpers_escape_delimiter_and_separator
92
+ # assert_helper '111&lt;script&gt;&lt;/script&gt;111&lt;script&gt;&lt;/script&gt;1111', :numberToPhone, 1111111111, delimiter: '<script></script>'
93
+ #
94
+ # assert_helper '$1&lt;script&gt;&lt;/script&gt;01', :numberToCurrency, 1.01, separator: '<script></script>'
95
+ # assert_helper '$1&lt;script&gt;&lt;/script&gt;000.00', :numberToCurrency, 1000, delimiter: '<script></script>'
96
+ #
97
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;010%', :numberToPercentage, 1.01, separator: '<script></script>'
98
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;000.000%', :numberToPercentage, 1000, delimiter: '<script></script>'
99
+ #
100
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;01', :numberWithDelimiter, 1.01, separator: '<script></script>'
101
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;000', :numberWithDelimiter, 1000, delimiter: '<script></script>'
102
+ #
103
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;010', :numberWithDrecision, 1.01, separator: '<script></script>'
104
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;000.000', :numberWithDrecision, 1000, delimiter: '<script></script>'
105
+ #
106
+ # assert_helper '9&lt;script&gt;&lt;/script&gt;86 KB', :numberToHuman_size, 10100, separator: '<script></script>'
107
+ #
108
+ # assert_helper '1&lt;script&gt;&lt;/script&gt;01', :numberToHuman, 1.01, separator: '<script></script>'
109
+ # assert_helper '100&lt;script&gt;&lt;/script&gt;000 Quadrillion', :numberToHuman, 10**20, delimiter: '<script></script>'
110
+ # end
111
+ end