Skip to content
Open
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
36 changes: 26 additions & 10 deletions core/kernel.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ module Kernel : BasicObject
# c(4) #=> []
# c(5) #=> nil
#
def self?.caller: (Integer start_or_range, ?Integer length) -> ::Array[String]?
| (::Range[Integer] start_or_range) -> ::Array[String]?
| () -> ::Array[String]
def self?.caller: () -> Array[String]
| (int start, ?int? length) -> Array[String]?
| (range[int] range) -> Array[String]?

# <!--
# rdoc-file=vm_backtrace.c
Expand All @@ -204,8 +204,9 @@ module Kernel : BasicObject
# Optionally you can pass a range, which will return an array containing the
# entries within the specified range.
#
def self?.caller_locations: (?Integer start_or_range, ?Integer length) -> ::Array[Thread::Backtrace::Location]?
| (?::Range[Integer] start_or_range) -> ::Array[Thread::Backtrace::Location]?
def self?.caller_locations: () -> Array[Thread::Backtrace::Location]
| (int start, ?int? length) -> Array[Thread::Backtrace::Location]?
| (range[int] range) -> Array[Thread::Backtrace::Location]?

# <!--
# rdoc-file=vm_eval.c
Expand Down Expand Up @@ -310,6 +311,16 @@ module Kernel : BasicObject
#
def self?.block_given?: () -> bool

alias self.iterator? self.block_given?

# <!--
# rdoc-file=vm_eval.c
# - iterator? -> true or false
# -->
# Deprecated. Use block_given? instead.
#
alias iterator? block_given?

# <!--
# rdoc-file=vm_eval.c
# - local_variables -> array
Expand All @@ -322,7 +333,7 @@ module Kernel : BasicObject
# end
# local_variables #=> [:fred, :i]
#
def self?.local_variables: () -> ::Array[Symbol]
def self?.local_variables: () -> Array[Symbol]

# <!--
# rdoc-file=random.c
Expand Down Expand Up @@ -777,7 +788,7 @@ module Kernel : BasicObject
# If *const* is defined as autoload, the file name to be loaded is replaced with
# *filename*. If *const* is defined but not as autoload, does nothing.
#
def self?.autoload: (interned _module, String filename) -> NilClass
def self?.autoload: (interned const, path filename) -> nil

# <!--
# rdoc-file=load.c
Expand All @@ -801,7 +812,7 @@ module Kernel : BasicObject
# autoload?(:B) #=> "b"
# end
#
def self?.autoload?: (interned name) -> String?
def self?.autoload?: (interned name, ?boolish inherit) -> String?

# <!--
# rdoc-file=proc.c
Expand Down Expand Up @@ -1175,7 +1186,7 @@ module Kernel : BasicObject
#
# global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
#
def self?.global_variables: () -> ::Array[Symbol]
def self?.global_variables: () -> Array[Symbol]

# <!--
# rdoc-file=load.c
Expand Down Expand Up @@ -1826,7 +1837,12 @@ module Kernel : BasicObject
# ------------|---------------------------------------------
# <tt>'-'</tt>|Whether the entities exist and are identical.
#
def self?.test: (String | Integer cmd, String | IO file1, ?String | IO file2) -> (TrueClass | FalseClass | Time | nil | Integer)
def self?.test: ('b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'G' | 'k' | 'l' | 'o' | 'O' | 'p' | 'r' | 'R' | 'S' | 'u' | 'w' | 'W' | 'x' | 'X' | 'z' |
98 | 99 | 100 | 101 | 102 | 103 | 71 | 107 | 108 | 111 | 79 | 112 | 114 | 82 | 83 | 117 | 119 | 87 | 120 | 88 | 122, path filepath) -> bool
| ('s' | 115, path filepath) -> Integer?
| ('A' | 'M' | 'C' | 65 | 77 | 67, path filepath) -> Time
| ('<' | '=' | '>' | '-' | 60 | 61 | 62 | 45, path filepath1, path filepath2) -> bool
| (String | int cmd, path filepath1, ?path filepath2) -> (bool | Time | Integer | nil)

# <!--
# rdoc-file=vm_eval.c
Expand Down
7 changes: 6 additions & 1 deletion lib/rbs/test/type_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ def value(val, type)
when Types::Variable
true
when Types::Literal
type.literal == val
begin
type.literal == val
rescue NoMethodError
raise if defined?(val.==)
false
end
when Types::Union
type.types.any? {|type| value(val, type) }
when Types::Intersection
Expand Down
236 changes: 229 additions & 7 deletions test/stdlib/Kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,239 @@ def test_String
Kernel, :String, ToS.new
end

TOPLEVEL___callee__ = __callee__ # outside of a method
def test___callee__
assert_send_type '() -> Symbol',
Kernel, :__callee__
assert_type 'nil', TOPLEVEL___callee__
end

TOPLEVEL___method__ = __method__ # outside of a method
def test___method__
assert_send_type '() -> Symbol',
Kernel, :__method__
assert_type 'nil', TOPLEVEL___method__
end

def test___dir__
assert_send_type '() -> String',
Kernel, :__dir__

# Make sure it can return `nil`; this can't go through `assert_send_type`,
# as it's only `nil` thru `eval`s
assert_equal nil, eval('__dir__')
end

def test_autoload
with_interned :TestModuleForAutoload do |const|
with_path '/does/not/exist' do |path|
assert_send_type '(interned, path) -> nil',
Kernel, :autoload, const, path
end
end
end

def test_autoload?
with_interned :TestModuleForAutoload do |interned|
assert_send_type "(::interned) -> String?",
Kernel, :autoload?, interned
with_interned :TestModuleForAutoloadP do |const|
assert_send_type '(interned) -> nil',
Kernel, :autoload?, const

with_boolish do |inherit|
assert_send_type '(interned, boolish) -> nil',
Kernel, :autoload?, const, inherit
end
end

# Unfortunately, `autoload` doesn't play well with `assert_send_type`
Kernel.autoload :TestModuleForAutoloadP, '/does/not/exist'

with_interned :TestModuleForAutoloadP do |const|
assert_type 'String', Kernel.autoload?(const)

with_boolish do |inherit|
assert_type 'String', Kernel.autoload?(const, inherit)
end
end
end

def test_binding
assert_send_type '() -> Binding',
Kernel, :binding
end

def test_block_given?(method: :block_given?)
assert_send_type '() -> bool',
Kernel, method
end

def test_iterator?
silence_warning :deprecated do
test_block_given?(method: :iterator?)
end
end

def test_caller
assert_send_type '() -> Array[String]',
Kernel, :caller

with_int 1 do |start|
assert_send_type '(int) -> Array[String]',
Kernel, :caller, start

with_int(2).and_nil do |length|
assert_send_type '(int, int?) -> Array[String]',
Kernel, :caller, start, length
end
end

with_int 100000 do |start|
assert_send_type '(int) -> nil',
Kernel, :caller, start

with_int(2).and_nil do |length|
assert_send_type '(int, int?) -> nil',
Kernel, :caller, start, length
end
end

with_range with_int(1), with_int(2) do |range|
assert_send_type '(range[int]) -> Array[String]',
Kernel, :caller, range
end

with_range with_int(100000) ,with_int(100001) do |range|
assert_send_type '(range[int]) -> nil',
Kernel, :caller, range
end
end

def test_caller_locations
assert_send_type '() -> Array[Thread::Backtrace::Location]',
Kernel, :caller_locations

with_int 1 do |start|
assert_send_type '(int) -> Array[Thread::Backtrace::Location]',
Kernel, :caller_locations, start

with_int(2).and_nil do |length|
assert_send_type '(int, int?) -> Array[Thread::Backtrace::Location]',
Kernel, :caller_locations, start, length
end
end

with_int 100000 do |start|
assert_send_type '(int) -> nil',
Kernel, :caller_locations, start

with_int(2).and_nil do |length|
assert_send_type '(int, int?) -> nil',
Kernel, :caller_locations, start, length
end
end

with_range with_int(1), with_int(2) do |range|
assert_send_type '(range[int]) -> Array[Thread::Backtrace::Location]',
Kernel, :caller_locations, range
end

with_range with_int(100000) ,with_int(100001) do |range|
assert_send_type '(range[int]) -> nil',
Kernel, :caller_locations, range
end
end

def test_global_variables
assert_send_type '() -> Array[Symbol]',
Kernel, :global_variables
end

def test_local_variables
assert_send_type '() -> Array[Symbol]',
Kernel, :local_variables
end

def test_test
# true/false tests
with_path do |filepath|
%w[b c d e f g G k l o O p r R S u w W x X z].each do |test_char|
test_ord = test_char.ord

autoload :TestModuleForAutoload, '/shouldnt/be/executed'
with test_char, test_ord do |test_literal|
assert_send_type "('b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'G' | 'k' | 'l' | 'o' | 'O' | 'p' | 'r' | 'R' | 'S' | 'u' | 'w' | 'W' | 'x' | 'X' | 'z' |
98 | 99 | 100 | 101 | 102 | 103 | 71 | 107 | 108 | 111 | 79 | 112 | 114 | 82 | 83 | 117 | 119 | 87 | 120 | 88 | 122, path) -> bool",
Kernel, :test, test_literal, filepath
end

with_interned :TestModuleForAutoload do |interned|
assert_send_type "(::interned) -> String?",
Kernel, :autoload?, interned
with_int(test_ord).and test_char do |test_nonliteral|
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
Kernel, :test, test_nonliteral, filepath
end
end
end

# Integer? tests
%w[s].each do |test_char|
test_ord = test_char.ord

with_path __FILE__ do |filepath|
with test_char, test_ord do |test_literal|
assert_send_type "('s' | 115, path) -> Integer",
Kernel, :test, test_literal, filepath
end

with_int(test_ord).and test_char do |test_nonliteral|
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
Kernel, :test, test_nonliteral, filepath
end
end

with_path '/not/a/file' do |filepath|
with test_char, test_ord do |test_literal|
assert_send_type "('s' | 115, path) -> nil",
Kernel, :test, test_literal, filepath
end

with_int(test_ord).and test_char do |test_nonliteral|
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
Kernel, :test, test_nonliteral, filepath
end
end
end

# Time tests
with_path __FILE__ do |filepath|
%w[A M C].each do |test_char|
test_ord = test_char.ord

with test_char, test_ord do |test_literal|
assert_send_type "('A' | 'M' | 'C' | 65 | 77 | 67, path) -> Time",
Kernel, :test, test_literal, filepath
end

with_int(test_ord).and test_char do |test_nonliteral|
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
Kernel, :test, test_nonliteral, filepath
end
end
end

# Comparison Tests
with_path __dir__ + '/Integer_test.rb' do |filepath1|
with_path __FILE__ do |filepath2|
%w[< = > -].each do |test_char|
test_ord = test_char.ord

with test_char, test_ord do |test_literal|
assert_send_type "('<' | '=' | '>' | '-' | 60 | 61 | 62 | 45, path, path) -> bool",
Kernel, :test, test_literal, filepath1, filepath2
end

with_int(test_ord).and test_char do |test_nonliteral|
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
Kernel, :test, test_nonliteral, filepath1, filepath2
end
end
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/stdlib/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ def self.included(base)
end

RUBY_EXECUTABLE = ENV["RUBY"] || RbConfig.ruby

def silence_warning(which)
old_warning = Warning[which]
Warning[which] = false
yield
ensure
Warning[which] = old_warning
end
end

class StdlibTest < Test::Unit::TestCase
Expand Down