normalizy 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +53 -28
  3. data/README.md +22 -16
  4. data/lib/normalizy/filters/money.rb +2 -2
  5. data/lib/normalizy/filters/percent.rb +2 -2
  6. data/lib/normalizy/version.rb +1 -1
  7. metadata +27 -94
  8. data/spec/normalizy/config/add_spec.rb +0 -21
  9. data/spec/normalizy/config/alias_spec.rb +0 -29
  10. data/spec/normalizy/config/default_filters_spec.rb +0 -19
  11. data/spec/normalizy/config/initialize_spec.rb +0 -16
  12. data/spec/normalizy/config/normalizy_aliases_spec.rb +0 -9
  13. data/spec/normalizy/extensions/filters/date_spec.rb +0 -23
  14. data/spec/normalizy/extensions/filters/money_spec.rb +0 -38
  15. data/spec/normalizy/extensions/filters/number_spec.rb +0 -9
  16. data/spec/normalizy/extensions/filters/percent_spec.rb +0 -30
  17. data/spec/normalizy/extensions/filters/slug_spec.rb +0 -14
  18. data/spec/normalizy/extensions/filters/strip_spec.rb +0 -21
  19. data/spec/normalizy/extensions/model_spec.rb +0 -107
  20. data/spec/normalizy/extensions/normalizy_rules_spec.rb +0 -198
  21. data/spec/normalizy/filters/date_spec.rb +0 -72
  22. data/spec/normalizy/filters/money_spec.rb +0 -282
  23. data/spec/normalizy/filters/number_spec.rb +0 -33
  24. data/spec/normalizy/filters/percent_spec.rb +0 -168
  25. data/spec/normalizy/filters/slug_spec.rb +0 -24
  26. data/spec/normalizy/filters/strip_spec.rb +0 -13
  27. data/spec/normalizy/normalizy/configure_spec.rb +0 -11
  28. data/spec/normalizy/rspec/matcher/description_spec.rb +0 -27
  29. data/spec/normalizy/rspec/matcher/failure_message_spec.rb +0 -48
  30. data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +0 -30
  31. data/spec/normalizy/rspec/matcher/from_spec.rb +0 -19
  32. data/spec/normalizy/rspec/matcher/matches_spec.rb +0 -117
  33. data/spec/normalizy/rspec/matcher/to_spec.rb +0 -19
  34. data/spec/rails_helper.rb +0 -9
  35. data/spec/support/common.rb +0 -11
  36. data/spec/support/db/schema.rb +0 -83
  37. data/spec/support/filters/blacklist.rb +0 -11
  38. data/spec/support/filters/block.rb +0 -11
  39. data/spec/support/filters/info.rb +0 -11
  40. data/spec/support/models/alias.rb +0 -7
  41. data/spec/support/models/match.rb +0 -9
  42. data/spec/support/models/model.rb +0 -28
  43. data/spec/support/models/model_date.rb +0 -9
  44. data/spec/support/models/model_money.rb +0 -10
  45. data/spec/support/models/model_number.rb +0 -5
  46. data/spec/support/models/model_percent.rb +0 -10
  47. data/spec/support/models/model_slug.rb +0 -6
  48. data/spec/support/models/model_strip.rb +0 -8
  49. data/spec/support/models/rule.rb +0 -4
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe '#apply_normalizy' do
6
- context 'when object has no normalizy' do
7
- it do
8
- expect(Model.create(none: ' Botelho ').none).to eq ' Botelho '
9
- end
10
- end
11
-
12
- context 'when object has normalizy' do
13
- it do
14
- Normalizy.configure do |config|
15
- config.default_filters = [:squish]
16
- end
17
-
18
- expect(Model.create(default: ' Botelho ').default).to eq 'Botelho'
19
- end
20
-
21
- it do
22
- expect(Model.create(block: 'Botelho').block).to eq 'BOTELHO'
23
- end
24
- end
25
-
26
- context 'when a filter is given' do
27
- it do
28
- expect(Model.create(symbol: ' Bote lho ').symbol).to eq 'Bote lho'
29
- end
30
-
31
- it do
32
- expect(Model.create(array_symbol: ' Bote lho ').array_symbol).to eq 'Bote lho'
33
- end
34
-
35
- it do
36
- expect(Model.create(array_symbols: ' Bote lho ').array_symbols).to eq 'bote lho'
37
- end
38
-
39
- it do
40
- expect(Model.create(hash_no_args: ' Bote lho ').hash_no_args).to eq 'Bote lho'
41
- end
42
-
43
- it do
44
- expect(Model.create(hash_with_args: ' Botelho ').hash_with_args).to eq 'Botelho '
45
- end
46
-
47
- it do
48
- expect(Model.create(module_one_arg: 'Fuck').module_one_arg).to eq 'filtered'
49
- end
50
-
51
- it do
52
- expect(Model.create(module_two_args: 'Botelho').module_two_args).to eq 'module_two_args, Botelho, Model'
53
- end
54
-
55
- it do
56
- Normalizy.configure do |config|
57
- config.add :blacklist, Normalizy::Filters::Block
58
- end
59
-
60
- expect(Model.create(module_and_block: 'Botelho').module_and_block).to eq 'BOTELHO'
61
- end
62
-
63
- it do
64
- expect(Model.create(method_with_no_options_field: 'Botelho').method_with_no_options_field).to eq 'Botelho'
65
- end
66
-
67
- it do
68
- expect(Model.create(method_with_options_field: 'Botelho').method_with_options_field).to eq [
69
- 'Botelho',
70
- {
71
- key: :value,
72
- attribute: :method_with_options_field,
73
- object: Model.new
74
- }
75
- ].join ', '
76
- end
77
-
78
- it do
79
- expect(Model.create(native: 'Botelho').native).to eq '["Botelho"]'
80
- end
81
-
82
- it do
83
- expect(Model.create(multiple: 'BoteLho').multiple).to eq 'bote lho'
84
- end
85
- end
86
-
87
- context 'when assign is made via set' do
88
- context 'with no save' do
89
- it do
90
- object = Model.new
91
- object.symbol = ' Bote lho '
92
-
93
- expect(object.symbol).to eq 'Bote lho'
94
- end
95
- end
96
-
97
- context 'with save' do
98
- it do
99
- object = Model.new
100
- object.symbol = ' Bote lho '
101
- object.save
102
-
103
- expect(object.symbol).to eq 'Bote lho'
104
- end
105
- end
106
- end
107
- end
@@ -1,198 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Extension, ':normalizy_rules' do
6
- let!(:model) { Rule }
7
-
8
- before { model.normalizy_rules = {} }
9
-
10
- context 'with default' do
11
- before do
12
- Normalizy.configure do |config|
13
- config.default_filters = :squish
14
- end
15
- end
16
-
17
- it do
18
- model.normalizy :name
19
-
20
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: nil }])
21
- end
22
-
23
- it do
24
- model.normalizy :name, with: :upcase
25
-
26
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
27
- end
28
-
29
- it do
30
- expected = { name: [{ block: nil, options: {}, rules: [:upcase, 'blank', { trim: { side: :left } }] }] }
31
-
32
- model.normalizy :name, with: [:upcase, 'blank', { trim: { side: :left } }]
33
-
34
- expect(model.normalizy_rules).to eq expected
35
- end
36
-
37
- it do
38
- model.normalizy :name, with: [:upcase, { trim: { side: :left } }]
39
- model.normalizy :name, with: :squish
40
- model.normalizy :name, with: [:upcase, { trim: { side: :right } }]
41
- model.normalizy :name, with: :squish
42
-
43
- expect(model.normalizy_rules).to eq(
44
- name: [
45
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
46
- { block: nil, options: {}, rules: :squish },
47
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
48
- { block: nil, options: {}, rules: :squish }
49
- ]
50
- )
51
- end
52
-
53
- it do
54
- model.normalizy :email, :name
55
-
56
- expect(model.normalizy_rules).to eq(
57
- email: [{ block: nil, options: {}, rules: nil }],
58
- name: [{ block: nil, options: {}, rules: nil }]
59
- )
60
- end
61
-
62
- it do
63
- model.normalizy :email, :name, with: :upcase
64
-
65
- expect(model.normalizy_rules).to eq(
66
- name: [{ block: nil, options: {}, rules: :upcase }],
67
- email: [{ block: nil, options: {}, rules: :upcase }]
68
- )
69
- end
70
-
71
- it do
72
- model.normalizy :email, :name, with: [:upcase, :blank, { trim: { side: :left } }]
73
-
74
- expect(model.normalizy_rules).to eq(
75
- email: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }],
76
- name: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }]
77
- )
78
- end
79
-
80
- it do
81
- model.normalizy :email, :name, with: [:upcase, { trim: { side: :left } }]
82
- model.normalizy :email, :name, with: :squish
83
- model.normalizy :email, :name, with: [:upcase, { trim: { side: :right } }]
84
- model.normalizy :email, :name, with: :squish
85
-
86
- expect(model.normalizy_rules).to eq(
87
- email: [
88
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
89
- { block: nil, options: {}, rules: :squish },
90
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
91
- { block: nil, options: {}, rules: :squish }
92
- ],
93
- name: [
94
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
95
- { block: nil, options: {}, rules: :squish },
96
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
97
- { block: nil, options: {}, rules: :squish }
98
- ]
99
- )
100
- end
101
- end
102
-
103
- context 'with no default' do
104
- before do
105
- Normalizy.configure do |config|
106
- config.default_filters = []
107
- end
108
- end
109
-
110
- it do
111
- model.normalizy :name
112
-
113
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: nil }])
114
- end
115
-
116
- it do
117
- model.normalizy :name, with: :upcase
118
-
119
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
120
- end
121
-
122
- it do
123
- model.normalizy :name, with: %i[upcase blank]
124
-
125
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: %i[upcase blank] }])
126
- end
127
-
128
- it do
129
- model.normalizy :name, with: :upcase
130
- model.normalizy :name, with: :squish
131
- model.normalizy :name, with: :upcase
132
-
133
- expect(model.normalizy_rules).to eq(
134
- name: [
135
- { block: nil, options: {}, rules: :upcase },
136
- { block: nil, options: {}, rules: :squish },
137
- { block: nil, options: {}, rules: :upcase }
138
- ]
139
- )
140
- end
141
-
142
- it do
143
- model.normalizy :email, :name
144
-
145
- expect(model.normalizy_rules).to eq(
146
- email: [{ block: nil, options: {}, rules: nil }],
147
- name: [{ block: nil, options: {}, rules: nil }]
148
- )
149
- end
150
-
151
- it do
152
- model.normalizy :email, :name, with: :upcase
153
-
154
- expect(model.normalizy_rules).to eq(
155
- email: [{ block: nil, options: {}, rules: :upcase }],
156
- name: [{ block: nil, options: {}, rules: :upcase }]
157
- )
158
- end
159
-
160
- it do
161
- model.normalizy :email, :name, with: %i[upcase blank]
162
-
163
- expect(model.normalizy_rules).to eq(
164
- email: [{ block: nil, options: {}, rules: %i[upcase blank] }],
165
- name: [{ block: nil, options: {}, rules: %i[upcase blank] }]
166
- )
167
- end
168
-
169
- it do
170
- model.normalizy :email, :name, with: :upcase
171
- model.normalizy :email, :name, with: :squish
172
- model.normalizy :email, :name, with: :upcase
173
-
174
- expect(model.normalizy_rules).to eq(
175
- email: [
176
- { block: nil, options: {}, rules: :upcase },
177
- { block: nil, options: {}, rules: :squish },
178
- { block: nil, options: {}, rules: :upcase }
179
- ],
180
- name: [
181
- { block: nil, options: {}, rules: :upcase },
182
- { block: nil, options: {}, rules: :squish },
183
- { block: nil, options: {}, rules: :upcase }
184
- ]
185
- )
186
- end
187
- end
188
-
189
- context 'when block is given' do
190
- let!(:block) { ->(value) { value.downcase } }
191
-
192
- it do
193
- model.normalizy :name, &block
194
-
195
- expect(model.normalizy_rules[:name][0][:block]).to eq block
196
- end
197
- end
198
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Filters::Date do
6
- it { expect(subject.call('')).to eq '' }
7
-
8
- it { expect(subject.call('1984-10-23')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }
9
-
10
- it { expect(subject.call('84/10/23', format: '%y/%m/%d')).to eq Time.new(1984, 10, 23, 0, 0, 0, 0) }
11
-
12
- it { expect(subject.call(Time.new(1984, 1, 1, 1), adjust: :begin)).to eq Time.new(1984) }
13
- it { expect(subject.call(Time.new(1984), adjust: :end)).to eq Time.new(1984).end_of_day }
14
-
15
- it { expect(subject.call(DateTime.new(1984, 1, 1, 1), adjust: :begin)).to eq DateTime.new(1984) }
16
- it { expect(subject.call(DateTime.new(1984), adjust: :end)).to eq DateTime.new(1984).end_of_day }
17
-
18
- it { expect(subject.call(OpenStruct.new(beginning_of_day: :success), adjust: :begin)).to eq :success }
19
- it { expect(subject.call(OpenStruct.new(end_of_day: :success), adjust: :end)).to eq :success }
20
-
21
- it do
22
- result = subject.call(Date.new(1984, 10, 23), adjust: :end)
23
-
24
- expect(result.year).to eq 1984
25
- expect(result.month).to eq 10
26
- expect(result.day).to eq 23
27
- end
28
-
29
- it do
30
- result = subject.call(Date.new(1984), adjust: :end)
31
-
32
- expect(result.year).to eq 1984
33
- expect(result.month).to eq 1
34
- expect(result.day).to eq 1
35
- end
36
-
37
- it 'accepts time zone' do
38
- time = subject.call('1984-10-23', time_zone: 'Tokelau Is.').utc
39
-
40
- expect(time).to eq Time.new(1984, 10, 23, time.hour, 0, 0, 0)
41
- end
42
-
43
- context 'with invalid date' do
44
- let!(:object) { ModelDate.new }
45
- let!(:options) { { attribute: :date, object: object } }
46
-
47
- context 'with i18n present' do
48
- before do
49
- allow(I18n).to receive(:t).with(:date,
50
- scope: ['normalizy.errors.date', 'model_date'],
51
- value: '1984-10-00',
52
- default: '%{value} is an invalid date.').and_return 'date.error'
53
- end
54
-
55
- it 'writes an error on object and does not set the values' do
56
- subject.call '1984-10-00', options
57
-
58
- expect(object.errors[:date]).to eq ['date.error']
59
- expect(object.date).to eq nil
60
- end
61
- end
62
-
63
- context 'with no I18n present' do
64
- it 'writes a default error on object and does not set the values' do
65
- subject.call '1984-10-00', options
66
-
67
- expect(object.errors[:date]).to eq ['1984-10-00 is an invalid date.']
68
- expect(object.date).to eq nil
69
- end
70
- end
71
- end
72
- end
@@ -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