Skip to content

Commit

Permalink
fix: Adjust negative form of ValidateNumericalityMatcher (#1603)
Browse files Browse the repository at this point in the history
This commit adjusts the negative form of the `ValidateNumericalityMatcher`
so that is does not use the `does_not_match?` method, but actually uses the
negative result of the `matches?` method. This is easy to understand and
does not require any special handling for the negative form.

Previously this matcher on the negative form was always returning `true`,
as there were no `does_not_match?` method defined, which caused it to
use the `does_not_match?` method from the `ValidationMatcher` parent class,
which always returns `true`, this commit fixes that.
  • Loading branch information
matsales28 committed Jan 3, 2024
1 parent 2a2b062 commit d611911
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
5 changes: 0 additions & 5 deletions lib/shoulda/matchers/active_model/comparison_matcher.rb
Expand Up @@ -78,11 +78,6 @@ def matches?(subject)
comparison_submatchers.matches?(subject)
end

def does_not_match?(subject)
@subject = subject
comparison_submatchers.does_not_match?(subject)
end

def comparison_description
"#{comparison_expectation} #{@value}"
end
Expand Down
Expand Up @@ -13,11 +13,6 @@ def matches?(subject)
failing_submatchers.empty?
end

def does_not_match?(subject)
@subject = subject
non_failing_submatchers.empty?
end

def failure_message
failing_submatcher.failure_message
end
Expand Down
Expand Up @@ -465,7 +465,7 @@ def first_submatcher_that_fails_to_match
def first_submatcher_that_fails_to_not_match
@_first_submatcher_that_fails_to_not_match ||=
@submatchers.detect do |submatcher|
!submatcher.does_not_match?(subject)
submatcher.matches?(subject)
end
end

Expand Down
Expand Up @@ -1987,6 +1987,56 @@ def validation_matcher_scenario_args
end
end

describe 'using in the negative form without numericality validations' do
context 'when qualified with is_greater_than' do
it 'accepts' do
record = (define_model :example, attr: :integer).new

expect(record).not_to validate_numericality.is_greater_than(18)
end
end

context 'when qualified with is_greater_than_or_equal_to' do
it 'accepts' do
record = (define_model :example, attr: :integer).new

expect(record).not_to validate_numericality.is_greater_than_or_equal_to(18)
end
end

context 'when qualified with is_less_than_or_equal_to' do
it 'accepts' do
record = (define_model :example, attr: :integer).new

expect(record).not_to validate_numericality.is_less_than_or_equal_to(18)
end
end

context 'when qualified with is_less_than' do
it 'accepts' do
record = (define_model :example, attr: :integer).new

expect(record).not_to validate_numericality.is_less_than(18)
end
end

context 'when qualified with is_equal_to' do
it 'accepts' do
record = (define_model :example, attr: :integer).new

expect(record).not_to validate_numericality.is_equal_to(18)
end
end

context 'when qualified with is_other_than' do
it 'accepts' do
record = (define_model :example, attr: :integer).new

expect(record).not_to validate_numericality.is_other_than(18)
end
end
end

describe '#description' do
context 'qualified with nothing' do
it 'describes that it allows numbers' do
Expand Down

0 comments on commit d611911

Please sign in to comment.