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

Add trace tests #101

Merged
merged 3 commits into from Jun 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/debug/session.rb
Expand Up @@ -499,7 +499,6 @@ def process_command line
case arg
when 'on'
dir = __dir__
i = 0
@tracer ||= TracePoint.new(:call, :return, :b_call, :b_return, :line, :class, :end){|tp|
next if File.dirname(tp.path) == dir
next if tp.path == '<internal:trace_point>'
Expand Down
60 changes: 60 additions & 0 deletions test/debug/trace_test.rb
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require_relative '../support/test_case'

module DEBUGGER__
class TraceTest < TestCase
def program
<<~RUBY
1| def foo
2| 10
3| end
4|
5| a = 1
6| foo + a
7|
8| binding.bp
9| __END__
RUBY
end

def test_trace_on_starts_tracing
debug_code(program) do
type "trace on"
type "continue"

trace_regexps = [
/Tracing: line at .*rb:5\r\n/,
/Tracing: line at .*rb:6\r\n/,
/Tracing: call Object#foo at .*rb:1\r\n/,
/Tracing: line at .*rb:2\r\n/,
/Tracing: return Object#foo => 10 at .*rb:3\r\n/,
/Tracing: line at .*rb:8\r\n/,
]
assert_line_text(Regexp.union(trace_regexps))

type "q!"
end
end

def test_trace_off_stops_tracing
debug_code(program) do
type "b 5"
type "trace on"
type "continue"

trace_regexps = [
/Tracing: line at .*rb:5\r\n/,
/Tracing: line at .*rb:6\r\n/,
]

assert_line_text(Regexp.union(trace_regexps))

type "trace off"
assert_no_line_text(/Tracing: call Object#foo at .*rb:1\r\n/)

type "q!"
end
end
end
end
25 changes: 23 additions & 2 deletions test/support/assertions.rb
Expand Up @@ -11,12 +11,33 @@ def assert_line_num(expected)

def assert_line_text(expected)
@queue.push(Proc.new {
result = @last_backlog[1..].join
result = collect_recent_backlog
expected = Regexp.escape(expected) if expected.is_a?(String)
msg = "Expected to include `#{expected}` in\n(\n#{result})\n"
assert_block(MessageCreationController.new { create_message(msg) }) { result.match? expected }

assert_block(MessageCreationController.new { create_message(msg) }) do
result.match? expected
end
})
end

def assert_no_line_text(expected)
@queue.push(Proc.new {
result = collect_recent_backlog
expected = Regexp.escape(expected) if expected.is_a?(String)
msg = "Expected not to include `#{expected}` in\n(\n#{result})\n"

assert_block(MessageCreationController.new { create_message(msg) }) do
!result.match? expected
end
})
end

private

def collect_recent_backlog
@last_backlog[1..].join
end
end
end

Expand Down