Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if it's a target on every event so that we don't get lost #92

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/orthoses/trace/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
26 changes: 26 additions & 0 deletions lib/orthoses/trace/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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