shoulda-matchers 5.1.0 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +41 -18
- data/lib/shoulda/matchers/action_controller/permit_matcher.rb +7 -9
- data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +1 -1
- data/lib/shoulda/matchers/action_controller/set_session_or_flash_matcher.rb +13 -15
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +46 -1
- data/lib/shoulda/matchers/active_model/comparison_matcher.rb +157 -0
- data/lib/shoulda/matchers/active_model/have_secure_password_matcher.rb +7 -0
- data/lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb +3 -5
- data/lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb +71 -0
- data/lib/shoulda/matchers/active_model/numericality_matchers/submatchers.rb +53 -0
- data/lib/shoulda/matchers/active_model/qualifiers/allow_blank.rb +26 -0
- data/lib/shoulda/matchers/active_model/qualifiers.rb +1 -0
- data/lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb +2 -7
- data/lib/shoulda/matchers/active_model/validate_comparison_of_matcher.rb +532 -0
- data/lib/shoulda/matchers/active_model/validate_exclusion_of_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +29 -14
- data/lib/shoulda/matchers/active_model/validate_length_of_matcher.rb +65 -10
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +76 -86
- data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +29 -4
- data/lib/shoulda/matchers/active_model/validation_matcher/build_description.rb +6 -7
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +6 -0
- data/lib/shoulda/matchers/active_model/validator.rb +4 -0
- data/lib/shoulda/matchers/active_model.rb +4 -1
- data/lib/shoulda/matchers/active_record/association_matcher.rb +543 -15
- data/lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb +34 -4
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +9 -1
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +1 -0
- data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +4 -0
- data/lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb +23 -19
- data/lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb +27 -23
- data/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb +338 -30
- data/lib/shoulda/matchers/active_record/encrypt_matcher.rb +174 -0
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +46 -6
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +24 -13
- data/lib/shoulda/matchers/active_record/have_implicit_order_column.rb +3 -5
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_record/normalize_matcher.rb +151 -0
- data/lib/shoulda/matchers/active_record/uniqueness/model.rb +13 -1
- data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +82 -70
- data/lib/shoulda/matchers/active_record.rb +2 -0
- data/lib/shoulda/matchers/doublespeak/double_collection.rb +2 -6
- data/lib/shoulda/matchers/doublespeak/world.rb +2 -6
- data/lib/shoulda/matchers/doublespeak.rb +0 -1
- data/lib/shoulda/matchers/independent/delegate_method_matcher.rb +13 -15
- data/lib/shoulda/matchers/integrations/libraries/action_controller.rb +7 -5
- data/lib/shoulda/matchers/integrations/libraries/routing.rb +5 -3
- data/lib/shoulda/matchers/rails_shim.rb +22 -6
- data/lib/shoulda/matchers/util/word_wrap.rb +2 -2
- data/lib/shoulda/matchers/util.rb +18 -20
- data/lib/shoulda/matchers/version.rb +1 -1
- data/lib/shoulda/matchers.rb +2 -2
- data/shoulda-matchers.gemspec +2 -2
- metadata +15 -9
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +0 -157
@@ -19,10 +19,7 @@ module Shoulda
|
|
19
19
|
def matches?(subject)
|
20
20
|
self.subject = ModelReflector.new(subject, name)
|
21
21
|
|
22
|
-
if
|
23
|
-
:counter_cache,
|
24
|
-
counter_cache,
|
25
|
-
)
|
22
|
+
if correct_value?
|
26
23
|
true
|
27
24
|
else
|
28
25
|
self.missing_option = "#{name} should have #{description}"
|
@@ -34,9 +31,42 @@ module Shoulda
|
|
34
31
|
|
35
32
|
attr_accessor :subject, :counter_cache, :name
|
36
33
|
|
34
|
+
def correct_value?
|
35
|
+
expected = normalize_value
|
36
|
+
|
37
|
+
if expected.is_a?(Hash)
|
38
|
+
option_verifier.correct_for_hash?(
|
39
|
+
:counter_cache,
|
40
|
+
expected,
|
41
|
+
)
|
42
|
+
else
|
43
|
+
option_verifier.correct_for_string?(
|
44
|
+
:counter_cache,
|
45
|
+
expected,
|
46
|
+
)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
37
50
|
def option_verifier
|
38
51
|
@_option_verifier ||= OptionVerifier.new(subject)
|
39
52
|
end
|
53
|
+
|
54
|
+
def normalize_value
|
55
|
+
if Rails::VERSION::STRING >= '7.2'
|
56
|
+
case counter_cache
|
57
|
+
when true
|
58
|
+
{ active: true, column: nil }
|
59
|
+
when String, Symbol
|
60
|
+
{ active: true, column: counter_cache.to_s }
|
61
|
+
when Hash
|
62
|
+
{ active: true, column: nil }.merge(counter_cache)
|
63
|
+
else
|
64
|
+
raise ArgumentError, 'Invalid counter_cache option'
|
65
|
+
end
|
66
|
+
else
|
67
|
+
counter_cache
|
68
|
+
end
|
69
|
+
end
|
40
70
|
end
|
41
71
|
end
|
42
72
|
end
|
@@ -13,7 +13,11 @@ module Shoulda
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def associated_class
|
16
|
-
|
16
|
+
if polymorphic?
|
17
|
+
subject
|
18
|
+
else
|
19
|
+
reflection.klass
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
def polymorphic?
|
@@ -70,6 +74,10 @@ module Shoulda
|
|
70
74
|
reflection.options[:through]
|
71
75
|
end
|
72
76
|
|
77
|
+
def strict_loading?
|
78
|
+
reflection.options.fetch(:strict_loading, subject.strict_loading_by_default)
|
79
|
+
end
|
80
|
+
|
73
81
|
protected
|
74
82
|
|
75
83
|
attr_reader :reflection, :subject
|
@@ -22,44 +22,48 @@ module Shoulda
|
|
22
22
|
if submatcher_passes?(subject)
|
23
23
|
true
|
24
24
|
else
|
25
|
-
@missing_option =
|
25
|
+
@missing_option = build_missing_option
|
26
26
|
|
27
|
-
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
attr_reader :attribute_name, :optional, :submatcher
|
34
|
+
|
35
|
+
def submatcher_passes?(subject)
|
36
|
+
if optional
|
37
|
+
submatcher.matches?(subject)
|
38
|
+
else
|
39
|
+
submatcher.does_not_match?(subject)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_missing_option
|
44
|
+
String.new('and for the record ').tap do |missing_option_string|
|
45
|
+
missing_option_string <<
|
28
46
|
if optional
|
29
47
|
'not to '
|
30
48
|
else
|
31
49
|
'to '
|
32
50
|
end
|
33
51
|
|
34
|
-
|
52
|
+
missing_option_string << (
|
35
53
|
'fail validation if '\
|
36
54
|
":#{attribute_name} is unset; i.e., either the association "\
|
37
55
|
'should have been defined with `optional: '\
|
38
56
|
"#{optional.inspect}`, or there "
|
39
57
|
)
|
40
58
|
|
41
|
-
|
59
|
+
missing_option_string <<
|
42
60
|
if optional
|
43
61
|
'should not '
|
44
62
|
else
|
45
63
|
'should '
|
46
64
|
end
|
47
65
|
|
48
|
-
|
49
|
-
|
50
|
-
false
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
attr_reader :attribute_name, :optional, :submatcher
|
57
|
-
|
58
|
-
def submatcher_passes?(subject)
|
59
|
-
if optional
|
60
|
-
submatcher.matches?(subject)
|
61
|
-
else
|
62
|
-
submatcher.does_not_match?(subject)
|
66
|
+
missing_option_string << "be a presence validation on :#{attribute_name}"
|
63
67
|
end
|
64
68
|
end
|
65
69
|
end
|
@@ -23,50 +23,54 @@ module Shoulda
|
|
23
23
|
if submatcher_passes?(subject)
|
24
24
|
true
|
25
25
|
else
|
26
|
-
@missing_option =
|
26
|
+
@missing_option = build_missing_option
|
27
27
|
|
28
|
-
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :attribute_name, :required, :submatcher
|
35
|
+
|
36
|
+
def submatcher_passes?(subject)
|
37
|
+
if required
|
38
|
+
submatcher.matches?(subject)
|
39
|
+
else
|
40
|
+
submatcher.does_not_match?(subject)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def validation_message_key
|
45
|
+
:required
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_missing_option
|
49
|
+
String.new('and for the record ').tap do |missing_option_string|
|
50
|
+
missing_option_string <<
|
29
51
|
if required
|
30
52
|
'to '
|
31
53
|
else
|
32
54
|
'not to '
|
33
55
|
end
|
34
56
|
|
35
|
-
|
57
|
+
missing_option_string << (
|
36
58
|
'fail validation if '\
|
37
59
|
":#{attribute_name} is unset; i.e., either the association "\
|
38
60
|
'should have been defined with `required: '\
|
39
61
|
"#{required.inspect}`, or there "
|
40
62
|
)
|
41
63
|
|
42
|
-
|
64
|
+
missing_option_string <<
|
43
65
|
if required
|
44
66
|
'should '
|
45
67
|
else
|
46
68
|
'should not '
|
47
69
|
end
|
48
70
|
|
49
|
-
|
50
|
-
|
51
|
-
false
|
71
|
+
missing_option_string << "be a presence validation on :#{attribute_name}"
|
52
72
|
end
|
53
73
|
end
|
54
|
-
|
55
|
-
private
|
56
|
-
|
57
|
-
attr_reader :attribute_name, :required, :submatcher
|
58
|
-
|
59
|
-
def submatcher_passes?(subject)
|
60
|
-
if required
|
61
|
-
submatcher.matches?(subject)
|
62
|
-
else
|
63
|
-
submatcher.does_not_match?(subject)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def validation_message_key
|
68
|
-
:required
|
69
|
-
end
|
70
74
|
end
|
71
75
|
end
|
72
76
|
end
|