Class: MockSuey::TypeChecks::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/mock_suey/type_checks/ruby.rb

Defined Under Namespace

Classes: SignatureGenerator

Instance Method Summary collapse

Constructor Details

#initialize(load_dirs: []) ⇒ Ruby

Returns a new instance of Ruby.



106
107
108
# File 'lib/mock_suey/type_checks/ruby.rb', line 106

def initialize(load_dirs: [])
  @load_dirs = Array(load_dirs)
end

Instance Method Details

#load_signatures_from_calls(calls) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/mock_suey/type_checks/ruby.rb', line 160

def load_signatures_from_calls(calls)
  constants = Set.new

  calls.group_by(&:receiver_class).each do |klass, klass_calls|
    calls_per_method = klass_calls.group_by(&:method_name)
    generator = SignatureGenerator.new(klass, calls_per_method)

    generator.to_rbs.then do |rbs|
      MockSuey.logger.debug "Generated RBS for #{klass.instance_class_name}:\n#{rbs}\n"
      load_rbs(rbs)
    end

    constants |= generator.constants
  end

  constants.each do |const|
    next if type_defined?(const)

    SignatureGenerator.new(const, {}).to_rbs.then do |rbs|
      MockSuey.logger.debug "Generated RBS for constant #{const.instance_class_name}:\n#{rbs}\n"
      load_rbs(rbs)
    end
  end
end

#typecheck!(call_obj, raise_on_missing: false) ⇒ Object

Raises:

  • (RBS::Test::Tester::TypeError)


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
# File 'lib/mock_suey/type_checks/ruby.rb', line 110

def typecheck!(call_obj, raise_on_missing: false)
  method_name = call_obj.method_name

  method_call = RBS::Test::ArgumentsReturn.return(
    arguments: call_obj.arguments,
    value: call_obj.return_value
  )

  call_trace = RBS::Test::CallTrace.new(
    method_name:,
    method_call:,
    # TODO: blocks support
    block_calls: [],
    block_given: false
  )

  method_type = type_for(call_obj.receiver_class, method_name)

  unless method_type
    raise MissingSignature, "No signature found for #{call_obj.method_desc}" if raise_on_missing
    return
  end

  self_class = call_obj.receiver_class
  instance_class = call_obj.receiver_class
  class_class = call_obj.receiver_class.singleton_class? ? call_obj.receiver_class : call_obj.receiver_class.singleton_class

  typecheck = RBS::Test::TypeCheck.new(
    self_class:,
    builder:,
    sample_size: 100, # What should be the value here?
    unchecked_classes: [],
    instance_class:,
    class_class:
  )

  errors = []
  typecheck.overloaded_call(
    method_type,
    "#{self_class.singleton_class? ? "." : "#"}#{method_name}",
    call_trace,
    errors:
  )

  reject_returned_doubles!(errors)

  # TODO: Use custom error class
  raise RBS::Test::Tester::TypeError.new(errors) unless errors.empty?
end