File: trace_test.rb

package info (click to toggle)
ruby-power-assert 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: ruby: 1,661; makefile: 5; sh: 4
file content (94 lines) | stat: -rw-r--r-- 2,026 bytes parent folder | download | duplicates (4)
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
if defined?(RubyVM) and ! RubyVM::InstructionSequence.compile_option[:specialized_instruction]
  warn "#{__FILE__}: specialized_instruction is set to false"
end

require_relative 'test_helper'

class TestTraceContext < Test::Unit::TestCase
  include PowerAssertTestHelper

  class TestInterface < Byebug::Interface
    def readline(prompt)
      'next'
    end

    def do_nothing(*)
    end

    alias puts do_nothing
    alias print do_nothing
    alias errmsg do_nothing
  end

  class << self
    def startup
      Byebug::Context.interface = TestInterface.new
      Byebug::Context.processor = PowerAssertTestHelper::TestProcessor
      Byebug::Setting[:autosave] = false
    end

    def iseq
      :iseq
    end

    define_method(:bmethod) do
      :bmethod
    end
  end

  setup do
    Byebug.start
  end

  teardown do
    Byebug.stop
  end

  def trace_test(expected_message, file, lineno, binding = TOPLEVEL_BINDING)
    code = "byebug\n#{expected_message.each_line.first}\n"
    lineno -= 1 # For 'byebug' at the first line
    eval(code, binding, file, lineno)
    pa = Byebug.current_context.__send__(:processor).pa_context
    assert_equal(expected_message, pa.message)
    assert_true(pa.enabled?)
    pa.disable
    assert_false(pa.enabled?)
  end

  def test_iseq
    trace_test(<<END.chomp, __FILE__, __LINE__ + 1)
      TestTraceContext.iseq
      |                |
      |                :iseq
      TestTraceContext
END
  end

  def test_bmethod
    trace_test(<<END.chomp, __FILE__, __LINE__ + 1)
      TestTraceContext.bmethod
      |                |
      |                :bmethod
      TestTraceContext
END
  end

  def test_cfunc
    trace_test(<<END.chomp, __FILE__, __LINE__ + 1)
      0 == 0
        |
        true
END
  end

  def test_all_refs
    a, b, c, = 0, 1, 2, a, b, c # suppress "assigned but unused variable" warning
    trace_test(<<END.chomp, __FILE__, __LINE__ + 1, binding)
      a ? b : c
      |   |   |
      |   |   2
      |   1
      0
END
  end
end if defined?(Byebug)