Skip to content

Commit

Permalink
Merge pull request #941 from ksss/trace_point
Browse files Browse the repository at this point in the history
Update TracePoint
  • Loading branch information
soutaro authored Mar 31, 2022
2 parents 292e346 + 9c5cea6 commit c00fc04
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 3 deletions.
45 changes: 42 additions & 3 deletions core/trace_point.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ class TracePoint < Object
# trace.enable { p tp.lineno }
# #=> RuntimeError: access from outside
#
def enable: () -> bool
| () { () -> void } -> void
def enable: (?target: (Method | UnboundMethod | Proc)?, ?target_line: Integer?, ?target_thread: Thread?) -> bool
| [R] (?target: (Method | UnboundMethod | Proc)?, ?target_line: Integer?, ?target_thread: Thread?) { () -> R } -> R

# <!--
# rdoc-file=trace_point.rb
Expand All @@ -315,6 +315,16 @@ class TracePoint < Object
#
def enabled?: () -> bool

# <!--
# rdoc-file=trace_point.rb
# - event()
# -->
# Type of event
#
# See TracePoint@Events for more information.
#
def event: () -> ::Symbol

# <!--
# rdoc-file=trace_point.rb
# - trace.inspect -> string
Expand Down Expand Up @@ -347,6 +357,15 @@ class TracePoint < Object
#
def path: () -> String

# <!--
# rdoc-file=trace_point.rb
# - parameters()
# -->
# Return the parameters definition of the method or block that the current hook
# belongs to. Format is the same as for Method#parameters
#
def parameters: () -> ::Array[[ :req | :opt | :rest | :keyreq | :key | :keyrest | :block, Symbol ] | [ :rest | :keyrest ]]

# <!--
# rdoc-file=trace_point.rb
# - raised_exception()
Expand Down Expand Up @@ -374,5 +393,25 @@ class TracePoint < Object
#
# trace.binding.eval('self')
#
def self: () -> Binding
def self: () -> untyped

# <!--
# rdoc-file=trace_point.rb
# - eval_script()
# -->
# Compiled source code (String) on *eval methods on the `:script_compiled`
# event. If loaded from a file, it will return nil.
#
def eval_script: () -> String?

# <!--
# rdoc-file=trace_point.rb
# - instruction_sequence()
# -->
# Compiled instruction sequence represented by a RubyVM::InstructionSequence
# instance on the `:script_compiled` event.
#
# Note that this method is MRI specific.
#
def instruction_sequence: () -> RubyVM::InstructionSequence
end
160 changes: 160 additions & 0 deletions test/stdlib/TracePoint_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
require_relative "test_helper"

class TracePointSingletonTest < Test::Unit::TestCase
include TypeAssertions

testing "singleton(::TracePoint)"

def test_new
assert_send_type "(*::Symbol events) { (::TracePoint tp) -> void } -> ::TracePoint",
TracePoint, :new, :line do end
end

# TODO
# def test_allow_reentry
# assert_send_type "() { () -> void } -> void",
# TracePoint, :allow_reentry
# end

def test_stat
assert_send_type "() -> untyped",
TracePoint, :stat
end

def test_trace
assert_send_type "(*::Symbol events) { (::TracePoint tp) -> void } -> ::TracePoint",
TracePoint, :trace, :line do |tp| tp.disable end
end
end

class TracePointTest < Test::Unit::TestCase
include TypeAssertions

testing "::TracePoint"

def test_initialize
assert_send_type "(*::Symbol events) { (::TracePoint tp) -> void } -> void",
TracePoint.new(:line){}, :initialize do end
end

def test_binding
TracePoint.new(:line) do |tp|
assert_send_type "() -> ::Binding",
tp, :binding
end.enable { 1 }
end

def test_inspect
TracePoint.new(:line) do |tp|
assert_send_type "() -> ::String",
tp, :inspect
end.enable { 1 }
end

def test_callee_id
TracePoint.new(:call) do |tp|
assert_send_type "() -> ::Symbol",
tp, :callee_id
end.enable { 1 }
end

def test_defined_class
TracePoint.new(:call) do |tp|
assert_send_type "() -> ::Module",
tp, :defined_class
end.enable { 1 }
end

def test_disable
tp = TracePoint.new(:line) {}
assert_send_type "() -> bool",
tp, :disable
assert_send_type "() { () -> void } -> void",
tp, :disable do end
end

def test_enable
tp = TracePoint.new(:line) {}
assert_send_type "(?target: (::Method | ::UnboundMethod | ::Proc)?, ?target_line: ::Integer?, ?target_thread: ::Thread?) -> bool",
tp, :enable
assert_send_type "[R] (?target: (::Method | ::UnboundMethod | ::Proc)?, ?target_line: ::Integer?, ?target_thread: ::Thread?) { () -> R } -> R",
tp, :enable do end
end

def test_enabled?
TracePoint.new(:line) do |tp|
assert_send_type "() -> bool",
tp, :enabled?
end.enable { 1 }
end

def test_event
TracePoint.new(:line) do |tp|
assert_send_type "() -> ::Symbol",
tp, :event
end.enable { 1 }
end

def test_lineno
TracePoint.new(:line) do |tp|
assert_send_type "() -> ::Integer",
tp, :lineno
end.enable { 1 }
end

def test_method_id
TracePoint.new(:line) do |tp|
assert_send_type "() -> ::Symbol",
tp, :method_id
end.enable { 1 }
end

def test_path
TracePoint.new(:line) do |tp|
assert_send_type "() -> ::String",
tp, :path
end.enable { 1 }
end

def test_parameters
TracePoint.new(:call) do |tp|
assert_send_type "() -> ::Array[[ :req | :opt | :rest | :keyreq | :key | :keyrest | :block, ::Symbol ] | [ :rest | :keyrest ]]",
tp, :parameters
end.enable { 1 }
end

def test_raised_exception
TracePoint.new(:raise) do |tp|
assert_send_type "() -> untyped",
tp, :raised_exception
end.enable { 1 }
end

def test_return_value
TracePoint.new(:call) do |tp|
assert_send_type "() -> untyped",
tp, :return_value
end.enable { 1 }
end

def test_self
TracePoint.new(:line) do |tp|
assert_send_type "() -> untyped",
tp, :self
end.enable { 1 }
end

def test_eval_script
TracePoint.new(:script_compiled) do |tp|
assert_send_type "() -> ::String?",
tp, :eval_script
end.enable { eval("'hello'") }
end

def test_instruction_sequence
TracePoint.new(:script_compiled) do |tp|
assert_send_type "() -> ::RubyVM::InstructionSequence",
tp, :instruction_sequence
end.enable { eval("'hello'") }
end
end

0 comments on commit c00fc04

Please sign in to comment.