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.



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

def initialize(env:)
  @env = env
  @targets = []
  @instance_testers = {}
  @singleton_testers = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#instance_testersObject (readonly)

Returns the value of attribute instance_testers.



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

def instance_testers
  @instance_testers
end

#singleton_testersObject (readonly)

Returns the value of attribute singleton_testers.



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

def singleton_testers
  @singleton_testers
end

#targetsObject (readonly)

Returns the value of attribute targets.



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

def targets
  @targets
end

Instance Method Details

#builderObject



20
21
22
# File 'lib/rbs/test/tester.rb', line 20

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

#factoryObject



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

def factory
  @factory ||= Factory.new
end

#install!(klass, sample_size:, unchecked_classes:) ⇒ Object



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

def install!(klass, sample_size:, unchecked_classes:)
  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")
    tester, set = instance_testers[klass] ||= [
      MethodCallTester.new(klass, builder, definition, kind: :instance, sample_size: sample_size, unchecked_classes: unchecked_classes),
      Set[]
    ]
    Observer.register(instance_key, tester)

    definition.methods.each do |name, method|
      if reason = skip_method?(type_name, method)
        unless reason == :implemented_in
          RBS.logger.info { "Skipping ##{name} because of `#{reason}`..." }
        end
      else
        if !set.include?(name) && (
            name == :initialize ||
            klass.instance_methods(false).include?(name) ||
            klass.private_instance_methods(false).include?(name))
          RBS.logger.info { "Setting up method hook in ##{name}..." }
          Hook.hook_instance_method klass, name, key: instance_key
          set << name
        end
      end
    end
  end

  builder.build_singleton(type_name).tap do |definition|
    singleton_key = new_key(type_name, "SingletonChecker")
    tester, set = singleton_testers[klass] ||= [
      MethodCallTester.new(klass.singleton_class, builder, definition, kind: :singleton, sample_size: sample_size, unchecked_classes: unchecked_classes),
      Set[]
    ]
    Observer.register(singleton_key, tester)

    definition.methods.each do |name, method|
      if reason = skip_method?(type_name, method)
        unless reason == :implemented_in
          RBS.logger.info { "Skipping .#{name} because of `#{reason}`..." }
        end
      else
        if klass.methods(false).include?(name) && !set.include?(name)
          RBS.logger.info { "Setting up method hook in .#{name}..." }
          Hook.hook_singleton_method klass, name, key: singleton_key
          set << name
        end
      end
    end
  end

  targets << klass
end

#new_key(type_name, prefix) ⇒ Object



97
98
99
# File 'lib/rbs/test/tester.rb', line 97

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

#skip_method?(type_name, method) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbs/test/tester.rb', line 24

def skip_method?(type_name, method)
  if method.implemented_in == type_name
    if method.annotations.any? {|a| a.string == "rbs:test:skip" }
      :skip
    else
      false
    end
  else
    if method.annotations.any? {|a| a.string == "rbs:test:target" }
      false
    else
      :implemented_in
    end
  end
end