shoulda-matchers 2.8.0.rc1 → 2.8.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +3 -0
  3. data/.hound_config/ruby.yml +5 -0
  4. data/.travis.yml +4 -0
  5. data/.yardopts +1 -1
  6. data/Appraisals +5 -2
  7. data/Gemfile +1 -1
  8. data/Gemfile.lock +7 -5
  9. data/NEWS.md +24 -0
  10. data/README.md +9 -0
  11. data/gemfiles/3.0.gemfile +1 -1
  12. data/gemfiles/3.0.gemfile.lock +7 -5
  13. data/gemfiles/3.1.gemfile +1 -1
  14. data/gemfiles/3.1.gemfile.lock +7 -5
  15. data/gemfiles/3.1_1.9.2.gemfile +1 -3
  16. data/gemfiles/3.1_1.9.2.gemfile.lock +7 -12
  17. data/gemfiles/3.2.gemfile +1 -1
  18. data/gemfiles/3.2.gemfile.lock +7 -5
  19. data/gemfiles/3.2_1.9.2.gemfile +1 -3
  20. data/gemfiles/3.2_1.9.2.gemfile.lock +5 -10
  21. data/gemfiles/4.0.0.gemfile +1 -1
  22. data/gemfiles/4.0.0.gemfile.lock +7 -5
  23. data/gemfiles/4.0.1.gemfile +1 -1
  24. data/gemfiles/4.0.1.gemfile.lock +7 -5
  25. data/gemfiles/4.1.gemfile +1 -1
  26. data/gemfiles/4.1.gemfile.lock +7 -5
  27. data/gemfiles/4.2.gemfile +5 -3
  28. data/gemfiles/4.2.gemfile.lock +26 -8
  29. data/lib/shoulda/matchers/action_controller/set_flash_matcher.rb +2 -0
  30. data/lib/shoulda/matchers/active_model.rb +3 -2
  31. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +44 -32
  32. data/lib/shoulda/matchers/active_model/helpers.rb +8 -22
  33. data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +66 -19
  34. data/lib/shoulda/matchers/active_model/strict_validator.rb +51 -0
  35. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +14 -13
  36. data/lib/shoulda/matchers/active_model/validator.rb +113 -0
  37. data/lib/shoulda/matchers/active_model/validator_with_captured_range_error.rb +12 -0
  38. data/lib/shoulda/matchers/active_record/association_matcher.rb +19 -1
  39. data/lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb +23 -2
  40. data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +14 -11
  41. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +1 -0
  42. data/lib/shoulda/matchers/active_record/uniqueness/model.rb +1 -0
  43. data/lib/shoulda/matchers/active_record/uniqueness/namespace.rb +1 -0
  44. data/lib/shoulda/matchers/active_record/uniqueness/test_model_creator.rb +1 -0
  45. data/lib/shoulda/matchers/active_record/uniqueness/test_models.rb +1 -0
  46. data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +22 -15
  47. data/lib/shoulda/matchers/matcher_context.rb +1 -0
  48. data/lib/shoulda/matchers/rails_shim.rb +22 -0
  49. data/lib/shoulda/matchers/util.rb +5 -0
  50. data/lib/shoulda/matchers/version.rb +1 -1
  51. data/script/update_gem_in_all_appraisals +15 -0
  52. data/shoulda-matchers.gemspec +1 -1
  53. data/spec/support/unit/helpers/active_model_helpers.rb +3 -1
  54. data/spec/support/unit/helpers/active_model_versions.rb +8 -0
  55. data/spec/support/unit/helpers/active_record_versions.rb +16 -0
  56. data/spec/support/unit/helpers/rails_versions.rb +4 -4
  57. data/spec/support/unit/matchers/fail_with_message_matcher.rb +9 -8
  58. data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +74 -0
  59. data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +75 -0
  60. data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +24 -0
  61. data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +78 -5
  62. data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +31 -2
  63. data/spec/unit_spec_helper.rb +2 -0
  64. metadata +11 -8
  65. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -58
  66. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +0 -69
  67. data/spec/unit/shoulda/matchers/active_model/exception_message_finder_spec.rb +0 -111
  68. data/spec/unit/shoulda/matchers/active_model/validation_message_finder_spec.rb +0 -129
@@ -6,7 +6,7 @@ gem "appraisal", "~> 1.0"
6
6
  gem "aruba"
7
7
  gem "bourne", "~> 1.3"
8
8
  gem "bundler", "~> 1.1"
9
- gem "pry"
9
+ gem "pry-nav"
10
10
  gem "rails", "4.0.1"
11
11
  gem "rake", ">= 0.9.2"
12
12
  gem "rspec-rails", "2.99.0"
@@ -98,10 +98,12 @@ GEM
98
98
  hashie (>= 1.1.0)
99
99
  protected_attributes (1.0.5)
100
100
  activemodel (>= 4.0.1, < 5.0)
101
- pry (0.9.12.6)
102
- coderay (~> 1.0)
103
- method_source (~> 0.8)
101
+ pry (0.10.1)
102
+ coderay (~> 1.1.0)
103
+ method_source (~> 0.8.1)
104
104
  slop (~> 3.4)
105
+ pry-nav (0.2.4)
106
+ pry (>= 0.9.10, < 0.11.0)
105
107
  pygments.rb (0.6.0)
106
108
  posix-spawn (~> 0.3.6)
107
109
  yajl-ruby (~> 1.1.0)
@@ -151,7 +153,7 @@ GEM
151
153
  json (~> 1.8)
152
154
  rdoc (~> 4.0, < 5.0)
153
155
  shoulda-context (1.2.0)
154
- slop (3.4.7)
156
+ slop (3.6.0)
155
157
  spring (1.1.3)
156
158
  spring-commands-rspec (1.0.2)
157
159
  spring (>= 0.9.1)
@@ -200,7 +202,7 @@ DEPENDENCIES
200
202
  jruby-openssl
201
203
  minitest-reporters
202
204
  protected_attributes
203
- pry
205
+ pry-nav
204
206
  pygments.rb
205
207
  rails (= 4.0.1)
206
208
  rake (>= 0.9.2)
data/gemfiles/4.1.gemfile CHANGED
@@ -6,7 +6,7 @@ gem "appraisal", "~> 1.0"
6
6
  gem "aruba"
7
7
  gem "bourne", "~> 1.3"
8
8
  gem "bundler", "~> 1.1"
9
- gem "pry"
9
+ gem "pry-nav"
10
10
  gem "rails", "~> 4.1.0"
11
11
  gem "rake", ">= 0.9.2"
12
12
  gem "rspec-rails", "~> 3.0.1"
@@ -94,10 +94,12 @@ GEM
94
94
  posix-spawn (0.3.8)
95
95
  protected_attributes (1.0.7)
96
96
  activemodel (>= 4.0.1, < 5.0)
97
- pry (0.9.12.6)
98
- coderay (~> 1.0)
99
- method_source (~> 0.8)
97
+ pry (0.10.1)
98
+ coderay (~> 1.1.0)
99
+ method_source (~> 0.8.1)
100
100
  slop (~> 3.4)
101
+ pry-nav (0.2.4)
102
+ pry (>= 0.9.10, < 0.11.0)
101
103
  pygments.rb (0.6.0)
102
104
  posix-spawn (~> 0.3.6)
103
105
  yajl-ruby (~> 1.1.0)
@@ -152,7 +154,7 @@ GEM
152
154
  json (~> 1.8)
153
155
  rdoc (~> 4.0, < 5.0)
154
156
  shoulda-context (1.2.0)
155
- slop (3.4.7)
157
+ slop (3.6.0)
156
158
  spring (1.1.2)
157
159
  spring-commands-rspec (1.0.2)
158
160
  spring (>= 0.9.1)
@@ -199,7 +201,7 @@ DEPENDENCIES
199
201
  jruby-openssl
200
202
  minitest-reporters
201
203
  protected_attributes (~> 1.0.6)
202
- pry
204
+ pry-nav
203
205
  pygments.rb
204
206
  rails (~> 4.1.0)
205
207
  rake (>= 0.9.2)
data/gemfiles/4.2.gemfile CHANGED
@@ -6,7 +6,7 @@ gem "appraisal", "~> 1.0"
6
6
  gem "aruba"
7
7
  gem "bourne", "~> 1.3"
8
8
  gem "bundler", "~> 1.1"
9
- gem "pry"
9
+ gem "pry-nav"
10
10
  gem "rails", "~> 4.2.0"
11
11
  gem "rake", ">= 0.9.2"
12
12
  gem "rspec-rails", "~> 3.0.1"
@@ -24,13 +24,15 @@ gem "therubyrhino", :platform => :jruby
24
24
  gem "spring"
25
25
  gem "spring-commands-rspec"
26
26
  gem "uglifier", ">= 1.3.0"
27
- gem "coffee-rails", "~> 4.0.0"
27
+ gem "coffee-rails", "~> 4.1.0"
28
28
  gem "jquery-rails"
29
29
  gem "turbolinks"
30
30
  gem "sdoc", "~> 0.4.0"
31
31
  gem "activeresource", "4.0.0"
32
32
  gem "protected_attributes", "~> 1.0.6"
33
33
  gem "minitest-reporters"
34
- gem "jbuilder", "~> 2.0"
35
34
  gem "sass-rails", "~> 5.0"
35
+ gem "jbuilder", "~> 2.0"
36
36
  gem "bcrypt", "~> 3.1.7"
37
+ gem "byebug"
38
+ gem "web-console", "~> 2.0"
@@ -51,25 +51,34 @@ GEM
51
51
  cucumber (>= 1.1.1)
52
52
  rspec-expectations (>= 2.7.0)
53
53
  bcrypt (3.1.9)
54
+ binding_of_caller (0.7.2)
55
+ debug_inspector (>= 0.0.1)
54
56
  bourne (1.6.0)
55
57
  mocha (~> 1.1)
56
58
  builder (3.2.2)
59
+ byebug (3.5.1)
60
+ columnize (~> 0.8)
61
+ debugger-linecache (~> 1.2)
62
+ slop (~> 3.6)
57
63
  childprocess (0.5.5)
58
64
  ffi (~> 1.0, >= 1.0.11)
59
65
  coderay (1.1.0)
60
- coffee-rails (4.0.1)
66
+ coffee-rails (4.1.0)
61
67
  coffee-script (>= 2.2.0)
62
68
  railties (>= 4.0.0, < 5.0)
63
69
  coffee-script (2.3.0)
64
70
  coffee-script-source
65
71
  execjs
66
72
  coffee-script-source (1.8.0)
73
+ columnize (0.9.0)
67
74
  cucumber (1.3.17)
68
75
  builder (>= 2.1.2)
69
76
  diff-lcs (>= 1.1.3)
70
77
  gherkin (~> 2.12)
71
78
  multi_json (>= 1.7.5, < 2.0)
72
79
  multi_test (>= 0.1.1)
80
+ debug_inspector (0.0.2)
81
+ debugger-linecache (1.2.0)
73
82
  diff-lcs (1.2.5)
74
83
  erubis (2.7.0)
75
84
  execjs (2.2.2)
@@ -87,7 +96,7 @@ GEM
87
96
  rails-dom-testing (~> 1.0)
88
97
  railties (>= 4.2.0)
89
98
  thor (>= 0.14, < 2.0)
90
- json (1.8.1)
99
+ json (1.8.2)
91
100
  loofah (2.0.1)
92
101
  nokogiri (>= 1.5.9)
93
102
  mail (2.6.3)
@@ -95,8 +104,8 @@ GEM
95
104
  metaclass (0.0.4)
96
105
  method_source (0.8.2)
97
106
  mime-types (2.4.3)
98
- mini_portile (0.6.1)
99
- minitest (5.5.0)
107
+ mini_portile (0.6.2)
108
+ minitest (5.5.1)
100
109
  minitest-reporters (1.0.8)
101
110
  ansi
102
111
  builder
@@ -106,7 +115,7 @@ GEM
106
115
  metaclass (~> 0.0.1)
107
116
  multi_json (1.10.1)
108
117
  multi_test (0.1.1)
109
- nokogiri (1.6.5)
118
+ nokogiri (1.6.6.1)
110
119
  mini_portile (~> 0.6.0)
111
120
  posix-spawn (0.3.9)
112
121
  protected_attributes (1.0.8)
@@ -115,11 +124,13 @@ GEM
115
124
  coderay (~> 1.1.0)
116
125
  method_source (~> 0.8.1)
117
126
  slop (~> 3.4)
127
+ pry-nav (0.2.4)
128
+ pry (>= 0.9.10, < 0.11.0)
118
129
  pygments.rb (0.6.0)
119
130
  posix-spawn (~> 0.3.6)
120
131
  yajl-ruby (~> 1.1.0)
121
132
  rack (1.6.0)
122
- rack-test (0.6.2)
133
+ rack-test (0.6.3)
123
134
  rack (>= 1.0)
124
135
  rails (4.2.0)
125
136
  actionmailer (= 4.2.0)
@@ -204,6 +215,11 @@ GEM
204
215
  execjs (>= 0.3.0)
205
216
  json (>= 1.8.0)
206
217
  watchr (0.7)
218
+ web-console (2.0.0)
219
+ activemodel (~> 4.0)
220
+ binding_of_caller (>= 0.7.2)
221
+ railties (~> 4.0)
222
+ sprockets-rails (>= 2.0, < 4.0)
207
223
  yajl-ruby (1.1.0)
208
224
  yard (0.8.7.6)
209
225
 
@@ -219,14 +235,15 @@ DEPENDENCIES
219
235
  bcrypt (~> 3.1.7)
220
236
  bourne (~> 1.3)
221
237
  bundler (~> 1.1)
222
- coffee-rails (~> 4.0.0)
238
+ byebug
239
+ coffee-rails (~> 4.1.0)
223
240
  jbuilder (~> 2.0)
224
241
  jdbc-sqlite3
225
242
  jquery-rails
226
243
  jruby-openssl
227
244
  minitest-reporters
228
245
  protected_attributes (~> 1.0.6)
229
- pry
246
+ pry-nav
230
247
  pygments.rb
231
248
  rails (~> 4.2.0)
232
249
  rake (>= 0.9.2)
@@ -242,4 +259,5 @@ DEPENDENCIES
242
259
  turbolinks
243
260
  uglifier (>= 1.3.0)
244
261
  watchr
262
+ web-console (~> 2.0)
245
263
  yard
@@ -149,6 +149,8 @@ module Shoulda
149
149
  SetFlashMatcher.new
150
150
  end
151
151
 
152
+ # @deprecated Use {#set_flash} instead.
153
+ # @return [SetFlashMatcher]
152
154
  def set_the_flash
153
155
  Shoulda::Matchers.warn_about_deprecated_method(
154
156
  :set_the_flash,
@@ -1,7 +1,8 @@
1
1
  require 'shoulda/matchers/active_model/helpers'
2
2
  require 'shoulda/matchers/active_model/validation_matcher'
3
- require 'shoulda/matchers/active_model/validation_message_finder'
4
- require 'shoulda/matchers/active_model/exception_message_finder'
3
+ require 'shoulda/matchers/active_model/validator'
4
+ require 'shoulda/matchers/active_model/strict_validator'
5
+ require 'shoulda/matchers/active_model/validator_with_captured_range_error'
5
6
  require 'shoulda/matchers/active_model/allow_value_matcher'
6
7
  require 'shoulda/matchers/active_model/disallow_value_matcher'
7
8
  require 'shoulda/matchers/active_model/validate_length_of_matcher'
@@ -177,9 +177,9 @@ module Shoulda
177
177
 
178
178
  def initialize(*values)
179
179
  self.values_to_match = values
180
- self.message_finder_factory = ValidationMessageFinder
181
180
  self.options = {}
182
181
  self.after_setting_value_callback = -> {}
182
+ self.validator = Validator.new
183
183
  end
184
184
 
185
185
  def for(attribute)
@@ -189,7 +189,7 @@ module Shoulda
189
189
  end
190
190
 
191
191
  def on(context)
192
- @context = context
192
+ validator.context = context
193
193
  self
194
194
  end
195
195
 
@@ -205,7 +205,7 @@ module Shoulda
205
205
  end
206
206
 
207
207
  def strict
208
- self.message_finder_factory = ExceptionMessageFinder
208
+ validator.strict = true
209
209
  self
210
210
  end
211
211
 
@@ -215,16 +215,18 @@ module Shoulda
215
215
 
216
216
  def matches?(instance)
217
217
  self.instance = instance
218
+ validator.record = instance
218
219
 
219
220
  values_to_match.none? do |value|
221
+ validator.reset
220
222
  self.value = value
221
- set_value(value)
222
- errors_match?
223
+ set_attribute(value)
224
+ errors_match? || any_range_error_occurred?
223
225
  end
224
226
  end
225
227
 
226
228
  def failure_message
227
- "Did not expect #{expectation},\ngot error: #{matched_error}"
229
+ "Did not expect #{expectation},\ngot#{error_description}"
228
230
  end
229
231
  alias failure_message_for_should failure_message
230
232
 
@@ -234,26 +236,45 @@ module Shoulda
234
236
  alias failure_message_for_should_not failure_message_when_negated
235
237
 
236
238
  def description
237
- message_finder.allow_description(allowed_values)
239
+ validator.allow_description(allowed_values)
238
240
  end
239
241
 
240
242
  protected
241
243
 
242
- attr_accessor :values_to_match, :message_finder_factory,
243
- :instance, :attribute_to_set, :attribute_to_check_message_against,
244
- :context, :value, :matched_error, :after_setting_value_callback
244
+ attr_reader :attribute_to_check_message_against
245
+ attr_accessor :values_to_match, :instance, :attribute_to_set, :value,
246
+ :matched_error, :after_setting_value_callback, :validator
245
247
 
246
- def set_value(value)
247
- instance.__send__("#{attribute_to_set}=", value)
248
+ def attribute_to_check_message_against=(attribute)
249
+ @attribute_to_check_message_against = attribute
250
+ validator.attribute = attribute
251
+ end
252
+
253
+ def set_attribute(value)
254
+ set_attribute_ignoring_range_errors(value)
248
255
  after_setting_value_callback.call
249
256
  end
250
257
 
258
+ def set_attribute_ignoring_range_errors(value)
259
+ instance.__send__("#{attribute_to_set}=", value)
260
+ rescue RangeError => exception
261
+ # Have to reset the attribute so that we don't get a RangeError the
262
+ # next time we attempt to write the attribute (ActiveRecord seems to
263
+ # set the attribute to the "bad" value anyway)
264
+ reset_attribute
265
+ validator.capture_range_error(exception)
266
+ end
267
+
268
+ def reset_attribute
269
+ instance.send(:raw_write_attribute, attribute_to_set, nil)
270
+ end
271
+
251
272
  def errors_match?
252
273
  has_messages? && errors_for_attribute_match?
253
274
  end
254
275
 
255
276
  def has_messages?
256
- message_finder.has_messages?
277
+ validator.has_messages?
257
278
  end
258
279
 
259
280
  def errors_for_attribute_match?
@@ -265,7 +286,7 @@ module Shoulda
265
286
  end
266
287
 
267
288
  def errors_for_attribute
268
- message_finder.messages
289
+ validator.formatted_messages
269
290
  end
270
291
 
271
292
  def errors_match_regexp?
@@ -280,30 +301,25 @@ module Shoulda
280
301
  end
281
302
  end
282
303
 
304
+ def any_range_error_occurred?
305
+ validator.captured_range_error?
306
+ end
307
+
283
308
  def expectation
284
309
  parts = [
285
- error_source,
286
- includes_expected_message,
310
+ expected_messages_description,
287
311
  "when #{attribute_to_set} is set to #{value.inspect}"
288
312
  ]
289
313
 
290
314
  parts.join(' ').squeeze(' ')
291
315
  end
292
316
 
293
- def includes_expected_message
294
- if expected_message
295
- "to include #{expected_message.inspect}"
296
- else
297
- ''
298
- end
299
- end
300
-
301
- def error_source
302
- message_finder.source_description
317
+ def expected_messages_description
318
+ validator.expected_messages_description(expected_message)
303
319
  end
304
320
 
305
321
  def error_description
306
- message_finder.messages_description
322
+ validator.messages_description
307
323
  end
308
324
 
309
325
  def allowed_values
@@ -325,7 +341,7 @@ module Shoulda
325
341
  end
326
342
 
327
343
  def default_expected_message
328
- message_finder.expected_message_from(default_attribute_message)
344
+ validator.expected_message_from(default_attribute_message)
329
345
  end
330
346
 
331
347
  def default_attribute_message
@@ -348,10 +364,6 @@ module Shoulda
348
364
  def model_name
349
365
  instance.class.to_s.underscore
350
366
  end
351
-
352
- def message_finder
353
- message_finder_factory.new(instance, attribute_to_check_message_against, context)
354
- end
355
367
  end
356
368
  end
357
369
  end
@@ -24,32 +24,18 @@ module Shoulda
24
24
  end.join("\n")
25
25
  end
26
26
 
27
- # Helper method that determines the default error message used by Active
28
- # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
29
- # introduced I18n module used for localization. Use with Rails 3.0 and
30
- # up will delegate to ActiveModel::Errors.generate_error if a model
31
- # instance is given.
32
- #
33
- # default_error_message(:blank)
34
- # default_error_message(:too_short, count: 5)
35
- # default_error_message(:too_long, count: 60)
36
- # default_error_message(:blank, model_name: 'user', attribute: 'name')
37
- # default_error_message(:blank, instance: #<Model>, attribute: 'name')
38
- def default_error_message(key, options = {})
27
+ def default_error_message(type, options = {})
39
28
  model_name = options.delete(:model_name)
40
29
  attribute = options.delete(:attribute)
41
30
  instance = options.delete(:instance)
42
31
 
43
- if instance && instance.errors.respond_to?(:generate_message)
44
- instance.errors.generate_message(attribute.to_sym, key, options)
45
- else
46
- default_translation = [ :"activerecord.errors.models.#{model_name}.#{key}",
47
- :"activerecord.errors.messages.#{key}",
48
- :"errors.attributes.#{attribute}.#{key}",
49
- :"errors.messages.#{key}" ]
50
- I18n.translate(:"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{key}",
51
- { default: default_translation }.merge(options))
52
- end
32
+ RailsShim.generate_validation_message(
33
+ instance,
34
+ attribute.to_sym,
35
+ type,
36
+ model_name,
37
+ options
38
+ )
53
39
  end
54
40
  end
55
41
  end