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

Move mismatch of Range#cover? and Range#include? out of shared folder #1053

Merged
merged 1 commit into from
Aug 17, 2023
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
4 changes: 4 additions & 0 deletions core/range/cover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
it_behaves_like :range_cover_and_include, :cover?
it_behaves_like :range_cover, :cover?
it_behaves_like :range_cover_subrange, :cover?

it "covers U+9995 in the range U+0999..U+9999" do
("\u{999}".."\u{9999}").cover?("\u{9995}").should be_true
end
end
4 changes: 4 additions & 0 deletions core/range/include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
describe "Range#include?" do
it_behaves_like :range_cover_and_include, :include?
it_behaves_like :range_include, :include?

it "does not include U+9995 in the range U+0999..U+9999" do
("\u{999}".."\u{9999}").include?("\u{9995}").should be_false
end
end
1 change: 0 additions & 1 deletion core/range/shared/cover_and_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
it "returns true if argument is less than the last value of the range and greater than the first value" do
(20..30).send(@method, 28).should be_true
('e'..'h').send(@method, 'g').should be_true
("\u{999}".."\u{9999}").send @method, "\u{9995}"
Copy link
Member

@andrykonchin andrykonchin Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

There is a missing should so no check actually performed.

But titles of the proposed new cases for include?/cover? don't describe their behaviour precisely.

The #cover? method returns true because it compares begin/end and value with <=> (or <, >) methods and indeed "\u{999}" < "\u{9995}" < "\u{9999}".

But #include? for non-ascii String begin/end uses the #succ method (that seems alphabetic-aware) and iterates from begin to end with #succ looking for a value. But "\u{9999}" and even "\u{9995}" isn't reachable from "\u{999}" - the largest reachable symbol is "\u{9b6}" (I suppose because "\u{999}" and "\u{9995}" belong to different alphabets). I assume #succ and consequently #include? should work well with characters of the same alphabet only.

So I would suggest changing titles of the new cases to describe the methods behaviour better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the descriptions. The inner encoding noob inside of me thinks they are correct now, so check them again please ;)

end

it "returns true if argument is sole element in the range" do
Expand Down