@@ -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
233196end
0 commit comments