shoulda-matchers 1.5.4 → 1.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,10 @@ a test!
11
11
 
12
12
  4. Make the test pass.
13
13
 
14
- 5. Push to your fork and submit a pull request.
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shoulda-matchers (1.5.4)
4
+ shoulda-matchers (1.5.5)
5
5
  activesupport (>= 3.0.0)
6
6
  bourne (~> 1.3)
7
7
 
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
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/melissaxie/thoughtbot/shoulda-matchers
3
3
  specs:
4
- shoulda-matchers (1.5.4)
4
+ shoulda-matchers (1.5.5)
5
5
  activesupport (>= 3.0.0)
6
6
  bourne (~> 1.3)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/melissaxie/thoughtbot/shoulda-matchers
3
3
  specs:
4
- shoulda-matchers (1.5.4)
4
+ shoulda-matchers (1.5.5)
5
5
  activesupport (>= 3.0.0)
6
6
  bourne (~> 1.3)
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/melissaxie/thoughtbot/shoulda-matchers
3
3
  specs:
4
- shoulda-matchers (1.5.4)
4
+ shoulda-matchers (1.5.5)
5
5
  activesupport (>= 3.0.0)
6
6
  bourne (~> 1.3)
7
7
 
@@ -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
- @values_to_match = values
35
- @message_finder_factory = ValidationMessageFinder
36
- @options = {}
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
- @attribute = attribute
42
+ self.attribute = attribute
41
43
  self
42
44
  end
43
45
 
44
46
  def with_message(message)
45
- @options[:expected_message] = message
47
+ self.options[:expected_message] = message
46
48
  self
47
49
  end
48
50
 
49
51
  def strict
50
- @message_finder_factory = ExceptionMessageFinder
52
+ self.message_finder_factory = ExceptionMessageFinder
51
53
  self
52
54
  end
53
55
 
54
56
  def matches?(instance)
55
- @instance = instance
56
- @values_to_match.none? do |value|
57
- @value = value
58
- @instance.send("#{@attribute}=", @value)
59
- errors_match?
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: #{@matched_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
- def errors_match?
78
- has_messages? && errors_for_attribute_match?
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 has_messages?
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
- @matched_error = errors_match_regexp? || errors_match_string?
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 #{@attribute} is set to #{@value.inspect}"].join(' ')
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 @values_to_match.length > 1
124
- "any of [#{@values_to_match.map(&:inspect).join(', ')}]"
137
+ if values_to_match.length > 1
138
+ "any of [#{values_to_match.map(&:inspect).join(', ')}]"
125
139
  else
126
- @values_to_match.first.inspect
140
+ values_to_match.first.inspect
127
141
  end
128
142
  end
129
143
 
130
144
  def expected_message
131
- if @options.key?(:expected_message)
132
- if Symbol === @options[:expected_message]
145
+ if options.key?(:expected_message)
146
+ if Symbol === options[:expected_message]
133
147
  default_expected_message
134
148
  else
135
- @options[:expected_message]
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
- @options[:expected_message],
160
+ options[:expected_message],
147
161
  :model_name => model_name,
148
- :instance => @instance,
149
- :attribute => @attribute
162
+ :instance => instance,
163
+ :attribute => attribute
150
164
  )
151
165
  end
152
166
 
153
167
  def model_name
154
- @instance.class.to_s.underscore
168
+ instance.class.to_s.underscore
155
169
  end
156
170
 
157
171
  def message_finder
158
- @message_finder_factory.new(@instance, @attribute)
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
@@ -24,7 +24,7 @@ module Shoulda # :nodoc:
24
24
  end
25
25
 
26
26
  def description
27
- "require #{@base_attribute} to match #{@attribute}"
27
+ "require #{@confirmation} to match #{@attribute}"
28
28
  end
29
29
 
30
30
  def matches?(subject)
@@ -38,14 +38,14 @@ module Shoulda # :nodoc:
38
38
  end
39
39
 
40
40
  def disallows_value_of(value, message = nil)
41
- disallow = allow_value_matcher(value, message)
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 makes use of the class_name option.
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 makes use of the class_name option.
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 makes use of the class_name option.
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.options[:class_name].to_s
213
+ if @options[:class_name].to_s == reflection.klass.to_s
213
214
  true
214
215
  else
215
- @missing = "#{@name} should have #{@options[:class_name]} as class_name"
216
+ @missing = "#{@name} should resolve to #{@options[:class_name]} for class_name"
216
217
  false
217
218
  end
218
219
  else
@@ -1,5 +1,5 @@
1
1
  module Shoulda
2
2
  module Matchers
3
- VERSION = '1.5.4'.freeze
3
+ VERSION = '1.5.5'.freeze
4
4
  end
5
5
  end
@@ -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
- define_model(:example, :attr => :string) do
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
- belonging_to_parent(:class_name => 'TreeParent').
69
- should belong_to(:parent).class_name('TreeParent')
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 => 'TreeParent').
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 :relatives
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 :relatives
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 :relatives, :person_id => :integer
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 :relatives, :adopted => :boolean
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 :relatives, :adopted => :boolean
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 :person_relatives, :adopted => :boolean
511
+ define_model :person_relative, :adopted => :boolean
492
512
  define_model :person do
493
- has_and_belongs_to_many :relatives, :class_name => 'PersonRelatives'
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('PersonRelatives')
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 :relatives
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
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-21 00:00:00.000000000 Z
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: -725860603889119000
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: -725860603889119000
321
+ hash: -727795483328027229
322
322
  requirements: []
323
323
  rubyforge_project:
324
324
  rubygems_version: 1.8.25