1require 'test/unit'
2
3class TestTrace < Test::Unit::TestCase
4  def test_trace
5    $x = 1234
6    $y = 0
7    trace_var :$x, proc{$y = $x}
8    $x = 40414
9    assert_equal($x, $y)
10
11    untrace_var :$x
12    $x = 19660208
13    assert_not_equal($x, $y)
14
15    trace_var :$x, proc{$x *= 2}
16    $x = 5
17    assert_equal(10, $x)
18
19    untrace_var :$x
20  end
21
22  def test_trace_tainted_proc
23    $x = 1234
24    s = proc { $y = :foo }
25    trace_var :$x, s
26    s.taint
27    $x = 42
28    assert_equal(:foo, $y)
29  ensure
30    untrace_var :$x
31  end
32
33  def test_trace_proc_that_raises_exception
34    $x = 1234
35    trace_var :$x, proc { raise }
36    assert_raise(RuntimeError) { $x = 42 }
37  ensure
38    untrace_var :$x
39  end
40
41  def test_trace_string
42    $x = 1234
43    trace_var :$x, "$y = :bar"
44    $x = 42
45    assert_equal(:bar, $y)
46  ensure
47    untrace_var :$x
48  end
49
50  def test_trace_break
51    bug2722 = '[ruby-core:31783]'
52    a = Object.new.extend(Enumerable)
53    def a.each
54      yield
55    end
56    assert(Thread.start {
57             Thread.current.add_trace_func(proc{})
58             a.any? {true}
59           }.value, bug2722)
60  end
61end
62