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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe 'TracePoint#enable' do
describe 'without a block' do
it 'returns false if trace was disabled' do
called = false
trace = TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
called = true
end
line_event = true
called.should == false
ret = trace.enable
begin
ret.should == false
line_event = true
called.should == true
ensure
trace.disable
end
end
it 'returns true if trace was already enabled' do
called = false
trace = TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
called = true
end
line_event = true
called.should == false
ret = trace.enable
begin
ret.should == false
trace.enable.should == true
line_event = true
called.should == true
ensure
trace.disable
trace.should_not.enabled?
end
end
end
describe 'with a block' do
it 'enables the trace object within a block' do
event_name = nil
TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
event_name = tp.event
end.enable { event_name.should equal(:line) }
end
ruby_version_is '3.2' do
it 'enables the trace object only for the current thread' do
threads = []
trace = TracePoint.new(:line) do |tp|
# Runs on purpose on any Thread
threads << Thread.current
end
thread = nil
trace.enable do
line_event = true
thread = Thread.new do
event_in_other_thread = true
end
thread.join
end
threads = threads.uniq
threads.should.include?(Thread.current)
threads.should_not.include?(thread)
end
end
ruby_version_is ''...'3.2' do
it 'enables the trace object for any thread' do
threads = []
trace = TracePoint.new(:line) do |tp|
# Runs on purpose on any Thread
threads << Thread.current
end
thread = nil
trace.enable do
line_event = true
thread = Thread.new do
event_in_other_thread = true
end
thread.join
end
threads = threads.uniq
threads.should.include?(Thread.current)
threads.should.include?(thread)
end
end
it 'can accept arguments within a block but it should not yield arguments' do
event_name = nil
trace = TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
event_name = tp.event
end
trace.enable do |*args|
event_name.should equal(:line)
args.should == []
end
trace.should_not.enabled?
end
it 'enables trace object on calling with a block if it was already enabled' do
enabled = nil
trace = TracePoint.new(:line) {}
trace.enable
begin
trace.enable { enabled = trace.enabled? }
enabled.should == true
ensure
trace.disable
end
end
it 'returns the return value of the block' do
trace = TracePoint.new(:line) {}
trace.enable { 42 }.should == 42
end
it 'disables the trace object outside the block' do
called = false
trace = TracePoint.new(:line) do
next unless TracePointSpec.target_thread?
called = true
end
trace.enable {
line_event = true
}
called.should == true
trace.should_not.enabled?
end
end
describe "when nested" do
before do
@path_prefix = ' '
end
it "enables both TracePoints but only calls the respective callbacks" do
called = false
first = TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
called = true
end
all = []
inspects = []
second = TracePoint.new(:line) { |tp|
next unless TracePointSpec.target_thread?
all << tp
inspects << tp.inspect
}
line = nil
first.enable do
second.enable do
line = __LINE__
end
end
all.uniq.should == [second]
inspects.uniq.should == ["#<TracePoint:line#{@path_prefix}#{__FILE__}:#{line}>"]
called.should == true
end
end
describe 'target: option' do
before :each do
ScratchPad.record []
end
it 'enables trace point for specific location' do
trace = TracePoint.new(:call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.method_id
end
obj = Object.new
def obj.foo; end
def obj.bar; end
trace.enable(target: obj.method(:foo)) do
obj.foo
obj.bar
end
ScratchPad.recorded.should == [:foo]
end
it 'traces all the events triggered in specified location' do
trace = TracePoint.new(:line, :call, :return, :b_call, :b_return) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.event
end
obj = Object.new
def obj.foo
bar
-> {}.call
end
def obj.bar; end
trace.enable(target: obj.method(:foo)) do
obj.foo
end
ScratchPad.recorded.uniq.sort.should == [:call, :return, :b_call, :b_return, :line].sort
end
it 'does not trace events in nested locations' do
trace = TracePoint.new(:call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.method_id
end
obj = Object.new
def obj.foo
bar
end
def obj.bar
baz
end
def obj.baz
end
trace.enable(target: obj.method(:foo)) do
obj.foo
end
ScratchPad.recorded.should == [:foo]
end
it "traces some events in nested blocks" do
klass = Class.new do
def foo
1.times do
1.times do
bar do
end
end
end
end
def bar(&blk)
blk.call
end
end
trace = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.lineno
end
obj = klass.new
_, lineno = obj.method(:foo).source_location
trace.enable(target: obj.method(:foo)) do
obj.foo
end
ScratchPad.recorded.should == (lineno+1..lineno+3).to_a
end
describe 'option value' do
it 'accepts Method' do
trace = TracePoint.new(:call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.method_id
end
obj = Object.new
def obj.foo; end
trace.enable(target: obj.method(:foo)) do
obj.foo
end
ScratchPad.recorded.should == [:foo]
end
it 'accepts UnboundMethod' do
trace = TracePoint.new(:call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.method_id
end
klass = Class.new do
def foo; end
end
unbound_method = klass.instance_method(:foo)
trace.enable(target: unbound_method) do
klass.new.foo
end
ScratchPad.recorded.should == [:foo]
end
it 'accepts Proc' do
trace = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.lineno
end
block = proc {}
_, lineno = block.source_location
trace.enable(target: block) do
block.call
end
ScratchPad.recorded.should == [lineno]
lineno.should be_kind_of(Integer)
end
end
it "raises ArgumentError if target object cannot trigger specified event" do
trace = TracePoint.new(:call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.method_id
end
block = proc {}
-> {
trace.enable(target: block) do
block.call # triggers :b_call and :b_return events
end
}.should raise_error(ArgumentError, /can not enable any hooks/)
end
it "raises ArgumentError if passed not Method/UnboundMethod/Proc" do
trace = TracePoint.new(:call) {}
-> {
trace.enable(target: Object.new) do
end
}.should raise_error(ArgumentError, /specified target is not supported/)
end
context "nested enabling and disabling" do
it "raises ArgumentError if trace point already enabled with target is re-enabled with target" do
trace = TracePoint.new(:b_call) {}
-> {
trace.enable(target: -> {}) do
trace.enable(target: -> {}) do
end
end
}.should raise_error(ArgumentError, /can't nest-enable a targett?ing TracePoint/)
end
it "raises ArgumentError if trace point already enabled without target is re-enabled with target" do
trace = TracePoint.new(:b_call) {}
-> {
trace.enable do
trace.enable(target: -> {}) do
end
end
}.should raise_error(ArgumentError, /can't nest-enable a targett?ing TracePoint/)
end
it "raises ArgumentError if trace point already enabled with target is re-enabled without target" do
trace = TracePoint.new(:b_call) {}
-> {
trace.enable(target: -> {}) do
trace.enable do
end
end
}.should raise_error(ArgumentError, /can't nest-enable a targett?ing TracePoint/)
end
it "raises ArgumentError if trace point already enabled with target is disabled with block" do
trace = TracePoint.new(:b_call) {}
-> {
trace.enable(target: -> {}) do
trace.disable do
end
end
}.should raise_error(ArgumentError, /can't disable a targett?ing TracePoint in a block/)
end
it "traces events when trace point with target is enabled in another trace point enabled without target" do
trace_outer = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << :outer
end
trace_inner = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << :inner
end
target = -> {}
trace_outer.enable do
trace_inner.enable(target: target) do
target.call
end
end
ScratchPad.recorded.should == [:outer, :outer, :outer, :inner]
end
it "traces events when trace point with target is enabled in another trace point enabled with target" do
trace_outer = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << :outer
end
trace_inner = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << :inner
end
target = -> {}
trace_outer.enable(target: target) do
trace_inner.enable(target: target) do
target.call
end
end
ScratchPad.recorded.should == [:inner, :outer]
end
it "traces events when trace point without target is enabled in another trace point enabled with target" do
trace_outer = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << :outer
end
trace_inner = TracePoint.new(:b_call) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << :inner
end
target = -> {}
trace_outer.enable(target: target) do
trace_inner.enable do
target.call
end
end
ScratchPad.recorded.should == [:inner, :inner, :outer]
end
end
end
describe 'target_line: option' do
before :each do
ScratchPad.record []
end
it "traces :line events only on specified line of code" do
trace = TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.lineno
end
target = -> {
x = 1
y = 2 # <= this line is target
z = x + y
}
_, lineno = target.source_location
target_line = lineno + 2
trace.enable(target_line: target_line, target: target) do
target.call
end
ScratchPad.recorded.should == [target_line]
end
it "raises ArgumentError if :target option isn't specified" do
trace = TracePoint.new(:line) {}
-> {
trace.enable(target_line: 67) do
end
}.should raise_error(ArgumentError, /only target_line is specified/)
end
it "raises ArgumentError if :line event isn't registered" do
trace = TracePoint.new(:call) {}
target = -> {
x = 1
y = 2 # <= this line is target
z = x + y
}
_, lineno = target.source_location
target_line = lineno + 2
-> {
trace.enable(target_line: target_line, target: target) do
end
}.should raise_error(ArgumentError, /target_line is specified, but line event is not specified/)
end
it "raises ArgumentError if :target_line value is out of target code lines range" do
trace = TracePoint.new(:line) {}
-> {
trace.enable(target_line: 1, target: -> { }) do
end
}.should raise_error(ArgumentError, /can not enable any hooks/)
end
it "raises TypeError if :target_line value couldn't be coerced to Integer" do
trace = TracePoint.new(:line) {}
-> {
trace.enable(target_line: Object.new, target: -> { }) do
end
}.should raise_error(TypeError, /no implicit conversion of \w+? into Integer/)
end
it "raises ArgumentError if :target_line value is negative" do
trace = TracePoint.new(:line) {}
-> {
trace.enable(target_line: -2, target: -> { }) do
end
}.should raise_error(ArgumentError, /can not enable any hooks/)
end
it "accepts value that could be coerced to Integer" do
trace = TracePoint.new(:line) do |tp|
next unless TracePointSpec.target_thread?
ScratchPad << tp.lineno
end
target = -> {
x = 1 # <= this line is target
}
_, lineno = target.source_location
target_line = lineno + 1
trace.enable(target_line: target_line.to_r, target: target) do
target.call
end
ScratchPad.recorded.should == [target_line]
end
end
end
|