padrino-helpers 0.16.0.pre4 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +29 -29
- data/Rakefile +1 -1
- data/lib/padrino/rendering/erb_template.rb +7 -7
- data/lib/padrino/rendering/erubi_template.rb +4 -4
- data/lib/padrino/rendering/erubis_template.rb +4 -4
- data/lib/padrino/rendering/haml_template.rb +1 -1
- data/lib/padrino/rendering/hamlit_template.rb +4 -4
- data/lib/padrino/rendering/slim_template.rb +5 -5
- data/lib/padrino/rendering.rb +54 -45
- data/lib/padrino/safe_buffer.rb +31 -36
- data/lib/padrino-helpers/asset_tag_helpers.rb +48 -48
- data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +74 -70
- data/lib/padrino-helpers/form_builder/standard_form_builder.rb +20 -20
- data/lib/padrino-helpers/form_helpers/errors.rb +14 -14
- data/lib/padrino-helpers/form_helpers/options.rb +25 -24
- data/lib/padrino-helpers/form_helpers/security.rb +5 -5
- data/lib/padrino-helpers/form_helpers.rb +133 -133
- data/lib/padrino-helpers/format_helpers.rb +61 -63
- data/lib/padrino-helpers/number_helpers.rb +82 -78
- data/lib/padrino-helpers/output_helpers/abstract_handler.rb +5 -3
- data/lib/padrino-helpers/output_helpers/erb_handler.rb +2 -2
- data/lib/padrino-helpers/output_helpers/haml_handler.rb +1 -1
- data/lib/padrino-helpers/output_helpers.rb +16 -17
- data/lib/padrino-helpers/render_helpers.rb +28 -27
- data/lib/padrino-helpers/tag_helpers.rb +62 -58
- data/lib/padrino-helpers/translation_helpers.rb +2 -2
- data/lib/padrino-helpers.rb +1 -2
- data/padrino-helpers.gemspec +17 -17
- data/test/fixtures/apps/render.rb +2 -2
- data/test/fixtures/markup_app/app.rb +9 -8
- data/test/fixtures/markup_app/views/button_to.erb +3 -3
- data/test/fixtures/markup_app/views/button_to.haml +2 -2
- data/test/fixtures/markup_app/views/button_to.slim +3 -3
- data/test/fixtures/markup_app/views/content_for.erb +4 -4
- data/test/fixtures/markup_app/views/content_for.haml +4 -4
- data/test/fixtures/markup_app/views/content_for.slim +2 -2
- data/test/fixtures/markup_app/views/content_tag.erb +3 -3
- data/test/fixtures/markup_app/views/content_tag.haml +3 -3
- data/test/fixtures/markup_app/views/content_tag.slim +3 -3
- data/test/fixtures/markup_app/views/fields_for.erb +2 -2
- data/test/fixtures/markup_app/views/fields_for.haml +2 -2
- data/test/fixtures/markup_app/views/fields_for.slim +2 -2
- data/test/fixtures/markup_app/views/form_for.erb +41 -41
- data/test/fixtures/markup_app/views/form_for.haml +41 -41
- data/test/fixtures/markup_app/views/form_for.slim +41 -42
- data/test/fixtures/markup_app/views/form_tag.erb +47 -47
- data/test/fixtures/markup_app/views/form_tag.haml +48 -48
- data/test/fixtures/markup_app/views/form_tag.slim +49 -49
- data/test/fixtures/markup_app/views/link_to.erb +2 -2
- data/test/fixtures/markup_app/views/link_to.haml +2 -2
- data/test/fixtures/markup_app/views/link_to.slim +2 -2
- data/test/fixtures/markup_app/views/meta_tag.erb +2 -2
- data/test/fixtures/markup_app/views/meta_tag.haml +2 -2
- data/test/fixtures/markup_app/views/meta_tag.slim +2 -2
- data/test/fixtures/markup_app/views/simple_partial.erb +1 -1
- data/test/fixtures/markup_app/views/simple_partial.haml +1 -1
- data/test/fixtures/markup_app/views/simple_partial.slim +1 -1
- data/test/fixtures/render_app/app.rb +13 -12
- data/test/fixtures/render_app/views/_unsafe.html.builder +1 -1
- data/test/fixtures/render_app/views/_unsafe_object.html.builder +1 -1
- data/test/fixtures/render_app/views/explicit_engine.haml +3 -3
- data/test/helper.rb +19 -19
- data/test/test_asset_tag_helpers.rb +133 -133
- data/test/test_form_builder.rb +629 -628
- data/test/test_form_helpers.rb +610 -610
- data/test/test_format_helpers.rb +66 -63
- data/test/test_helpers.rb +2 -2
- data/test/test_locale.rb +13 -11
- data/test/test_number_helpers.rb +65 -65
- data/test/test_output_helpers.rb +76 -76
- data/test/test_render_helpers.rb +101 -101
- data/test/test_rendering.rb +329 -326
- data/test/test_rendering_extensions.rb +4 -4
- data/test/test_tag_helpers.rb +55 -55
- metadata +6 -6
data/test/test_format_helpers.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
require_relative 'helper'
|
|
2
|
+
require_relative 'fixtures/markup_app/app'
|
|
3
3
|
|
|
4
|
-
describe
|
|
4
|
+
describe 'FormatHelpers' do
|
|
5
5
|
include Padrino::Helpers::FormatHelpers
|
|
6
6
|
|
|
7
7
|
def app
|
|
@@ -15,7 +15,7 @@ describe "FormatHelpers" do
|
|
|
15
15
|
describe 'for #simple_format method' do
|
|
16
16
|
it 'should format simple text into html format' do
|
|
17
17
|
actual_text = simple_format("Here is some basic text...\n...with a line break.")
|
|
18
|
-
|
|
18
|
+
assert actual_text.html_safe?
|
|
19
19
|
assert_equal "<p>Here is some basic text...\n<br />...with a line break.</p>", actual_text
|
|
20
20
|
end
|
|
21
21
|
|
|
@@ -25,13 +25,13 @@ describe "FormatHelpers" do
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it 'should support defining a class for the paragraphs' do
|
|
28
|
-
actual_text = simple_format(
|
|
29
|
-
assert_equal
|
|
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
30
|
end
|
|
31
31
|
|
|
32
32
|
it 'should escape html tags' do
|
|
33
|
-
actual_text = simple_format(
|
|
34
|
-
assert_equal
|
|
33
|
+
actual_text = simple_format('Will you escape <b>that</b>?')
|
|
34
|
+
assert_equal '<p>Will you escape <b>that</b>?</p>', actual_text
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
it 'should support already sanitized text' do
|
|
@@ -41,36 +41,36 @@ describe "FormatHelpers" do
|
|
|
41
41
|
|
|
42
42
|
describe 'wrapped in a custom tag' do
|
|
43
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.", :
|
|
44
|
+
actual_text = simple_format("Here is some basic text...\n...with a line break.", tag: :div)
|
|
45
45
|
assert_equal "<div>Here is some basic text...\n<br />...with a line break.</div>", actual_text
|
|
46
46
|
end
|
|
47
47
|
|
|
48
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.", :
|
|
49
|
+
actual_text = simple_format("We want to put a paragraph...\n\n...right there.", tag: :div)
|
|
50
50
|
assert_equal "<div>We want to put a paragraph...</div>\n\n<div>...right there.</div>", actual_text
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
it 'should support defining a class for the paragraphs' do
|
|
54
|
-
actual_text = simple_format(
|
|
55
|
-
assert_equal
|
|
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
56
|
end
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
describe 'for #word_wrap method' do
|
|
61
61
|
it 'should return proper formatting for 8 max width' do
|
|
62
|
-
actual_text = word_wrap('Once upon a time', :
|
|
62
|
+
actual_text = word_wrap('Once upon a time', line_width: 8)
|
|
63
63
|
assert_equal "Once\nupon a\ntime", actual_text
|
|
64
64
|
end
|
|
65
65
|
it 'should return proper formatting for 1 max width' do
|
|
66
|
-
actual_text = word_wrap('Once upon a time', :
|
|
66
|
+
actual_text = word_wrap('Once upon a time', line_width: 1)
|
|
67
67
|
assert_equal "Once\nupon\na\ntime", actual_text
|
|
68
68
|
end
|
|
69
69
|
it 'should return proper formatting for default width' do
|
|
70
|
-
actual_text = word_wrap((1..50).to_a.join(
|
|
71
|
-
assert_equal (1..30).to_a.join(
|
|
72
|
-
actual_text = word_wrap((1..50).to_a.join(
|
|
73
|
-
assert_equal (1..30).to_a.join(
|
|
70
|
+
actual_text = word_wrap((1..50).to_a.join(' '))
|
|
71
|
+
assert_equal "#{(1..30).to_a.join(' ')}\n#{(31..50).to_a.join(' ')}", actual_text
|
|
72
|
+
actual_text = word_wrap((1..50).to_a.join(' '), 80)
|
|
73
|
+
assert_equal "#{(1..30).to_a.join(' ')}\n#{(31..50).to_a.join(' ')}", actual_text
|
|
74
74
|
end
|
|
75
75
|
end
|
|
76
76
|
|
|
@@ -81,38 +81,41 @@ describe "FormatHelpers" do
|
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
it 'should highlight with highlighter' do
|
|
84
|
-
actual_text = highlight('Lorem ipsum dolor sit amet', 'dolor', :
|
|
84
|
+
actual_text = highlight('Lorem ipsum dolor sit amet', 'dolor', highlighter: '<span class="custom">\1</span>')
|
|
85
85
|
assert_equal 'Lorem ipsum <span class="custom">dolor</span> sit amet', actual_text
|
|
86
86
|
end
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
describe 'for #truncate method' do
|
|
90
90
|
it 'should support default truncation' do
|
|
91
|
-
actual_text = truncate(
|
|
92
|
-
assert_equal
|
|
91
|
+
actual_text = truncate('Once upon a time in a world far far away')
|
|
92
|
+
assert_equal 'Once upon a time in a world...', actual_text
|
|
93
93
|
end
|
|
94
94
|
it 'should support specifying length' do
|
|
95
|
-
actual_text = truncate(
|
|
96
|
-
assert_equal
|
|
95
|
+
actual_text = truncate('Once upon a time in a world far far away', length: 14)
|
|
96
|
+
assert_equal 'Once upon a...', actual_text
|
|
97
97
|
end
|
|
98
98
|
it 'should support specifying omission text' do
|
|
99
|
-
actual_text = truncate(
|
|
100
|
-
assert_equal
|
|
99
|
+
actual_text = truncate('And they found that many people were sleeping better.', length: 25, omission: '(clipped)')
|
|
100
|
+
assert_equal 'And they found t(clipped)', actual_text
|
|
101
101
|
end
|
|
102
102
|
end
|
|
103
103
|
|
|
104
104
|
describe 'for #truncate_words method' do
|
|
105
105
|
it 'should support default truncation' do
|
|
106
|
-
actual_text = truncate_words(
|
|
107
|
-
|
|
106
|
+
actual_text = truncate_words(<<~TEXT.chomp)
|
|
107
|
+
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,
|
|
108
|
+
but most of them were about the ways of fairies and giants.
|
|
109
|
+
TEXT
|
|
110
|
+
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
|
|
108
111
|
end
|
|
109
112
|
it 'should support specifying length' do
|
|
110
|
-
actual_text = truncate_words(
|
|
111
|
-
assert_equal
|
|
113
|
+
actual_text = truncate_words('Once upon a time in a world far far away', length: 8)
|
|
114
|
+
assert_equal 'Once upon a time in a world far...', actual_text
|
|
112
115
|
end
|
|
113
116
|
it 'should support specifying omission text' do
|
|
114
|
-
actual_text = truncate_words(
|
|
115
|
-
assert_equal
|
|
117
|
+
actual_text = truncate_words('And they found that many people were sleeping better.', length: 4, omission: '(clipped)')
|
|
118
|
+
assert_equal 'And they found that(clipped)', actual_text
|
|
116
119
|
end
|
|
117
120
|
end
|
|
118
121
|
|
|
@@ -125,107 +128,107 @@ describe "FormatHelpers" do
|
|
|
125
128
|
assert_equal '<h1><>"&demo&"<></h1>', h('<h1><>"&demo&"<></h1>')
|
|
126
129
|
end
|
|
127
130
|
it 'should return default text if text is empty' do
|
|
128
|
-
assert_equal 'default', h!(
|
|
129
|
-
assert_equal ' ', h!(
|
|
131
|
+
assert_equal 'default', h!('', 'default')
|
|
132
|
+
assert_equal ' ', h!('')
|
|
130
133
|
end
|
|
131
134
|
it 'should return text escaped if not empty' do
|
|
132
135
|
assert_equal '<h1>hello</h1>', h!('<h1>hello</h1>')
|
|
133
136
|
end
|
|
134
137
|
it 'should mark escaped text as safe' do
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
+
refute '<h1>hello</h1>'.html_safe?
|
|
139
|
+
assert h('<h1>hello</h1>').html_safe?
|
|
140
|
+
assert h!('', 'default').html_safe?
|
|
138
141
|
end
|
|
139
142
|
end
|
|
140
143
|
|
|
141
144
|
describe 'for #time_ago_in_words method' do
|
|
142
|
-
|
|
145
|
+
A_DAY = 24 * 60 * 60
|
|
143
146
|
|
|
144
147
|
it 'should less than 5 seconds' do
|
|
145
148
|
assert_equal 'less than 5 seconds', time_ago_in_words(Time.now, true)
|
|
146
149
|
end
|
|
147
150
|
it 'should less than 10 seconds' do
|
|
148
|
-
assert_equal 'less than 10 seconds', time_ago_in_words(Time.now-5, true)
|
|
151
|
+
assert_equal 'less than 10 seconds', time_ago_in_words(Time.now - 5, true)
|
|
149
152
|
end
|
|
150
153
|
it 'should less than 20 seconds' do
|
|
151
|
-
assert_equal 'less than 20 seconds', time_ago_in_words(Time.now-10, true)
|
|
154
|
+
assert_equal 'less than 20 seconds', time_ago_in_words(Time.now - 10, true)
|
|
152
155
|
end
|
|
153
156
|
it 'should less than a minute' do
|
|
154
|
-
assert_equal 'less than a minute', time_ago_in_words(Time.now-40, true)
|
|
157
|
+
assert_equal 'less than a minute', time_ago_in_words(Time.now - 40, true)
|
|
155
158
|
end
|
|
156
159
|
it 'should 2 minutes' do
|
|
157
|
-
assert_equal '2 minutes', time_ago_in_words(Time.now-120, true)
|
|
160
|
+
assert_equal '2 minutes', time_ago_in_words(Time.now - 120, true)
|
|
158
161
|
end
|
|
159
162
|
it 'should display today' do
|
|
160
163
|
assert_equal 'less than a minute', time_ago_in_words(Time.now)
|
|
161
164
|
end
|
|
162
165
|
it 'should display yesterday' do
|
|
163
|
-
assert_equal '1 day', time_ago_in_words(Time.now -
|
|
166
|
+
assert_equal '1 day', time_ago_in_words(Time.now - A_DAY)
|
|
164
167
|
end
|
|
165
168
|
it 'should display tomorrow' do
|
|
166
|
-
assert_equal '1 day', time_ago_in_words(Time.now +
|
|
169
|
+
assert_equal '1 day', time_ago_in_words(Time.now + A_DAY)
|
|
167
170
|
end
|
|
168
171
|
it 'should return future number of days' do
|
|
169
|
-
assert_equal '4 days', time_ago_in_words(Time.now + 4*
|
|
172
|
+
assert_equal '4 days', time_ago_in_words(Time.now + 4 * A_DAY)
|
|
170
173
|
end
|
|
171
174
|
it 'should return past days ago' do
|
|
172
|
-
assert_equal '4 days', time_ago_in_words(Time.now - 4*
|
|
175
|
+
assert_equal '4 days', time_ago_in_words(Time.now - 4 * A_DAY)
|
|
173
176
|
end
|
|
174
177
|
it 'should return formatted archived date' do
|
|
175
|
-
assert_equal '3 months', time_ago_in_words(Time.now - 100*
|
|
178
|
+
assert_equal '3 months', time_ago_in_words(Time.now - 100 * A_DAY)
|
|
176
179
|
end
|
|
177
180
|
it 'should return formatted archived year date' do
|
|
178
|
-
assert_equal 'over 1 year', time_ago_in_words(Time.now - 500*
|
|
181
|
+
assert_equal 'over 1 year', time_ago_in_words(Time.now - 500 * A_DAY)
|
|
179
182
|
end
|
|
180
183
|
it 'should display now as a minute ago' do
|
|
181
184
|
assert_equal '1 minute', time_ago_in_words(Time.now - 60)
|
|
182
185
|
end
|
|
183
186
|
it 'should display a few minutes ago' do
|
|
184
|
-
assert_equal '4 minutes', time_ago_in_words(Time.now - 4*60)
|
|
187
|
+
assert_equal '4 minutes', time_ago_in_words(Time.now - 4 * 60)
|
|
185
188
|
end
|
|
186
189
|
it 'should display an hour ago' do
|
|
187
|
-
assert_equal 'about 1 hour', time_ago_in_words(Time.now - 60*60 + 5)
|
|
190
|
+
assert_equal 'about 1 hour', time_ago_in_words(Time.now - 60 * 60 + 5)
|
|
188
191
|
end
|
|
189
192
|
it 'should display a few hours ago' do
|
|
190
|
-
assert_equal 'about 3 hours', time_ago_in_words(Time.now - 3*60*60 + 5*60)
|
|
193
|
+
assert_equal 'about 3 hours', time_ago_in_words(Time.now - 3 * 60 * 60 + 5 * 60)
|
|
191
194
|
end
|
|
192
195
|
it 'should display a few days ago' do
|
|
193
|
-
assert_equal '5 days', time_ago_in_words(Time.now - 5*
|
|
196
|
+
assert_equal '5 days', time_ago_in_words(Time.now - 5 * A_DAY - 5 * 60)
|
|
194
197
|
end
|
|
195
198
|
it 'should display a month ago' do
|
|
196
|
-
assert_equal 'about 1 month', time_ago_in_words(Time.now - 32*
|
|
199
|
+
assert_equal 'about 1 month', time_ago_in_words(Time.now - 32 * A_DAY + 5 * 60)
|
|
197
200
|
end
|
|
198
201
|
it 'should display a few months ago' do
|
|
199
|
-
assert_equal '6 months', time_ago_in_words(Time.now - 180*
|
|
202
|
+
assert_equal '6 months', time_ago_in_words(Time.now - 180 * A_DAY - 5 * 60)
|
|
200
203
|
end
|
|
201
204
|
it 'should display a year ago' do
|
|
202
|
-
assert_equal 'about 1 year', time_ago_in_words(Time.now - 365*
|
|
205
|
+
assert_equal 'about 1 year', time_ago_in_words(Time.now - 365 * A_DAY - 5 * 60)
|
|
203
206
|
end
|
|
204
207
|
it 'should display a few years ago' do
|
|
205
|
-
assert_equal 'over 7 years', time_ago_in_words(Time.now - 2800*
|
|
208
|
+
assert_equal 'over 7 years', time_ago_in_words(Time.now - 2800 * A_DAY - 5 * 60)
|
|
206
209
|
end
|
|
207
210
|
end
|
|
208
211
|
|
|
209
212
|
describe 'for #js_escape_html method' do
|
|
210
213
|
it 'should escape double quotes' do
|
|
211
|
-
assert_equal "
|
|
212
|
-
assert_equal "
|
|
214
|
+
assert_equal '\"hello\"', js_escape_html('"hello"')
|
|
215
|
+
assert_equal '\"hello\"', js_escape_html(SafeBuffer.new('"hello"'))
|
|
213
216
|
end
|
|
214
217
|
it 'should escape single quotes' do
|
|
215
218
|
assert_equal "\\'hello\\'", js_escape_html("'hello'")
|
|
216
219
|
assert_equal "\\'hello\\'", js_escape_html(SafeBuffer.new("'hello'"))
|
|
217
220
|
end
|
|
218
221
|
it 'should escape html tags and breaks' do
|
|
219
|
-
assert_equal
|
|
220
|
-
assert_equal
|
|
222
|
+
assert_equal '\\n\\n<p>hello<\\/p>\\n', js_escape_html("\n\r<p>hello</p>\r\n")
|
|
223
|
+
assert_equal '\\n\\n<p>hello<\\/p>\\n', js_escape_html(SafeBuffer.new("\n\r<p>hello</p>\r\n"))
|
|
221
224
|
end
|
|
222
225
|
it 'should escape data-confirm attribute' do
|
|
223
|
-
assert_equal
|
|
224
|
-
assert_equal
|
|
226
|
+
assert_equal '<data-confirm=\"are you sure\">', js_escape_html('<data-confirm="are you sure">')
|
|
227
|
+
assert_equal '<data-confirm=\"are you sure\">', js_escape_html(SafeBuffer.new('<data-confirm="are you sure">'))
|
|
225
228
|
end
|
|
226
229
|
it 'should keep html_safe content html_safe' do
|
|
227
|
-
|
|
228
|
-
|
|
230
|
+
refute js_escape_html('"hello"').html_safe?
|
|
231
|
+
assert js_escape_html(SafeBuffer.new('"hello"')).html_safe?
|
|
229
232
|
end
|
|
230
233
|
end
|
|
231
234
|
end
|
data/test/test_helpers.rb
CHANGED
data/test/test_locale.rb
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative 'helper'
|
|
2
2
|
|
|
3
|
-
describe
|
|
4
|
-
Dir[File.expand_path(
|
|
3
|
+
describe 'Locale Helpers' do
|
|
4
|
+
Dir[File.expand_path('../lib/padrino-helpers/locale/*.yml', __dir__)].each do |file|
|
|
5
5
|
base_original = YAML.load_file(file)
|
|
6
6
|
name = File.basename(file, '.yml')
|
|
7
|
+
|
|
7
8
|
it "should should have correct locale for #{name}" do
|
|
8
9
|
base = base_original[name]['number']['format']
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
refute_nil base['separator']
|
|
11
|
+
refute_nil base['delimiter']
|
|
12
|
+
refute_nil base['precision']
|
|
13
|
+
|
|
12
14
|
base = base_original[name]['number']['currency']['format']
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
refute_nil base['format']
|
|
16
|
+
refute_nil base['unit']
|
|
17
|
+
refute_nil base['separator']
|
|
18
|
+
refute_nil base['delimiter']
|
|
19
|
+
refute_nil base['precision']
|
|
18
20
|
end
|
|
19
21
|
end
|
|
20
22
|
end
|
data/test/test_number_helpers.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
require_relative 'helper'
|
|
2
|
+
require_relative 'fixtures/markup_app/app'
|
|
3
3
|
|
|
4
|
-
describe
|
|
4
|
+
describe 'NumberHelpers' do
|
|
5
5
|
include Padrino::Helpers::NumberHelpers
|
|
6
6
|
|
|
7
7
|
def app
|
|
@@ -26,70 +26,70 @@ describe "NumberHelpers" do
|
|
|
26
26
|
|
|
27
27
|
describe 'for number helpers functionality' do
|
|
28
28
|
it 'should display number_to_currency' do
|
|
29
|
-
assert_equal
|
|
30
|
-
assert_equal
|
|
31
|
-
assert_equal
|
|
32
|
-
assert_equal
|
|
33
|
-
assert_equal
|
|
34
|
-
assert_equal
|
|
35
|
-
assert_equal
|
|
36
|
-
assert_equal
|
|
29
|
+
assert_equal '$1,234,567,890.50', number_to_currency(1_234_567_890.50)
|
|
30
|
+
assert_equal '$1,234,567,890.51', number_to_currency(1_234_567_890.506)
|
|
31
|
+
assert_equal '$1,234,567,892', number_to_currency(1_234_567_891.60, { precision: 0 })
|
|
32
|
+
assert_equal '$1,234,567,890.5', number_to_currency(1_234_567_890.50, { precision: 1 })
|
|
33
|
+
assert_equal '£1234567890,50', number_to_currency(1_234_567_890.50, { unit: '£', separator: ',', delimiter: '' })
|
|
34
|
+
assert_equal '$1,234,567,890.50', number_to_currency('1234567890.50')
|
|
35
|
+
assert_equal '1,234,567,890.50 Kč', number_to_currency('1234567890.50', { unit: 'Kč', format: '%n %u' })
|
|
36
|
+
assert_equal '$x', number_to_currency('x')
|
|
37
37
|
|
|
38
38
|
assert_nil number_to_currency(nil)
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it 'should display number_to_percentage' do
|
|
42
|
-
assert_equal
|
|
43
|
-
assert_equal
|
|
44
|
-
assert_equal
|
|
45
|
-
assert_equal
|
|
46
|
-
assert_equal
|
|
47
|
-
assert_equal
|
|
48
|
-
assert_equal
|
|
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
49
|
|
|
50
50
|
assert_nil number_to_percentage(nil)
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
it 'should display number_with_delimiter' do
|
|
54
|
-
assert_equal
|
|
55
|
-
assert_equal
|
|
56
|
-
assert_equal
|
|
57
|
-
assert_equal
|
|
58
|
-
assert_equal
|
|
59
|
-
assert_equal
|
|
60
|
-
assert_equal
|
|
61
|
-
assert_equal
|
|
62
|
-
assert_equal
|
|
63
|
-
assert_equal
|
|
64
|
-
assert_equal
|
|
54
|
+
assert_equal '12,345,678', number_with_delimiter(12_345_678)
|
|
55
|
+
assert_equal '0', number_with_delimiter(0)
|
|
56
|
+
assert_equal '123', number_with_delimiter(123)
|
|
57
|
+
assert_equal '123,456', number_with_delimiter(123_456)
|
|
58
|
+
assert_equal '123,456.78', number_with_delimiter(123_456.78)
|
|
59
|
+
assert_equal '123,456.789', number_with_delimiter(123_456.789)
|
|
60
|
+
assert_equal '123,456.78901', number_with_delimiter(123_456.78901)
|
|
61
|
+
assert_equal '123,456,789.78901', number_with_delimiter(123_456_789.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
65
|
|
|
66
66
|
assert_nil number_with_delimiter(nil)
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it 'should display number_with_delimiter with options' do
|
|
70
|
-
assert_equal '12 345 678', number_with_delimiter(
|
|
71
|
-
assert_equal '12,345,678-05', number_with_delimiter(
|
|
72
|
-
assert_equal '12.345.678,05', number_with_delimiter(
|
|
73
|
-
assert_equal '12.345.678,05', number_with_delimiter(
|
|
70
|
+
assert_equal '12 345 678', number_with_delimiter(12_345_678, delimiter: ' ')
|
|
71
|
+
assert_equal '12,345,678-05', number_with_delimiter(12_345_678.05, separator: '-')
|
|
72
|
+
assert_equal '12.345.678,05', number_with_delimiter(12_345_678.05, separator: ',', delimiter: '.')
|
|
73
|
+
assert_equal '12.345.678,05', number_with_delimiter(12_345_678.05, delimiter: '.', separator: ',')
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
it 'should display number_with_precision' do
|
|
77
|
-
assert_equal
|
|
78
|
-
assert_equal
|
|
79
|
-
assert_equal
|
|
80
|
-
assert_equal
|
|
81
|
-
assert_equal
|
|
82
|
-
assert_equal
|
|
83
|
-
assert_equal
|
|
77
|
+
assert_equal '111.235', number_with_precision(111.2346)
|
|
78
|
+
assert_equal '111.23', number_with_precision(111.2346, precision: 2)
|
|
79
|
+
assert_equal '111.00', number_with_precision(111, precision: 2)
|
|
80
|
+
assert_equal '111.235', number_with_precision('111.2346')
|
|
81
|
+
assert_equal '3268', number_with_precision(32.6751 * 100.00, precision: 0)
|
|
82
|
+
assert_equal '112', number_with_precision(111.60, precision: 0)
|
|
83
|
+
assert_equal '1234567892', number_with_precision(1_234_567_891.60, precision: 0)
|
|
84
84
|
|
|
85
85
|
# Return non-numeric params unchanged.
|
|
86
|
-
assert_equal
|
|
86
|
+
assert_equal 'x', number_with_precision('x')
|
|
87
87
|
assert_nil number_with_precision(nil)
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
it 'should display number_with_precision with custom delimiter and separator' do
|
|
91
|
-
assert_equal '31,83', number_with_precision(31.826, :
|
|
92
|
-
assert_equal '1.231,83', number_with_precision(1231.826, :
|
|
91
|
+
assert_equal '31,83', number_with_precision(31.826, precision: 2, separator: ',')
|
|
92
|
+
assert_equal '1.231,83', number_with_precision(1231.826, precision: 2, separator: ',', delimiter: '.')
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
it 'should display number_to_human_size' do
|
|
@@ -99,20 +99,20 @@ describe "NumberHelpers" do
|
|
|
99
99
|
assert_equal '123 Bytes', number_to_human_size(123.0)
|
|
100
100
|
assert_equal '123 Bytes', number_to_human_size(123)
|
|
101
101
|
assert_equal '1.2 KB', number_to_human_size(1234)
|
|
102
|
-
assert_equal '12.1 KB', number_to_human_size(
|
|
103
|
-
assert_equal '1.2 MB', number_to_human_size(
|
|
104
|
-
assert_equal '1.1 GB', number_to_human_size(
|
|
105
|
-
assert_equal '1.1 TB', number_to_human_size(
|
|
102
|
+
assert_equal '12.1 KB', number_to_human_size(12_345)
|
|
103
|
+
assert_equal '1.2 MB', number_to_human_size(1_234_567)
|
|
104
|
+
assert_equal '1.1 GB', number_to_human_size(1_234_567_890)
|
|
105
|
+
assert_equal '1.1 TB', number_to_human_size(1_234_567_890_123)
|
|
106
106
|
assert_equal '1025 TB', number_to_human_size(terabytes(1025))
|
|
107
107
|
assert_equal '444 KB', number_to_human_size(kilobytes(444))
|
|
108
108
|
assert_equal '1023 MB', number_to_human_size(megabytes(1023))
|
|
109
109
|
assert_equal '3 TB', number_to_human_size(terabytes(3))
|
|
110
|
-
assert_equal '1.18 MB', number_to_human_size(
|
|
111
|
-
assert_equal '3 Bytes', number_to_human_size(3.14159265, :
|
|
112
|
-
assert_equal '123 Bytes', number_to_human_size(
|
|
113
|
-
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :
|
|
114
|
-
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :
|
|
115
|
-
assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :
|
|
110
|
+
assert_equal '1.18 MB', number_to_human_size(1_234_567, precision: 2)
|
|
111
|
+
assert_equal '3 Bytes', number_to_human_size(3.14159265, precision: 4)
|
|
112
|
+
assert_equal '123 Bytes', number_to_human_size('123')
|
|
113
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), precision: 2)
|
|
114
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), precision: 4)
|
|
115
|
+
assert_equal '10 KB', number_to_human_size(kilobytes(10.000), precision: 4)
|
|
116
116
|
assert_equal '1 Byte', number_to_human_size(1.1)
|
|
117
117
|
assert_equal '10 Bytes', number_to_human_size(10)
|
|
118
118
|
|
|
@@ -120,21 +120,21 @@ describe "NumberHelpers" do
|
|
|
120
120
|
end
|
|
121
121
|
|
|
122
122
|
it 'should display number_to_human_size with options' do
|
|
123
|
-
assert_equal '1.18 MB', number_to_human_size(
|
|
124
|
-
assert_equal '3 Bytes', number_to_human_size(3.14159265, :
|
|
125
|
-
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), :
|
|
126
|
-
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), :
|
|
127
|
-
assert_equal '10 KB', number_to_human_size(kilobytes(10.000), :
|
|
128
|
-
assert_equal '1 TB', number_to_human_size(
|
|
129
|
-
assert_equal '500 MB', number_to_human_size(
|
|
130
|
-
assert_equal '40 KB', number_to_human_size(
|
|
131
|
-
assert_equal '40 KB', number_to_human_size(
|
|
123
|
+
assert_equal '1.18 MB', number_to_human_size(1_234_567, precision: 2)
|
|
124
|
+
assert_equal '3 Bytes', number_to_human_size(3.14159265, precision: 4)
|
|
125
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0123), precision: 2)
|
|
126
|
+
assert_equal '1.01 KB', number_to_human_size(kilobytes(1.0100), precision: 4)
|
|
127
|
+
assert_equal '10 KB', number_to_human_size(kilobytes(10.000), precision: 4)
|
|
128
|
+
assert_equal '1 TB', number_to_human_size(1_234_567_890_123, precision: 0)
|
|
129
|
+
assert_equal '500 MB', number_to_human_size(524_288_000, precision: 0)
|
|
130
|
+
assert_equal '40 KB', number_to_human_size(41_010, precision: 0)
|
|
131
|
+
assert_equal '40 KB', number_to_human_size(41_100, precision: 0)
|
|
132
132
|
end
|
|
133
133
|
|
|
134
134
|
it 'should display number_to_human_size with custom delimiter and separator' do
|
|
135
|
-
assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0123), :
|
|
136
|
-
assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0100), :
|
|
137
|
-
assert_equal '1.000,1 TB', number_to_human_size(terabytes(1000.1), :
|
|
135
|
+
assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0123), precision: 2, separator: ',')
|
|
136
|
+
assert_equal '1,01 KB', number_to_human_size(kilobytes(1.0100), precision: 4, separator: ',')
|
|
137
|
+
assert_equal '1.000,1 TB', number_to_human_size(terabytes(1000.1), delimiter: '.', separator: ',')
|
|
138
138
|
end
|
|
139
139
|
end
|
|
140
140
|
end
|