unidom-common-rspec 0.8 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -2
- data/Gemfile.lock +1 -1
- data/README.md +48 -9
- data/ROADMAP.md +6 -2
- data/lib/unidom/common/rspec/validates_shared_examples.rb +87 -0
- data/lib/unidom/common/rspec/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f097e72d01cc1632058b1f840918b13b8699b27
|
4
|
+
data.tar.gz: 88122fdb2b5ef945dc9168dc2b03ab180b878472
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f25e70951fc961ef8354ebf52926b9207d9051c716f4b9505f6666610c9b127d0a544e96bea4dc089295609581d78d008425576a598a67f1f5a0203bf5431401
|
7
|
+
data.tar.gz: a6537d558ed74092157e7f0d4ff64aa722b6793d115d7063a63d6f22e03d86942da0cdcfca397b52a9abbfc215c5e43e8561d57122307d16756bb6bb5fd05218
|
data/CHANGELOG.md
CHANGED
@@ -25,13 +25,13 @@
|
|
25
25
|
2. Improved the Has One shared examples for the #``build_association`` method, the #``create_association`` method, & the #``create_association!`` method
|
26
26
|
|
27
27
|
## v0.6.2
|
28
|
-
1. Improved the Has Many shared examples for the #``collection.build`` method, the #``collection.create`` method, & the
|
28
|
+
1. Improved the Has Many shared examples for the #``collection.build`` method, the #``collection.create`` method, & the #``collection.create!`` method
|
29
29
|
|
30
30
|
## v0.7
|
31
31
|
1. Each Validator shared examples
|
32
32
|
|
33
33
|
## v0.7.1
|
34
|
-
1. Improved the Model Extension shared examples for the
|
34
|
+
1. Improved the Model Extension shared examples for the ``ordinal_is`` scope, the .grade_is scope, the .grade_higher_than scope, the .grade_lower_than scope, the .priority_is scope, the .priority_higher_than scope, & the .priority_lower_than scope
|
35
35
|
2. Improved the Model Extension shared examples for the #ordinal validation
|
36
36
|
|
37
37
|
## v0.7.2
|
@@ -40,3 +40,7 @@
|
|
40
40
|
## v0.8
|
41
41
|
1. Monomorphic Scope shared examples
|
42
42
|
2. Polymorphic Scope shared examples
|
43
|
+
|
44
|
+
## v0.9
|
45
|
+
1. Validates Text shared examples
|
46
|
+
2. Validates Numericality shared examples
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -92,7 +92,7 @@ class IdentificationNumberValidator < ActiveModel::EachValidator
|
|
92
92
|
end
|
93
93
|
```
|
94
94
|
|
95
|
-
### Scope shared examples
|
95
|
+
### Scope shared examples 范围共享用例
|
96
96
|
|
97
97
|
The ``person_spec.rb`` looks like the following:
|
98
98
|
If the ``count_diff`` is set to 'E', an error was expected to be raised.
|
@@ -131,7 +131,7 @@ describe Person, type: :model do
|
|
131
131
|
end
|
132
132
|
```
|
133
133
|
|
134
|
-
### Monomorphic Scope shared examples
|
134
|
+
### Monomorphic Scope shared examples 单态范围共享用例
|
135
135
|
|
136
136
|
The ``identity_card_spec.rb`` looks like the following:
|
137
137
|
```ruby
|
@@ -150,7 +150,7 @@ describe IdentityCard, type: :model do
|
|
150
150
|
end
|
151
151
|
```
|
152
152
|
|
153
|
-
### Polymorphic Scope shared examples
|
153
|
+
### Polymorphic Scope shared examples 多态范围共享用例
|
154
154
|
|
155
155
|
The ``pet_spec.rb`` looks like the following:
|
156
156
|
```ruby
|
@@ -169,7 +169,7 @@ describe Pet, type: :model do
|
|
169
169
|
end
|
170
170
|
```
|
171
171
|
|
172
|
-
### Validates shared examples
|
172
|
+
### Validates shared examples 验证规则共享用例
|
173
173
|
|
174
174
|
The ``person_spec.rb`` looks like the following:
|
175
175
|
```ruby
|
@@ -196,7 +196,46 @@ describe Person, type: :model do
|
|
196
196
|
end
|
197
197
|
```
|
198
198
|
|
199
|
-
###
|
199
|
+
### Validates Text shared examples 自由文本验证规则共享用例
|
200
|
+
|
201
|
+
The ``person_spec.rb`` looks like the following:
|
202
|
+
```ruby
|
203
|
+
require 'rails_helper'
|
204
|
+
|
205
|
+
describe Person, type: :model do
|
206
|
+
|
207
|
+
context '.validates' do
|
208
|
+
|
209
|
+
tim_attributes = { name: 'Tim' }
|
210
|
+
|
211
|
+
it_behaves_like 'validates text', tim_attributes, :name, length: 2..columns_hash['name'].limit
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
```
|
217
|
+
|
218
|
+
### Validates Numericality shared examples 数字验证规则共享用例
|
219
|
+
|
220
|
+
The ``person_spec.rb`` looks like the following:
|
221
|
+
```ruby
|
222
|
+
require 'rails_helper'
|
223
|
+
|
224
|
+
describe Person, type: :model do
|
225
|
+
|
226
|
+
context '.validates' do
|
227
|
+
|
228
|
+
tim_attributes = { name: 'Tim', age: 28 }
|
229
|
+
|
230
|
+
it_behaves_like 'validates numericality', tim_attributes, :age,
|
231
|
+
range: 0..150, minimum_inclusive: true, maximum_inclusive: true, only_integer: true
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
```
|
237
|
+
|
238
|
+
### Belongs To shared examples 属于关联共享用例
|
200
239
|
|
201
240
|
The ``pet_spec.rb`` looks like the following:
|
202
241
|
```ruby
|
@@ -234,7 +273,7 @@ describe IdentityCard, type: :model do
|
|
234
273
|
end
|
235
274
|
```
|
236
275
|
|
237
|
-
### Has Many shared examples
|
276
|
+
### Has Many shared examples 拥有多个共享用例
|
238
277
|
|
239
278
|
The ``person_spec.rb`` looks like the following:
|
240
279
|
```ruby
|
@@ -255,7 +294,7 @@ describe Person, type: :model do
|
|
255
294
|
end
|
256
295
|
```
|
257
296
|
|
258
|
-
### Has One shared examples
|
297
|
+
### Has One shared examples 拥有单个共享用例
|
259
298
|
|
260
299
|
The ``person_spec.rb`` looks like the following:
|
261
300
|
```ruby
|
@@ -275,7 +314,7 @@ describe Person, type: :model do
|
|
275
314
|
end
|
276
315
|
```
|
277
316
|
|
278
|
-
### Model Extension shared examples
|
317
|
+
### Model Extension shared examples 模型公共扩展共享用例
|
279
318
|
|
280
319
|
The ``person_spec.rb`` looks like the following:
|
281
320
|
```ruby
|
@@ -295,7 +334,7 @@ end
|
|
295
334
|
```
|
296
335
|
|
297
336
|
|
298
|
-
### Each Validator shared examples
|
337
|
+
### Each Validator shared examples 单属性验证器共享用例
|
299
338
|
|
300
339
|
The ``identification_number_validator_spec.rb`` looks like the following:
|
301
340
|
```ruby
|
data/ROADMAP.md
CHANGED
@@ -25,13 +25,13 @@
|
|
25
25
|
2. Improve the Has One shared examples for the #``build_association`` method, the #``create_association`` method, & the #``create_association!`` method
|
26
26
|
|
27
27
|
## v0.6.2
|
28
|
-
1. Improve the Has Many shared examples for the #``collection.build`` method, the #``collection.create`` method, & the
|
28
|
+
1. Improve the Has Many shared examples for the #``collection.build`` method, the #``collection.create`` method, & the #``collection.create!`` method
|
29
29
|
|
30
30
|
## v0.7
|
31
31
|
1. Each Validator shared examples
|
32
32
|
|
33
33
|
## v0.7.1
|
34
|
-
1. Improve the Model Extension shared examples for the
|
34
|
+
1. Improve the Model Extension shared examples for the ``ordinal_is`` scope, the .grade_is scope, the .grade_higher_than scope, the .grade_lower_than scope, the .priority_is scope, the .priority_higher_than scope, & the .priority_lower_than scope
|
35
35
|
2. Improve the Model Extension shared examples for the #ordinal validation
|
36
36
|
|
37
37
|
## v0.7.2
|
@@ -40,3 +40,7 @@
|
|
40
40
|
## v0.8
|
41
41
|
1. Monomorphic Scope shared examples
|
42
42
|
2. Polymorphic Scope shared examples
|
43
|
+
|
44
|
+
## v0.9
|
45
|
+
1. Validates Text shared examples
|
46
|
+
2. Validates Numericality shared examples
|
@@ -34,3 +34,90 @@ shared_examples 'validates' do |model_attributes, attribute_name, error_attribut
|
|
34
34
|
end
|
35
35
|
|
36
36
|
end
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
shared_examples 'validates text' do |model_attributes, attribute_name, options|
|
41
|
+
|
42
|
+
attribute_name = attribute_name.to_sym
|
43
|
+
presence_validator = described_class.validators.select { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator)&&v.attributes.include?(attribute_name) }.first
|
44
|
+
excluded_validators = described_class.validators.select { |v| v.attributes.include?(attribute_name)&&![ ActiveRecord::Validations::PresenceValidator, ActiveRecord::Validations::LengthValidator ].include?(v.class) }
|
45
|
+
|
46
|
+
if excluded_validators.present?
|
47
|
+
raise ArgumentError.new("The validators on the #{attribute_name.inspect} attribute must be PresenceValidator or LengthValidator. The excluded validator#{excluded_validators.size>1 ? 's are' : ' is'}: #{excluded_validators.inspect}.")
|
48
|
+
end
|
49
|
+
|
50
|
+
attributes_collection = { {} => 0 }
|
51
|
+
|
52
|
+
attributes_collection[ { attribute_name => nil } ] = presence_validator.present? ? 2 : 0
|
53
|
+
attributes_collection[ { attribute_name => '' } ] = presence_validator.present? ? 2 : 0
|
54
|
+
|
55
|
+
minimum_length = options[:length].min
|
56
|
+
maximum_length = options[:length].max
|
57
|
+
|
58
|
+
numbers = ('0'..'9').to_a
|
59
|
+
letters = ('a'..'z').to_a+('A'..'Z').to_a
|
60
|
+
symbols = '~`!@#$%^&*()-_=+{}[];:"\',.<>/?\\|'.chars
|
61
|
+
chars = numbers+letters+symbols
|
62
|
+
|
63
|
+
chars.each do |c| attributes_collection[ { attribute_name => c } ] = 0 end if 1==minimum_length
|
64
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(minimum_length-1) } ] = 1 end if minimum_length>1
|
65
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*minimum_length } ] = 0 end
|
66
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(minimum_length+1) } ] = 0 end if minimum_length<maximum_length
|
67
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(maximum_length-1) } ] = 0 end
|
68
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*maximum_length } ] = 0 end
|
69
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(maximum_length+1) } ] = 1 end
|
70
|
+
|
71
|
+
it_behaves_like 'validates', model_attributes, attribute_name, attributes_collection
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
shared_examples 'validates numericality' do |model_attributes, attribute_name, options|
|
78
|
+
|
79
|
+
attribute_name = attribute_name.to_sym
|
80
|
+
presence_validator = described_class.validators.select { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator)&&v.attributes.include?(attribute_name) }.first
|
81
|
+
excluded_validators = described_class.validators.select { |v| v.attributes.include?(attribute_name)&&![ ActiveRecord::Validations::PresenceValidator, ActiveModel::Validations::NumericalityValidator ].include?(v.class) }
|
82
|
+
|
83
|
+
if excluded_validators.present?
|
84
|
+
raise ArgumentError.new("The validators on the #{attribute_name.inspect} attribute must be PresenceValidator or NumericalityValidator. The excluded validator#{excluded_validators.size>1 ? 's are' : ' is'}: #{excluded_validators.inspect}.")
|
85
|
+
end
|
86
|
+
|
87
|
+
attributes_collection = { {} => 0 }
|
88
|
+
|
89
|
+
attributes_collection[ { attribute_name => nil } ] = presence_validator.present? ? 2 : 0
|
90
|
+
attributes_collection[ { attribute_name => '' } ] = presence_validator.present? ? 2 : 0
|
91
|
+
|
92
|
+
minimum = options[:range].min
|
93
|
+
maximum = options[:range].max
|
94
|
+
minimum_inclusive = options[:minimum_inclusive]
|
95
|
+
maximum_inclusive = options[:maximum_inclusive]
|
96
|
+
only_integer = options[:only_integer]
|
97
|
+
scale = described_class.columns_hash[attribute_name.to_s].scale.to_i
|
98
|
+
unit = only_integer ? 1 : (scale>0 ? (10**-scale).to_f : 0.01)
|
99
|
+
|
100
|
+
attributes_collection[ { attribute_name => minimum-unit } ] = 1
|
101
|
+
attributes_collection[ { attribute_name => minimum } ] = minimum_inclusive ? 0 : 1
|
102
|
+
attributes_collection[ { attribute_name => minimum+unit } ] = 0
|
103
|
+
|
104
|
+
attributes_collection[ { attribute_name => maximum-unit } ] = 0
|
105
|
+
attributes_collection[ { attribute_name => maximum } ] = maximum_inclusive ? 0 : 1
|
106
|
+
attributes_collection[ { attribute_name => maximum+unit } ] = 1
|
107
|
+
|
108
|
+
average = (minimum+maximum)/(only_integer ? 2 : 2.0)
|
109
|
+
attributes_collection[ { attribute_name => average } ] = 0
|
110
|
+
|
111
|
+
if only_integer
|
112
|
+
attributes_collection[ { attribute_name => minimum+0.01 } ] = 1
|
113
|
+
attributes_collection[ { attribute_name => maximum-0.01 } ] = 1
|
114
|
+
end
|
115
|
+
|
116
|
+
letters = ('a'..'z').to_a+('A'..'Z').to_a
|
117
|
+
symbols = '~`!@#$%^&*()-_=+{}[];:"\',.<>/?\\|'.chars
|
118
|
+
|
119
|
+
(letters+symbols).each do |c| attributes_collection[ { attribute_name => c } ] = 1 end
|
120
|
+
|
121
|
+
it_behaves_like 'validates', model_attributes, attribute_name, attributes_collection
|
122
|
+
|
123
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unidom-common-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.9'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Topbit Du
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|