Project

General

Profile

Bug #5614 ยป proc_test.rb

ngtzeyang (TzeYang Ng), 11/11/2011 02:17 PM

 
1
require 'minitest/unit'
2
require 'minitest/autorun'
3

    
4
class ProcTest < MiniTest::Unit::TestCase
5

    
6
  class Foo
7
    class << self
8

    
9
      def grab_source_location(&blk)
10
        blk.call(blk.source_location)
11
        self
12
      end
13

    
14
      def grab_inspect(&blk)
15
        blk.call(blk.inspect)
16
        self
17
      end
18
    end
19
  end
20

    
21
  if Proc.new {}.respond_to?(:source_location)
22
    def test_source_location
23
      line = __LINE__ + 3
24
      results = []
25

    
26
      Foo.grab_source_location do |location|
27
        results << location[1]
28
      end.grab_source_location do |location|
29
        results << location[1]
30
      end
31

    
32
      assert_equal results[0], line
33
      assert_equal results[1], line + 2
34
    end
35
  end
36

    
37
  def test_inspect
38
    line = __LINE__ + 3
39
    results = []
40

    
41
    Foo.grab_inspect do |line|
42
      results << line
43
    end.grab_inspect do |line|
44
      results << line
45
    end
46

    
47
    assert_match results[0], /:#{line}/
48
    assert_match results[1], /:#{line + 2}/
49
  end
50

    
51
end