From af843e04419b2be77e5f64c5e44b668233d11492 Mon Sep 17 00:00:00 2001 From: Gustavo Cunha Date: Sat, 21 Mar 2020 09:30:27 +0100 Subject: [PATCH] Add support to numericality: :other_than --- .../comparison_matcher.rb | 6 +++- .../validate_numericality_of_matcher.rb | 32 +++++++++++++++++++ .../validate_numericality_of_matcher_spec.rb | 7 ++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb b/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb index 8110a8329..43307be95 100644 --- a/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +++ b/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb @@ -9,7 +9,8 @@ class ComparisonMatcher < ValidationMatcher :>= => :greater_than_or_equal_to, :< => :less_than, :<= => :less_than_or_equal_to, - :== => :equal_to + :== => :equal_to, + :!= => :other_than, } def initialize(numericality_matcher, value, operator) @@ -125,6 +126,8 @@ def assertions [true, false, false] when :<= [true, true, false] + when :!= + [true, false, true] end end @@ -146,6 +149,7 @@ def comparison_expectation when :== then "equal to" when :< then "less than" when :<= then "less than or equal to" + when :!= then 'other than' end end end diff --git a/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb index a4281be74..80484f79a 100644 --- a/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb @@ -204,6 +204,33 @@ module ActiveModel # is_greater_than(21) # end # + # ##### is_other_than + # + # Use `is_other_than` to test usage of the `:other_than` option. + # This asserts that the attribute can take a number which is not equal to + # the given value. + # + # class Person + # include ActiveModel::Model + # attr_accessor :legal_age + # + # validates_numericality_of :legal_age, other_than: 21 + # end + # + # # RSpec + # RSpec.describe Person, type: :model do + # it do + # should validate_numericality_of(:legal_age). + # is_other_than(21) + # end + # end + # + # # Minitest (Shoulda) + # class PersonTest < ActiveSupport::TestCase + # should validate_numericality_of(:legal_age). + # is_other_than(21) + # end + # # ##### even # # Use `even` to test usage of the `:even` option. This asserts that the @@ -395,6 +422,11 @@ def is_less_than_or_equal_to(value) self end + def is_other_than(value) + prepare_submatcher(comparison_matcher_for(value, :!=).for(@attribute)) + self + end + def with_message(message) @expects_custom_validation_message = true @expected_message = message diff --git a/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb index 6d492f01d..6c42f8dd6 100644 --- a/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb @@ -39,6 +39,13 @@ def all_qualifiers validation_name: :equal_to, validation_value: 1, }, + { + category: :comparison, + name: :is_other_than, + argument: 1, + validation_name: :other_than, + validation_value: 1, + }, { category: :cardinality, name: :odd,