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

Update TracePoint #941

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 32 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 Down Expand Up @@ -347,6 +347,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 +383,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
153 changes: 153 additions & 0 deletions test/stdlib/TracePoint_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
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_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