Skip to content

Commit

Permalink
Fix Kernel.rand signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Apr 3, 2024
1 parent 7084d83 commit 3470374
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
8 changes: 4 additions & 4 deletions core/kernel.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,10 +1318,10 @@ module Kernel : BasicObject
#
# See also Random.rand.
#
def self?.rand: () -> Float
| (Integer arg0) -> Integer
| (::Range[Integer] arg0) -> Integer
| (::Range[Float] arg0) -> Float
def self?.rand: (?0) -> Float
| (int arg0) -> (Integer | Float)
| (::Range[Integer] arg0) -> Integer?
| (::Range[Float] arg0) -> Float?

# <!--
# rdoc-file=io.c
Expand Down
1 change: 1 addition & 0 deletions lib/rbs/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Test
INSPECT = Kernel.instance_method(:inspect)
METHODS = Kernel.instance_method(:methods)
RESPOND_TOP = Kernel.instance_method(:respond_to?)
EQEQ = BasicObject.instance_method(:==)

class ArgumentsReturn
include Guaranteed::Inspect
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/test/type_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def value(val, type)
when Types::Variable
true
when Types::Literal
type.literal == val
Test.call(val, EQEQ, type.literal)
when Types::Union
type.types.any? {|type| value(val, type) }
when Types::Intersection
Expand Down
3 changes: 3 additions & 0 deletions sig/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ module RBS
# Kernel#respond_to?
RESPOND_TOP: UnboundMethod

# BasicObject#==
EQEQ: UnboundMethod

class ArgumentsReturn
type exit_type = :return | :exception | :break

Expand Down
13 changes: 13 additions & 0 deletions test/stdlib/Kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ def test_autoload?
Kernel, :autoload?, interned
end
end

def test_rand
assert_send_type "() -> Float", Kernel, :rand
assert_send_type "(0) -> Float", Kernel, :rand, 0
assert_send_type "(_ToInt) -> Float", Kernel, :rand, 0.0
assert_send_type "(_ToInt) -> Float", Kernel, :rand, 0r
assert_send_type "(_ToInt) -> Float", Kernel, :rand, 0i
assert_send_type "(_ToInt) -> Integer", Kernel, :rand, 10
assert_send_type "(Range[Integer]) -> Integer", Kernel, :rand, 1..10
assert_send_type "(Range[Integer]) -> nil", Kernel, :rand, 0...0
assert_send_type "(Range[Float]) -> Float", Kernel, :rand, 0.0...10.0
assert_send_type "(Range[Float]) -> nil", Kernel, :rand, 0.0...0.0
end
end

class KernelTest < StdlibTest
Expand Down

0 comments on commit 3470374

Please sign in to comment.