unidom-common-rspec 0.10 → 0.10.1
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 +16 -16
- data/ROADMAP.md +6 -2
- data/lib/unidom/common/rspec.rb +5 -0
- data/lib/unidom/common/rspec/monomorphic_scope_shared_examples.rb +17 -0
- data/lib/unidom/common/rspec/polymorphic_scope_shared_examples.rb +23 -0
- data/lib/unidom/common/rspec/scope_shared_examples.rb +0 -46
- data/lib/unidom/common/rspec/validates_numericality_shared_examples.rb +47 -0
- data/lib/unidom/common/rspec/validates_shared_examples.rb +0 -94
- data/lib/unidom/common/rspec/validates_text_shared_examples.rb +41 -0
- data/lib/unidom/common/rspec/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3d845d151cd31eea09c477c6480c4d449a7613f
|
4
|
+
data.tar.gz: ca78b9f4cac3f8ffd4bd2b7d2f0db1c713d48c1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a270c233748a619668cf878e86a9dd50c39b8ed09676b5ea7fdd32452237c7f86601c811fd21d81b00ce2eeb0074454755b41bbd2bf0c296093c7db1ac39177
|
7
|
+
data.tar.gz: 0ce16a83944dca17202ec40ff0f801a62dded783fe5686fad4ba395540ad36c11bfbb1f60f8bd0fd584adc822f9454119b138ad397535518fb283161e9a44e44
|
data/CHANGELOG.md
CHANGED
@@ -31,8 +31,8 @@
|
|
31
31
|
1. Each Validator shared examples
|
32
32
|
|
33
33
|
## v0.7.1
|
34
|
-
1. Improved the Model Extension shared examples for the ``ordinal_is`` scope, the ``grade_is`` scope, the
|
35
|
-
2. 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
|
+
2. Improved the Model Extension shared examples for the validations on the #``ordinal`` attribute
|
36
36
|
|
37
37
|
## v0.7.2
|
38
38
|
1. Improved the Validates shared examples for the failure message
|
@@ -50,3 +50,7 @@
|
|
50
50
|
|
51
51
|
## v0.10
|
52
52
|
1. Assert Present shared examples
|
53
|
+
|
54
|
+
## v0.10.1
|
55
|
+
1. Improve the Scope shared examples to separate the Monomorphic Scope shared examples, & the Polymorphic Scope shared examples
|
56
|
+
2. Improve the Validates shared examples to separate the Validates Text shared examples, & the Validates Numericality shared examples
|
data/Gemfile.lock
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
unidom-common-rspec (0.
|
4
|
+
unidom-common-rspec (0.10)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
actionpack (5.0.
|
10
|
-
actionview (= 5.0.
|
11
|
-
activesupport (= 5.0.
|
9
|
+
actionpack (5.0.2)
|
10
|
+
actionview (= 5.0.2)
|
11
|
+
activesupport (= 5.0.2)
|
12
12
|
rack (~> 2.0)
|
13
13
|
rack-test (~> 0.6.3)
|
14
14
|
rails-dom-testing (~> 2.0)
|
15
15
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
16
|
-
actionview (5.0.
|
17
|
-
activesupport (= 5.0.
|
16
|
+
actionview (5.0.2)
|
17
|
+
activesupport (= 5.0.2)
|
18
18
|
builder (~> 3.1)
|
19
19
|
erubis (~> 2.7.0)
|
20
20
|
rails-dom-testing (~> 2.0)
|
21
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.
|
22
|
-
activesupport (5.0.
|
21
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
22
|
+
activesupport (5.0.2)
|
23
23
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
24
|
i18n (~> 0.7)
|
25
25
|
minitest (~> 5.1)
|
26
26
|
tzinfo (~> 1.1)
|
27
27
|
builder (3.2.3)
|
28
|
-
concurrent-ruby (1.0.
|
28
|
+
concurrent-ruby (1.0.5)
|
29
29
|
diff-lcs (1.3)
|
30
30
|
erubis (2.7.0)
|
31
|
-
i18n (0.8.
|
31
|
+
i18n (0.8.1)
|
32
32
|
loofah (2.0.3)
|
33
33
|
nokogiri (>= 1.5.9)
|
34
34
|
method_source (0.8.2)
|
35
35
|
mini_portile2 (2.1.0)
|
36
36
|
minitest (5.10.1)
|
37
|
-
nokogiri (1.7.
|
37
|
+
nokogiri (1.7.1)
|
38
38
|
mini_portile2 (~> 2.1.0)
|
39
39
|
rack (2.0.1)
|
40
40
|
rack-test (0.6.3)
|
@@ -44,9 +44,9 @@ GEM
|
|
44
44
|
nokogiri (~> 1.6)
|
45
45
|
rails-html-sanitizer (1.0.3)
|
46
46
|
loofah (~> 2.0)
|
47
|
-
railties (5.0.
|
48
|
-
actionpack (= 5.0.
|
49
|
-
activesupport (= 5.0.
|
47
|
+
railties (5.0.2)
|
48
|
+
actionpack (= 5.0.2)
|
49
|
+
activesupport (= 5.0.2)
|
50
50
|
method_source
|
51
51
|
rake (>= 0.8.7)
|
52
52
|
thor (>= 0.18.1, < 2.0)
|
@@ -73,8 +73,8 @@ GEM
|
|
73
73
|
rspec-support (~> 3.5.0)
|
74
74
|
rspec-support (3.5.0)
|
75
75
|
thor (0.19.4)
|
76
|
-
thread_safe (0.3.
|
77
|
-
tzinfo (1.2.
|
76
|
+
thread_safe (0.3.6)
|
77
|
+
tzinfo (1.2.3)
|
78
78
|
thread_safe (~> 0.1)
|
79
79
|
|
80
80
|
PLATFORMS
|
data/ROADMAP.md
CHANGED
@@ -31,8 +31,8 @@
|
|
31
31
|
1. Each Validator shared examples
|
32
32
|
|
33
33
|
## v0.7.1
|
34
|
-
1. Improve the Model Extension shared examples for the ``ordinal_is`` scope, the ``grade_is`` scope, the
|
35
|
-
2. 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
|
+
2. Improve the Model Extension shared examples for the validations on the #``ordinal`` attribute
|
36
36
|
|
37
37
|
## v0.7.2
|
38
38
|
1. Improve the Validates shared examples for the failure message
|
@@ -50,3 +50,7 @@
|
|
50
50
|
|
51
51
|
## v0.10
|
52
52
|
1. Assert Present shared examples
|
53
|
+
|
54
|
+
## v0.10.1
|
55
|
+
1. Improve the Scope shared examples to separate the Monomorphic Scope shared examples, & the Polymorphic Scope shared examples
|
56
|
+
2. Improve the Validates shared examples to separate the Validates Text shared examples, & the Validates Numericality shared examples
|
data/lib/unidom/common/rspec.rb
CHANGED
@@ -5,7 +5,12 @@ require 'unidom/common/rspec/assert_present_shared_examples'
|
|
5
5
|
require 'unidom/common/rspec/each_validator_shared_examples'
|
6
6
|
|
7
7
|
require 'unidom/common/rspec/scope_shared_examples'
|
8
|
+
require 'unidom/common/rspec/monomorphic_scope_shared_examples'
|
9
|
+
require 'unidom/common/rspec/polymorphic_scope_shared_examples'
|
10
|
+
|
8
11
|
require 'unidom/common/rspec/validates_shared_examples'
|
12
|
+
require 'unidom/common/rspec/validates_numericality_shared_examples'
|
13
|
+
require 'unidom/common/rspec/validates_text_shared_examples'
|
9
14
|
|
10
15
|
require 'unidom/common/rspec/belongs_to_shared_examples'
|
11
16
|
require 'unidom/common/rspec/has_many_shared_examples'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
shared_examples 'monomorphic scope' do |model_attributes, scope_name, association_name|
|
2
|
+
|
3
|
+
attribute_name = :"#{association_name}_id"
|
4
|
+
reflection = described_class.reflections[association_name.to_s]
|
5
|
+
association_class = reflection.options[:class_name].safe_constantize
|
6
|
+
association_id = model_attributes[attribute_name]
|
7
|
+
association_instance = association_class.new id: association_id
|
8
|
+
variant_attribtues = model_attributes.merge attribute_name => Unidom::Common::NULL_UUID
|
9
|
+
|
10
|
+
it_behaves_like 'scope', scope_name, [
|
11
|
+
{ attributes_collection: [ model_attributes ], count_diff: 1, args: [ association_id ] },
|
12
|
+
{ attributes_collection: [ model_attributes ], count_diff: 1, args: [ association_instance ] },
|
13
|
+
{ attributes_collection: [ variant_attribtues ], count_diff: 0, args: [ association_id ] },
|
14
|
+
{ attributes_collection: [ variant_attribtues ], count_diff: 0, args: [ association_instance ] }
|
15
|
+
]
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
shared_examples 'polymorphic scope' do |model_attributes, scope_name, association_name, association_class_list|
|
2
|
+
|
3
|
+
attribute_name = :"#{association_name}_id"
|
4
|
+
reflection = described_class.reflections[association_name.to_s]
|
5
|
+
association_id = model_attributes[attribute_name]
|
6
|
+
|
7
|
+
association_class_list.each do |association_class|
|
8
|
+
|
9
|
+
association_instance = association_class.new id: association_id
|
10
|
+
variant_instance = association_class.new id: Unidom::Common::NULL_UUID
|
11
|
+
model_attributes = model_attributes.merge :"#{association_name}_type" => association_class.name
|
12
|
+
variant_attribtues = model_attributes.merge attribute_name => Unidom::Common::NULL_UUID
|
13
|
+
|
14
|
+
it_behaves_like 'scope', scope_name, [
|
15
|
+
{ attributes_collection: [ model_attributes ], count_diff: 1, args: [ association_instance ] },
|
16
|
+
{ attributes_collection: [ model_attributes ], count_diff: 0, args: [ variant_instance ] },
|
17
|
+
{ attributes_collection: [ variant_attribtues ], count_diff: 0, args: [ association_instance ] },
|
18
|
+
{ attributes_collection: [ variant_attribtues ], count_diff: 1, args: [ variant_instance ] }
|
19
|
+
]
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -29,49 +29,3 @@ shared_examples 'scope' do |scope_name, fixtures|
|
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
shared_examples 'monomorphic scope' do |model_attributes, scope_name, association_name|
|
36
|
-
|
37
|
-
attribute_name = :"#{association_name}_id"
|
38
|
-
reflection = described_class.reflections[association_name.to_s]
|
39
|
-
association_class = reflection.options[:class_name].safe_constantize
|
40
|
-
association_id = model_attributes[attribute_name]
|
41
|
-
association_instance = association_class.new id: association_id
|
42
|
-
variant_attribtues = model_attributes.merge attribute_name => Unidom::Common::NULL_UUID
|
43
|
-
|
44
|
-
it_behaves_like 'scope', scope_name, [
|
45
|
-
{ attributes_collection: [ model_attributes ], count_diff: 1, args: [ association_id ] },
|
46
|
-
{ attributes_collection: [ model_attributes ], count_diff: 1, args: [ association_instance ] },
|
47
|
-
{ attributes_collection: [ variant_attribtues ], count_diff: 0, args: [ association_id ] },
|
48
|
-
{ attributes_collection: [ variant_attribtues ], count_diff: 0, args: [ association_instance ] }
|
49
|
-
]
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
shared_examples 'polymorphic scope' do |model_attributes, scope_name, association_name, association_class_list|
|
56
|
-
|
57
|
-
attribute_name = :"#{association_name}_id"
|
58
|
-
reflection = described_class.reflections[association_name.to_s]
|
59
|
-
association_id = model_attributes[attribute_name]
|
60
|
-
|
61
|
-
association_class_list.each do |association_class|
|
62
|
-
|
63
|
-
association_instance = association_class.new id: association_id
|
64
|
-
variant_instance = association_class.new id: Unidom::Common::NULL_UUID
|
65
|
-
model_attributes = model_attributes.merge :"#{association_name}_type" => association_class.name
|
66
|
-
variant_attribtues = model_attributes.merge attribute_name => Unidom::Common::NULL_UUID
|
67
|
-
|
68
|
-
it_behaves_like 'scope', scope_name, [
|
69
|
-
{ attributes_collection: [ model_attributes ], count_diff: 1, args: [ association_instance ] },
|
70
|
-
{ attributes_collection: [ model_attributes ], count_diff: 0, args: [ variant_instance ] },
|
71
|
-
{ attributes_collection: [ variant_attribtues ], count_diff: 0, args: [ association_instance ] },
|
72
|
-
{ attributes_collection: [ variant_attribtues ], count_diff: 1, args: [ variant_instance ] }
|
73
|
-
]
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
shared_examples 'validates numericality' do |model_attributes, attribute_name, options|
|
2
|
+
|
3
|
+
attribute_name = attribute_name.to_sym
|
4
|
+
presence_validator = described_class.validators.select { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator)&&v.attributes.include?(attribute_name) }.first
|
5
|
+
excluded_validators = described_class.validators.select { |v| v.attributes.include?(attribute_name)&&![ ActiveRecord::Validations::PresenceValidator, ActiveModel::Validations::NumericalityValidator ].include?(v.class) }
|
6
|
+
|
7
|
+
if excluded_validators.present?
|
8
|
+
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}.")
|
9
|
+
end
|
10
|
+
|
11
|
+
attributes_collection = { {} => 0 }
|
12
|
+
|
13
|
+
attributes_collection[ { attribute_name => nil } ] = presence_validator.present? ? 2 : 0
|
14
|
+
attributes_collection[ { attribute_name => '' } ] = presence_validator.present? ? 2 : 0
|
15
|
+
|
16
|
+
minimum = options[:range].min
|
17
|
+
maximum = options[:range].max
|
18
|
+
minimum_inclusive = options[:minimum_inclusive]
|
19
|
+
maximum_inclusive = options[:maximum_inclusive]
|
20
|
+
only_integer = options[:only_integer]
|
21
|
+
scale = described_class.columns_hash[attribute_name.to_s].scale.to_i
|
22
|
+
unit = only_integer ? 1 : (scale>0 ? (10**-scale).to_f : 0.01)
|
23
|
+
|
24
|
+
attributes_collection[ { attribute_name => minimum-unit } ] = 1
|
25
|
+
attributes_collection[ { attribute_name => minimum } ] = minimum_inclusive ? 0 : 1
|
26
|
+
attributes_collection[ { attribute_name => minimum+unit } ] = 0
|
27
|
+
|
28
|
+
attributes_collection[ { attribute_name => maximum-unit } ] = 0
|
29
|
+
attributes_collection[ { attribute_name => maximum } ] = maximum_inclusive ? 0 : 1
|
30
|
+
attributes_collection[ { attribute_name => maximum+unit } ] = 1
|
31
|
+
|
32
|
+
average = (minimum+maximum)/(only_integer ? 2 : 2.0)
|
33
|
+
attributes_collection[ { attribute_name => average } ] = 0
|
34
|
+
|
35
|
+
if only_integer
|
36
|
+
attributes_collection[ { attribute_name => minimum+0.01 } ] = 1
|
37
|
+
attributes_collection[ { attribute_name => maximum-0.01 } ] = 1
|
38
|
+
end
|
39
|
+
|
40
|
+
letters = ('a'..'z').to_a+('A'..'Z').to_a
|
41
|
+
symbols = '~`!@#$%^&*()-_=+{}[];:"\',.<>/?\\|'.chars
|
42
|
+
|
43
|
+
(letters+symbols).each do |c| attributes_collection[ { attribute_name => c } ] = 1 end
|
44
|
+
|
45
|
+
it_behaves_like 'validates', model_attributes, attribute_name, attributes_collection
|
46
|
+
|
47
|
+
end
|
@@ -34,97 +34,3 @@ 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
|
-
if minimum_length==maximum_length
|
64
|
-
length = maximum_length
|
65
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*(length-1) } ] = 1 end
|
66
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*length } ] = 0 end
|
67
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*(length+1) } ] = 1 end
|
68
|
-
else
|
69
|
-
chars.each do |c| attributes_collection[ { attribute_name => c } ] = 0 end if 1==minimum_length
|
70
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*(minimum_length-1) } ] = 1 end if minimum_length>1
|
71
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*minimum_length } ] = 0 end
|
72
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*(minimum_length+1) } ] = 0 end if minimum_length<maximum_length
|
73
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*(maximum_length-1) } ] = 0 end
|
74
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*maximum_length } ] = 0 end
|
75
|
-
chars.each do |c| attributes_collection[ { attribute_name => c*(maximum_length+1) } ] = 1 end
|
76
|
-
end
|
77
|
-
|
78
|
-
it_behaves_like 'validates', model_attributes, attribute_name, attributes_collection
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
shared_examples 'validates numericality' do |model_attributes, attribute_name, options|
|
85
|
-
|
86
|
-
attribute_name = attribute_name.to_sym
|
87
|
-
presence_validator = described_class.validators.select { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator)&&v.attributes.include?(attribute_name) }.first
|
88
|
-
excluded_validators = described_class.validators.select { |v| v.attributes.include?(attribute_name)&&![ ActiveRecord::Validations::PresenceValidator, ActiveModel::Validations::NumericalityValidator ].include?(v.class) }
|
89
|
-
|
90
|
-
if excluded_validators.present?
|
91
|
-
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}.")
|
92
|
-
end
|
93
|
-
|
94
|
-
attributes_collection = { {} => 0 }
|
95
|
-
|
96
|
-
attributes_collection[ { attribute_name => nil } ] = presence_validator.present? ? 2 : 0
|
97
|
-
attributes_collection[ { attribute_name => '' } ] = presence_validator.present? ? 2 : 0
|
98
|
-
|
99
|
-
minimum = options[:range].min
|
100
|
-
maximum = options[:range].max
|
101
|
-
minimum_inclusive = options[:minimum_inclusive]
|
102
|
-
maximum_inclusive = options[:maximum_inclusive]
|
103
|
-
only_integer = options[:only_integer]
|
104
|
-
scale = described_class.columns_hash[attribute_name.to_s].scale.to_i
|
105
|
-
unit = only_integer ? 1 : (scale>0 ? (10**-scale).to_f : 0.01)
|
106
|
-
|
107
|
-
attributes_collection[ { attribute_name => minimum-unit } ] = 1
|
108
|
-
attributes_collection[ { attribute_name => minimum } ] = minimum_inclusive ? 0 : 1
|
109
|
-
attributes_collection[ { attribute_name => minimum+unit } ] = 0
|
110
|
-
|
111
|
-
attributes_collection[ { attribute_name => maximum-unit } ] = 0
|
112
|
-
attributes_collection[ { attribute_name => maximum } ] = maximum_inclusive ? 0 : 1
|
113
|
-
attributes_collection[ { attribute_name => maximum+unit } ] = 1
|
114
|
-
|
115
|
-
average = (minimum+maximum)/(only_integer ? 2 : 2.0)
|
116
|
-
attributes_collection[ { attribute_name => average } ] = 0
|
117
|
-
|
118
|
-
if only_integer
|
119
|
-
attributes_collection[ { attribute_name => minimum+0.01 } ] = 1
|
120
|
-
attributes_collection[ { attribute_name => maximum-0.01 } ] = 1
|
121
|
-
end
|
122
|
-
|
123
|
-
letters = ('a'..'z').to_a+('A'..'Z').to_a
|
124
|
-
symbols = '~`!@#$%^&*()-_=+{}[];:"\',.<>/?\\|'.chars
|
125
|
-
|
126
|
-
(letters+symbols).each do |c| attributes_collection[ { attribute_name => c } ] = 1 end
|
127
|
-
|
128
|
-
it_behaves_like 'validates', model_attributes, attribute_name, attributes_collection
|
129
|
-
|
130
|
-
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
shared_examples 'validates text' do |model_attributes, attribute_name, options|
|
2
|
+
|
3
|
+
attribute_name = attribute_name.to_sym
|
4
|
+
presence_validator = described_class.validators.select { |v| v.is_a?(ActiveRecord::Validations::PresenceValidator)&&v.attributes.include?(attribute_name) }.first
|
5
|
+
excluded_validators = described_class.validators.select { |v| v.attributes.include?(attribute_name)&&![ ActiveRecord::Validations::PresenceValidator, ActiveRecord::Validations::LengthValidator ].include?(v.class) }
|
6
|
+
|
7
|
+
if excluded_validators.present?
|
8
|
+
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}.")
|
9
|
+
end
|
10
|
+
|
11
|
+
attributes_collection = { {} => 0 }
|
12
|
+
|
13
|
+
attributes_collection[ { attribute_name => nil } ] = presence_validator.present? ? 2 : 0
|
14
|
+
attributes_collection[ { attribute_name => '' } ] = presence_validator.present? ? 2 : 0
|
15
|
+
|
16
|
+
minimum_length = options[:length].min
|
17
|
+
maximum_length = options[:length].max
|
18
|
+
|
19
|
+
numbers = ('0'..'9').to_a
|
20
|
+
letters = ('a'..'z').to_a+('A'..'Z').to_a
|
21
|
+
symbols = '~`!@#$%^&*()-_=+{}[];:"\',.<>/?\\|'.chars
|
22
|
+
chars = numbers+letters+symbols
|
23
|
+
|
24
|
+
if minimum_length==maximum_length
|
25
|
+
length = maximum_length
|
26
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(length-1) } ] = 1 end
|
27
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*length } ] = 0 end
|
28
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(length+1) } ] = 1 end
|
29
|
+
else
|
30
|
+
chars.each do |c| attributes_collection[ { attribute_name => c } ] = 0 end if 1==minimum_length
|
31
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(minimum_length-1) } ] = 1 end if minimum_length>1
|
32
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*minimum_length } ] = 0 end
|
33
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(minimum_length+1) } ] = 0 end if minimum_length<maximum_length
|
34
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(maximum_length-1) } ] = 0 end
|
35
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*maximum_length } ] = 0 end
|
36
|
+
chars.each do |c| attributes_collection[ { attribute_name => c*(maximum_length+1) } ] = 1 end
|
37
|
+
end
|
38
|
+
|
39
|
+
it_behaves_like 'validates', model_attributes, attribute_name, attributes_collection
|
40
|
+
|
41
|
+
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:
|
4
|
+
version: 0.10.1
|
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-
|
11
|
+
date: 2017-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -94,8 +94,12 @@ files:
|
|
94
94
|
- lib/unidom/common/rspec/has_many_shared_examples.rb
|
95
95
|
- lib/unidom/common/rspec/has_one_shared_examples.rb
|
96
96
|
- lib/unidom/common/rspec/model_extension_shared_examples.rb
|
97
|
+
- lib/unidom/common/rspec/monomorphic_scope_shared_examples.rb
|
98
|
+
- lib/unidom/common/rspec/polymorphic_scope_shared_examples.rb
|
97
99
|
- lib/unidom/common/rspec/scope_shared_examples.rb
|
100
|
+
- lib/unidom/common/rspec/validates_numericality_shared_examples.rb
|
98
101
|
- lib/unidom/common/rspec/validates_shared_examples.rb
|
102
|
+
- lib/unidom/common/rspec/validates_text_shared_examples.rb
|
99
103
|
- lib/unidom/common/rspec/version.rb
|
100
104
|
- unidom-common-rspec.gemspec
|
101
105
|
homepage: https://github.com/topbitdu/unidom-common-rspec
|