Skip to content

Commit

Permalink
+ Add Source::Range#eql? and hash [#670]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Apr 14, 2020
1 parent f780d9a commit d315cbd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit d315cbd

Please sign in to comment.