Skip to content

Commit

Permalink
Merge pull request #80 from hibariya/attached-object
Browse files Browse the repository at this point in the history
Use Class#attached_object instead of Regex to support customized `#inspect` methods
  • Loading branch information
ksss authored Jul 18, 2024
2 parents e087ba7 + 367faf3 commit 2ef8376
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions known_sig/orthoses/utils.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Orthoses
def self.object_to_rbs: (untyped object, ?strict: bool) -> String
def self.module_name: (Module mod) -> String?
def self.module_to_type_name: (Module) -> RBS::TypeName?
def self.attached_module_name: (Module) -> String?
def self.known_type_params: (Module | String) -> Array[RBS::AST::TypeParam]?
def self.new_store: () -> Orthoses::store
end
Expand Down
4 changes: 1 addition & 3 deletions lib/orthoses/trace/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def build_trace_point
case tp.event
when :call
if tp.defined_class.singleton_class?
# e.g. `Minitest::Spec::DSL#to_s`` may return `nil` with `#to_s`
m = tp.defined_class.to_s&.match(/#<Class:([\w:]+)>/) or next
mod_name = m[1] or next
mod_name = Utils.attached_module_name(tp.defined_class) or next
kind = :singleton
else
mod_name = Utils.module_name(tp.defined_class) or next
Expand Down
28 changes: 28 additions & 0 deletions lib/orthoses/trace/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ def priv(bool)
end
end
end

class CustomClassInspect
def self.inspect = "it's customized"
def self.a = 1
end
}

def test_method(t)
store = Orthoses::Trace::Method.new(->{
LOADER_METHOD.call
Expand Down Expand Up @@ -97,6 +103,28 @@ def self.singleton_method?: () -> bool
end
end

if Class.instance_methods.include?(:attached_object)
def test_custom_inspect(t)
store = Orthoses::Trace::Method.new(->{
LOADER_METHOD.call

CustomClassInspect.a

Orthoses::Utils.new_store
}, patterns: ['TraceMethodTest::CustomClassInspect']).call

actual = store.map { |n, c| c.to_rbs }.join("\n")
expect = <<~RBS
class TraceMethodTest::CustomClassInspect
def self.a: () -> Integer
end
RBS
unless expect == actual
t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n")
end
end
end

def test_raise_first(t)
Orthoses::Trace::Method.new(->{
raise rescue nil
Expand Down
11 changes: 11 additions & 0 deletions lib/orthoses/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ def self.module_to_type_name(mod)
end
end

def self.attached_module_name(mod)
if mod.respond_to?(:attached_object)
attached_object = mod.attached_object
(attached_object&.is_a?(Module) && attached_object.name) || nil
else
# e.g. `Minitest::Spec::DSL#to_s` may return `nil` with `#to_s`
m = mod.to_s&.match(/#<Class:([\w:]+)>/)
m && m[1]
end
end

def self.known_type_params(name)
type_name =
case name
Expand Down
2 changes: 2 additions & 0 deletions sig/orthoses/utils.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Orthoses::Utils

def self.module_to_type_name: (Module) -> RBS::TypeName?

def self.attached_module_name: (Module) -> String?

def self.known_type_params: (Module | String) -> Array[RBS::AST::TypeParam]?

def self.new_store: () -> Orthoses::store
Expand Down

0 comments on commit 2ef8376

Please sign in to comment.