shoulda-matchers 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +8 -8
  2. data/.travis.yml +9 -15
  3. data/Appraisals +10 -8
  4. data/Gemfile.lock +1 -1
  5. data/NEWS.md +62 -32
  6. data/gemfiles/3.0.gemfile.lock +2 -2
  7. data/gemfiles/3.1.gemfile.lock +2 -2
  8. data/gemfiles/3.2.gemfile +1 -1
  9. data/gemfiles/3.2.gemfile.lock +3 -3
  10. data/lib/shoulda/matchers/active_model.rb +1 -0
  11. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +7 -2
  12. data/lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb +1 -1
  13. data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +1 -1
  14. data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +2 -0
  15. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +3 -2
  16. data/lib/shoulda/matchers/active_model/helpers.rb +5 -2
  17. data/lib/shoulda/matchers/active_model/odd_even_number_matcher.rb +47 -0
  18. data/lib/shoulda/matchers/active_model/only_integer_matcher.rb +4 -0
  19. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +26 -3
  20. data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +68 -16
  21. data/lib/shoulda/matchers/active_model/validation_matcher.rb +5 -0
  22. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +3 -2
  23. data/lib/shoulda/matchers/active_record/association_matcher.rb +29 -4
  24. data/lib/shoulda/matchers/version.rb +1 -1
  25. data/shoulda-matchers.gemspec +1 -0
  26. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +195 -0
  27. data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +18 -0
  28. data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +21 -0
  29. data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +23 -0
  30. data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +46 -0
  31. data/spec/shoulda/matchers/active_model/odd_even_number_matcher_spec.rb +93 -0
  32. data/spec/shoulda/matchers/active_model/only_integer_matcher_spec.rb +4 -4
  33. data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +60 -0
  34. data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +16 -0
  35. data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +45 -8
  36. data/spec/shoulda/matchers/active_model/validation_message_finder_spec.rb +13 -0
  37. data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +50 -2
  38. data/spec/support/i18n_faker.rb +10 -0
  39. metadata +10 -3
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWMwNTNlMjgwZTMwZGU1ZDFmMjdhYTY3MjU2NDNiMGNhNjQwNjZhMw==
4
+ YzVkMDJkYmRiNjE2ODM0MDMzMzQxODQwZTBjMWEzM2RmYTMyZTIzYQ==
5
5
  data.tar.gz: !binary |-
6
- YjM1NmVhYTc5YzYwY2NhNzFmZmM0Yjk0YjM0ODRhZDJkZTQyMThiNw==
6
+ NTExOTlmMTUyOGU1NjVlZjM0YzVhN2FiNDVjYTE3NmI0ZDU0ZjNiMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MTUyNjY4M2RjMjM1NTQ0ZWU5M2RiNDUwYTZhOTUzNGEyYzIyNzAyNDY4MWQ1
10
- ZTFjMGJlMzgxY2NhY2U0OTgwMTI2MjlhYTY3MTU0ZjlhMzk0ZDIzYmVhMjdj
11
- NDkzOTU5ZjI2NWQxZTZmNzVlZWVmMTYxZTNjNzAzZjkzMzc4MTU=
9
+ NWI5ZTA2ZGJjMTJmNjgxYjI0Mjk1NGZhYjZkYzUyMmIwNjJjZmJhYjRjZGRh
10
+ NTA2MjIwZDMzZjQ3YmIxMTNjNGUwMjEzYzg1ZjkzZDA2ZThlY2UyZWY5OGNm
11
+ OTY0ZTg4ZjM2ODQxZTU0OTg4MDhmMTA0ZDU3YjEyNTcxZmVlNDQ=
12
12
  data.tar.gz: !binary |-
13
- ODNhMDRhMjBkYWE5OTkzNzUwNTkzYmE0MmE2OWM5MzgwODBkOTIwZGQ4ZTAz
14
- OWJhNDA2MzI3MmE4NmYzNzFmYjAxNjhlYzFhMjAxOWI5Y2E1ZDQxMDE4MTI2
15
- NDRlNmI0ZmI0ODM5NzA3MzJkZDVmMzJlNzQ1YzljNWNhMWZhOTg=
13
+ N2FkMWNjZTgzOTgwZjcwOWE2YjE2NzU1ZDNmMDQwZDEzNjQzMjVhMTAxYjQy
14
+ NjJjYmI0NmFlN2E3OTZkOGU0ODIzNDllMDgwZjlhNjRmNTRmMWQ3YWU5ZGNk
15
+ NzA0NmE5MzVkM2JhODAzNjMyNDU3NzBlYzIyZWUwYmRiNzEwMjI=
@@ -1,11 +1,8 @@
1
1
  script: "bundle exec rake spec cucumber"
2
2
 
3
3
  rvm:
4
- - 1.8.7
5
4
  - 1.9.2
6
5
  - 1.9.3
7
- - rbx-18mode
8
- - jruby
9
6
 
10
7
  gemfile:
11
8
  - gemfiles/3.0.gemfile
@@ -14,15 +11,12 @@ gemfile:
14
11
 
15
12
  matrix:
16
13
  allow_failures:
17
- - rvm: 1.8.7
18
- - rvm: rbx-18mode
19
- - rvm: jruby
20
- exclude:
21
- - rvm: rbx-18mode
22
- gemfile: gemfiles/3.0.gemfile
23
- - rvm: rbx-18mode
24
- gemfile: gemfiles/3.1.gemfile
25
- - rvm: jruby
26
- gemfile: gemfiles/3.0.gemfile
27
- - rvm: jruby
28
- gemfile: gemfiles/3.1.gemfile
14
+ - rvm: rbx-19mode
15
+ - rvm: jruby-19mode
16
+ include:
17
+ - rvm: 2.0.0
18
+ gemfile: gemfiles/3.2.gemfile
19
+ - rvm: rbx-19mode
20
+ gemfile: gemfiles/3.2.gemfile
21
+ - rvm: jruby-19mode
22
+ gemfile: gemfiles/3.2.gemfile
data/Appraisals CHANGED
@@ -1,15 +1,17 @@
1
- appraise '3.0' do
2
- gem 'rails', '~> 3.0.17'
3
- end
1
+ if RUBY_VERSION < '2.0'
2
+ appraise '3.0' do
3
+ gem 'rails', '~> 3.0.17'
4
+ end
4
5
 
5
- appraise '3.1' do
6
- gem 'rails', '~> 3.1.8'
7
- gem 'jquery-rails'
8
- gem 'sass-rails'
6
+ appraise '3.1' do
7
+ gem 'rails', '~> 3.1.8'
8
+ gem 'jquery-rails'
9
+ gem 'sass-rails'
10
+ end
9
11
  end
10
12
 
11
13
  appraise '3.2' do
12
- gem 'rails', '~> 3.2.8'
14
+ gem 'rails', '~> 3.2.13'
13
15
  gem 'jquery-rails'
14
16
  gem 'sass-rails'
15
17
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shoulda-matchers (2.0.0)
4
+ shoulda-matchers (2.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
data/NEWS.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # HEAD
2
2
 
3
+ # v 2.1.0
4
+
5
+ * Add missing `failure_message_for_should_not` implementations to
6
+ `validate_numericality_of` and its submatchers
7
+
8
+ * Support validation contexts for testing validations `on: :create` and when using custom contexts like `model.valid?(:my_context)`.
9
+
10
+ * Fix a bug in validations with autosaved models.
11
+
12
+ * Fix maximum value detection for the `ensure_inclusion_of` and
13
+ `ensure_exclusion_of` matchers.
14
+
15
+ * Add `:odd` and `:even` options to the `validate_numericality_of` matcher.
16
+
17
+ * Add `:touch` option to AssociationMatcher.
18
+
19
+ * Ruby 2.0.0 is now officially supported.
20
+
21
+ * Fix the issue where using `%{attribute}` or `%{model}` in I18n translations
22
+ raised exceptions.
23
+
24
+ * Support datetime columns in `validate_uniqueness_of.scoped_to`.
25
+
26
+ * Add `allow_nil` option to the `validate_uniqueness_of` matcher.
27
+
3
28
  # v 2.0.0
4
29
  * Remove the following matchers:
5
30
  * `assign_to`
@@ -9,60 +34,65 @@
9
34
  * `have_sent_email`
10
35
  * `permit` (strong parameters matcher)
11
36
  * `delegate_method`
37
+
12
38
  * For more information about 2.0 changes, see:
13
- http://robots.thoughtbot.com/post/47031676783/shoulda-matchers-2-0
39
+ http://robots.thoughtbot.com/post/47031676783/shoulda-matchers-2-0.
14
40
 
15
41
  # v 1.5.6
16
- * Revert previous change in `AllowValueMatcher` that added a check for a
42
+ * Revert previous change in AllowValueMatcher that added a check for a
17
43
  properly-set attribute.
18
44
 
19
45
  # v 1.5.5
20
- * `AllowValueMatcher` checks that the right value is used for attempts at
21
- setting the attribute with it
46
+ * AllowValueMatcher checks that the right value is used for attempts at
47
+ setting the attribute with it.
22
48
  * Please note that previously-passing tests might now fail. It is likely that
23
49
  it's not a bug, but please make sure that the code you're testing is written
24
50
  properly before submitting an issue.
25
- * Use DisallowValueMatcher for `disallows_value_of` method
26
- * Assert `class_name` value on real class name for `AssociationMatcher`
27
- * Correct the variable used for `validate_confirmation_of` matcher description
51
+
52
+ * Use DisallowValueMatcher for `disallows_value_of` method.
53
+
54
+ * Assert `class_name` value on real class name for AssociationMatcher.
55
+
56
+ * Correct the variable used for `validate_confirmation_of` matcher description.
28
57
 
29
58
  # v 1.5.4
30
- * Properly-released version of 1.5.3
59
+ * Properly-released version of 1.5.3.
31
60
 
32
61
  # v 1.5.3 - yanked due to mis-release
33
- * Alleviate the need to add `rspec` gem to your app
62
+ * Alleviate the need to add `rspec` gem to your app.
34
63
 
35
64
  # v 1.5.1
36
- * Bump version depedency of Bourne to allow for Mocha upgrade.
37
- * Should fix incompatiblity with MiniTest.
65
+ * Bump version dependency of Bourne to allow for Mocha upgrade.
66
+
67
+ * Should fix incompatibility with MiniTest.
38
68
 
39
69
  # v 1.5.0
40
70
  * Deprecate the following matchers:
41
- * assign_to
42
- * respond_with_content_type
43
- * query_the_database
44
- * validate_format_of
45
- * have_sent_email
46
- * strong_parameters_matcher
47
- * delegate_method
71
+ * `assign_to`
72
+ * `respond_with_content_type`
73
+ * `query_the_database`
74
+ * `validate_format_of`
75
+ * `have_sent_email`
76
+ * `permit` (strong parameters matcher)
77
+ * `delegate_method`
48
78
 
49
79
  * Use RSpec's native `configure.include` syntax for including matchers into
50
- RSpec (#204)
80
+ RSpec (#204).
51
81
 
52
- * Do not force minitest loading when test-unit is available (this was fixed
53
- before 1.3.0 then reverted in 1.3.0) (#181)
82
+ * Do not force MiniTest loading when test-unit is available (this was fixed
83
+ before 1.3.0 then reverted in 1.3.0).
54
84
 
55
85
  # v1.4.2
56
- * Added a new `delegate_method` matcher.
86
+ * Add a new `delegate_method` matcher.
57
87
 
58
88
  # v1.4.1
59
- * Fixes an issue when used with Test::Unit on the allow value matcher.
89
+ * Fix an issue when used with Test::Unit on the allow value matcher.
60
90
 
61
- * Fixes an issue with using `ensure_inclusion_of(:attr)` given an array of true or false values.
91
+ * Fix an issue with using `ensure_inclusion_of(:attr)` given an array of true or false values.
62
92
 
63
93
  # v1.4.0
64
94
 
65
- * Added `strict` option to validation matchers.
95
+ * Add `strict` option to validation matchers.
66
96
 
67
97
  * Verify that arguments to `set_the_flash` matcher are valid.
68
98
 
@@ -78,13 +108,13 @@ setting the attribute with it
78
108
 
79
109
  * Test outside values for `ensure_inclusion_of` when given an array.
80
110
 
81
- * Fixed the output of the `set_the_flash` matcher.
111
+ * Fix the output of the `set_the_flash` matcher.
82
112
 
83
113
  # v1.3.0
84
114
 
85
- * `validate_format_of` will accept `allow_blank(bool)` and `allow_nil(bool)`
115
+ * `validate_format_of` will accept `allow_blank(bool)` and `allow_nil(bool)`.
86
116
 
87
- * Prefer Test::Unit to Minitest when loading integrations so that RubyMine is
117
+ * Prefer Test::Unit to MiniTest when loading integrations so that RubyMine is
88
118
  happy (#88).
89
119
 
90
120
  * `validates_uniqueness_of` will now create a record if one does not exist.
@@ -119,10 +149,10 @@ setting the attribute with it
119
149
 
120
150
  # v1.1.0
121
151
 
122
- * Added `only_integer` option to `validate_numericality_of`:
152
+ * Add `only_integer` option to `validate_numericality_of`:
123
153
  `should validate_numericality_of(:attribute).only_integer`
124
154
 
125
- * Added a `query_the_database` matcher:
155
+ * Add a `query_the_database` matcher:
126
156
 
127
157
  `it { should query_the_database(4.times).when_calling(:complicated_method) }`
128
158
  `it { should query_the_database(4.times).or_less.when_calling(:complicated_method) }`
@@ -137,10 +167,10 @@ setting the attribute with it
137
167
  * The `have_sent_email` matcher can check `reply_to`:
138
168
  ` it { should have_sent_email.reply_to([user, other]) }`
139
169
 
140
- * Added `validates_confirmation_of` matcher:
170
+ * Add `validates_confirmation_of` matcher:
141
171
  `it { should validate_confirmation_of(:password) }`
142
172
 
143
- * Added `serialize` matcher:
173
+ * Add `serialize` matcher:
144
174
  `it { should serialize(:details).as(Hash).as_instance_of(Hash) }`
145
175
 
146
176
  * shoulda-matchers checks for all possible I18n keys, instead of just
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: /Users/melissaxie/thoughtbot/shoulda-matchers
2
+ remote: ../
3
3
  specs:
4
- shoulda-matchers (2.0.0)
4
+ shoulda-matchers (2.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: /Users/melissaxie/thoughtbot/shoulda-matchers
2
+ remote: ../
3
3
  specs:
4
- shoulda-matchers (2.0.0)
4
+ shoulda-matchers (2.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ gem "activerecord-jdbcsqlite3-adapter", :platform=>:jruby
9
9
  gem "jdbc-sqlite3", :platform=>:jruby
10
10
  gem "jruby-openssl", :platform=>:jruby
11
11
  gem "therubyrhino", :platform=>:jruby
12
- gem "rails", "~> 3.2.8"
12
+ gem "rails", "~> 3.2.13"
13
13
  gem "jquery-rails"
14
14
  gem "sass-rails"
15
15
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: /Users/melissaxie/thoughtbot/shoulda-matchers
2
+ remote: ../
3
3
  specs:
4
- shoulda-matchers (2.0.0)
4
+ shoulda-matchers (2.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -147,7 +147,7 @@ DEPENDENCIES
147
147
  jdbc-sqlite3
148
148
  jquery-rails
149
149
  jruby-openssl
150
- rails (~> 3.2.8)
150
+ rails (~> 3.2.13)
151
151
  rake (>= 0.9.2)
152
152
  rspec-rails (~> 2.13)
153
153
  sass-rails
@@ -5,6 +5,7 @@ require 'shoulda/matchers/active_model/exception_message_finder'
5
5
  require 'shoulda/matchers/active_model/allow_value_matcher'
6
6
  require 'shoulda/matchers/active_model/disallow_value_matcher'
7
7
  require 'shoulda/matchers/active_model/only_integer_matcher'
8
+ require 'shoulda/matchers/active_model/odd_even_number_matcher'
8
9
  require 'shoulda/matchers/active_model/ensure_length_of_matcher'
9
10
  require 'shoulda/matchers/active_model/ensure_inclusion_of_matcher'
10
11
  require 'shoulda/matchers/active_model/ensure_exclusion_of_matcher'
@@ -43,6 +43,11 @@ module Shoulda # :nodoc:
43
43
  self
44
44
  end
45
45
 
46
+ def on(context)
47
+ @context = context
48
+ self
49
+ end
50
+
46
51
  def with_message(message)
47
52
  self.options[:expected_message] = message
48
53
  self
@@ -78,7 +83,7 @@ module Shoulda # :nodoc:
78
83
  private
79
84
 
80
85
  attr_accessor :values_to_match, :message_finder_factory,
81
- :instance, :attribute, :value, :matched_error
86
+ :instance, :attribute, :context, :value, :matched_error
82
87
 
83
88
  def errors_match?
84
89
  has_messages? && errors_for_attribute_match?
@@ -161,7 +166,7 @@ module Shoulda # :nodoc:
161
166
  end
162
167
 
163
168
  def message_finder
164
- message_finder_factory.new(instance, attribute)
169
+ message_finder_factory.new(instance, attribute, context)
165
170
  end
166
171
  end
167
172
  end
@@ -27,7 +27,7 @@ module Shoulda # :nodoc:
27
27
  def in_range(range)
28
28
  @range = range
29
29
  @minimum = range.first
30
- @maximum = range.last
30
+ @maximum = range.max
31
31
  self
32
32
  end
33
33
 
@@ -37,7 +37,7 @@ module Shoulda # :nodoc:
37
37
  def in_range(range)
38
38
  @range = range
39
39
  @minimum = range.first
40
- @maximum = range.last
40
+ @maximum = range.max
41
41
  self
42
42
  end
43
43
 
@@ -104,6 +104,7 @@ module Shoulda # :nodoc:
104
104
  if Symbol === @short_message
105
105
  @short_message = default_error_message(@short_message,
106
106
  :model_name => @subject.class.to_s.underscore,
107
+ :instance => @subject,
107
108
  :attribute => @attribute,
108
109
  :count => @options[:minimum])
109
110
  end
@@ -111,6 +112,7 @@ module Shoulda # :nodoc:
111
112
  if Symbol === @long_message
112
113
  @long_message = default_error_message(@long_message,
113
114
  :model_name => @subject.class.to_s.underscore,
115
+ :instance => @subject,
114
116
  :attribute => @attribute,
115
117
  :count => @options[:maximum])
116
118
  end
@@ -3,9 +3,10 @@ module Shoulda
3
3
  module ActiveModel
4
4
  # Finds message information from exceptions thrown by #valid?
5
5
  class ExceptionMessageFinder
6
- def initialize(instance, attribute)
6
+ def initialize(instance, attribute, context=nil)
7
7
  @instance = instance
8
8
  @attribute = attribute
9
+ @context = context
9
10
  end
10
11
 
11
12
  def allow_description(allowed_values)
@@ -39,7 +40,7 @@ module Shoulda
39
40
  private
40
41
 
41
42
  def validate_and_rescue
42
- @instance.valid?
43
+ @instance.valid?(@context)
43
44
  []
44
45
  rescue ::ActiveModel::StrictValidationFailed => exception
45
46
  [exception.message]
@@ -5,7 +5,10 @@ module Shoulda # :nodoc:
5
5
  def pretty_error_messages(obj) # :nodoc:
6
6
  obj.errors.map do |attribute, model|
7
7
  msg = "#{attribute} #{model}"
8
- msg << " (#{obj.send(attribute).inspect})" unless attribute.to_sym == :base
8
+ if attribute.to_sym != :base && obj.respond_to?(attribute)
9
+ msg << " (#{obj.send(attribute).inspect})"
10
+ end
11
+ msg
9
12
  end
10
13
  end
11
14
 
@@ -26,7 +29,7 @@ module Shoulda # :nodoc:
26
29
  instance = options.delete(:instance)
27
30
 
28
31
  if instance && instance.errors.respond_to?(:generate_message)
29
- instance.errors.generate_message(attribute.to_sym, key, {})
32
+ instance.errors.generate_message(attribute.to_sym, key, options)
30
33
  else
31
34
  default_translation = [ :"activerecord.errors.models.#{model_name}.#{key}",
32
35
  :"activerecord.errors.messages.#{key}",
@@ -0,0 +1,47 @@
1
+ module Shoulda # :nodoc:
2
+ module Matchers
3
+ module ActiveModel # :nodoc:
4
+ class OddEvenNumberMatcher # :nodoc:
5
+ NON_EVEN_NUMBER_VALUE = 1
6
+ NON_ODD_NUMBER_VALUE = 2
7
+
8
+ def initialize(attribute, options = {})
9
+ @attribute = attribute
10
+ options[:odd] ||= true
11
+ options[:even] ||= false
12
+
13
+ if options[:odd] && !options[:even]
14
+ @disallow_value_matcher = DisallowValueMatcher.new(NON_ODD_NUMBER_VALUE).
15
+ for(@attribute).
16
+ with_message(:odd)
17
+ else
18
+ @disallow_value_matcher = DisallowValueMatcher.new(NON_EVEN_NUMBER_VALUE).
19
+ for(@attribute).
20
+ with_message(:even)
21
+ end
22
+ end
23
+
24
+ def matches?(subject)
25
+ @disallow_value_matcher.matches?(subject)
26
+ end
27
+
28
+ def with_message(message)
29
+ @disallow_value_matcher.with_message(message)
30
+ self
31
+ end
32
+
33
+ def allowed_types
34
+ 'integer'
35
+ end
36
+
37
+ def failure_message_for_should
38
+ @disallow_value_matcher.failure_message_for_should
39
+ end
40
+
41
+ def failure_message_for_should_not
42
+ @disallow_value_matcher.failure_message_for_should_not
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end