tennpipes-helper 3.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.rdoc +239 -0
  4. data/Rakefile +1 -0
  5. data/lib/tennpipes-helper.rb +62 -0
  6. data/lib/tennpipes-helper/asset_tag_helpers.rb +394 -0
  7. data/lib/tennpipes-helper/form_builder/abstract_form_builder.rb +279 -0
  8. data/lib/tennpipes-helper/form_builder/standard_form_builder.rb +40 -0
  9. data/lib/tennpipes-helper/form_helpers.rb +639 -0
  10. data/lib/tennpipes-helper/form_helpers/errors.rb +138 -0
  11. data/lib/tennpipes-helper/form_helpers/options.rb +98 -0
  12. data/lib/tennpipes-helper/form_helpers/security.rb +70 -0
  13. data/lib/tennpipes-helper/format_helpers.rb +372 -0
  14. data/lib/tennpipes-helper/locale/cs.yml +103 -0
  15. data/lib/tennpipes-helper/locale/da.yml +91 -0
  16. data/lib/tennpipes-helper/locale/de.yml +81 -0
  17. data/lib/tennpipes-helper/locale/en.yml +103 -0
  18. data/lib/tennpipes-helper/locale/es.yml +103 -0
  19. data/lib/tennpipes-helper/locale/fr.yml +79 -0
  20. data/lib/tennpipes-helper/locale/hu.yml +103 -0
  21. data/lib/tennpipes-helper/locale/it.yml +89 -0
  22. data/lib/tennpipes-helper/locale/ja.yml +103 -0
  23. data/lib/tennpipes-helper/locale/lv.yml +103 -0
  24. data/lib/tennpipes-helper/locale/nl.yml +82 -0
  25. data/lib/tennpipes-helper/locale/no.yml +91 -0
  26. data/lib/tennpipes-helper/locale/pl.yml +95 -0
  27. data/lib/tennpipes-helper/locale/pt_br.yml +103 -0
  28. data/lib/tennpipes-helper/locale/ro.yml +103 -0
  29. data/lib/tennpipes-helper/locale/ru.yml +103 -0
  30. data/lib/tennpipes-helper/locale/sv.yml +103 -0
  31. data/lib/tennpipes-helper/locale/tr.yml +103 -0
  32. data/lib/tennpipes-helper/locale/uk.yml +103 -0
  33. data/lib/tennpipes-helper/locale/zh_cn.yml +103 -0
  34. data/lib/tennpipes-helper/locale/zh_tw.yml +103 -0
  35. data/lib/tennpipes-helper/number_helpers.rb +283 -0
  36. data/lib/tennpipes-helper/output_helpers.rb +226 -0
  37. data/lib/tennpipes-helper/output_helpers/abstract_handler.rb +61 -0
  38. data/lib/tennpipes-helper/output_helpers/erb_handler.rb +27 -0
  39. data/lib/tennpipes-helper/output_helpers/haml_handler.rb +25 -0
  40. data/lib/tennpipes-helper/output_helpers/slim_handler.rb +18 -0
  41. data/lib/tennpipes-helper/render_helpers.rb +63 -0
  42. data/lib/tennpipes-helper/tag_helpers.rb +294 -0
  43. data/lib/tennpipes-helper/translation_helpers.rb +36 -0
  44. data/lib/tennpipes/rendering.rb +369 -0
  45. data/lib/tennpipes/rendering/erb_template.rb +40 -0
  46. data/lib/tennpipes/rendering/erubis_template.rb +66 -0
  47. data/lib/tennpipes/rendering/haml_template.rb +26 -0
  48. data/lib/tennpipes/rendering/slim_template.rb +20 -0
  49. data/test/fixtures/apps/render.rb +25 -0
  50. data/test/fixtures/apps/views/article/comment/show.slim +1 -0
  51. data/test/fixtures/apps/views/blog/post.erb +1 -0
  52. data/test/fixtures/apps/views/layouts/specific.erb +1 -0
  53. data/test/fixtures/apps/views/test/post.erb +1 -0
  54. data/test/fixtures/layouts/layout.erb +1 -0
  55. data/test/fixtures/markup_app/app.rb +87 -0
  56. data/test/fixtures/markup_app/views/button_to.erb +8 -0
  57. data/test/fixtures/markup_app/views/button_to.haml +5 -0
  58. data/test/fixtures/markup_app/views/button_to.slim +6 -0
  59. data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
  60. data/test/fixtures/markup_app/views/capture_concat.haml +12 -0
  61. data/test/fixtures/markup_app/views/capture_concat.slim +12 -0
  62. data/test/fixtures/markup_app/views/content_for.erb +23 -0
  63. data/test/fixtures/markup_app/views/content_for.haml +19 -0
  64. data/test/fixtures/markup_app/views/content_for.slim +19 -0
  65. data/test/fixtures/markup_app/views/content_tag.erb +13 -0
  66. data/test/fixtures/markup_app/views/content_tag.haml +11 -0
  67. data/test/fixtures/markup_app/views/content_tag.slim +11 -0
  68. data/test/fixtures/markup_app/views/current_engine.erb +5 -0
  69. data/test/fixtures/markup_app/views/current_engine.haml +5 -0
  70. data/test/fixtures/markup_app/views/current_engine.slim +5 -0
  71. data/test/fixtures/markup_app/views/fields_for.erb +20 -0
  72. data/test/fixtures/markup_app/views/fields_for.haml +15 -0
  73. data/test/fixtures/markup_app/views/fields_for.slim +15 -0
  74. data/test/fixtures/markup_app/views/form_for.erb +72 -0
  75. data/test/fixtures/markup_app/views/form_for.haml +59 -0
  76. data/test/fixtures/markup_app/views/form_for.slim +59 -0
  77. data/test/fixtures/markup_app/views/form_tag.erb +95 -0
  78. data/test/fixtures/markup_app/views/form_tag.haml +78 -0
  79. data/test/fixtures/markup_app/views/form_tag.slim +79 -0
  80. data/test/fixtures/markup_app/views/link_to.erb +5 -0
  81. data/test/fixtures/markup_app/views/link_to.haml +4 -0
  82. data/test/fixtures/markup_app/views/link_to.slim +4 -0
  83. data/test/fixtures/markup_app/views/mail_to.erb +3 -0
  84. data/test/fixtures/markup_app/views/mail_to.haml +3 -0
  85. data/test/fixtures/markup_app/views/mail_to.slim +3 -0
  86. data/test/fixtures/markup_app/views/meta_tag.erb +3 -0
  87. data/test/fixtures/markup_app/views/meta_tag.haml +3 -0
  88. data/test/fixtures/markup_app/views/meta_tag.slim +3 -0
  89. data/test/fixtures/markup_app/views/partials/_erb.erb +1 -0
  90. data/test/fixtures/markup_app/views/partials/_haml.haml +1 -0
  91. data/test/fixtures/markup_app/views/partials/_slim.slim +1 -0
  92. data/test/fixtures/markup_app/views/simple_partial.erb +1 -0
  93. data/test/fixtures/markup_app/views/simple_partial.haml +1 -0
  94. data/test/fixtures/markup_app/views/simple_partial.slim +1 -0
  95. data/test/fixtures/render_app/app.rb +110 -0
  96. data/test/fixtures/render_app/views/_deep.erb +3 -0
  97. data/test/fixtures/render_app/views/_deep.haml +2 -0
  98. data/test/fixtures/render_app/views/_deep.slim +2 -0
  99. data/test/fixtures/render_app/views/_partial_block_erb.erb +10 -0
  100. data/test/fixtures/render_app/views/_partial_block_haml.haml +7 -0
  101. data/test/fixtures/render_app/views/_partial_block_slim.slim +7 -0
  102. data/test/fixtures/render_app/views/_unsafe.html.builder +2 -0
  103. data/test/fixtures/render_app/views/_unsafe_object.html.builder +2 -0
  104. data/test/fixtures/render_app/views/current_engine.haml +5 -0
  105. data/test/fixtures/render_app/views/current_engines/_erb.erb +1 -0
  106. data/test/fixtures/render_app/views/current_engines/_haml.haml +1 -0
  107. data/test/fixtures/render_app/views/current_engines/_slim.slim +1 -0
  108. data/test/fixtures/render_app/views/dive_inner_erb.erb +3 -0
  109. data/test/fixtures/render_app/views/dive_inner_haml.haml +2 -0
  110. data/test/fixtures/render_app/views/dive_inner_slim.slim +2 -0
  111. data/test/fixtures/render_app/views/dive_outer_erb.erb +3 -0
  112. data/test/fixtures/render_app/views/dive_outer_haml.haml +2 -0
  113. data/test/fixtures/render_app/views/dive_outer_slim.slim +2 -0
  114. data/test/fixtures/render_app/views/double_capture_erb.erb +3 -0
  115. data/test/fixtures/render_app/views/double_capture_haml.haml +2 -0
  116. data/test/fixtures/render_app/views/double_capture_slim.slim +2 -0
  117. data/test/fixtures/render_app/views/erb/test.erb +1 -0
  118. data/test/fixtures/render_app/views/explicit_engine.haml +5 -0
  119. data/test/fixtures/render_app/views/haml/test.haml +1 -0
  120. data/test/fixtures/render_app/views/render_block_erb.erb +5 -0
  121. data/test/fixtures/render_app/views/render_block_haml.haml +4 -0
  122. data/test/fixtures/render_app/views/render_block_slim.slim +4 -0
  123. data/test/fixtures/render_app/views/ruby_block_capture_erb.erb +1 -0
  124. data/test/fixtures/render_app/views/ruby_block_capture_haml.haml +1 -0
  125. data/test/fixtures/render_app/views/ruby_block_capture_slim.slim +1 -0
  126. data/test/fixtures/render_app/views/template/_user.haml +7 -0
  127. data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
  128. data/test/fixtures/render_app/views/template/some_template.haml +2 -0
  129. data/test/fixtures/render_app/views/wrong_capture_erb.erb +3 -0
  130. data/test/fixtures/render_app/views/wrong_capture_haml.haml +2 -0
  131. data/test/fixtures/render_app/views/wrong_capture_slim.slim +2 -0
  132. data/test/helper.rb +88 -0
  133. data/test/test_asset_tag_helpers.rb +401 -0
  134. data/test/test_form_builder.rb +1216 -0
  135. data/test/test_form_helpers.rb +1056 -0
  136. data/test/test_format_helpers.rb +251 -0
  137. data/test/test_helpers.rb +10 -0
  138. data/test/test_locale.rb +20 -0
  139. data/test/test_number_helpers.rb +142 -0
  140. data/test/test_output_helpers.rb +157 -0
  141. data/test/test_render_helpers.rb +225 -0
  142. data/test/test_rendering.rb +706 -0
  143. data/test/test_rendering_extensions.rb +14 -0
  144. data/test/test_tag_helpers.rb +131 -0
  145. metadata +299 -0
@@ -0,0 +1,251 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/markup_app/app')
3
+
4
+ describe "FormatHelpers" do
5
+ include Tennpipes::Helpers::FormatHelpers
6
+
7
+ def app
8
+ MarkupDemo
9
+ end
10
+
11
+ def setup
12
+ Time.stubs(:now).returns(Time.utc(1983, 11, 9, 5))
13
+ end
14
+
15
+ describe 'for #simple_format method' do
16
+ it 'should format simple text into html format' do
17
+ actual_text = simple_format("Here is some basic text...\n...with a line break.")
18
+ assert_equal true, actual_text.html_safe?
19
+ assert_equal "<p>Here is some basic text...\n<br />...with a line break.</p>", actual_text
20
+ end
21
+
22
+ it 'should format more text into html format' do
23
+ actual_text = simple_format("We want to put a paragraph...\n\n...right there.")
24
+ assert_equal "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>", actual_text
25
+ end
26
+
27
+ it 'should support defining a class for the paragraphs' do
28
+ actual_text = simple_format("Look me! A class!", :class => 'description')
29
+ assert_equal "<p class=\"description\">Look me! A class!</p>", actual_text
30
+ end
31
+
32
+ it 'should escape html tags' do
33
+ actual_text = simple_format("Will you escape <b>that</b>?")
34
+ assert_equal "<p>Will you escape &lt;b&gt;that&lt;&#x2F;b&gt;?</p>", actual_text
35
+ end
36
+
37
+ it 'should support already sanitized text' do
38
+ actual_text = simple_format("Don't try to escape <b>me</b>!".html_safe)
39
+ assert_equal "<p>Don't try to escape <b>me</b>!</p>", actual_text
40
+ end
41
+
42
+ describe 'wrapped in a custom tag' do
43
+ it 'should format simple text into html format' do
44
+ actual_text = simple_format("Here is some basic text...\n...with a line break.", :tag => :div)
45
+ assert_equal "<div>Here is some basic text...\n<br />...with a line break.</div>", actual_text
46
+ end
47
+
48
+ it 'should format more text into html format' do
49
+ actual_text = simple_format("We want to put a paragraph...\n\n...right there.", :tag => :div)
50
+ assert_equal "<div>We want to put a paragraph...</div>\n\n<div>...right there.</div>", actual_text
51
+ end
52
+
53
+ it 'should support defining a class for the paragraphs' do
54
+ actual_text = simple_format("Look me! A class!", :class => 'description', :tag => :div)
55
+ assert_equal "<div class=\"description\">Look me! A class!</div>", actual_text
56
+ end
57
+ end
58
+ end
59
+
60
+ describe 'for #pluralize method' do
61
+ it 'should return singular count for 1 item collections' do
62
+ actual_text = pluralize(1, 'person')
63
+ assert_equal '1 person', actual_text
64
+ end
65
+ it 'should return plural count for empty collections' do
66
+ actual_text = pluralize(0, 'person')
67
+ assert_equal '0 people', actual_text
68
+ end
69
+ it 'should return plural count for many collections' do
70
+ actual_text = pluralize(2, 'person')
71
+ assert_equal '2 people', actual_text
72
+ end
73
+ it 'should return pluralized word specified as argument' do
74
+ actual_text = pluralize(3, 'person', 'users')
75
+ assert_equal '3 users', actual_text
76
+ end
77
+ end
78
+
79
+ describe 'for #word_wrap method' do
80
+ it 'should return proper formatting for 8 max width' do
81
+ actual_text = word_wrap('Once upon a time', :line_width => 8)
82
+ assert_equal "Once\nupon a\ntime", actual_text
83
+ end
84
+ it 'should return proper formatting for 1 max width' do
85
+ actual_text = word_wrap('Once upon a time', :line_width => 1)
86
+ assert_equal "Once\nupon\na\ntime", actual_text
87
+ end
88
+ it 'should return proper formatting for default width' do
89
+ actual_text = word_wrap((1..50).to_a.join(" "))
90
+ assert_equal (1..30).to_a.join(" ") + "\n" + (31..50).to_a.join(" "), actual_text
91
+ actual_text = word_wrap((1..50).to_a.join(" "), 80)
92
+ assert_equal (1..30).to_a.join(" ") + "\n" + (31..50).to_a.join(" "), actual_text
93
+ end
94
+ end
95
+
96
+ describe 'for #highlight method' do
97
+ it 'should highligth with defaults' do
98
+ actual_text = highlight('Lorem ipsum dolor sit amet', 'dolor')
99
+ assert_equal 'Lorem ipsum <strong class="highlight">dolor</strong> sit amet', actual_text
100
+ end
101
+
102
+ it 'should highlight with highlighter' do
103
+ actual_text = highlight('Lorem ipsum dolor sit amet', 'dolor', :highlighter => '<span class="custom">\1</span>')
104
+ assert_equal 'Lorem ipsum <span class="custom">dolor</span> sit amet', actual_text
105
+ end
106
+ end
107
+
108
+ describe 'for #truncate method' do
109
+ it 'should support default truncation' do
110
+ actual_text = truncate("Once upon a time in a world far far away")
111
+ assert_equal "Once upon a time in a world...", actual_text
112
+ end
113
+ it 'should support specifying length' do
114
+ actual_text = truncate("Once upon a time in a world far far away", :length => 14)
115
+ assert_equal "Once upon a...", actual_text
116
+ end
117
+ it 'should support specifying omission text' do
118
+ actual_text = truncate("And they found that many people were sleeping better.", :length => 25, :omission => "(clipped)")
119
+ assert_equal "And they found t(clipped)", actual_text
120
+ end
121
+ end
122
+
123
+ describe 'for #truncate_words method' do
124
+ it 'should support default truncation' do
125
+ actual_text = truncate_words("Long before books were made, people told stories. They told them to one another and to the children as they sat before the fire. Many of these stories were about interesting people, but most of them were about the ways of fairies and giants.")
126
+ assert_equal "Long before books were made, people told stories. They told them to one another and to the children as they sat before the fire. Many of these stories were about...", actual_text
127
+ end
128
+ it 'should support specifying length' do
129
+ actual_text = truncate_words("Once upon a time in a world far far away", :length => 8)
130
+ assert_equal "Once upon a time in a world far...", actual_text
131
+ end
132
+ it 'should support specifying omission text' do
133
+ actual_text = truncate_words("And they found that many people were sleeping better.", :length => 4, :omission => "(clipped)")
134
+ assert_equal "And they found that(clipped)", actual_text
135
+ end
136
+ end
137
+
138
+ describe 'for #h and #h! method' do
139
+ it 'should escape the simple html' do
140
+ assert_equal '&lt;h1&gt;hello&lt;&#x2F;h1&gt;', h('<h1>hello</h1>')
141
+ assert_equal '&lt;h1&gt;hello&lt;&#x2F;h1&gt;', escape_html('<h1>hello</h1>')
142
+ end
143
+ it 'should escape all brackets, quotes and ampersands' do
144
+ assert_equal '&lt;h1&gt;&lt;&gt;&quot;&amp;demo&amp;&quot;&lt;&gt;&lt;&#x2F;h1&gt;', h('<h1><>"&demo&"<></h1>')
145
+ end
146
+ it 'should return default text if text is empty' do
147
+ assert_equal 'default', h!("", "default")
148
+ assert_equal '&nbsp;', h!("")
149
+ end
150
+ it 'should return text escaped if not empty' do
151
+ assert_equal '&lt;h1&gt;hello&lt;&#x2F;h1&gt;', h!('<h1>hello</h1>')
152
+ end
153
+ it 'should mark escaped text as safe' do
154
+ assert_equal false, '<h1>hello</h1>'.html_safe?
155
+ assert_equal true, h('<h1>hello</h1>').html_safe?
156
+ assert_equal true, h!("", "default").html_safe?
157
+ end
158
+ end
159
+
160
+ describe 'for #time_ago_in_words method' do
161
+ it 'should less than 5 seconds' do
162
+ assert_equal 'less than 5 seconds', time_ago_in_words(Time.now, true)
163
+ end
164
+ it 'should less than 10 seconds' do
165
+ assert_equal 'less than 10 seconds', time_ago_in_words(Time.now-5, true)
166
+ end
167
+ it 'should less than 20 seconds' do
168
+ assert_equal 'less than 20 seconds', time_ago_in_words(Time.now-10, true)
169
+ end
170
+ it 'should less than a minute' do
171
+ assert_equal 'less than a minute', time_ago_in_words(Time.now-40, true)
172
+ end
173
+ it 'should 2 minutes' do
174
+ assert_equal '2 minutes', time_ago_in_words(Time.now-120, true)
175
+ end
176
+ it 'should display today' do
177
+ assert_equal 'less than a minute', time_ago_in_words(Time.now)
178
+ end
179
+ it 'should display yesterday' do
180
+ assert_equal '1 day', time_ago_in_words(1.day.ago)
181
+ end
182
+ it 'should display tomorrow' do
183
+ assert_equal '1 day', time_ago_in_words(1.day.from_now)
184
+ end
185
+ it 'should return future number of days' do
186
+ assert_equal '4 days', time_ago_in_words(4.days.from_now)
187
+ end
188
+ it 'should return past days ago' do
189
+ assert_equal '4 days', time_ago_in_words(4.days.ago)
190
+ end
191
+ it 'should return formatted archived date' do
192
+ assert_equal '3 months', time_ago_in_words(100.days.ago)
193
+ end
194
+ it 'should return formatted archived year date' do
195
+ assert_equal 'over 1 year', time_ago_in_words(500.days.ago)
196
+ end
197
+ it 'should display now as a minute ago' do
198
+ assert_equal '1 minute', time_ago_in_words(1.minute.ago)
199
+ end
200
+ it 'should display a few minutes ago' do
201
+ assert_equal '4 minutes', time_ago_in_words(4.minute.ago)
202
+ end
203
+ it 'should display an hour ago' do
204
+ assert_equal 'about 1 hour', time_ago_in_words(1.hour.ago + 5.minutes.ago.sec)
205
+ end
206
+ it 'should display a few hours ago' do
207
+ assert_equal 'about 3 hours', time_ago_in_words(3.hour.ago + 5.minutes.ago.sec)
208
+ end
209
+ it 'should display a day ago' do
210
+ assert_equal '1 day', time_ago_in_words(1.day.ago)
211
+ end
212
+ it 'should display a few days ago' do
213
+ assert_equal '5 days', time_ago_in_words(5.days.ago - 5.minutes.ago.sec)
214
+ end
215
+ it 'should display a month ago' do
216
+ assert_equal 'about 1 month', time_ago_in_words(32.days.ago + 5.minutes.ago.sec)
217
+ end
218
+ it 'should display a few months ago' do
219
+ assert_equal '6 months', time_ago_in_words(180.days.ago - 5.minutes.ago.sec)
220
+ end
221
+ it 'should display a year ago' do
222
+ assert_equal 'about 1 year', time_ago_in_words(365.days.ago - 5.minutes.ago.sec)
223
+ end
224
+ it 'should display a few years ago' do
225
+ assert_equal 'over 7 years', time_ago_in_words(2800.days.ago - 5.minutes.ago.sec)
226
+ end
227
+ end
228
+
229
+ describe 'for #js_escape_html method' do
230
+ it 'should escape double quotes' do
231
+ assert_equal "\\\"hello\\\"", js_escape_html('"hello"')
232
+ assert_equal "\\\"hello\\\"", js_escape_html(ActiveSupport::SafeBuffer.new('"hello"'))
233
+ end
234
+ it 'should escape single quotes' do
235
+ assert_equal "\\'hello\\'", js_escape_html("'hello'")
236
+ assert_equal "\\'hello\\'", js_escape_html(ActiveSupport::SafeBuffer.new("'hello'"))
237
+ end
238
+ it 'should escape html tags and breaks' do
239
+ assert_equal "\\n\\n<p>hello<\\/p>\\n", js_escape_html("\n\r<p>hello</p>\r\n")
240
+ assert_equal "\\n\\n<p>hello<\\/p>\\n", js_escape_html(ActiveSupport::SafeBuffer.new("\n\r<p>hello</p>\r\n"))
241
+ end
242
+ it 'should escape data-confirm attribute' do
243
+ assert_equal "<data-confirm=\\\"are you sure\\\">", js_escape_html("<data-confirm=\"are you sure\">")
244
+ assert_equal "<data-confirm=\\\"are you sure\\\">", js_escape_html(ActiveSupport::SafeBuffer.new("<data-confirm=\"are you sure\">"))
245
+ end
246
+ it 'should keep html_safe content html_safe' do
247
+ assert_equal false, js_escape_html('"hello"').html_safe?
248
+ assert_equal true, js_escape_html(ActiveSupport::SafeBuffer.new('"hello"')).html_safe?
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ describe "Tennpipes::Helpers" do
4
+ it 'should allow including without registering' do
5
+ class Foo
6
+ include Tennpipes::Helpers
7
+ end
8
+ assert_equal '<div>bar</div>', Foo.new.content_tag(:div, 'bar')
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ describe "Locale Helpers" do
4
+ Dir[File.expand_path("../../lib/tennpipes-helper/locale/*.yml", __FILE__)].each do |file|
5
+ base_original = YAML.load_file(file)
6
+ name = File.basename(file, '.yml')
7
+ it "should should have correct locale for #{name}" do
8
+ base = base_original[name]['number']['format']
9
+ assert !base['separator'].nil?
10
+ assert !base['delimiter'].nil?
11
+ assert !base['precision'].nil?
12
+ base = base_original[name]['number']['currency']['format']
13
+ assert !base['format'].nil?
14
+ assert !base['unit'].nil?
15
+ assert !base['separator'].nil?
16
+ assert !base['delimiter'].nil?
17
+ assert !base['precision'].nil?
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,142 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/markup_app/app')
3
+
4
+ describe "NumberHelpers" do
5
+ include Tennpipes::Helpers::NumberHelpers
6
+
7
+ def app
8
+ MarkupDemo
9
+ end
10
+
11
+ def kilobytes(number)
12
+ number * 1024
13
+ end
14
+
15
+ def megabytes(number)
16
+ kilobytes(number) * 1024
17
+ end
18
+
19
+ def gigabytes(number)
20
+ megabytes(number) * 1024
21
+ end
22
+
23
+ def terabytes(number)
24
+ gigabytes(number) * 1024
25
+ end
26
+
27
+ describe 'for number helpers functionality' do
28
+ it 'should display number_to_currency' do
29
+ assert_equal "$1,234,567,890.50", number_to_currency(1234567890.50)
30
+ assert_equal "$1,234,567,890.51", number_to_currency(1234567890.506)
31
+ assert_equal "$1,234,567,892", number_to_currency(1234567891.50, {:precision => 0})
32
+ assert_equal "$1,234,567,890.5", number_to_currency(1234567890.50, {:precision => 1})
33
+ assert_equal "&pound;1234567890,50", number_to_currency(1234567890.50, {:unit => "&pound;", :separator => ",", :delimiter => ""})
34
+ assert_equal "$1,234,567,890.50", number_to_currency("1234567890.50")
35
+ assert_equal "1,234,567,890.50 K&#269;", number_to_currency("1234567890.50", {:unit => "K&#269;", :format => "%n %u"})
36
+ assert_equal "$x", number_to_currency("x")
37
+
38
+ assert_nil number_to_currency(nil)
39
+ end
40
+
41
+ it 'should display number_to_percentage' do
42
+ assert_equal "100.000%", number_to_percentage(100)
43
+ assert_equal "100%", number_to_percentage(100, {:precision => 0})
44
+ assert_equal "302.06%", number_to_percentage(302.0574, {:precision => 2})
45
+ assert_equal "100.000%", number_to_percentage("100")
46
+ assert_equal "1000.000%", number_to_percentage("1000")
47
+ assert_equal "x%", number_to_percentage("x")
48
+ assert_equal "1.000,000%", number_to_percentage(1000, :delimiter => '.', :separator => ',')
49
+
50
+ assert_nil number_to_percentage(nil)
51
+ end
52
+
53
+ it 'should display number_with_delimiter' do
54
+ assert_equal "12,345,678", number_with_delimiter(12345678)
55
+ assert_equal "0", number_with_delimiter(0)
56
+ assert_equal "123", number_with_delimiter(123)
57
+ assert_equal "123,456", number_with_delimiter(123456)
58
+ assert_equal "123,456.78", number_with_delimiter(123456.78)
59
+ assert_equal "123,456.789", number_with_delimiter(123456.789)
60
+ assert_equal "123,456.78901", number_with_delimiter(123456.78901)
61
+ assert_equal "123,456,789.78901", number_with_delimiter(123456789.78901)
62
+ assert_equal "0.78901", number_with_delimiter(0.78901)
63
+ assert_equal "123,456.78", number_with_delimiter("123456.78")
64
+ assert_equal "x", number_with_delimiter("x")
65
+
66
+ assert_nil number_with_delimiter(nil)
67
+ end
68
+
69
+ it 'should display number_with_delimiter with options' do
70
+ assert_equal '12 345 678', number_with_delimiter(12345678, :delimiter => ' ')
71
+ assert_equal '12,345,678-05', number_with_delimiter(12345678.05, :separator => '-')
72
+ assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :separator => ',', :delimiter => '.')
73
+ assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :delimiter => '.', :separator => ',')
74
+ end
75
+
76
+ it 'should display number_with_precision' do
77
+ assert_equal "111.235", number_with_precision(111.2346)
78
+ assert_equal "31.83", number_with_precision(31.825, :precision => 2)
79
+ assert_equal "111.23", number_with_precision(111.2346, :precision => 2)
80
+ assert_equal "111.00", number_with_precision(111, :precision => 2)
81
+ assert_equal "111.235", number_with_precision("111.2346")
82
+ assert_equal "31.83", number_with_precision("31.825", :precision => 2)
83
+ assert_equal "3268", number_with_precision((32.6751 * 100.00), :precision => 0)
84
+ assert_equal "112", number_with_precision(111.50, :precision => 0)
85
+ assert_equal "1234567892", number_with_precision(1234567891.50, :precision => 0)
86
+
87
+ # Return non-numeric params unchanged.
88
+ assert_equal "x", number_with_precision("x")
89
+ assert_nil number_with_precision(nil)
90
+ end
91
+
92
+ it 'should display number_with_precision with custom delimiter and separator' do
93
+ assert_equal '31,83', number_with_precision(31.825, :precision => 2, :separator => ',')
94
+ assert_equal '1.231,83', number_with_precision(1231.825, :precision => 2, :separator => ',', :delimiter => '.')
95
+ end
96
+
97
+ it 'should display number_to_human_size' do
98
+ assert_equal '0 Bytes', number_to_human_size(0)
99
+ assert_equal '1 Byte', number_to_human_size(1)
100
+ assert_equal '3 Bytes', number_to_human_size(3.14159265)
101
+ assert_equal '123 Bytes', number_to_human_size(123.0)
102
+ assert_equal '123 Bytes', number_to_human_size(123)
103
+ assert_equal '1.2 KB', number_to_human_size(1234)
104
+ assert_equal '12.1 KB', number_to_human_size(12345)
105
+ assert_equal '1.2 MB', number_to_human_size(1234567)
106
+ assert_equal '1.1 GB', number_to_human_size(1234567890)
107
+ assert_equal '1.1 TB', number_to_human_size(1234567890123)
108
+ assert_equal '1025 TB', number_to_human_size(terabytes(1025))
109
+ assert_equal '444 KB', number_to_human_size(kilobytes(444))
110
+ assert_equal '1023 MB', number_to_human_size(megabytes(1023))
111
+ assert_equal '3 TB', number_to_human_size(terabytes(3))
112
+ assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2)
113
+ assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4)
114
+ assert_equal '123 Bytes', number_to_human_size("123")
115
+ assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2)
116
+ assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4)
117
+ assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :precision => 4)
118
+ assert_equal '1 Byte', number_to_human_size(1.1)
119
+ assert_equal '10 Bytes', number_to_human_size(10)
120
+
121
+ assert_nil number_to_human_size(nil)
122
+ end
123
+
124
+ it 'should display number_to_human_size with options' do
125
+ assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2)
126
+ assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4)
127
+ assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2)
128
+ assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4)
129
+ assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :precision => 4)
130
+ assert_equal '1 TB', number_to_human_size(1234567890123, :precision => 0)
131
+ assert_equal '500 MB', number_to_human_size(524288000, :precision => 0)
132
+ assert_equal '40 KB', number_to_human_size(41010, :precision => 0)
133
+ assert_equal '40 KB', number_to_human_size(41100, :precision => 0)
134
+ end
135
+
136
+ it 'should display number_to_human_size with custom delimiter and separator' do
137
+ assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0123), :precision => 2, :separator => ',')
138
+ assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0100), :precision => 4, :separator => ',')
139
+ assert_equal '1.000,1 TB', number_to_human_size(terabytes(1000.1), :delimiter => '.', :separator => ',')
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,157 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/markup_app/app')
3
+
4
+ describe "OutputHelpers" do
5
+ def app
6
+ MarkupDemo
7
+ end
8
+
9
+ describe 'for #content_for method' do
10
+ it 'should work for erb templates' do
11
+ visit '/erb/content_for'
12
+ assert_have_selector '.demo h1', :content => "This is content yielded from a content_for", :count => 1
13
+ assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith", :count => 1
14
+ assert_have_no_selector '.demo3 p', :content => "One", :class => "duplication"
15
+ assert_have_selector '.demo3 p', :content => "Two", :class => "duplication"
16
+ end
17
+
18
+ it 'should work for haml templates' do
19
+ visit '/haml/content_for'
20
+ assert_have_selector '.demo h1', :content => "This is content yielded from a content_for", :count => 1
21
+ assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith", :count => 1
22
+ assert_have_no_selector '.demo3 p', :content => "One", :class => "duplication"
23
+ assert_have_selector '.demo3 p', :content => "Two", :class => "duplication"
24
+ end
25
+
26
+ it 'should work for slim templates' do
27
+ visit '/slim/content_for'
28
+ assert_have_selector '.demo h1', :content => "This is content yielded from a content_for", :count => 1
29
+ assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith", :count => 1
30
+ assert_have_no_selector '.demo3 p', :content => "One", :class => "duplication"
31
+ assert_have_selector '.demo3 p', :content => "Two", :class => "duplication"
32
+ end
33
+ end # content_for
34
+
35
+ describe "for #content_for? method" do
36
+ it 'should work for erb templates' do
37
+ visit '/erb/content_for'
38
+ assert_have_selector '.demo_has_content', :content => "true"
39
+ assert_have_selector '.fake_has_content', :content => "false"
40
+ end
41
+
42
+ it 'should work for haml templates' do
43
+ visit '/haml/content_for'
44
+ assert_have_selector '.demo_has_content', :content => "true"
45
+ assert_have_selector '.fake_has_content', :content => "false"
46
+ end
47
+
48
+ it 'should work for slim templates' do
49
+ visit '/slim/content_for'
50
+ assert_have_selector '.demo_has_content', :content => "true"
51
+ assert_have_selector '.fake_has_content', :content => "false"
52
+ end
53
+ end # content_for?
54
+
55
+ describe 'for #capture_html method' do
56
+ it 'should work for erb templates' do
57
+ visit '/erb/capture_concat'
58
+ assert_have_selector 'p span', :content => "Captured Line 1", :count => 1
59
+ assert_have_selector 'p span', :content => "Captured Line 2", :count => 1
60
+ end
61
+
62
+ it 'should work for haml templates' do
63
+ visit '/haml/capture_concat'
64
+ assert_have_selector 'p span', :content => "Captured Line 1", :count => 1
65
+ assert_have_selector 'p span', :content => "Captured Line 2", :count => 1
66
+ end
67
+
68
+ it 'should work for slim templates' do
69
+ visit '/slim/capture_concat'
70
+ assert_have_selector 'p span', :content => "Captured Line 1", :count => 1
71
+ assert_have_selector 'p span', :content => "Captured Line 2", :count => 1
72
+ end
73
+ end
74
+
75
+ describe 'for #concat_content method' do
76
+ it 'should work for erb templates' do
77
+ visit '/erb/capture_concat'
78
+ assert_have_selector 'p', :content => "Concat Line 3", :count => 1
79
+ end
80
+
81
+ it 'should work for haml templates' do
82
+ visit '/haml/capture_concat'
83
+ assert_have_selector 'p', :content => "Concat Line 3", :count => 1
84
+ end
85
+
86
+ it 'should work for slim templates' do
87
+ visit '/slim/capture_concat'
88
+ assert_have_selector 'p', :content => "Concat Line 3", :count => 1
89
+ end
90
+ end
91
+
92
+ describe 'for #block_is_template?' do
93
+ it 'should work for erb templates' do
94
+ visit '/erb/capture_concat'
95
+ assert_have_selector 'p', :content => "The erb block passed in is a template", :class => 'is_template', :count => 1
96
+ assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template', :count => 1
97
+ end
98
+
99
+ it 'should work for haml templates' do
100
+ visit '/haml/capture_concat'
101
+ assert_have_selector 'p', :content => "The haml block passed in is a template", :class => 'is_template', :count => 1
102
+ assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template', :count => 1
103
+ end
104
+
105
+ it 'should work for slim templates' do
106
+ visit '/slim/capture_concat'
107
+ assert_have_selector 'p', :content => "The slim block passed in is a template", :class => 'is_template', :count => 1
108
+ assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template', :count => 1
109
+ end
110
+ end
111
+
112
+ describe 'for #current_engine method' do
113
+ it 'should detect correctly current engine for erb' do
114
+ visit '/erb/current_engine'
115
+ assert_have_selector 'p.start', :content => "erb"
116
+ assert_have_selector 'p.haml', :content => "haml"
117
+ assert_have_selector 'p.erb', :content => "erb"
118
+ assert_have_selector 'p.slim', :content => "slim"
119
+ assert_have_selector 'p.end', :content => "erb"
120
+ end
121
+
122
+ it 'should detect correctly current engine for haml' do
123
+ visit '/haml/current_engine'
124
+ assert_have_selector 'p.start', :content => "haml"
125
+ assert_have_selector 'p.haml', :content => "haml"
126
+ assert_have_selector 'p.erb', :content => "erb"
127
+ assert_have_selector 'p.slim', :content => "slim"
128
+ assert_have_selector 'p.end', :content => "haml"
129
+ end
130
+
131
+ it 'should detect correctly current engine for slim' do
132
+ visit '/slim/current_engine'
133
+ assert_have_selector 'p.start', :content => "slim"
134
+ assert_have_selector 'p.haml', :content => "haml"
135
+ assert_have_selector 'p.erb', :content => "erb"
136
+ assert_have_selector 'p.slim', :content => "slim"
137
+ assert_have_selector 'p.end', :content => "slim"
138
+ end
139
+ end
140
+
141
+ describe 'for #partial method in simple sinatra application' do
142
+ it 'should properly output in erb' do
143
+ visit '/erb/simple_partial'
144
+ assert_have_selector 'p.erb', :content => "erb"
145
+ end
146
+
147
+ it 'should properly output in haml' do
148
+ visit '/haml/simple_partial'
149
+ assert_have_selector 'p.haml', :content => "haml"
150
+ end
151
+
152
+ it 'should properly output in slim' do
153
+ visit '/slim/simple_partial'
154
+ assert_have_selector 'p.slim', :content => "slim"
155
+ end
156
+ end
157
+ end