Skip to content

Commit

Permalink
Add test and note for ULID#<=> signature (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick authored Jul 3, 2022
1 parent 07cefa0 commit 14fcca1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/ulid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def hash
[ULID, @integer].hash
end

# @return [Integer, nil]
# @return [-1, 0, 1, nil]
def <=>(other)
(ULID === other) ? (@integer <=> other.to_i) : nil
end
Expand Down
7 changes: 6 additions & 1 deletion sig/ulid.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,12 @@ class ULID < Object
# #=> true
# ```
#
# To be precise, this sorting unaffected with `case sensitive or not` and might handle [ulid/spec#57](https://github.com/ulid/spec/pull/57) in future. So preferable than `lexicographically sortable` in actual case.
# To be precise, this sorting unaffected with `case sensitive or not` and might handle [ulid/spec#57](https://github.com/ulid/spec/pull/57) in future.
# So preferable than `lexicographically sortable` in actual case.
#
# This returns -1 | 0 | 1 for ULIDs. However defined as returning Integer. It is caused on ruby/rbs current definition.
# https://github.com/ruby/ruby/blob/cd34f56d450f2310cceaf4c5f34d23eddfda58e8/numeric.c#L4646-L4660
# https://github.com/ruby/rbs/blob/14abbbae8885a09a2ed82de2ef31d67a9c0a108d/core/integer.rbs#L461-L462
#
def <=>: (ULID other) -> Integer
| (untyped other) -> nil
Expand Down
5 changes: 5 additions & 0 deletions test/core/test_ulid_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ def test_eqq

def test_cmp
ulid = ULID.sample
assert_equal(1, ulid <=> ulid.pred)
assert_equal(1, ulid <=> ulid.pred.pred.pred)
assert_equal(0, ulid <=> ULID.parse(ulid.to_s))
assert_equal(-1, ulid <=> ulid.next)
assert_equal(-1, ulid <=> ulid.next.next.next)
[nil, BasicObject.new, '01ARZ3NDEKTSV4RRFFQ69G5FAV', 42, Time.now].each do |not_comparable|
assert_nil(ulid <=> not_comparable)
end
Expand Down

0 comments on commit 14fcca1

Please sign in to comment.