Skip to content

Commit

Permalink
Refactor Breakpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Nov 21, 2021
1 parent 381ea09 commit 74db2f7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 80 deletions.
96 changes: 34 additions & 62 deletions test/debug/break_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,42 +205,32 @@ def test_debugger_doesnt_stop_when_other_instance_calls_the_inherited_method
end

class PathOptionTest < TestCase
def additional_file
include ExtraFileHelper

def extra_file
<<~RUBY
Foo.new.bar
RUBY
end

ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16)

def program(additional_file_path)
def program(extra_file_path)
<<~RUBY
1| class Foo
2| def bar; end
3| end
4|
5| Foo.new.bar
6|
7| load "#{additional_file_path}"
7| load "#{extra_file_path}"
RUBY
end

def with_tempfile
t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f|
f.write(additional_file)
f.close
end
yield t
ensure
File.unlink t if t
end

def test_break_only_stops_when_path_matches
with_tempfile do |additional_file|
debug_code(program(additional_file.path)) do
type "break Foo#bar path: #{additional_file.path}"
with_extra_tempfile do |extra_file|
debug_code(program(extra_file.path)) do
type "break Foo#bar path: #{extra_file.path}"
type 'c'
assert_line_text(/#{ADDITIONAL_FILE_BASENAME}/)
assert_line_text(/#{extra_file.path}/)
type 'c'
assert_finish
end
Expand Down Expand Up @@ -322,37 +312,27 @@ def test_break_C_method_with_singleton_method
end

class PathOptionTest < TestCase
def additional_file
include ExtraFileHelper

def extra_file
<<~RUBY
1.abs
RUBY
end

ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16)

def program(additional_file_path)
def program(extra_file_path)
<<~RUBY
1| load "#{additional_file_path}"
1| load "#{extra_file_path}"
2| 1.abs
RUBY
end

def with_tempfile
t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f|
f.write(additional_file)
f.close
end
yield t
ensure
File.unlink t if t
end

def test_break_only_stops_when_path_matches
with_tempfile do |additional_file|
debug_code(program(additional_file.path)) do
type "break Integer#abs path: #{additional_file.path}"
with_extra_tempfile do |extra_file|
debug_code(program(extra_file.path)) do
type "break Integer#abs path: #{extra_file.path}"
type 'c'
assert_line_text(/#{ADDITIONAL_FILE_BASENAME}/)
assert_line_text(/#{extra_file.path}/)
type 'c'
assert_finish
end
Expand Down Expand Up @@ -643,40 +623,32 @@ def test_conditional_breakpoint_shows_error
end

class PathOptionTest < TestCase
def additional_file
include ExtraFileHelper

def extra_file
<<~RUBY
a = 1
b = 200
a = 100
b = 1
_ = 0
RUBY
end

ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16)

def program(additional_file_path)
def program(extra_file_path)
<<~RUBY
1| a = 1
2| b = 100
3| load "#{additional_file_path}"
1| a = 200
2| b = 1
3| _ = 0
4| load "#{extra_file_path}"
RUBY
end

def with_tempfile
t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f|
f.write(additional_file)
f.close
end
yield t
ensure
File.unlink t if t
end

def test_conditional_breakpoint_only_stops_when_path_matches
with_tempfile do |additional_file|
debug_code(program(additional_file.path)) do
type "break if: a == 1 path: #{additional_file.path}"
with_extra_tempfile do |extra_file|
debug_code(program(extra_file.path)) do
type "break if: b == 1 path: #{extra_file.path}"
type 'c'
assert_line_text(/#{additional_file.path}/)
assert_line_text(/b = 200/)
type 'a + b'
assert_line_text(/101/)
type 'c'
assert_finish
end
Expand Down
26 changes: 8 additions & 18 deletions test/debug/catch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ def test_catch_with_namespace_stops_at_exception
end

class PathOptionTest < TestCase
def additional_file
include ExtraFileHelper

def extra_file
<<~RUBY
def bar
raise "bar"
Expand All @@ -159,11 +161,9 @@ def bar
RUBY
end

ADDITIONAL_FILE_BASENAME = __FILE__.hash.abs.to_s(16)

def program(additional_file_path)
def program(extra_file_path)
<<~RUBY
1| load "#{additional_file_path}"
1| load "#{extra_file_path}"
2|
3| def foo
4| raise "foo"
Expand All @@ -175,20 +175,10 @@ def program(additional_file_path)
RUBY
end

def with_tempfile
t = Tempfile.create([ADDITIONAL_FILE_BASENAME, '.rb']).tap do |f|
f.write(additional_file)
f.close
end
yield t
ensure
File.unlink t if t
end

def test_catch_only_stops_when_path_matches
with_tempfile do |additional_file|
debug_code(program(additional_file.path)) do
type "catch RuntimeError path: #{additional_file.path}"
with_extra_tempfile do |extra_file|
debug_code(program(extra_file.path)) do
type "catch RuntimeError path: #{extra_file.path}"
type 'c'
assert_line_text(/bar/)
type 'c'
Expand Down
15 changes: 15 additions & 0 deletions test/support/extra_file_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'securerandom'

module DEBUGGER__
module ExtraFileHelper
def with_extra_tempfile
t = Tempfile.create([SecureRandom.hex(5), '.rb']).tap do |f|
f.write(extra_file)
f.close
end
yield t
ensure
File.unlink t if t
end
end
end
1 change: 1 addition & 0 deletions test/support/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require_relative 'utils'
require_relative 'assertions'
require_relative 'extra_file_helper'

module DEBUGGER__
class TestCase < Test::Unit::TestCase
Expand Down

0 comments on commit 74db2f7

Please sign in to comment.