Skip to content

Commit ff561ff

Browse files
committed
[Kernel] Updated querying methods
1 parent 4482ed2 commit ff561ff

File tree

4 files changed

+269
-18
lines changed

4 files changed

+269
-18
lines changed

core/kernel.rbs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ module Kernel : BasicObject
179179
# c(4) #=> []
180180
# c(5) #=> nil
181181
#
182-
def self?.caller: (Integer start_or_range, ?Integer length) -> ::Array[String]?
183-
| (::Range[Integer] start_or_range) -> ::Array[String]?
184-
| () -> ::Array[String]
182+
def self?.caller: () -> Array[String]
183+
| (int start, ?int? length) -> Array[String]?
184+
| (range[int] range) -> Array[String]?
185185

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

210211
# <!--
211212
# rdoc-file=vm_eval.c
@@ -310,6 +311,16 @@ module Kernel : BasicObject
310311
#
311312
def self?.block_given?: () -> bool
312313

314+
alias self.iterator? self.block_given?
315+
316+
# <!--
317+
# rdoc-file=vm_eval.c
318+
# - iterator? -> true or false
319+
# -->
320+
# Deprecated. Use block_given? instead.
321+
#
322+
alias iterator? block_given?
323+
313324
# <!--
314325
# rdoc-file=vm_eval.c
315326
# - local_variables -> array
@@ -322,7 +333,7 @@ module Kernel : BasicObject
322333
# end
323334
# local_variables #=> [:fred, :i]
324335
#
325-
def self?.local_variables: () -> ::Array[Symbol]
336+
def self?.local_variables: () -> Array[Symbol]
326337

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

782793
# <!--
783794
# rdoc-file=load.c
@@ -801,7 +812,7 @@ module Kernel : BasicObject
801812
# autoload?(:B) #=> "b"
802813
# end
803814
#
804-
def self?.autoload?: (interned name) -> String?
815+
def self?.autoload?: (interned name, ?boolish inherit) -> String?
805816

806817
# <!--
807818
# rdoc-file=proc.c
@@ -1175,7 +1186,7 @@ module Kernel : BasicObject
11751186
#
11761187
# global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
11771188
#
1178-
def self?.global_variables: () -> ::Array[Symbol]
1189+
def self?.global_variables: () -> Array[Symbol]
11791190

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

18311847
# <!--
18321848
# rdoc-file=vm_eval.c

lib/rbs/test/type_check.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,12 @@ def value(val, type)
340340
when Types::Variable
341341
true
342342
when Types::Literal
343-
type.literal == val
343+
begin
344+
type.literal == val
345+
rescue NoMethodError
346+
raise if defined?(val.==)
347+
false
348+
end
344349
when Types::Union
345350
type.types.any? {|type| value(val, type) }
346351
when Types::Intersection

test/stdlib/Kernel_test.rb

Lines changed: 229 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,239 @@ def test_String
8181
Kernel, :String, ToS.new
8282
end
8383

84+
TOPLEVEL___callee__ = __callee__ # outside of a method
85+
def test___callee__
86+
assert_send_type '() -> Symbol',
87+
Kernel, :__callee__
88+
assert_type 'nil', TOPLEVEL___callee__
89+
end
90+
91+
TOPLEVEL___method__ = __method__ # outside of a method
92+
def test___method__
93+
assert_send_type '() -> Symbol',
94+
Kernel, :__method__
95+
assert_type 'nil', TOPLEVEL___method__
96+
end
97+
98+
def test___dir__
99+
assert_send_type '() -> String',
100+
Kernel, :__dir__
101+
102+
# Make sure it can return `nil`; this can't go through `assert_send_type`,
103+
# as it's only `nil` thru `eval`s
104+
assert_equal nil, eval('__dir__')
105+
end
106+
107+
def test_autoload
108+
with_interned :TestModuleForAutoload do |const|
109+
with_path '/does/not/exist' do |path|
110+
assert_send_type '(interned, path) -> nil',
111+
Kernel, :autoload, const, path
112+
end
113+
end
114+
end
115+
84116
def test_autoload?
85-
with_interned :TestModuleForAutoload do |interned|
86-
assert_send_type "(::interned) -> String?",
87-
Kernel, :autoload?, interned
117+
with_interned :TestModuleForAutoloadP do |const|
118+
assert_send_type '(interned) -> nil',
119+
Kernel, :autoload?, const
120+
121+
with_boolish do |inherit|
122+
assert_send_type '(interned, boolish) -> nil',
123+
Kernel, :autoload?, const, inherit
124+
end
125+
end
126+
127+
# Unfortunately, `autoload` doesn't play well with `assert_send_type`
128+
Kernel.autoload :TestModuleForAutoloadP, '/does/not/exist'
129+
130+
with_interned :TestModuleForAutoloadP do |const|
131+
assert_type 'String', Kernel.autoload?(const)
132+
133+
with_boolish do |inherit|
134+
assert_type 'String', Kernel.autoload?(const, inherit)
135+
end
136+
end
137+
end
138+
139+
def test_binding
140+
assert_send_type '() -> Binding',
141+
Kernel, :binding
142+
end
143+
144+
def test_block_given?(method: :block_given?)
145+
assert_send_type '() -> bool',
146+
Kernel, method
147+
end
148+
149+
def test_iterator?
150+
silence_warning :deprecated do
151+
test_block_given?(method: :iterator?)
152+
end
153+
end
154+
155+
def test_caller
156+
assert_send_type '() -> Array[String]',
157+
Kernel, :caller
158+
159+
with_int 1 do |start|
160+
assert_send_type '(int) -> Array[String]',
161+
Kernel, :caller, start
162+
163+
with_int(2).and_nil do |length|
164+
assert_send_type '(int, int?) -> Array[String]',
165+
Kernel, :caller, start, length
166+
end
167+
end
168+
169+
with_int 100000 do |start|
170+
assert_send_type '(int) -> nil',
171+
Kernel, :caller, start
172+
173+
with_int(2).and_nil do |length|
174+
assert_send_type '(int, int?) -> nil',
175+
Kernel, :caller, start, length
176+
end
177+
end
178+
179+
with_range with_int(1), with_int(2) do |range|
180+
assert_send_type '(range[int]) -> Array[String]',
181+
Kernel, :caller, range
182+
end
183+
184+
with_range with_int(100000) ,with_int(100001) do |range|
185+
assert_send_type '(range[int]) -> nil',
186+
Kernel, :caller, range
88187
end
188+
end
189+
190+
def test_caller_locations
191+
assert_send_type '() -> Array[Thread::Backtrace::Location]',
192+
Kernel, :caller_locations
193+
194+
with_int 1 do |start|
195+
assert_send_type '(int) -> Array[Thread::Backtrace::Location]',
196+
Kernel, :caller_locations, start
197+
198+
with_int(2).and_nil do |length|
199+
assert_send_type '(int, int?) -> Array[Thread::Backtrace::Location]',
200+
Kernel, :caller_locations, start, length
201+
end
202+
end
203+
204+
with_int 100000 do |start|
205+
assert_send_type '(int) -> nil',
206+
Kernel, :caller_locations, start
207+
208+
with_int(2).and_nil do |length|
209+
assert_send_type '(int, int?) -> nil',
210+
Kernel, :caller_locations, start, length
211+
end
212+
end
213+
214+
with_range with_int(1), with_int(2) do |range|
215+
assert_send_type '(range[int]) -> Array[Thread::Backtrace::Location]',
216+
Kernel, :caller_locations, range
217+
end
218+
219+
with_range with_int(100000) ,with_int(100001) do |range|
220+
assert_send_type '(range[int]) -> nil',
221+
Kernel, :caller_locations, range
222+
end
223+
end
224+
225+
def test_global_variables
226+
assert_send_type '() -> Array[Symbol]',
227+
Kernel, :global_variables
228+
end
229+
230+
def test_local_variables
231+
assert_send_type '() -> Array[Symbol]',
232+
Kernel, :local_variables
233+
end
234+
235+
def test_test
236+
# true/false tests
237+
with_path do |filepath|
238+
%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|
239+
test_ord = test_char.ord
89240

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

92-
with_interned :TestModuleForAutoload do |interned|
93-
assert_send_type "(::interned) -> String?",
94-
Kernel, :autoload?, interned
247+
with_int(test_ord).and test_char do |test_nonliteral|
248+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
249+
Kernel, :test, test_nonliteral, filepath
250+
end
251+
end
252+
end
253+
254+
# Integer? tests
255+
%w[s].each do |test_char|
256+
test_ord = test_char.ord
257+
258+
with_path __FILE__ do |filepath|
259+
with test_char, test_ord do |test_literal|
260+
assert_send_type "('s' | 115, path) -> Integer",
261+
Kernel, :test, test_literal, filepath
262+
end
263+
264+
with_int(test_ord).and test_char do |test_nonliteral|
265+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
266+
Kernel, :test, test_nonliteral, filepath
267+
end
268+
end
269+
270+
with_path '/not/a/file' do |filepath|
271+
with test_char, test_ord do |test_literal|
272+
assert_send_type "('s' | 115, path) -> nil",
273+
Kernel, :test, test_literal, filepath
274+
end
275+
276+
with_int(test_ord).and test_char do |test_nonliteral|
277+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
278+
Kernel, :test, test_nonliteral, filepath
279+
end
280+
end
281+
end
282+
283+
# Time tests
284+
with_path __FILE__ do |filepath|
285+
%w[A M C].each do |test_char|
286+
test_ord = test_char.ord
287+
288+
with test_char, test_ord do |test_literal|
289+
assert_send_type "('A' | 'M' | 'C' | 65 | 77 | 67, path) -> Time",
290+
Kernel, :test, test_literal, filepath
291+
end
292+
293+
with_int(test_ord).and test_char do |test_nonliteral|
294+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
295+
Kernel, :test, test_nonliteral, filepath
296+
end
297+
end
298+
end
299+
300+
# Comparison Tests
301+
with_path __dir__ + '/Integer_test.rb' do |filepath1|
302+
with_path __FILE__ do |filepath2|
303+
%w[< = > -].each do |test_char|
304+
test_ord = test_char.ord
305+
306+
with test_char, test_ord do |test_literal|
307+
assert_send_type "('<' | '=' | '>' | '-' | 60 | 61 | 62 | 45, path, path) -> bool",
308+
Kernel, :test, test_literal, filepath1, filepath2
309+
end
310+
311+
with_int(test_ord).and test_char do |test_nonliteral|
312+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
313+
Kernel, :test, test_nonliteral, filepath1, filepath2
314+
end
315+
end
316+
end
95317
end
96318
end
97319

test/stdlib/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def self.included(base)
149149
end
150150

151151
RUBY_EXECUTABLE = ENV["RUBY"] || RbConfig.ruby
152+
153+
def silence_warning(which)
154+
old_warning = Warning[which]
155+
Warning[which] = false
156+
yield
157+
ensure
158+
Warning[which] = old_warning
159+
end
152160
end
153161

154162
class StdlibTest < Test::Unit::TestCase

0 commit comments

Comments
 (0)