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 test and note for ULID#<=> signature #204

Merged
merged 1 commit into from
Jul 3, 2022
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
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