Skip to content

Commit

Permalink
Personnummer can be used in hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mange committed Jul 4, 2022
1 parent 930195c commit 7800b9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/swedish_pin/personnummer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ def <=>(other)
end
end

def hash
[self.class, format_long].hash
end
alias_method :eql?, :==

private

def short_separator(now)
Expand Down
16 changes: 16 additions & 0 deletions test/test_personnummer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,20 @@ def test_sorting_with_nil_crashes_with_expected_error
[pin, nil].sort
end
end

def test_hashes_on_underlying_pin
pin1 = SwedishPIN.generate
pin2 = SwedishPIN.generate

hash = {pin1 => 1}

assert_equal 1, hash[pin1]
assert_nil hash[pin2]

pin1_copy = SwedishPIN.parse(pin1.format_long)
assert_equal 1, hash[pin1_copy]

assert_nil hash[pin1.format_long]
assert_nil hash[pin1.format_short]
end
end

0 comments on commit 7800b9f

Please sign in to comment.