Class: RBS::Test::Tester

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/test/tester.rb

Defined Under Namespace

Classes: MethodCallTester, TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ Tester

Returns a new instance of Tester.



7
8
9
10
# File 'lib/rbs/test/tester.rb', line 7

def initialize(env:)
  @env = env
  @checkers = []
end

Instance Attribute Details

#checkersObject (readonly)

Returns the value of attribute checkers.



5
6
7
# File 'lib/rbs/test/tester.rb', line 5

def checkers
  @checkers
end

#envObject (readonly)

Returns the value of attribute env.



4
5
6
# File 'lib/rbs/test/tester.rb', line 4

def env
  @env
end

Instance Method Details

#builderObject



16
17
18
# File 'lib/rbs/test/tester.rb', line 16

def builder
  @builder ||= DefinitionBuilder.new(env: env)
end

#factoryObject



12
13
14
# File 'lib/rbs/test/tester.rb', line 12

def factory
  @factory ||= Factory.new
end

#install!(klass, sample_size:) ⇒ Object



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
# File 'lib/rbs/test/tester.rb', line 20

def install!(klass, sample_size:)
  RBS.logger.info { "Installing runtime type checker in #{klass}..." }

  type_name = factory.type_name(klass.name).absolute!

  builder.build_instance(type_name).tap do |definition|
    instance_key = new_key(type_name, "InstanceChecker")
    Observer.register(instance_key, MethodCallTester.new(klass, builder, definition, kind: :instance, sample_size: sample_size))

    definition.methods.each do |name, method|
      if method.implemented_in == type_name
        RBS.logger.info { "Setting up method hook in ##{name}..." }
        Hook.hook_instance_method klass, name, key: instance_key
      end
    end
  end

  builder.build_singleton(type_name).tap do |definition|
    singleton_key = new_key(type_name, "SingletonChecker")
    Observer.register(singleton_key, MethodCallTester.new(klass.singleton_class, builder, definition, kind: :singleton, sample_size: sample_size))

    definition.methods.each do |name, method|
      if method.implemented_in == type_name || name == :new
        RBS.logger.info { "Setting up method hook in .#{name}..." }
        Hook.hook_singleton_method klass, name, key: singleton_key
      end
    end
  end
end

#new_key(type_name, prefix) ⇒ Object



50
51
52
# File 'lib/rbs/test/tester.rb', line 50

def new_key(type_name, prefix)
  "#{prefix}__#{type_name}__#{SecureRandom.hex(10)}"
end