normalizy 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +25 -55
- data/lib/normalizy/config.rb +3 -7
- data/lib/normalizy/extensions.rb +45 -49
- data/lib/normalizy/rspec/matcher.rb +14 -2
- data/lib/normalizy/version.rb +1 -1
- data/lib/normalizy.rb +1 -1
- data/spec/normalizy/config/alias_spec.rb +14 -42
- data/spec/normalizy/extensions/filters/date_spec.rb +4 -14
- data/spec/normalizy/extensions/filters/money_spec.rb +9 -19
- data/spec/normalizy/extensions/filters/number_spec.rb +2 -6
- data/spec/normalizy/extensions/filters/percent_spec.rb +9 -19
- data/spec/normalizy/extensions/filters/strip_spec.rb +8 -8
- data/spec/normalizy/extensions/model_spec.rb +107 -0
- data/spec/normalizy/extensions/normalizy_rules_spec.rb +132 -180
- data/spec/normalizy/filters/date_spec.rb +9 -9
- data/spec/normalizy/filters/money_spec.rb +42 -37
- data/spec/normalizy/rspec/matcher/description_spec.rb +8 -7
- data/spec/normalizy/rspec/matcher/failure_message_spec.rb +25 -47
- data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +13 -15
- data/spec/normalizy/rspec/matcher/from_spec.rb +4 -3
- data/spec/normalizy/rspec/matcher/matches_spec.rb +117 -0
- data/spec/normalizy/rspec/matcher/to_spec.rb +4 -3
- data/spec/support/db/schema.rb +67 -9
- data/spec/support/filters/{blacklist_1.rb → blacklist.rb} +1 -1
- data/spec/support/filters/{blacklist_block.rb → block.rb} +1 -1
- data/spec/support/filters/info.rb +11 -0
- data/spec/support/models/alias.rb +7 -0
- data/spec/support/models/match.rb +9 -0
- data/spec/support/models/model.rb +28 -0
- data/spec/support/models/model_date.rb +7 -0
- data/spec/support/models/model_money.rb +10 -0
- data/spec/support/models/model_number.rb +5 -0
- data/spec/support/models/model_percent.rb +10 -0
- data/spec/support/models/model_strip.rb +8 -0
- data/spec/support/models/{clean.rb → rule.rb} +1 -1
- metadata +30 -20
- data/spec/normalizy/config/normalizy_raws_spec.rb +0 -9
- data/spec/normalizy/extensions/apply_normalizy_spec.rb +0 -171
- data/spec/normalizy/rspec/matcher/matchers_spec.rb +0 -97
- data/spec/normalizy/rspec/normalizy_spec.rb +0 -8
- data/spec/support/filters/blacklist_2.rb +0 -13
- data/spec/support/models/user.rb +0 -9
@@ -0,0 +1,107 @@
|
|
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
|
+
specify do
|
8
|
+
expect(Model.create(none: ' Botelho ').none).to eq ' Botelho '
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when object has normalizy' do
|
13
|
+
specify 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
|
+
specify 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
|
+
specify do
|
28
|
+
expect(Model.create(symbol: ' Bote lho ').symbol).to eq 'Bote lho'
|
29
|
+
end
|
30
|
+
|
31
|
+
specify do
|
32
|
+
expect(Model.create(array_symbol: ' Bote lho ').array_symbol).to eq 'Bote lho'
|
33
|
+
end
|
34
|
+
|
35
|
+
specify do
|
36
|
+
expect(Model.create(array_symbols: ' Bote lho ').array_symbols).to eq 'bote lho'
|
37
|
+
end
|
38
|
+
|
39
|
+
specify do
|
40
|
+
expect(Model.create(hash_no_args: ' Bote lho ').hash_no_args).to eq 'Bote lho'
|
41
|
+
end
|
42
|
+
|
43
|
+
specify do
|
44
|
+
expect(Model.create(hash_with_args: ' Botelho ').hash_with_args).to eq 'Botelho '
|
45
|
+
end
|
46
|
+
|
47
|
+
specify do
|
48
|
+
expect(Model.create(module_one_arg: 'Fuck').module_one_arg).to eq 'filtered'
|
49
|
+
end
|
50
|
+
|
51
|
+
specify do
|
52
|
+
expect(Model.create(module_two_args: 'Botelho').module_two_args).to eq 'module_two_args, Botelho, Model'
|
53
|
+
end
|
54
|
+
|
55
|
+
specify 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
|
+
specify do
|
64
|
+
expect(Model.create(method_with_no_options_field: 'Botelho').method_with_no_options_field).to eq 'Botelho'
|
65
|
+
end
|
66
|
+
|
67
|
+
specify 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
|
+
specify do
|
79
|
+
expect(Model.create(native: 'Botelho').native).to eq '["Botelho"]'
|
80
|
+
end
|
81
|
+
|
82
|
+
specify 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
|
+
specify 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
|
+
specify 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
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Normalizy::Extension, ':normalizy_rules' do
|
6
|
-
let!(:model) {
|
6
|
+
let!(:model) { Rule }
|
7
7
|
|
8
8
|
before { model.normalizy_rules = {} }
|
9
9
|
|
@@ -14,111 +14,87 @@ RSpec.describe Normalizy::Extension, ':normalizy_rules' do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
before { model.normalizy :name }
|
17
|
+
specify do
|
18
|
+
model.normalizy :name
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
20
|
+
expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: nil }])
|
21
|
+
end
|
25
22
|
|
26
|
-
|
27
|
-
|
23
|
+
specify do
|
24
|
+
model.normalizy :name, with: :upcase
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|
26
|
+
expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
|
27
|
+
end
|
33
28
|
|
34
|
-
|
35
|
-
|
29
|
+
specify do
|
30
|
+
model.normalizy :name, with: [:upcase, 'blank', { trim: { side: :left } }]
|
36
31
|
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
32
|
+
expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: [:upcase, 'blank', { trim: { side: :left } }] }])
|
33
|
+
end
|
41
34
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
{ block: nil, options: {}, rules: :squish }
|
57
|
-
]
|
58
|
-
)
|
59
|
-
end
|
60
|
-
end
|
35
|
+
specify do
|
36
|
+
model.normalizy :name, with: [:upcase, { trim: { side: :left } }]
|
37
|
+
model.normalizy :name, with: :squish
|
38
|
+
model.normalizy :name, with: [:upcase, { trim: { side: :right } }]
|
39
|
+
model.normalizy :name, with: :squish
|
40
|
+
|
41
|
+
expect(model.normalizy_rules).to eq(
|
42
|
+
name: [
|
43
|
+
{ block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
|
44
|
+
{ block: nil, options: {}, rules: :squish },
|
45
|
+
{ block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
|
46
|
+
{ block: nil, options: {}, rules: :squish }
|
47
|
+
]
|
48
|
+
)
|
61
49
|
end
|
62
50
|
|
63
|
-
|
64
|
-
|
65
|
-
before { model.normalizy :email, :name }
|
51
|
+
specify do
|
52
|
+
model.normalizy :email, :name
|
66
53
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
73
|
-
end
|
54
|
+
expect(model.normalizy_rules).to eq(
|
55
|
+
email: [{ block: nil, options: {}, rules: nil }],
|
56
|
+
name: [{ block: nil, options: {}, rules: nil }]
|
57
|
+
)
|
58
|
+
end
|
74
59
|
|
75
|
-
|
76
|
-
|
60
|
+
specify do
|
61
|
+
model.normalizy :email, :name, with: :upcase
|
77
62
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
84
|
-
end
|
63
|
+
expect(model.normalizy_rules).to eq(
|
64
|
+
name: [{ block: nil, options: {}, rules: :upcase }],
|
65
|
+
email: [{ block: nil, options: {}, rules: :upcase }]
|
66
|
+
)
|
67
|
+
end
|
85
68
|
|
86
|
-
|
87
|
-
|
69
|
+
specify do
|
70
|
+
model.normalizy :email, :name, with: [:upcase, :blank, { trim: { side: :left } }]
|
88
71
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
95
|
-
end
|
72
|
+
expect(model.normalizy_rules).to eq(
|
73
|
+
email: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }],
|
74
|
+
name: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }]
|
75
|
+
)
|
76
|
+
end
|
96
77
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
{ block: nil, options: {}, rules: :squish }
|
118
|
-
]
|
119
|
-
)
|
120
|
-
end
|
121
|
-
end
|
78
|
+
specify do
|
79
|
+
model.normalizy :email, :name, with: [:upcase, { trim: { side: :left } }]
|
80
|
+
model.normalizy :email, :name, with: :squish
|
81
|
+
model.normalizy :email, :name, with: [:upcase, { trim: { side: :right } }]
|
82
|
+
model.normalizy :email, :name, with: :squish
|
83
|
+
|
84
|
+
expect(model.normalizy_rules).to eq(
|
85
|
+
email: [
|
86
|
+
{ block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
|
87
|
+
{ block: nil, options: {}, rules: :squish },
|
88
|
+
{ block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
|
89
|
+
{ block: nil, options: {}, rules: :squish }
|
90
|
+
],
|
91
|
+
name: [
|
92
|
+
{ block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
|
93
|
+
{ block: nil, options: {}, rules: :squish },
|
94
|
+
{ block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
|
95
|
+
{ block: nil, options: {}, rules: :squish }
|
96
|
+
]
|
97
|
+
)
|
122
98
|
end
|
123
99
|
end
|
124
100
|
|
@@ -129,115 +105,91 @@ RSpec.describe Normalizy::Extension, ':normalizy_rules' do
|
|
129
105
|
end
|
130
106
|
end
|
131
107
|
|
132
|
-
|
133
|
-
|
134
|
-
before { model.normalizy :name }
|
108
|
+
specify do
|
109
|
+
model.normalizy :name
|
135
110
|
|
136
|
-
|
137
|
-
|
138
|
-
end
|
139
|
-
end
|
111
|
+
expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: nil }])
|
112
|
+
end
|
140
113
|
|
141
|
-
|
142
|
-
|
114
|
+
specify do
|
115
|
+
model.normalizy :name, with: :upcase
|
143
116
|
|
144
|
-
|
145
|
-
|
146
|
-
end
|
147
|
-
end
|
117
|
+
expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
|
118
|
+
end
|
148
119
|
|
149
|
-
|
150
|
-
|
120
|
+
specify do
|
121
|
+
model.normalizy :name, with: %i[upcase blank]
|
151
122
|
|
152
|
-
|
153
|
-
|
154
|
-
end
|
155
|
-
end
|
123
|
+
expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: %i[upcase blank] }])
|
124
|
+
end
|
156
125
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
{ block: nil, options: {}, rules: :upcase }
|
170
|
-
]
|
171
|
-
)
|
172
|
-
end
|
173
|
-
end
|
126
|
+
specify do
|
127
|
+
model.normalizy :name, with: :upcase
|
128
|
+
model.normalizy :name, with: :squish
|
129
|
+
model.normalizy :name, with: :upcase
|
130
|
+
|
131
|
+
expect(model.normalizy_rules).to eq(
|
132
|
+
name: [
|
133
|
+
{ block: nil, options: {}, rules: :upcase },
|
134
|
+
{ block: nil, options: {}, rules: :squish },
|
135
|
+
{ block: nil, options: {}, rules: :upcase }
|
136
|
+
]
|
137
|
+
)
|
174
138
|
end
|
175
139
|
|
176
|
-
|
177
|
-
|
178
|
-
before { model.normalizy :email, :name }
|
140
|
+
specify do
|
141
|
+
model.normalizy :email, :name
|
179
142
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
end
|
186
|
-
end
|
143
|
+
expect(model.normalizy_rules).to eq(
|
144
|
+
email: [{ block: nil, options: {}, rules: nil }],
|
145
|
+
name: [{ block: nil, options: {}, rules: nil }]
|
146
|
+
)
|
147
|
+
end
|
187
148
|
|
188
|
-
|
189
|
-
|
149
|
+
specify do
|
150
|
+
model.normalizy :email, :name, with: :upcase
|
190
151
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
end
|
197
|
-
end
|
152
|
+
expect(model.normalizy_rules).to eq(
|
153
|
+
email: [{ block: nil, options: {}, rules: :upcase }],
|
154
|
+
name: [{ block: nil, options: {}, rules: :upcase }]
|
155
|
+
)
|
156
|
+
end
|
198
157
|
|
199
|
-
|
200
|
-
|
158
|
+
specify do
|
159
|
+
model.normalizy :email, :name, with: %i[upcase blank]
|
201
160
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
end
|
208
|
-
end
|
161
|
+
expect(model.normalizy_rules).to eq(
|
162
|
+
email: [{ block: nil, options: {}, rules: %i[upcase blank] }],
|
163
|
+
name: [{ block: nil, options: {}, rules: %i[upcase blank] }]
|
164
|
+
)
|
165
|
+
end
|
209
166
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
{ block: nil, options: {}, rules: :upcase }
|
228
|
-
]
|
229
|
-
)
|
230
|
-
end
|
231
|
-
end
|
167
|
+
specify do
|
168
|
+
model.normalizy :email, :name, with: :upcase
|
169
|
+
model.normalizy :email, :name, with: :squish
|
170
|
+
model.normalizy :email, :name, with: :upcase
|
171
|
+
|
172
|
+
expect(model.normalizy_rules).to eq(
|
173
|
+
email: [
|
174
|
+
{ block: nil, options: {}, rules: :upcase },
|
175
|
+
{ block: nil, options: {}, rules: :squish },
|
176
|
+
{ block: nil, options: {}, rules: :upcase }
|
177
|
+
],
|
178
|
+
name: [
|
179
|
+
{ block: nil, options: {}, rules: :upcase },
|
180
|
+
{ block: nil, options: {}, rules: :squish },
|
181
|
+
{ block: nil, options: {}, rules: :upcase }
|
182
|
+
]
|
183
|
+
)
|
232
184
|
end
|
233
185
|
end
|
234
186
|
|
235
187
|
context 'when block is given' do
|
236
188
|
let!(:block) { ->(value) { value.downcase } }
|
237
189
|
|
238
|
-
before { model.normalizy(:name, &block) }
|
239
|
-
|
240
190
|
specify do
|
191
|
+
model.normalizy :name, &block
|
192
|
+
|
241
193
|
expect(model.normalizy_rules[:name][0][:block]).to eq block
|
242
194
|
end
|
243
195
|
end
|
@@ -21,15 +21,15 @@ RSpec.describe Normalizy::Filters::Date do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
context 'with invalid date' do
|
24
|
-
let!(:object) {
|
25
|
-
let!(:options) { { attribute: :
|
24
|
+
let!(:object) { ModelDate.new }
|
25
|
+
let!(:options) { { attribute: :date, object: object } }
|
26
26
|
|
27
27
|
context 'with i18n present' do
|
28
28
|
before do
|
29
|
-
allow(I18n).to receive(:t).with(:
|
30
|
-
scope: ['normalizy.errors.date', '
|
29
|
+
allow(I18n).to receive(:t).with(:date,
|
30
|
+
scope: ['normalizy.errors.date', 'model_date'],
|
31
31
|
value: '1984-10-00',
|
32
|
-
default: '%{value} is an invalid date.') { '
|
32
|
+
default: '%{value} is an invalid date.') { 'date.error' }
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'writes an error on object and does not set the values' do
|
@@ -37,8 +37,8 @@ RSpec.describe Normalizy::Filters::Date do
|
|
37
37
|
|
38
38
|
subject.call '1984-10-00', options
|
39
39
|
|
40
|
-
expect(object.errors[:
|
41
|
-
expect(object.
|
40
|
+
expect(object.errors[:date]).to eq ['date.error']
|
41
|
+
expect(object.date).to eq nil
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -48,8 +48,8 @@ RSpec.describe Normalizy::Filters::Date do
|
|
48
48
|
|
49
49
|
subject.call '1984-10-00', options
|
50
50
|
|
51
|
-
expect(object.errors[:
|
52
|
-
expect(object.
|
51
|
+
expect(object.errors[:date]).to eq ['1984-10-00 is an invalid date.']
|
52
|
+
expect(object.date).to eq nil
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|