shoulda-matchers 1.5.4 → 1.5.5
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.
- data/CONTRIBUTING.md +4 -1
- data/Gemfile.lock +1 -1
- data/NEWS.md +10 -0
- data/gemfiles/3.0.gemfile.lock +1 -1
- data/gemfiles/3.1.gemfile.lock +1 -1
- data/gemfiles/3.2.gemfile.lock +1 -1
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +43 -29
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +9 -0
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +17 -4
- data/lib/shoulda/matchers/active_record/association_matcher.rb +6 -5
- data/lib/shoulda/matchers/version.rb +1 -1
- data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +4 -0
- data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +10 -1
- data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +6 -0
- data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +33 -13
- metadata +4 -4
data/CONTRIBUTING.md
CHANGED
@@ -11,7 +11,10 @@ a test!
|
|
11
11
|
|
12
12
|
4. Make the test pass.
|
13
13
|
|
14
|
-
5.
|
14
|
+
5. Update NEWS.md with a brief description of your changes under the HEAD
|
15
|
+
heading.
|
16
|
+
|
17
|
+
6. Push to your fork and submit a pull request.
|
15
18
|
|
16
19
|
|
17
20
|
At this point you're waiting on us. We like to at least comment on, if not
|
data/Gemfile.lock
CHANGED
data/NEWS.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# HEAD
|
2
2
|
|
3
|
+
# v 1.5.5
|
4
|
+
* `AllowValueMatcher` checks that the right value is used for attempts at
|
5
|
+
setting the attribute with it
|
6
|
+
* Please note that previously-passing tests might now fail. It is likely that
|
7
|
+
it's not a bug, but please make sure that the code you're testing is written
|
8
|
+
properly before submitting an issue.
|
9
|
+
* Use DisallowValueMatcher for `disallows_value_of` method
|
10
|
+
* Assert `class_name` value on real class name for `AssociationMatcher`
|
11
|
+
* Correct the variable used for `validate_confirmation_of` matcher description
|
12
|
+
|
3
13
|
# v 1.5.4
|
4
14
|
* Properly-released version of 1.5.3
|
5
15
|
|
data/gemfiles/3.0.gemfile.lock
CHANGED
data/gemfiles/3.1.gemfile.lock
CHANGED
data/gemfiles/3.2.gemfile.lock
CHANGED
@@ -30,38 +30,40 @@ module Shoulda # :nodoc:
|
|
30
30
|
class AllowValueMatcher # :nodoc:
|
31
31
|
include Helpers
|
32
32
|
|
33
|
+
attr_accessor :options
|
34
|
+
|
33
35
|
def initialize(*values)
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
self.values_to_match = values
|
37
|
+
self.message_finder_factory = ValidationMessageFinder
|
38
|
+
self.options = {}
|
37
39
|
end
|
38
40
|
|
39
41
|
def for(attribute)
|
40
|
-
|
42
|
+
self.attribute = attribute
|
41
43
|
self
|
42
44
|
end
|
43
45
|
|
44
46
|
def with_message(message)
|
45
|
-
|
47
|
+
self.options[:expected_message] = message
|
46
48
|
self
|
47
49
|
end
|
48
50
|
|
49
51
|
def strict
|
50
|
-
|
52
|
+
self.message_finder_factory = ExceptionMessageFinder
|
51
53
|
self
|
52
54
|
end
|
53
55
|
|
54
56
|
def matches?(instance)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
self.instance = instance
|
58
|
+
|
59
|
+
values_to_match.all? do |current_value|
|
60
|
+
set_attribute_on_instance(current_value)
|
61
|
+
matches_attribute_value?(current_value) && errors_do_not_match?
|
60
62
|
end
|
61
63
|
end
|
62
64
|
|
63
65
|
def failure_message_for_should
|
64
|
-
"Did not expect #{expectation}, got error: #{
|
66
|
+
"Did not expect #{expectation}, got error: #{matched_error}"
|
65
67
|
end
|
66
68
|
|
67
69
|
def failure_message_for_should_not
|
@@ -74,17 +76,29 @@ module Shoulda # :nodoc:
|
|
74
76
|
|
75
77
|
private
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
+
attr_accessor :values_to_match, :message_finder_factory,
|
80
|
+
:instance, :attribute, :value, :matched_error
|
81
|
+
|
82
|
+
def set_attribute_on_instance(current_value)
|
83
|
+
self.value = current_value
|
84
|
+
instance.send("#{attribute}=", current_value)
|
85
|
+
end
|
86
|
+
|
87
|
+
def matches_attribute_value?(current_value)
|
88
|
+
instance.send(attribute.to_sym) == current_value
|
89
|
+
end
|
90
|
+
|
91
|
+
def errors_do_not_match?
|
92
|
+
has_no_messages? || !errors_for_attribute_match?
|
79
93
|
end
|
80
94
|
|
81
|
-
def
|
82
|
-
message_finder.has_messages?
|
95
|
+
def has_no_messages?
|
96
|
+
!message_finder.has_messages?
|
83
97
|
end
|
84
98
|
|
85
99
|
def errors_for_attribute_match?
|
86
100
|
if expected_message
|
87
|
-
|
101
|
+
self.matched_error = errors_match_regexp? || errors_match_string?
|
88
102
|
else
|
89
103
|
errors_for_attribute.compact.any?
|
90
104
|
end
|
@@ -108,7 +122,7 @@ module Shoulda # :nodoc:
|
|
108
122
|
|
109
123
|
def expectation
|
110
124
|
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
|
111
|
-
[error_source, includes_expected_message, "when #{
|
125
|
+
[error_source, includes_expected_message, "when #{attribute} is set to #{value.inspect}"].join(' ')
|
112
126
|
end
|
113
127
|
|
114
128
|
def error_source
|
@@ -120,19 +134,19 @@ module Shoulda # :nodoc:
|
|
120
134
|
end
|
121
135
|
|
122
136
|
def allowed_values
|
123
|
-
if
|
124
|
-
"any of [#{
|
137
|
+
if values_to_match.length > 1
|
138
|
+
"any of [#{values_to_match.map(&:inspect).join(', ')}]"
|
125
139
|
else
|
126
|
-
|
140
|
+
values_to_match.first.inspect
|
127
141
|
end
|
128
142
|
end
|
129
143
|
|
130
144
|
def expected_message
|
131
|
-
if
|
132
|
-
if Symbol ===
|
145
|
+
if options.key?(:expected_message)
|
146
|
+
if Symbol === options[:expected_message]
|
133
147
|
default_expected_message
|
134
148
|
else
|
135
|
-
|
149
|
+
options[:expected_message]
|
136
150
|
end
|
137
151
|
end
|
138
152
|
end
|
@@ -143,19 +157,19 @@ module Shoulda # :nodoc:
|
|
143
157
|
|
144
158
|
def default_attribute_message
|
145
159
|
default_error_message(
|
146
|
-
|
160
|
+
options[:expected_message],
|
147
161
|
:model_name => model_name,
|
148
|
-
:instance =>
|
149
|
-
:attribute =>
|
162
|
+
:instance => instance,
|
163
|
+
:attribute => attribute
|
150
164
|
)
|
151
165
|
end
|
152
166
|
|
153
167
|
def model_name
|
154
|
-
|
168
|
+
instance.class.to_s.underscore
|
155
169
|
end
|
156
170
|
|
157
171
|
def message_finder
|
158
|
-
|
172
|
+
message_finder_factory.new(instance, attribute)
|
159
173
|
end
|
160
174
|
end
|
161
175
|
end
|
@@ -24,9 +24,18 @@ module Shoulda # :nodoc:
|
|
24
24
|
@allow_matcher.failure_message_for_should_not
|
25
25
|
end
|
26
26
|
|
27
|
+
def failure_message_for_should_not
|
28
|
+
@allow_matcher.failure_message_for_should
|
29
|
+
end
|
30
|
+
|
27
31
|
def allowed_types
|
28
32
|
''
|
29
33
|
end
|
34
|
+
|
35
|
+
def strict
|
36
|
+
@allow_matcher.strict
|
37
|
+
self
|
38
|
+
end
|
30
39
|
end
|
31
40
|
end
|
32
41
|
end
|
@@ -38,14 +38,14 @@ module Shoulda # :nodoc:
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def disallows_value_of(value, message = nil)
|
41
|
-
disallow =
|
41
|
+
disallow = disallow_value_matcher(value, message)
|
42
42
|
|
43
43
|
if disallow.matches?(@subject)
|
44
|
-
@failure_message_for_should = disallow.failure_message_for_should_not
|
45
|
-
false
|
46
|
-
else
|
47
44
|
@failure_message_for_should_not = disallow.failure_message_for_should
|
48
45
|
true
|
46
|
+
else
|
47
|
+
@failure_message_for_should = disallow.failure_message_for_should_not
|
48
|
+
false
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -62,6 +62,19 @@ module Shoulda # :nodoc:
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
def disallow_value_matcher(value, message)
|
66
|
+
matcher = DisallowValueMatcher.
|
67
|
+
new(value).
|
68
|
+
for(@attribute).
|
69
|
+
with_message(message)
|
70
|
+
|
71
|
+
if strict?
|
72
|
+
matcher.strict
|
73
|
+
else
|
74
|
+
matcher
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
65
78
|
def strict?
|
66
79
|
@strict
|
67
80
|
end
|
@@ -4,7 +4,7 @@ module Shoulda # :nodoc:
|
|
4
4
|
# Ensure that the belongs_to relationship exists.
|
5
5
|
#
|
6
6
|
# Options:
|
7
|
-
# * <tt>:class_name</tt> - tests that the association
|
7
|
+
# * <tt>:class_name</tt> - tests that the association resolves to class_name.
|
8
8
|
# * <tt>:validate</tt> - tests that the association makes use of the validate
|
9
9
|
# option.
|
10
10
|
#
|
@@ -23,7 +23,7 @@ module Shoulda # :nodoc:
|
|
23
23
|
# * <tt>through</tt> - association name for <tt>has_many :through</tt>
|
24
24
|
# * <tt>dependent</tt> - tests that the association makes use of the
|
25
25
|
# dependent option.
|
26
|
-
# * <tt>:class_name</tt> - tests that the association
|
26
|
+
# * <tt>:class_name</tt> - tests that the association resoves to class_name.
|
27
27
|
# * <tt>:validate</tt> - tests that the association makes use of the validate
|
28
28
|
# option.
|
29
29
|
#
|
@@ -43,7 +43,7 @@ module Shoulda # :nodoc:
|
|
43
43
|
# Options:
|
44
44
|
# * <tt>:dependent</tt> - tests that the association makes use of the
|
45
45
|
# dependent option.
|
46
|
-
# * <tt>:class_name</tt> - tests that the association
|
46
|
+
# * <tt>:class_name</tt> - tests that the association resolves to class_name.
|
47
47
|
# * <tt>:validate</tt> - tests that the association makes use of the validate
|
48
48
|
# option.
|
49
49
|
#
|
@@ -58,6 +58,7 @@ module Shoulda # :nodoc:
|
|
58
58
|
# the join table is in place.
|
59
59
|
#
|
60
60
|
# Options:
|
61
|
+
# * <tt>:class_name</tt> - tests that the association resolves to class_name.
|
61
62
|
# * <tt>:validate</tt> - tests that the association makes use of the validate
|
62
63
|
# option.
|
63
64
|
#
|
@@ -209,10 +210,10 @@ module Shoulda # :nodoc:
|
|
209
210
|
|
210
211
|
def class_name_correct?
|
211
212
|
if @options.key?(:class_name)
|
212
|
-
if @options[:class_name].to_s == reflection.
|
213
|
+
if @options[:class_name].to_s == reflection.klass.to_s
|
213
214
|
true
|
214
215
|
else
|
215
|
-
@missing = "#{@name} should
|
216
|
+
@missing = "#{@name} should resolve to #{@options[:class_name]} for class_name"
|
216
217
|
false
|
217
218
|
end
|
218
219
|
else
|
@@ -80,6 +80,10 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
|
80
80
|
it "rejects several bad values (#{bad_values.map(&:inspect).join(', ')})" do
|
81
81
|
model.should_not allow_value(*bad_values).for(:attr)
|
82
82
|
end
|
83
|
+
|
84
|
+
it "rejects a mix of both good and bad values" do
|
85
|
+
model.should_not allow_value('12345', *bad_values).for(:attr)
|
86
|
+
end
|
83
87
|
end
|
84
88
|
|
85
89
|
context 'with a single value' do
|
@@ -79,6 +79,13 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
|
79
79
|
end
|
80
80
|
|
81
81
|
context 'an attribute which must be included in an array' do
|
82
|
+
context 'given an attribute that does not accept strings' do
|
83
|
+
it 'allows an attribute to be set as an integer' do
|
84
|
+
validating_inclusion(:in => [0,1,2], :column_type => :integer).
|
85
|
+
should ensure_inclusion_of(:attr).in_array([0,1,2])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
82
89
|
it 'accepts with correct array' do
|
83
90
|
validating_inclusion(:in => %w(one two)).
|
84
91
|
should ensure_inclusion_of(:attr).in_array(%w(one two))
|
@@ -165,7 +172,9 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
|
165
172
|
end
|
166
173
|
|
167
174
|
def validating_inclusion(options)
|
168
|
-
|
175
|
+
options[:column_type] ||= :string
|
176
|
+
|
177
|
+
define_model(:example, :attr => options[:column_type]) do
|
169
178
|
validates_inclusion_of :attr, options
|
170
179
|
end.new
|
171
180
|
end
|
@@ -1,6 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
|
4
|
+
context '#description' do
|
5
|
+
it 'states that the confirmation must match its base attribute' do
|
6
|
+
matcher.description.should == 'require attr_confirmation to match attr'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
4
10
|
context 'a model with a confirmation validation' do
|
5
11
|
it 'accepts' do
|
6
12
|
validating_confirmation.should matcher
|
@@ -64,14 +64,21 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
64
64
|
Child.new.should_not belong_to(:parent).conditions(:adopter => true)
|
65
65
|
end
|
66
66
|
|
67
|
+
it 'accepts an association without a :class_name option' do
|
68
|
+
belonging_to_parent.should belong_to(:parent).class_name('Parent')
|
69
|
+
end
|
70
|
+
|
67
71
|
it 'accepts an association with a valid :class_name option' do
|
68
|
-
|
69
|
-
|
72
|
+
define_model :tree_parent
|
73
|
+
define_model :child, :parent_id => :integer do
|
74
|
+
belongs_to :parent, :class_name => 'TreeParent'
|
75
|
+
end
|
76
|
+
|
77
|
+
Child.new.should belong_to(:parent).class_name('TreeParent')
|
70
78
|
end
|
71
79
|
|
72
80
|
it 'rejects an association with a bad :class_name option' do
|
73
|
-
belonging_to_parent(:class_name
|
74
|
-
should_not belong_to(:parent).class_name('TreeChild')
|
81
|
+
belonging_to_parent.should_not belong_to(:parent).class_name('TreeChild')
|
75
82
|
end
|
76
83
|
|
77
84
|
context 'validate' do
|
@@ -231,6 +238,10 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
231
238
|
Parent.new.should_not have_many(:children).conditions(:adopted => true)
|
232
239
|
end
|
233
240
|
|
241
|
+
it 'accepts an association without a :class_name option' do
|
242
|
+
having_many_children.should have_many(:children).class_name('Child')
|
243
|
+
end
|
244
|
+
|
234
245
|
it 'accepts an association with a valid :class_name option' do
|
235
246
|
define_model :node, :parent_id => :integer
|
236
247
|
define_model :parent do
|
@@ -371,6 +382,10 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
371
382
|
Person.new.should_not have_one(:detail).conditions(:disabled => true)
|
372
383
|
end
|
373
384
|
|
385
|
+
it 'accepts an association without a :class_name option' do
|
386
|
+
having_one_detail.should have_one(:detail).class_name('Detail')
|
387
|
+
end
|
388
|
+
|
374
389
|
it 'accepts an association with a valid :class_name option' do
|
375
390
|
define_model :person_detail, :person_id => :integer
|
376
391
|
define_model :person do
|
@@ -439,7 +454,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
439
454
|
end
|
440
455
|
|
441
456
|
it 'rejects a nonexistent association' do
|
442
|
-
define_model :
|
457
|
+
define_model :relative
|
443
458
|
define_model :person
|
444
459
|
define_model :people_relative, :id => false, :person_id => :integer,
|
445
460
|
:relative_id => :integer
|
@@ -448,7 +463,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
448
463
|
end
|
449
464
|
|
450
465
|
it 'rejects an association with a nonexistent join table' do
|
451
|
-
define_model :
|
466
|
+
define_model :relative
|
452
467
|
define_model :person do
|
453
468
|
has_and_belongs_to_many :relatives
|
454
469
|
end
|
@@ -457,7 +472,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
457
472
|
end
|
458
473
|
|
459
474
|
it 'rejects an association of the wrong type' do
|
460
|
-
define_model :
|
475
|
+
define_model :relative, :person_id => :integer
|
461
476
|
define_model :person do
|
462
477
|
has_many :relatives
|
463
478
|
end
|
@@ -466,7 +481,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
466
481
|
end
|
467
482
|
|
468
483
|
it 'accepts an association with a valid :conditions option' do
|
469
|
-
define_model :
|
484
|
+
define_model :relative, :adopted => :boolean
|
470
485
|
define_model :person do
|
471
486
|
has_and_belongs_to_many :relatives, :conditions => { :adopted => true }
|
472
487
|
end
|
@@ -477,7 +492,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
477
492
|
end
|
478
493
|
|
479
494
|
it 'rejects an association with a bad :conditions option' do
|
480
|
-
define_model :
|
495
|
+
define_model :relative, :adopted => :boolean
|
481
496
|
define_model :person do
|
482
497
|
has_and_belongs_to_many :relatives
|
483
498
|
end
|
@@ -487,16 +502,21 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
487
502
|
Person.new.should_not have_and_belong_to_many(:relatives).conditions(:adopted => true)
|
488
503
|
end
|
489
504
|
|
505
|
+
it 'accepts an association without a :class_name option' do
|
506
|
+
having_and_belonging_to_many_relatives.
|
507
|
+
should have_and_belong_to_many(:relatives).class_name('Relative')
|
508
|
+
end
|
509
|
+
|
490
510
|
it 'accepts an association with a valid :class_name option' do
|
491
|
-
define_model :
|
511
|
+
define_model :person_relative, :adopted => :boolean
|
492
512
|
define_model :person do
|
493
|
-
has_and_belongs_to_many :relatives, :class_name => '
|
513
|
+
has_and_belongs_to_many :relatives, :class_name => 'PersonRelative'
|
494
514
|
end
|
495
515
|
|
496
516
|
define_model :people_person_relative, :person_id => :integer,
|
497
517
|
:person_relative_id => :integer
|
498
518
|
|
499
|
-
Person.new.should have_and_belong_to_many(:relatives).class_name('
|
519
|
+
Person.new.should have_and_belong_to_many(:relatives).class_name('PersonRelative')
|
500
520
|
end
|
501
521
|
|
502
522
|
it 'rejects an association with a bad :class_name option' do
|
@@ -527,7 +547,7 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|
527
547
|
end
|
528
548
|
|
529
549
|
def having_and_belonging_to_many_relatives(options = {})
|
530
|
-
define_model :
|
550
|
+
define_model :relative
|
531
551
|
define_model :people_relative, :id => false, :person_id => :integer,
|
532
552
|
:relative_id => :integer
|
533
553
|
define_model :person do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shoulda-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ authors:
|
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
|
-
date: 2013-03-
|
17
|
+
date: 2013-03-28 00:00:00.000000000 Z
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: activesupport
|
@@ -309,7 +309,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
309
309
|
version: '0'
|
310
310
|
segments:
|
311
311
|
- 0
|
312
|
-
hash: -
|
312
|
+
hash: -727795483328027229
|
313
313
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
314
314
|
none: false
|
315
315
|
requirements:
|
@@ -318,7 +318,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
318
318
|
version: '0'
|
319
319
|
segments:
|
320
320
|
- 0
|
321
|
-
hash: -
|
321
|
+
hash: -727795483328027229
|
322
322
|
requirements: []
|
323
323
|
rubyforge_project:
|
324
324
|
rubygems_version: 1.8.25
|