Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow beginless and endless ranges in validates_inclusion_of #1615

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def in_array(array)

def in_range(range)
@range = range
@minimum = range.first
@maximum = range.max
@minimum = minimum_range_value
@maximum = maximum_range_value
self
end

Expand Down Expand Up @@ -400,6 +400,18 @@ def does_not_match?(subject)

private

def minimum_range_value
@range.begin
end

def maximum_range_value
if @range.exclude_end?
@range.end ? (@range.end - 1) : nil
else
@range.end
end
end

def matches_for_range?
disallows_lower_value &&
allows_minimum_value &&
Expand Down Expand Up @@ -441,27 +453,27 @@ def disallows_lower_value
end

def allows_minimum_value
allows_value_of(@minimum, @low_message)
@minimum.nil? || allows_value_of(@minimum, @low_message)
end

def disallows_minimum_value
disallows_value_of(@minimum, @low_message)
@minimum.nil? || disallows_value_of(@minimum, @low_message)
end

def allows_maximum_value
allows_value_of(@maximum, @high_message)
@maximum.nil? || allows_value_of(@maximum, @high_message)
end

def disallows_maximum_value
disallows_value_of(@maximum, @high_message)
@maximum.nil? || disallows_value_of(@maximum, @high_message)
end

def allows_higher_value
allows_value_of(@maximum + 1, @high_message)
@maximum.nil? || allows_value_of(@maximum + 1, @high_message)
end

def disallows_higher_value
disallows_value_of(@maximum + 1, @high_message)
@maximum.nil? || disallows_value_of(@maximum + 1, @high_message)
end

def allows_all_values_in_array?
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def self.inspect_values(values)
end

def self.inspect_range(range)
"#{inspect_value(range.first)} to #{inspect_value(range.last)}"
"#{inspect_value(range.begin)} to #{inspect_value(range.end)}"
end

def self.inspect_hash(hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,22 @@ def configure_validation_matcher(matcher)
expect_to_match_on_values(builder, possible_values)
end

it 'matches given a beginless range that covers the possible values' do
builder = build_object_allowing(possible_values)
expect_to_match_on_values(
builder,
Range.new(nil, possible_values.last),
)
end

it 'matches given a endless range that covers the possible values' do
builder = build_object_allowing(possible_values)
expect_to_match_on_values(
builder,
Range.new(possible_values.first, nil),
)
end

it 'does not match given a range whose start value falls outside valid range' do
builder = build_object_allowing(possible_values)
expect_not_to_match_on_values(
Expand Down
Loading