Skip to content

Commit 1bc02fe

Browse files
committed
Remove PowerAssert.trace
1 parent 447f4cc commit 1bc02fe

File tree

7 files changed

+54
-239
lines changed

7 files changed

+54
-239
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ group :development do
1414
gem 'simplecov'
1515
gem 'bundler'
1616
gem 'irb', '>= 1.3.1'
17-
gem 'byebug'
1817
gem 'benchmark-ips'
1918
end

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Use following test frameworks or extensions instead.
2020
* [rspec-power_assert](https://github.com/joker1007/rspec-power_assert)
2121
* [rspec-matchers-power_assert_matchers](https://github.com/kachick/rspec-matchers-power_assert_matchers)
2222
* [pry-power_assert](https://github.com/yui-knk/pry-power_assert)
23-
* [pry-byebug-power_assert](https://github.com/k-tsj/pry-byebug-power_assert)
2423
* [irb-power_assert](https://github.com/kachick/irb-power_assert)
2524
* [power_p](https://github.com/k-tsj/power_p)
2625

lib/power_assert.rb

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
# Copyright (C) 2014 Kazuki Tsujimoto
44

55
begin
6-
unless defined?(Byebug)
7-
captured = false
8-
target_thread = Thread.current
9-
TracePoint.new(:return, :c_return) do |tp|
10-
next unless Thread.current == target_thread
11-
captured = true
12-
unless tp.return_value and tp.callee_id
13-
raise ''
14-
end
15-
end.enable { __id__ }
16-
raise '' unless captured
17-
end
6+
captured = false
7+
target_thread = Thread.current
8+
TracePoint.new(:return, :c_return) do |tp|
9+
next unless Thread.current == target_thread
10+
captured = true
11+
unless tp.return_value and tp.callee_id
12+
raise ''
13+
end
14+
end.enable { __id__ }
15+
raise '' unless captured
1816
rescue
1917
raise LoadError, 'Fully compatible TracePoint API required'
2018
end
@@ -33,18 +31,7 @@ def start(assertion_proc_or_source, assertion_method: nil, source_binding: TOPLE
3331
if respond_to?(:clear_global_method_cache, true)
3432
clear_global_method_cache
3533
end
36-
yield BlockContext.new(assertion_proc_or_source, assertion_method, source_binding)
37-
end
38-
39-
def trace(frame)
40-
begin
41-
raise 'Byebug is not started yet' unless Byebug.started?
42-
rescue NameError
43-
raise "PowerAssert.#{__method__} requires Byebug"
44-
end
45-
ctx = TraceContext.new(frame._binding)
46-
ctx.enable
47-
ctx
34+
yield Context.new(assertion_proc_or_source, assertion_method, source_binding)
4835
end
4936

5037
def app_caller_locations
@@ -59,24 +46,11 @@ def app_context?
5946
private
6047

6148
def internal_file?(file)
62-
setup_internal_lib_dir(Byebug, :attach, 2) if defined?(Byebug)
63-
setup_internal_lib_dir(PryByebug, :start_with_pry_byebug, 2, Pry) if defined?(PryByebug)
6449
INTERNAL_LIB_DIRS.find do |_, dir|
6550
file.start_with?(dir)
6651
end
6752
end
6853

69-
def setup_internal_lib_dir(lib, mid, depth, lib_obj = lib)
70-
unless INTERNAL_LIB_DIRS.key?(lib)
71-
INTERNAL_LIB_DIRS[lib] = lib_dir(lib_obj, mid, depth)
72-
end
73-
rescue NameError
74-
end
75-
76-
def lib_dir(obj, mid, depth)
77-
File.expand_path('../' * depth, obj.method(mid).source_location[0])
78-
end
79-
8054
if defined?(RubyVM)
8155
CLEAR_CACHE_ISEQ = RubyVM::InstructionSequence.compile('using PowerAssert.const_get(:Empty)')
8256
private_constant :CLEAR_CACHE_ISEQ

lib/power_assert/context.rb

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,34 @@ module PowerAssert
77
class Context
88
Value = Struct.new(:name, :value, :lineno, :column, :display_offset)
99

10-
def initialize(base_caller_length)
10+
def initialize(assertion_proc_or_source, assertion_method, source_binding)
1111
@fired = false
1212
@target_thread = Thread.current
13+
14+
if assertion_proc_or_source.respond_to?(:to_proc)
15+
@assertion_proc = assertion_proc_or_source.to_proc
16+
line = nil
17+
else
18+
@assertion_proc = source_binding.eval "Proc.new {#{assertion_proc_or_source}}"
19+
line = assertion_proc_or_source
20+
end
21+
22+
@parser = Parser::DUMMY
23+
@trace_call = TracePoint.new(:call, :c_call) do
24+
if PowerAssert.app_context? and Thread.current == @target_thread
25+
@trace_call.disable
26+
locs = PowerAssert.app_caller_locations
27+
path = locs.last.path
28+
lineno = locs.last.lineno
29+
if File.exist?(path)
30+
line ||= File.open(path) {|fp| fp.each_line.drop(lineno - 1).first }
31+
end
32+
if line
33+
@parser = Parser.new(line, path, lineno, @assertion_proc.binding, assertion_method.to_s, @assertion_proc)
34+
end
35+
end
36+
end
37+
1338
method_id_set = nil
1439
@return_values = []
1540
@trace_return = TracePoint.new(:return, :c_return) do |tp|
@@ -22,14 +47,12 @@ def initialize(base_caller_length)
2247
next if tp.event == :c_return and
2348
not (@parser.lineno == tp.lineno and @parser.path == tp.path)
2449
locs = PowerAssert.app_caller_locations
25-
diff = locs.length - base_caller_length
26-
if (tp.event == :c_return && diff == 1 || tp.event == :return && diff <= 2) and Thread.current == @target_thread
27-
idx = -(base_caller_length + 1)
28-
if @parser.path == locs[idx].path and @parser.lineno == locs[idx].lineno
50+
if (tp.event == :c_return && locs.length == 1 || tp.event == :return && locs.length <= 2) and Thread.current == @target_thread
51+
if @parser.path == locs.last.path and @parser.lineno == locs.last.lineno
2952
val = PowerAssert.configuration.lazy_inspection ?
3053
tp.return_value :
3154
InspectedValue.new(SafeInspectable.new(tp.return_value).inspect)
32-
@return_values << Value[method_id.to_s, val, locs[idx].lineno, nil]
55+
@return_values << Value[method_id.to_s, val, locs.last.lineno, nil]
3356
end
3457
end
3558
rescue Exception => e
@@ -41,16 +64,29 @@ def initialize(base_caller_length)
4164
end
4265

4366
def message
44-
raise 'call #yield or #enable at first' unless fired?
67+
raise 'call #yield at first' unless fired?
4568
@message ||= build_assertion_message(@parser, @return_values).freeze
4669
end
4770

4871
def message_proc
4972
-> { message }
5073
end
5174

75+
def yield
76+
@fired = true
77+
invoke_yield(&@assertion_proc)
78+
end
79+
5280
private
5381

82+
def invoke_yield
83+
@trace_return.enable do
84+
@trace_call.enable do
85+
yield
86+
end
87+
end
88+
end
89+
5490
def fired?
5591
@fired
5692
end
@@ -157,77 +193,4 @@ def column2display_offset(str)
157193
end
158194
end
159195
private_constant :Context
160-
161-
class BlockContext < Context
162-
def initialize(assertion_proc_or_source, assertion_method, source_binding)
163-
super(0)
164-
if assertion_proc_or_source.respond_to?(:to_proc)
165-
@assertion_proc = assertion_proc_or_source.to_proc
166-
line = nil
167-
else
168-
@assertion_proc = source_binding.eval "Proc.new {#{assertion_proc_or_source}}"
169-
line = assertion_proc_or_source
170-
end
171-
@parser = Parser::DUMMY
172-
@trace_call = TracePoint.new(:call, :c_call) do
173-
if PowerAssert.app_context? and Thread.current == @target_thread
174-
@trace_call.disable
175-
locs = PowerAssert.app_caller_locations
176-
path = locs.last.path
177-
lineno = locs.last.lineno
178-
if File.exist?(path)
179-
line ||= File.open(path) {|fp| fp.each_line.drop(lineno - 1).first }
180-
end
181-
if line
182-
@parser = Parser.new(line, path, lineno, @assertion_proc.binding, assertion_method.to_s, @assertion_proc)
183-
end
184-
end
185-
end
186-
end
187-
188-
def yield
189-
@fired = true
190-
invoke_yield(&@assertion_proc)
191-
end
192-
193-
private
194-
195-
def invoke_yield
196-
@trace_return.enable do
197-
@trace_call.enable do
198-
yield
199-
end
200-
end
201-
end
202-
end
203-
private_constant :BlockContext
204-
205-
class TraceContext < Context
206-
def initialize(binding)
207-
target_frame, *base = PowerAssert.app_caller_locations
208-
super(base.length)
209-
path = target_frame.path
210-
lineno = target_frame.lineno
211-
if File.exist?(path)
212-
line = File.open(path) {|fp| fp.each_line.drop(lineno - 1).first }
213-
@parser = Parser.new(line, path, lineno, binding)
214-
else
215-
@parser = Parser::DUMMY
216-
end
217-
end
218-
219-
def enable
220-
@fired = true
221-
@trace_return.enable
222-
end
223-
224-
def disable
225-
@trace_return.disable
226-
end
227-
228-
def enabled?
229-
@trace_return.enabled?
230-
end
231-
end
232-
private_constant :TraceContext
233196
end

test/test_core_ext_helper.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/test_helper.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
require 'power_assert'
1717
require 'ripper'
1818

19-
begin
20-
require_relative 'test_core_ext_helper'
21-
rescue LoadError
22-
end
23-
2419
module PowerAssertTestHelper
2520
class << self
2621
def included(base)

test/trace_test.rb

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)