diff --git a/lib/orthoses/trace/method.rb b/lib/orthoses/trace/method.rb index 7a1e8de..ba84803 100644 --- a/lib/orthoses/trace/method.rb +++ b/lib/orthoses/trace/method.rb @@ -34,18 +34,20 @@ def call def build_trace_point TracePoint.new(:call, :return, :raise) do |tp| - case tp.event - when :call - if tp.defined_class.singleton_class? - mod_name = Utils.attached_module_name(tp.defined_class) or next - kind = :singleton - else - mod_name = Utils.module_name(tp.defined_class) or next - kind = :instance - end + next unless tp.defined_class + + if tp.defined_class.singleton_class? + mod_name = Utils.attached_module_name(tp.defined_class) or next + kind = :singleton + else + mod_name = Utils.module_name(tp.defined_class) or next + kind = :instance + end - next unless target?(mod_name) + next unless target?(mod_name) + case tp.event + when :call visibility = tp.self.private_methods.include?(tp.method_id) ? :private : nil key = [mod_name, kind, visibility, tp.method_id] op_name_types = tp.parameters.map do |op, name| diff --git a/lib/orthoses/trace/method_test.rb b/lib/orthoses/trace/method_test.rb index 2be18c4..70cf54d 100644 --- a/lib/orthoses/trace/method_test.rb +++ b/lib/orthoses/trace/method_test.rb @@ -68,6 +68,12 @@ class CustomClassInspect def self.inspect = "it's customized" def self.a = 1 end + + module CallsOtherModule + def self.calls_other_module_method + "#{M.new(0).a_ten}" + end + end } def test_method(t) @@ -196,4 +202,24 @@ def multi_types: (String key) -> (Integer | String) t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n") end end + + def test_filtering(t) + store = Orthoses::Trace::Method.new(->{ + LOADER_METHOD.call + + CallsOtherModule.calls_other_module_method + + Orthoses::Utils.new_store + }, patterns: ['TraceMethodTest::CallsOtherModule']).call + + actual = store.map { |n, c| c.to_rbs }.join("\n") + expect = <<~RBS + module TraceMethodTest::CallsOtherModule + def self.calls_other_module_method: () -> String + end + RBS + unless expect == actual + t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n") + end + end end