-
Notifications
You must be signed in to change notification settings - Fork 24
/
power_assert.rb
324 lines (296 loc) · 9.9 KB
/
power_assert.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# power_assert.rb
#
# Copyright (C) 2014-2015 Kazuki Tsujimoto, All rights reserved.
begin
captured = false
TracePoint.new(:return, :c_return) do |tp|
captured = true
unless tp.binding and tp.return_value
raise
end
end.enable { __id__ }
raise unless captured
rescue
raise LoadError, 'Fully compatible TracePoint API required'
end
require 'power_assert/version'
require 'power_assert/enable_tracepoint_events'
require 'ripper'
module PowerAssert
class << self
def configuration
@configuration ||= Configuration[false]
end
def configure
yield configuration
end
def start(assertion_proc_or_source, assertion_method: nil, source_binding: TOPLEVEL_BINDING)
if respond_to?(:clear_global_method_cache, true)
clear_global_method_cache
end
yield Context.new(assertion_proc_or_source, assertion_method, source_binding)
end
private
if defined?(RubyVM)
def clear_global_method_cache
eval('using PowerAssert.const_get(:Empty)', TOPLEVEL_BINDING)
end
end
end
Configuration = Struct.new(:lazy_inspection)
private_constant :Configuration
module Empty
end
private_constant :Empty
class InspectedValue
def initialize(value)
@value = value
end
def inspect
@value
end
end
private_constant :InspectedValue
class SafeInspectable
def initialize(value)
@value = value
end
def inspect
inspected = @value.inspect
if Encoding.compatible?(Encoding.default_external, inspected)
inspected
else
begin
"#{inspected.encode(Encoding.default_external)}(#{inspected.encoding})"
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
inspected.force_encoding(Encoding.default_external)
end
end
rescue => e
"InspectionFailure: #{e.class}: #{e.message.each_line.first}"
end
end
private_constant :SafeInspectable
class Context
Value = Struct.new(:name, :value, :column)
Ident = Struct.new(:type, :name, :column)
TARGET_CALLER_DIFF = {return: 5, c_return: 4}
TARGET_INDEX_OFFSET = {bmethod: 3, method: 2}
attr_reader :message_proc
def initialize(assertion_proc_or_source, assertion_method, source_binding)
if assertion_proc_or_source.kind_of?(Proc)
@assertion_proc = assertion_proc_or_source
@line = nil
else
@assertion_proc = source_binding.eval "Proc.new {#{assertion_proc_or_source}}"
@line = assertion_proc_or_source
end
path = nil
lineno = nil
methods = nil
refs = nil
method_ids = nil
return_values = []
@base_caller_length = -1
@assertion_method_name = assertion_method.to_s
@message_proc = -> {
return nil if @base_caller_length < 0
@message ||= build_assertion_message(@line || '', methods || [], return_values, refs || [], @assertion_proc.binding).freeze
}
@proc_local_variables = @assertion_proc.binding.eval('local_variables').map(&:to_s)
target_thread = Thread.current
@trace = TracePoint.new(:return, :c_return) do |tp|
next if method_ids and ! method_ids.include?(tp.method_id)
next unless tp.binding # workaround for ruby 2.2
locs = tp.binding.eval('::Kernel.caller_locations')
current_diff = locs.length - @base_caller_length
target_diff = TARGET_CALLER_DIFF[tp.event]
is_target_bmethod = current_diff < target_diff
if (is_target_bmethod or current_diff == target_diff) and Thread.current == target_thread
idx = target_diff - TARGET_INDEX_OFFSET[is_target_bmethod ? :bmethod : :method]
unless path
path = locs[idx].path
lineno = locs[idx].lineno
@line ||= open(path).each_line.drop(lineno - 1).first
idents = extract_idents(Ripper.sexp(@line))
methods, refs = idents.partition {|i| i.type == :method }
method_ids = methods.map(&:name).map(&:to_sym).uniq
end
if path == locs[idx].path and lineno == locs[idx].lineno
val = PowerAssert.configuration.lazy_inspection ?
tp.return_value :
InspectedValue.new(SafeInspectable.new(tp.return_value).inspect)
return_values << Value[tp.method_id.to_s, val, nil]
end
end
end
end
def yield
do_yield(&@assertion_proc)
end
def message
@message_proc.()
end
private
def do_yield
@trace.enable do
@base_caller_length = caller_locations.length
yield
end
end
def build_assertion_message(line, methods, return_values, refs, proc_binding)
set_column(methods, return_values)
ref_values = refs.map {|i| Value[i.name, proc_binding.eval(i.name), i.column] }
vals = (return_values + ref_values).find_all(&:column).sort_by(&:column).reverse
if vals.empty?
return line
end
fmt = (0..vals[0].column).map {|i| vals.find {|v| v.column == i } ? "%<#{i}>s" : ' ' }.join
lines = []
lines << line.chomp
lines << sprintf(fmt, vals.each_with_object({}) {|v, h| h[v.column.to_s.to_sym] = '|' }).chomp
vals.each do |i|
inspected_vals = vals.each_with_object({}) do |j, h|
h[j.column.to_s.to_sym] = [SafeInspectable.new(i.value).inspect, '|', ' '][i.column <=> j.column]
end
lines << encoding_safe_rstrip(sprintf(fmt, inspected_vals))
end
lines.join("\n")
end
def set_column(methods, return_values)
methods = methods.dup
return_values.each do |val|
idx = methods.index {|method| method.name == val.name }
if idx
val.column = methods.delete_at(idx).column
end
end
end
def encoding_safe_rstrip(str)
str.rstrip
rescue ArgumentError, Encoding::CompatibilityError
enc = str.encoding
if enc.ascii_compatible?
str.b.rstrip.force_encoding(enc)
else
str
end
end
def extract_idents(sexp)
tag, * = sexp
case tag
when :arg_paren, :assoc_splat, :fcall, :hash, :method_add_block, :string_literal
extract_idents(sexp[1])
when :assign, :massign
extract_idents(sexp[2])
when :assoclist_from_args, :bare_assoc_hash, :dyna_symbol, :paren, :string_embexpr,
:regexp_literal, :xstring_literal
sexp[1].flat_map {|s| extract_idents(s) }
when :assoc_new, :command, :dot2, :dot3, :string_content
sexp[1..-1].flat_map {|s| extract_idents(s) }
when :unary
handle_columnless_ident([], sexp[1], extract_idents(sexp[2]))
when :binary
handle_columnless_ident(extract_idents(sexp[1]), sexp[2], extract_idents(sexp[3]))
when :call
if sexp[3] == :call
handle_columnless_ident(extract_idents(sexp[1]), :call, [])
else
[sexp[1], sexp[3]].flat_map {|s| extract_idents(s) }
end
when :array
sexp[1] ? sexp[1].flat_map {|s| extract_idents(s) } : []
when :command_call
[sexp[1], sexp[4], sexp[3]].flat_map {|s| extract_idents(s) }
when :aref
handle_columnless_ident(extract_idents(sexp[1]), :[], extract_idents(sexp[2]))
when :method_add_arg
idents = extract_idents(sexp[1])
if idents.empty?
# idents may be empty(e.g. ->{}.())
extract_idents(sexp[2])
else
idents[0..-2] + extract_idents(sexp[2]) + [idents[-1]]
end
when :args_add_block
_, (tag, ss0, *ss1), _ = sexp
if tag == :args_add_star
(ss0 + ss1).flat_map {|s| extract_idents(s) }
else
sexp[1].flat_map {|s| extract_idents(s) }
end
when :vcall
_, (tag, name, (_, column)) = sexp
if tag == :@ident
[Ident[@proc_local_variables.include?(name) ? :ref : :method, name, column]]
else
[]
end
when :program
_, ((tag0, (tag1, (tag2, (tag3, mname, _)), _), (tag4, _, ss))) = sexp
if tag0 == :method_add_block and tag1 == :method_add_arg and tag2 == :fcall and
(tag3 == :@ident or tag3 == :@const) and mname == @assertion_method_name and (tag4 == :brace_block or tag4 == :do_block)
ss.flat_map {|s| extract_idents(s) }
else
_, (s, *) = sexp
extract_idents(s)
end
when :var_ref
_, (tag, ref_name, (_, column)) = sexp
case tag
when :@kw
if ref_name == 'self'
[Ident[:ref, 'self', column]]
else
[]
end
when :@const, :@cvar, :@ivar, :@gvar
[Ident[:ref, ref_name, column]]
else
[]
end
when :@ident, :@const
_, method_name, (_, column) = sexp
[Ident[:method, method_name, column]]
else
[]
end
end
def str_indices(str, re, offset, limit)
idx = str.index(re, offset)
if idx and idx <= limit
[idx, *str_indices(str, re, idx + 1, limit)]
else
[]
end
end
MID2SRCTXT = {
:[] => '[',
:+@ => '+',
:-@ => '-',
:call => '('
}
def handle_columnless_ident(left_idents, mid, right_idents)
left_max = left_idents.max_by(&:column)
right_min = right_idents.min_by(&:column)
bg = left_max ? left_max.column + left_max.name.length : 0
ed = right_min ? right_min.column - 1 : @line.length - 1
mname = mid.to_s
srctxt = MID2SRCTXT[mid] || mname
re = /
#{'\b' if /\A\w/ =~ srctxt}
#{Regexp.escape(srctxt)}
#{'\b' if /\w\z/ =~ srctxt}
/x
indices = str_indices(@line, re, bg, ed)
if left_idents.empty? and right_idents.empty?
left_idents + right_idents
elsif left_idents.empty?
left_idents + right_idents + [Ident[:method, mname, indices.last]]
else
left_idents + right_idents + [Ident[:method, mname, indices.first]]
end
end
end
private_constant :Context
end