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

+ Add Source::Range#eql? and hash #675

Merged
merged 1 commit into from Apr 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/parser/source/range.rb
Expand Up @@ -298,6 +298,15 @@ def <=>(other)
(@end_pos <=> other.end_pos)
end

alias_method :eql?, :==

##
# Support for Ranges be used in as Hash indices and in Sets.
#
def hash
[@source_buffer, @begin_pos, @end_pos].hash
end

##
# @return [String] a human-readable representation of this range.
#
Expand Down
15 changes: 15 additions & 0 deletions test/test_source_range.rb
Expand Up @@ -169,4 +169,19 @@ def test_with
assert_equal 1, sr3.begin_pos
assert_equal 4, sr3.end_pos
end

def test_eql_and_hash
assert_equal false, @sr1_3.eql?(@sr3_3)
assert @sr1_3.hash != @sr3_3.hash

also_1_3 = @sr3_3.with(begin_pos: 1)
assert_equal true, @sr1_3.eql?(also_1_3)
assert_equal @sr1_3.hash, also_1_3.hash

buf2 = Parser::Source::Buffer.new('(string)')
buf2.source = "foobar\nbaz"
from_other_buf = Parser::Source::Range.new(buf2, 1, 3)
assert_equal false, @sr1_3.eql?(from_other_buf)
assert @sr1_3.hash != from_other_buf.hash
end
end