normalizy 1.4.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -24
  3. data/README.md +33 -16
  4. data/lib/normalizy/config.rb +2 -1
  5. data/lib/normalizy/filters/date.rb +2 -4
  6. data/lib/normalizy/filters/money.rb +2 -2
  7. data/lib/normalizy/filters/percent.rb +2 -2
  8. data/lib/normalizy/filters/slug.rb +1 -1
  9. data/lib/normalizy/filters/strip.rb +1 -1
  10. data/lib/normalizy/filters/truncate.rb +16 -0
  11. data/lib/normalizy/version.rb +1 -1
  12. metadata +31 -96
  13. data/spec/normalizy/config/add_spec.rb +0 -21
  14. data/spec/normalizy/config/alias_spec.rb +0 -29
  15. data/spec/normalizy/config/default_filters_spec.rb +0 -19
  16. data/spec/normalizy/config/initialize_spec.rb +0 -16
  17. data/spec/normalizy/config/normalizy_aliases_spec.rb +0 -9
  18. data/spec/normalizy/extensions/filters/date_spec.rb +0 -23
  19. data/spec/normalizy/extensions/filters/money_spec.rb +0 -38
  20. data/spec/normalizy/extensions/filters/number_spec.rb +0 -9
  21. data/spec/normalizy/extensions/filters/percent_spec.rb +0 -30
  22. data/spec/normalizy/extensions/filters/slug_spec.rb +0 -14
  23. data/spec/normalizy/extensions/filters/strip_spec.rb +0 -21
  24. data/spec/normalizy/extensions/model_spec.rb +0 -107
  25. data/spec/normalizy/extensions/normalizy_rules_spec.rb +0 -198
  26. data/spec/normalizy/filters/date_spec.rb +0 -69
  27. data/spec/normalizy/filters/money_spec.rb +0 -282
  28. data/spec/normalizy/filters/number_spec.rb +0 -33
  29. data/spec/normalizy/filters/percent_spec.rb +0 -168
  30. data/spec/normalizy/filters/slug_spec.rb +0 -24
  31. data/spec/normalizy/filters/strip_spec.rb +0 -13
  32. data/spec/normalizy/normalizy/configure_spec.rb +0 -11
  33. data/spec/normalizy/rspec/matcher/description_spec.rb +0 -27
  34. data/spec/normalizy/rspec/matcher/failure_message_spec.rb +0 -48
  35. data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +0 -30
  36. data/spec/normalizy/rspec/matcher/from_spec.rb +0 -19
  37. data/spec/normalizy/rspec/matcher/matches_spec.rb +0 -117
  38. data/spec/normalizy/rspec/matcher/to_spec.rb +0 -19
  39. data/spec/rails_helper.rb +0 -9
  40. data/spec/support/common.rb +0 -11
  41. data/spec/support/db/schema.rb +0 -83
  42. data/spec/support/filters/blacklist.rb +0 -11
  43. data/spec/support/filters/block.rb +0 -11
  44. data/spec/support/filters/info.rb +0 -11
  45. data/spec/support/models/alias.rb +0 -7
  46. data/spec/support/models/match.rb +0 -9
  47. data/spec/support/models/model.rb +0 -28
  48. data/spec/support/models/model_date.rb +0 -9
  49. data/spec/support/models/model_money.rb +0 -10
  50. data/spec/support/models/model_number.rb +0 -5
  51. data/spec/support/models/model_percent.rb +0 -10
  52. data/spec/support/models/model_slug.rb +0 -6
  53. data/spec/support/models/model_strip.rb +0 -8
  54. data/spec/support/models/rule.rb +0 -4
@@ -1,282 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Filters::Money do
6
- describe 'default options' do
7
- it { expect(subject.call('')).to eq nil }
8
-
9
- it { expect(subject.call(1)).to be 1 }
10
- it { expect(subject.call(1.70)).to be 1.70 }
11
- it { expect(subject.call(103.70)).to be 103.70 }
12
- it { expect(subject.call(1030.70)).to be 1030.70 }
13
- it { expect(subject.call(10_300.70)).to be 10_300.70 }
14
-
15
- it { expect(subject.call(-1)).to be -1 }
16
- it { expect(subject.call(-1.70)).to be -1.70 }
17
- it { expect(subject.call(-103.70)).to be -103.70 }
18
- it { expect(subject.call(-1030.70)).to be -1030.70 }
19
- it { expect(subject.call(-10_300.70)).to be -10_300.70 }
20
-
21
- it { expect(subject.call('0')).to eq '0.00' }
22
- it { expect(subject.call('100')).to eq '100.00' }
23
- it { expect(subject.call('100.0')).to eq '100.00' }
24
- it { expect(subject.call('1.70')).to eq '1.70' }
25
- it { expect(subject.call('103.70')).to eq '103.70' }
26
- it { expect(subject.call('1030.70')).to eq '1030.70' }
27
- it { expect(subject.call('10300.70')).to eq '10300.70' }
28
-
29
- it { expect(subject.call('-0')).to eq '-0.00' }
30
- it { expect(subject.call('-100')).to eq '-100.00' }
31
- it { expect(subject.call('-100.0')).to eq '-100.00' }
32
- it { expect(subject.call('-1.70')).to eq '-1.70' }
33
- it { expect(subject.call('-103.70')).to eq '-103.70' }
34
- it { expect(subject.call('-1030.70')).to eq '-1030.70' }
35
- it { expect(subject.call('-10300.70')).to eq '-10300.70' }
36
-
37
- it { expect(subject.call('$ 0.01')).to eq '0.01' }
38
- it { expect(subject.call('$ 1')).to eq '1.00' }
39
- it { expect(subject.call('$ 1.70')).to eq '1.70' }
40
- it { expect(subject.call('$ 103.70')).to eq '103.70' }
41
- it { expect(subject.call('$ 1030.70')).to eq '1030.70' }
42
- it { expect(subject.call('$ 10300.70')).to eq '10300.70' }
43
-
44
- it { expect(subject.call('$ -0.01')).to eq '-0.01' }
45
- it { expect(subject.call('$ -1')).to eq '-1.00' }
46
- it { expect(subject.call('$ -1.70')).to eq '-1.70' }
47
- it { expect(subject.call('$ -103.70')).to eq '-103.70' }
48
- it { expect(subject.call('$ -1030.70')).to eq '-1030.70' }
49
- it { expect(subject.call('$ -10300.70')).to eq '-10300.70' }
50
- end
51
-
52
- describe 'type' do
53
- it { expect(subject.call('', type: :cents)).to eq nil }
54
-
55
- it { expect(subject.call(1 , type: :cents)).to be 1 }
56
- it { expect(subject.call(1.70 , type: :cents)).to be 1.70 }
57
- it { expect(subject.call(103.70 , type: :cents)).to be 103.70 }
58
- it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
59
- it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
60
-
61
- it { expect(subject.call(-1 , type: :cents)).to be -1 }
62
- it { expect(subject.call(-1.70 , type: :cents)).to be -1.70 }
63
- it { expect(subject.call(-103.70 , type: :cents)).to be -103.70 }
64
- it { expect(subject.call(-1030.70 , type: :cents)).to be -1030.70 }
65
- it { expect(subject.call(-10_300.70, type: :cents)).to be -10_300.70 }
66
-
67
- context 'with different separator' do
68
- it { expect(subject.call('1,7', separator: ',', type: :cents)).to eq '170' }
69
- it { expect(subject.call('-1,7', separator: ',', type: :cents)).to eq '-170' }
70
- end
71
-
72
- context 'with no decimal precision' do
73
- it { expect(subject.call('1' , type: :cents)).to eq '1' }
74
- it { expect(subject.call('10' , type: :cents)).to eq '10' }
75
- it { expect(subject.call('100', type: :cents)).to eq '100' }
76
-
77
- it { expect(subject.call('-1' , type: :cents)).to eq '-1' }
78
- it { expect(subject.call('-10' , type: :cents)).to eq '-10' }
79
- it { expect(subject.call('-100', type: :cents)).to eq '-100' }
80
-
81
- context 'with float :cast' do
82
- it { expect(subject.call('1' , cast: :to_f, type: :cents)).to eq 1.0 }
83
- it { expect(subject.call('10' , cast: :to_f, type: :cents)).to eq 10.0 }
84
- it { expect(subject.call('100', cast: :to_f, type: :cents)).to eq 100.0 }
85
-
86
- it { expect(subject.call('-1' , cast: :to_f, type: :cents)).to eq -1.0 }
87
- it { expect(subject.call('-10' , cast: :to_f, type: :cents)).to eq -10.0 }
88
- it { expect(subject.call('-100', cast: :to_f, type: :cents)).to eq -100.0 }
89
- end
90
- end
91
-
92
- context 'with one decimal precision' do
93
- it { expect(subject.call('1.7' , type: :cents)).to eq '170' }
94
- it { expect(subject.call('103.7' , type: :cents)).to eq '10370' }
95
- it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
96
- it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
97
-
98
- it { expect(subject.call('-1.7' , type: :cents)).to eq '-170' }
99
- it { expect(subject.call('-103.7' , type: :cents)).to eq '-10370' }
100
- it { expect(subject.call('-1030.7' , type: :cents)).to eq '-103070' }
101
- it { expect(subject.call('-10300.7', type: :cents)).to eq '-1030070' }
102
-
103
- it { expect(subject.call('$ 1.7' , type: :cents)).to eq '170' }
104
- it { expect(subject.call('$ 103.7' , type: :cents)).to eq '10370' }
105
- it { expect(subject.call('$ 1030.7' , type: :cents)).to eq '103070' }
106
- it { expect(subject.call('$ 10300.7', type: :cents)).to eq '1030070' }
107
-
108
- it { expect(subject.call('$ -1.7' , type: :cents)).to eq '-170' }
109
- it { expect(subject.call('$ -103.7' , type: :cents)).to eq '-10370' }
110
- it { expect(subject.call('$ -1030.7' , type: :cents)).to eq '-103070' }
111
- it { expect(subject.call('$ -10300.7', type: :cents)).to eq '-1030070' }
112
-
113
- context 'with float :cast' do
114
- it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
115
- it { expect(subject.call('103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
116
- it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
117
- it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
118
-
119
- it { expect(subject.call('-1.7' , cast: :to_f, type: :cents)).to be -170.0 }
120
- it { expect(subject.call('-103.7' , cast: :to_f, type: :cents)).to be -10_370.0 }
121
- it { expect(subject.call('-1030.7' , cast: :to_f, type: :cents)).to be -103_070.0 }
122
- it { expect(subject.call('-10300.7', cast: :to_f, type: :cents)).to be -1_030_070.0 }
123
-
124
- it { expect(subject.call('$ 1.7' , cast: :to_f, type: :cents)).to be 170.0 }
125
- it { expect(subject.call('$ 103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
126
- it { expect(subject.call('$ 1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
127
- it { expect(subject.call('$ 10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
128
-
129
- it { expect(subject.call('$ -1.7' , cast: :to_f, type: :cents)).to be -170.0 }
130
- it { expect(subject.call('$ -103.7' , cast: :to_f, type: :cents)).to be -10_370.0 }
131
- it { expect(subject.call('$ -1030.7' , cast: :to_f, type: :cents)).to be -103_070.0 }
132
- it { expect(subject.call('$ -10300.7', cast: :to_f, type: :cents)).to be -1_030_070.0 }
133
- end
134
- end
135
-
136
- context 'with two decimal precision' do
137
- it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
138
- it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
139
- it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
140
- it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
141
-
142
- it { expect(subject.call('-1.70' , type: :cents)).to eq '-170' }
143
- it { expect(subject.call('-103.70' , type: :cents)).to eq '-10370' }
144
- it { expect(subject.call('-1030.70' , type: :cents)).to eq '-103070' }
145
- it { expect(subject.call('-10300.70', type: :cents)).to eq '-1030070' }
146
-
147
- it { expect(subject.call('$ 1.70' , type: :cents)).to eq '170' }
148
- it { expect(subject.call('$ 103.70' , type: :cents)).to eq '10370' }
149
- it { expect(subject.call('$ 1030.70' , type: :cents)).to eq '103070' }
150
- it { expect(subject.call('$ 10300.70', type: :cents)).to eq '1030070' }
151
-
152
- it { expect(subject.call('$ -1.70' , type: :cents)).to eq '-170' }
153
- it { expect(subject.call('$ -103.70' , type: :cents)).to eq '-10370' }
154
- it { expect(subject.call('$ -1030.70' , type: :cents)).to eq '-103070' }
155
- it { expect(subject.call('$ -10300.70', type: :cents)).to eq '-1030070' }
156
-
157
- context 'with float :cast' do
158
- it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
159
- it { expect(subject.call('103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
160
- it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
161
- it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
162
-
163
- it { expect(subject.call('-1.70' , cast: :to_f, type: :cents)).to be -170.0 }
164
- it { expect(subject.call('-103.70' , cast: :to_f, type: :cents)).to be -10_370.0 }
165
- it { expect(subject.call('-1030.70' , cast: :to_f, type: :cents)).to be -103_070.0 }
166
- it { expect(subject.call('-10300.70', cast: :to_f, type: :cents)).to be -1_030_070.0 }
167
-
168
- it { expect(subject.call('$ 1.70' , cast: :to_f, type: :cents)).to be 170.0 }
169
- it { expect(subject.call('$ 103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
170
- it { expect(subject.call('$ 1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
171
- it { expect(subject.call('$ 10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
172
-
173
- it { expect(subject.call('$ -1.70' , cast: :to_f, type: :cents)).to be -170.0 }
174
- it { expect(subject.call('$ -103.70' , cast: :to_f, type: :cents)).to be -10_370.0 }
175
- it { expect(subject.call('$ -1030.70' , cast: :to_f, type: :cents)).to be -103_070.0 }
176
- it { expect(subject.call('$ -10300.70', cast: :to_f, type: :cents)).to be -1_030_070.0 }
177
- end
178
- end
179
- end
180
-
181
- describe 'separator' do
182
- context 'when provided inline' do
183
- it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
184
- it { expect(subject.call('R$ -0,01', separator: ',')).to eq '-0.01' }
185
- end
186
-
187
- context 'when provided I18n' do
188
- before do
189
- allow(I18n).to receive(:t).with('currency.format.separator', default: '.').and_return 'x'
190
- allow(I18n).to receive(:t).with('currency.format.precision', default: 2).and_return 2
191
- end
192
-
193
- it { expect(subject.call('1x2')).to eq '1.20' }
194
- it { expect(subject.call('-1x2')).to eq '-1.20' }
195
- end
196
-
197
- context 'when not provided' do
198
- it { expect(subject.call('1.2')).to eq '1.20' }
199
- it { expect(subject.call('-1.2')).to eq '-1.20' }
200
- end
201
- end
202
-
203
- describe 'cast' do
204
- context 'with no decimal precision' do
205
- it { expect(subject.call('1' , cast: :to_f)).to eq 1.0 }
206
- it { expect(subject.call('10' , cast: :to_f)).to eq 10.0 }
207
- it { expect(subject.call('100', cast: :to_f)).to eq 100.0 }
208
-
209
- it { expect(subject.call('-1' , cast: :to_f)).to eq -1.0 }
210
- it { expect(subject.call('-10' , cast: :to_f)).to eq -10.0 }
211
- it { expect(subject.call('-100', cast: :to_f)).to eq -100.0 }
212
- end
213
-
214
- context 'when value has one decimal precision' do
215
- it { expect(subject.call('1.7' , cast: :to_f)).to eq 1.7 }
216
- it { expect(subject.call('103.7' , cast: :to_f)).to eq 103.7 }
217
- it { expect(subject.call('1030.7' , cast: :to_f)).to eq 1030.7 }
218
- it { expect(subject.call('10300.7', cast: :to_f)).to eq 10_300.7 }
219
-
220
- it { expect(subject.call('-1.7' , cast: :to_f)).to eq -1.7 }
221
- it { expect(subject.call('-103.7' , cast: :to_f)).to eq -103.7 }
222
- it { expect(subject.call('-1030.7' , cast: :to_f)).to eq -1030.7 }
223
- it { expect(subject.call('-10300.7', cast: :to_f)).to eq -10_300.7 }
224
-
225
- it { expect(subject.call('$ 1.7' , cast: :to_f)).to eq 1.7 }
226
- it { expect(subject.call('$ 103.7' , cast: :to_f)).to eq 103.7 }
227
- it { expect(subject.call('$ 1030.7' , cast: :to_f)).to eq 1030.7 }
228
- it { expect(subject.call('$ 10300.7', cast: :to_f)).to eq 10_300.7 }
229
-
230
- it { expect(subject.call('$ -1.7' , cast: :to_f)).to eq -1.7 }
231
- it { expect(subject.call('$ -103.7' , cast: :to_f)).to eq -103.7 }
232
- it { expect(subject.call('$ -1030.7' , cast: :to_f)).to eq -1030.7 }
233
- it { expect(subject.call('$ -10300.7', cast: :to_f)).to eq -10_300.7 }
234
-
235
- context 'with calls cast' do
236
- it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
237
- it { expect(subject.call('103.7' , cast: :to_f)).to be 103.7 }
238
- it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
239
- it { expect(subject.call('10300.7', cast: :to_f)).to be 10_300.7 }
240
-
241
- it { expect(subject.call('-1.7' , cast: :to_f)).to be -1.7 }
242
- it { expect(subject.call('-103.7' , cast: :to_f)).to be -103.7 }
243
- it { expect(subject.call('-1030.7' , cast: :to_f)).to be -1030.7 }
244
- it { expect(subject.call('-10300.7', cast: :to_f)).to be -10_300.7 }
245
-
246
- it { expect(subject.call('$ 1.7' , cast: :to_f)).to be 1.7 }
247
- it { expect(subject.call('$ 103.7' , cast: :to_f)).to be 103.7 }
248
- it { expect(subject.call('$ 1030.7' , cast: :to_f)).to be 1030.7 }
249
- it { expect(subject.call('$ 10300.7', cast: :to_f)).to be 10_300.7 }
250
-
251
- it { expect(subject.call('$ -1.7' , cast: :to_f)).to be -1.7 }
252
- it { expect(subject.call('$ -103.7' , cast: :to_f)).to be -103.7 }
253
- it { expect(subject.call('$ -1030.7' , cast: :to_f)).to be -1030.7 }
254
- it { expect(subject.call('$ -10300.7', cast: :to_f)).to be -10_300.7 }
255
- end
256
- end
257
-
258
- context 'when value has two decimal precision' do
259
- it { expect(subject.call('1.70' , cast: :to_f)).to be 1.7 }
260
- it { expect(subject.call('103.70' , cast: :to_f)).to be 103.7 }
261
- it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
262
- it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
263
-
264
- it { expect(subject.call('-1.70' , cast: :to_f)).to be -1.7 }
265
- it { expect(subject.call('-103.70' , cast: :to_f)).to be -103.7 }
266
- it { expect(subject.call('-1030.70' , cast: :to_f)).to be -1030.7 }
267
- it { expect(subject.call('-10300.70', cast: :to_f)).to be -10_300.7 }
268
-
269
- it { expect(subject.call('$ 1' , cast: :to_f)).to be 1.0 }
270
- it { expect(subject.call('$ 1.70' , cast: :to_f)).to be 1.7 }
271
- it { expect(subject.call('$ 103.70' , cast: :to_f)).to be 103.7 }
272
- it { expect(subject.call('$ 1030.70' , cast: :to_f)).to be 1030.7 }
273
- it { expect(subject.call('$ 10300.70', cast: :to_f)).to be 10_300.7 }
274
-
275
- it { expect(subject.call('$ -1' , cast: :to_f)).to be -1.0 }
276
- it { expect(subject.call('$ -1.70' , cast: :to_f)).to be -1.7 }
277
- it { expect(subject.call('$ -103.70' , cast: :to_f)).to be -103.7 }
278
- it { expect(subject.call('$ -1030.70' , cast: :to_f)).to be -1030.7 }
279
- it { expect(subject.call('$ -10300.70', cast: :to_f)).to be -10_300.7 }
280
- end
281
- end
282
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Filters::Number do
6
- context 'with no cast' do
7
- it { expect(subject.call('abcdefghijklmnopkrstuvxyz')).to eq nil }
8
- it { expect(subject.call('ABCDEFGHIJKLMNOPKRSTUVXYZ')).to eq nil }
9
- it { expect(subject.call('áéíóúàâêôãõ')).to eq nil }
10
- it { expect(subject.call('0123456789')).to eq '0123456789' }
11
- it { expect(subject.call('012345x6789')).to eq '0123456789' }
12
- it { expect(subject.call("\n")).to eq nil }
13
- it { expect(subject.call(42)).to eq 42 }
14
- it { expect(subject.call(nil)).to eq nil }
15
- it { expect(subject.call('nil')).to eq nil }
16
- it { expect(subject.call('')).to eq nil }
17
- it { expect(subject.call('1 2 3')).to eq '123' }
18
- end
19
-
20
- context 'with :cast' do
21
- it { expect(subject.call('abcdefghijklmnopkrstuvxyz', cast: :to_i)).to eq nil }
22
- it { expect(subject.call('ABCDEFGHIJKLMNOPKRSTUVXYZ', cast: :to_i)).to eq nil }
23
- it { expect(subject.call('áéíóúàâêôãõ', cast: :to_i)).to eq nil }
24
- it { expect(subject.call('0123456789', cast: :to_i)).to eq 123_456_789 }
25
- it { expect(subject.call('012345x6789', cast: :to_i)).to eq 123_456_789 }
26
- it { expect(subject.call("\n", cast: :to_i)).to eq nil }
27
- it { expect(subject.call(42, cast: :to_i)).to eq 42 }
28
- it { expect(subject.call(nil, cast: :to_i)).to eq nil }
29
- it { expect(subject.call('nil', cast: :to_i)).to eq nil }
30
- it { expect(subject.call('', cast: :to_i)).to eq nil }
31
- it { expect(subject.call('1 2 3', cast: :to_i)).to eq 123 }
32
- end
33
- end
@@ -1,168 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Filters::Percent do
6
- describe 'default options' do
7
- it { expect(subject.call('')).to eq nil }
8
-
9
- it { expect(subject.call(1)).to be 1 }
10
- it { expect(subject.call(1.70)).to be 1.70 }
11
- it { expect(subject.call(103.70)).to be 103.70 }
12
- it { expect(subject.call(1030.70)).to be 1030.70 }
13
- it { expect(subject.call(10_300.70)).to be 10_300.70 }
14
-
15
- it { expect(subject.call('0')).to eq '0.00' }
16
- it { expect(subject.call('100')).to eq '100.00' }
17
- it { expect(subject.call('100.0')).to eq '100.00' }
18
- it { expect(subject.call('1.70')).to eq '1.70' }
19
- it { expect(subject.call('103.70')).to eq '103.70' }
20
- it { expect(subject.call('1030.70')).to eq '1030.70' }
21
- it { expect(subject.call('10300.70')).to eq '10300.70' }
22
-
23
- it { expect(subject.call('$ 0.01')).to eq '0.01' }
24
- it { expect(subject.call('$ 1')).to eq '1.00' }
25
- it { expect(subject.call('$ 1.70')).to eq '1.70' }
26
- it { expect(subject.call('$ 103.70')).to eq '103.70' }
27
- it { expect(subject.call('$ 1030.70')).to eq '1030.70' }
28
- it { expect(subject.call('$ 10300.70')).to eq '10300.70' }
29
- end
30
-
31
- describe 'type' do
32
- it { expect(subject.call('', type: :cents)).to eq nil }
33
-
34
- it { expect(subject.call(1 , type: :cents)).to be 1 }
35
- it { expect(subject.call(1.70 , type: :cents)).to be 1.70 }
36
- it { expect(subject.call(103.70 , type: :cents)).to be 103.70 }
37
- it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
38
- it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
39
-
40
- context 'with different separator' do
41
- it { expect(subject.call('1,7', separator: ',', type: :cents)).to eq '170' }
42
- end
43
-
44
- context 'with no decimal precision' do
45
- it { expect(subject.call('1' , type: :cents)).to eq '1' }
46
- it { expect(subject.call('10' , type: :cents)).to eq '10' }
47
- it { expect(subject.call('100', type: :cents)).to eq '100' }
48
-
49
- context 'with float :cast' do
50
- it { expect(subject.call('1' , cast: :to_f, type: :cents)).to eq 1.0 }
51
- it { expect(subject.call('10' , cast: :to_f, type: :cents)).to eq 10.0 }
52
- it { expect(subject.call('100', cast: :to_f, type: :cents)).to eq 100.0 }
53
- end
54
- end
55
-
56
- context 'with one decimal precision' do
57
- it { expect(subject.call('1.7' , type: :cents)).to eq '170' }
58
- it { expect(subject.call('103.7' , type: :cents)).to eq '10370' }
59
- it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
60
- it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
61
-
62
- it { expect(subject.call('$ 1.7' , type: :cents)).to eq '170' }
63
- it { expect(subject.call('$ 103.7' , type: :cents)).to eq '10370' }
64
- it { expect(subject.call('$ 1030.7' , type: :cents)).to eq '103070' }
65
- it { expect(subject.call('$ 10300.7', type: :cents)).to eq '1030070' }
66
-
67
- context 'with float :cast' do
68
- it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
69
- it { expect(subject.call('103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
70
- it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
71
- it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
72
-
73
- it { expect(subject.call('$ 1.7' , cast: :to_f, type: :cents)).to be 170.0 }
74
- it { expect(subject.call('$ 103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
75
- it { expect(subject.call('$ 1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
76
- it { expect(subject.call('$ 10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
77
- end
78
- end
79
-
80
- context 'with two decimal precision' do
81
- it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
82
- it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
83
- it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
84
- it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
85
-
86
- it { expect(subject.call('$ 1.70' , type: :cents)).to eq '170' }
87
- it { expect(subject.call('$ 103.70' , type: :cents)).to eq '10370' }
88
- it { expect(subject.call('$ 1030.70' , type: :cents)).to eq '103070' }
89
- it { expect(subject.call('$ 10300.70', type: :cents)).to eq '1030070' }
90
-
91
- context 'with float :cast' do
92
- it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
93
- it { expect(subject.call('103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
94
- it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
95
- it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
96
-
97
- it { expect(subject.call('$ 1.70' , cast: :to_f, type: :cents)).to be 170.0 }
98
- it { expect(subject.call('$ 103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
99
- it { expect(subject.call('$ 1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
100
- it { expect(subject.call('$ 10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
101
- end
102
- end
103
- end
104
-
105
- describe 'separator' do
106
- context 'when provided inline' do
107
- it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
108
- end
109
-
110
- context 'when provided I18n' do
111
- before do
112
- allow(I18n).to receive(:t).with('percentage.format.separator', default: '.').and_return 'x'
113
- allow(I18n).to receive(:t).with('percentage.format.precision', default: 2).and_return 2
114
- end
115
-
116
- it { expect(subject.call('1x2')).to eq '1.20' }
117
- end
118
-
119
- context 'when not provided' do
120
- it { expect(subject.call('1.2')).to eq '1.20' }
121
- end
122
- end
123
-
124
- describe 'cast' do
125
- context 'with no decimal precision' do
126
- it { expect(subject.call('1' , cast: :to_f)).to eq 1.0 }
127
- it { expect(subject.call('10' , cast: :to_f)).to eq 10.0 }
128
- it { expect(subject.call('100', cast: :to_f)).to eq 100.0 }
129
- end
130
-
131
- context 'when value has one decimal precision' do
132
- it { expect(subject.call('1.7' , cast: :to_f)).to eq 1.7 }
133
- it { expect(subject.call('103.7' , cast: :to_f)).to eq 103.7 }
134
- it { expect(subject.call('1030.7' , cast: :to_f)).to eq 1030.7 }
135
- it { expect(subject.call('10300.7', cast: :to_f)).to eq 10_300.7 }
136
-
137
- it { expect(subject.call('$ 1.7' , cast: :to_f)).to eq 1.7 }
138
- it { expect(subject.call('$ 103.7' , cast: :to_f)).to eq 103.7 }
139
- it { expect(subject.call('$ 1030.7' , cast: :to_f)).to eq 1030.7 }
140
- it { expect(subject.call('$ 10300.7', cast: :to_f)).to eq 10_300.7 }
141
-
142
- context 'with calls cast' do
143
- it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
144
- it { expect(subject.call('103.7' , cast: :to_f)).to be 103.7 }
145
- it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
146
- it { expect(subject.call('10300.7', cast: :to_f)).to be 10_300.7 }
147
-
148
- it { expect(subject.call('$ 1.7' , cast: :to_f)).to be 1.7 }
149
- it { expect(subject.call('$ 103.7' , cast: :to_f)).to be 103.7 }
150
- it { expect(subject.call('$ 1030.7' , cast: :to_f)).to be 1030.7 }
151
- it { expect(subject.call('$ 10300.7', cast: :to_f)).to be 10_300.7 }
152
- end
153
- end
154
-
155
- context 'when value has two decimal precision' do
156
- it { expect(subject.call('1.70' , cast: :to_f)).to be 1.7 }
157
- it { expect(subject.call('103.70' , cast: :to_f)).to be 103.7 }
158
- it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
159
- it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
160
-
161
- it { expect(subject.call('$ 1' , cast: :to_f)).to be 1.0 }
162
- it { expect(subject.call('$ 1.70' , cast: :to_f)).to be 1.7 }
163
- it { expect(subject.call('$ 103.70' , cast: :to_f)).to be 103.7 }
164
- it { expect(subject.call('$ 1030.70' , cast: :to_f)).to be 1030.7 }
165
- it { expect(subject.call('$ 10300.70', cast: :to_f)).to be 10_300.7 }
166
- end
167
- end
168
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Filters::Slug do
6
- describe 'default options' do
7
- it { expect(subject.call(nil)).to eq nil }
8
- it { expect(subject.call('')).to eq '' }
9
-
10
- it { expect(subject.call('The Title')).to eq 'the-title' }
11
- end
12
-
13
- describe 'to' do
14
- it { expect(subject.call(nil , to: :slug)).to eq nil }
15
- it { expect(subject.call('' , to: :slug)).to eq '' }
16
- it { expect(subject.call('The Title', to: :slug)).to eq 'the-title' }
17
-
18
- context 'when is not :to key' do
19
- it { expect(subject.call(nil , from: :slug)).to eq nil }
20
- it { expect(subject.call('' , from: :slug)).to eq '' }
21
- it { expect(subject.call('The Title', from: :slug)).to eq 'the-title' }
22
- end
23
- end
24
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Filters::Strip do
6
- it { expect(subject.call(' Some Text ')).to eq 'Some Text' }
7
- it { expect(subject.call(' Some Text ', side: :left)).to eq 'Some Text ' }
8
- it { expect(subject.call(' Some Text ', side: :right)).to eq ' Some Text' }
9
- it { expect(subject.call(' Some Text ', side: :both)).to eq 'Some Text' }
10
- it { expect(subject.call("\n")).to eq '' }
11
- it { expect(subject.call(42)).to eq 42 }
12
- it { expect(subject.call(nil)).to eq nil }
13
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy, '#configure' do
6
- it 'yields the default config' do
7
- described_class.configure do |conf|
8
- expect(conf).to be_a_instance_of Normalizy::Config
9
- end
10
- end
11
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::RSpec::Matcher, '.description' do
6
- let!(:matcher) { described_class.new :name }
7
-
8
- context 'with no :with expectation' do
9
- it do
10
- matcher.from :from
11
- matcher.to :to
12
-
13
- expect(matcher.description).to eq 'normalizy name from "from" to "to"'
14
- end
15
- end
16
-
17
- context 'with :with expectation' do
18
- it do
19
- matcher.with :blank
20
-
21
- matcher.from :from
22
- matcher.to :to
23
-
24
- expect(matcher.description).to eq 'normalizy name with blank'
25
- end
26
- end
27
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::RSpec::Matcher, '.failure_message' do
6
- let!(:model) { Match }
7
-
8
- context 'with no :with expectation' do
9
- it do
10
- matcher = described_class.new(:downcase_field)
11
-
12
- matcher.from :from
13
- matcher.to :to
14
- matcher.matches? model.new
15
-
16
- expect(matcher.failure_message).to eq %(expected: "to"\n got: "from")
17
- end
18
- end
19
-
20
- context 'with :with expectation' do
21
- it do
22
- matcher = described_class.new(:alone)
23
-
24
- matcher.with :missing
25
- matcher.matches? model.new
26
-
27
- expect(matcher.failure_message).to eq %(expected: missing\n got: nil)
28
- end
29
-
30
- it do
31
- matcher = described_class.new(:downcase_field_array)
32
-
33
- matcher.with :missing
34
- matcher.matches? model.new
35
-
36
- expect(matcher.failure_message).to eq %(expected: missing\n got: downcase)
37
- end
38
-
39
- it do
40
- matcher = described_class.new(:trim_side_left)
41
-
42
- matcher.with :missing
43
- matcher.matches? model.new
44
-
45
- expect(matcher.failure_message).to eq %(expected: missing\n got: {:trim=>{:side=>:left}})
46
- end
47
- end
48
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::RSpec::Matcher, '.failure_message_when_negated' do
6
- let!(:model) { Match }
7
-
8
- context 'with no :with expectation' do
9
- it do
10
- matcher = described_class.new(:downcase_field)
11
-
12
- matcher.from 'from'
13
- matcher.to 'from'
14
- matcher.matches? model.new
15
-
16
- expect(matcher.failure_message_when_negated).to eq %(expected: value != "from"\n got: "from")
17
- end
18
- end
19
-
20
- context 'with :with expectation' do
21
- it do
22
- matcher = described_class.new(:downcase_field)
23
-
24
- matcher.with :downcase
25
- matcher.matches? model.new
26
-
27
- expect(matcher.failure_message_when_negated).to eq %(expected: value != downcase\n got: downcase)
28
- end
29
- end
30
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::RSpec::Matcher, '.from' do
6
- it 'caches the value' do
7
- matcher = described_class.new(:downcase_field)
8
-
9
- matcher.from :from
10
-
11
- expect(matcher.instance_variable_get(:@from)).to eq :from
12
- end
13
-
14
- it 'returns it self' do
15
- matcher = described_class.new(:downcase_field)
16
-
17
- expect(matcher.from(:from)).to be matcher
18
- end
19
- end