-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathtest_helper.rb
More file actions
544 lines (443 loc) · 13.1 KB
/
test_helper.rb
File metadata and controls
544 lines (443 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require "bundler/setup"
require 'steep'
require "minitest/reporters"
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new]
require 'minitest/autorun'
require "pp"
require "open3"
require "tmpdir"
require 'minitest/hooks/test'
require "lsp_double"
Rainbow.enabled = false
module Steep::AST::Types::Name
def self.new_singleton(name:, location: nil)
name = TypeName(name.to_s) unless name.is_a?(RBS::TypeName)
Steep::AST::Types::Name::Singleton.new(name: name, location: location)
end
def self.new_instance(location: nil, name:, args: [])
name = TypeName(name.to_s) unless name.is_a?(RBS::TypeName)
Steep::AST::Types::Name::Instance.new(location: location, name: name, args: args)
end
end
module TestHelper
class <<self
attr_accessor :timeout
end
def assert_any(collection, &block)
assert collection.any?(&block)
end
def assert_any!(collection, size: nil, &block)
errors = []
count = 0
assert_equal size, collection.count if size
collection.each do |c|
begin
block[c]
count += 1
rescue Minitest::Assertion => error
errors << error
end
end
if count == 0
raise Minitest::Assertion.new("Assertion should hold one of the collection members: #{collection.to_a.join(', ')}")
end
end
def assert_all(collection, &block)
assert collection.all?(&block)
end
def assert_all!(collection)
collection.each do |c|
yield c
end
end
def refute_any(collection, &block)
refute collection.any?(&block)
end
def assert_size(size, collection)
assert_equal size, collection.size
end
def finally(timeout: TestHelper.timeout)
started_at = Time.now
while Time.now < started_at + timeout
yield
sleep 0.2
end
end
def finally_holds(timeout: TestHelper.timeout)
finally(timeout: timeout) do
begin
yield
return
rescue Minitest::Assertion
# ignore
end
end
yield
end
def assert_finally(timeout: TestHelper.timeout, &block)
finally(timeout: timeout) do
yield.tap do |result|
return result if result
end
end
assert yield
end
def dig(node, *indexes)
case indexes.size
when 0
node
when 1
node.children[indexes.first]
else
dig(node.children[indexes.first], *indexes.drop(1))
end
end
def lvar_in(node, name)
if (node.type == :lvar || node.type == :lvasgn) && node.children[0].name == name
return node
else
node.children.each do |child|
if child.is_a?(AST::Node)
lvar = lvar_in(child, name)
return lvar if lvar
end
end
nil
end
end
end
module TypeErrorAssertions
def assert_incompatible_assignment(error, node: nil, lhs_type: nil, rhs_type:)
assert_instance_of Steep::Errors::IncompatibleAssignment, error
assert_equal node, error.node if node
assert_equal lhs_type, error.lhs_type if lhs_type
assert_equal rhs_type, error.rhs_type if rhs_type
yield error if block_given?
end
def assert_no_method_error(error, node: nil, method: nil, type: nil)
assert_instance_of Steep::Errors::NoMethod, error
node and assert_equal node, error.node
method and assert_equal method, error.method
type and assert_equal type, error.type
block_given? and yield error
end
def assert_argument_type_mismatch(error, expected: nil, actual: nil)
assert_instance_of Steep::Errors::ArgumentTypeMismatch, error
assert_equal expected, error.expected if expected
assert_equal actual, error.actual if actual
yield error.expected, error.actual if block_given?
end
def assert_break_type_mismatch(error, expected: nil, actual: nil)
assert_instance_of Steep::Errors::BreakTypeMismatch, error
assert_equal expected, error.expected if expected
assert_equal actual, error.actual if actual
yield expected, actual if block_given?
end
end
module ASTAssertion
def assert_type_var(type, name: nil)
assert_instance_of Steep::AST::Types::Var, type
assert_equal name, type.name if name
end
def assert_any_type(type)
assert_instance_of Steep::AST::Types::Any, type
end
def assert_location(located, name: nil, start_line: nil, start_column: nil, end_line: nil, end_column: nil)
loc = located.location
assert_equal name, loc.name if name
assert_equal start_line, loc.start_line if start_line
assert_equal start_column, loc.start_column if start_column
assert_equal end_line, loc.end_line if end_line
assert_equal end_column, loc.end_column if end_column
end
def assert_instance_name_type(type, name: nil)
assert_instance_of Steep::AST::Types::Name::Instance, type
assert_equal name, type.name if name
yield type.args if block_given?
end
def assert_union_type(type)
assert_instance_of Steep::AST::Types::Union, type
yield type.types if block_given?
end
def assert_instance_type(type)
assert_instance_of Steep::AST::Types::Instance, type
end
end
module SubtypingHelper
BUILTIN = <<-EOS
class BasicObject
def initialize: () -> void
def !: -> bool
end
class Object < BasicObject
def class: -> class
def tap: { (instance) -> untyped } -> instance
def to_s: -> String
def nil?: -> bool
def itself: -> self
def is_a?: (Module) -> bool
def ===: (untyped) -> bool
private
def require: (String) -> void
def puts: (*String) -> void
def gets: -> String?
end
class Class < Module
def new: (*untyped, **untyped) ?{ (*untyped, **untyped) -> void } -> void
end
class Module
def block_given?: -> untyped
def attr_reader: (*Symbol) -> void
def ===: (untyped) -> bool
end
class String
def to_str: -> String
def `+`: (String) -> String
def size: -> Integer
def `-@`: -> String
end
class Numeric
def `+`: (Numeric) -> Numeric
def to_int: -> Integer
def zero?: () -> bool
end
class Integer < Numeric
def +: (Integer) -> Integer
| (Numeric) -> Numeric
end
class Float < Numeric
def +: (Float) -> Float
| (Integer) -> Float
| (Numeric) -> Numeric
end
class Symbol
def id2name: -> String
end
class Range[A]
def begin: -> A
def end: -> A
end
class Regexp
end
class Array[unchecked out A]
def initialize: () -> untyped
| (Integer, A) -> untyped
| (Integer) -> untyped
def `[]`: (Integer) -> A
def `[]=`: (Integer, A) -> A
def `<<`: (A) -> self
def each: { (A) -> untyped } -> self
def zip: [B] (Array[B]) -> Array[A | B]
def each_with_object: [B] (B) { (A, B) -> untyped } -> B
def map: [X] { (A) -> X } -> Array[X]
def first: () -> A?
def last: () -> A?
end
class Hash[A, B]
def `[]`: (A) -> B
def `[]=`: (A, B) -> B
def each: { ([A, B]) -> void } -> self
end
class NilClass
end
class Proc
def `[]`: (*untyped) -> untyped
def call: (*untyped) -> untyped
def `===`: (*untyped) -> untyped
def yield: (*untyped) -> untyped
def arity: -> Integer
end
class TrueClass
end
class FalseClass
end
EOS
def checker
@checker or raise "#checker should be used within from #with_checker"
end
def with_checker(*files, with_stdlib: false, &block)
paths = {}
files.each.with_index do |content, index|
if content.is_a?(Hash)
paths.merge!(content)
else
paths["#{index}.rbs"] = content
end
end
unless with_stdlib
paths["builtin.rbs"] = BUILTIN
end
with_factory(paths, nostdlib: !with_stdlib) do |factory|
@checker = Steep::Subtyping::Check.new(factory: factory)
yield @checker
ensure
@checker = nil
end
end
end
module ShellHelper
def chdir(path)
if path.relative?
path = current_dir + path
end
dirs.push(path)
yield
ensure
dirs.pop
end
def current_dir
dirs.last
end
def push_env(env)
envs.push env
yield
ensure
envs.pop
end
def env_vars
envs.each.with_object({}) do |update_env, env_vars|
env_vars.merge!(update_env)
end
end
def sh(*command)
Open3.capture3(env_vars, *command, chdir: current_dir.to_s)
end
def sh!(*command)
stdout, stderr, status = sh(*command)
unless status.success?
raise "Failed to execute: #{command.join(" ")}, #{status.inspect}, stdout=#{stdout.inspect}, stderr=#{stderr.inspect}"
end
[stdout, stderr]
end
def in_tmpdir(&block)
Dir.mktmpdir do |dir|
chdir(Pathname(dir), &block)
end
end
end
module FactoryHelper
def with_factory(paths = {}, nostdlib: false)
Dir.mktmpdir do |dir|
root = Pathname(dir)
paths.each do |path, content|
absolute_path = root + path
absolute_path.parent.mkpath
absolute_path.write(content)
end
core_root = nostdlib ? nil : RBS::EnvironmentLoader::DEFAULT_CORE_ROOT
env_loader = RBS::EnvironmentLoader.new(core_root: core_root)
env_loader.add path: root
env = RBS::Environment.new()
env_loader.load(env: env)
env = env.resolve_type_names
definition_builder = RBS::DefinitionBuilder.new(env: env)
@factory = Steep::AST::Types::Factory.new(builder: definition_builder)
yield factory
ensure
@factory = nil
end
end
def factory
@factory or raise "#factory should be called from inside with_factory"
end
def parse_type(string, factory: self.factory, variables: [])
type = RBS::Parser.parse_type(string, variables: variables)
factory.type(type)
end
def parse_ruby(string, factory: self.factory)
Steep::Source.parse(string, path: Pathname("test.rb"), factory: factory)
end
def parse_method_type(string, factory: self.factory, variables: [], self_type: Steep::AST::Types::Self.new)
type = RBS::Parser.parse_method_type(string, variables: variables)
factory.method_type type, self_type: self_type, method_decls: Set[]
end
end
module LSPTestHelper
LSP = LanguageServer::Protocol
def reader_pipe
@reader_pipe ||= IO.pipe
end
def writer_pipe
@writer_pipe ||= IO.pipe
end
def worker_reader
@worker_reader ||= LSP::Transport::Io::Reader.new(reader_pipe[0])
end
def worker_writer
@worker_writer ||= LSP::Transport::Io::Writer.new(writer_pipe[1])
end
def master_writer
@master_writer ||= LSP::Transport::Io::Writer.new(reader_pipe[1])
end
def master_reader
@master_reader ||= LSP::Transport::Io::Reader.new(writer_pipe[0])
end
end
module TypeConstructionHelper
Namespace = RBS::Namespace
Typing = Steep::Typing
ConstantEnv = Steep::TypeInference::ConstantEnv
TypeEnv = Steep::TypeInference::TypeEnv
TypeConstruction = Steep::TypeConstruction
Annotation = Steep::AST::Annotation
Context = Steep::TypeInference::Context
LocalVariableTypeEnv = Steep::TypeInference::LocalVariableTypeEnv
AST = Steep::AST
TypeInference = Steep::TypeInference
def with_standard_construction(checker, source)
self_type = parse_type("::Object")
annotations = source.annotations(block: source.node, factory: checker.factory, current_module: Namespace.root)
const_env = ConstantEnv.new(factory: factory,
context: [Namespace.root])
type_env = TypeEnv.build(annotations: annotations,
subtyping: checker,
const_env: const_env,
signatures: checker.factory.env)
lvar_env = LocalVariableTypeEnv.empty(
subtyping: checker,
self_type: self_type
).annotate(annotations)
context = Context.new(
block_context: nil,
method_context: nil,
module_context: Context::ModuleContext.new(
instance_type: AST::Builtin::Object.instance_type,
module_type: AST::Builtin::Object.module_type,
implement_name: nil,
current_namespace: Namespace.root,
const_env: const_env,
class_name: AST::Builtin::Object.module_name,
instance_definition: checker.factory.definition_builder.build_instance(AST::Builtin::Object.module_name),
module_definition: checker.factory.definition_builder.build_singleton(AST::Builtin::Object.module_name)
),
break_context: nil,
self_type: self_type,
type_env: type_env,
lvar_env: lvar_env,
call_context: TypeInference::MethodCall::TopLevelContext.new()
)
typing = Typing.new(source: source, root_context: context)
construction = TypeConstruction.new(checker: checker,
source: source,
annotations: annotations,
context: context,
typing: typing)
yield construction, typing
end
def assert_no_error(typing)
assert_instance_of Typing, typing
assert_predicate typing.errors.map {|e| StringIO.new().tap {|io| e.print_to(io) }.string }, :empty?
end
def assert_typing_error(typing, size: nil)
assert_instance_of Typing, typing
messages = typing.errors.map {|e| StringIO.new().tap {|io| e.print_to(io) }.string }
if size
assert_equal size, messages.size, "errors=#{messages.inspect}"
yield(typing.errors) if block_given?
else
refute_empty messages
end
end
end
TestHelper.timeout = ENV["CI"] ? 50 : 10