File: test_instance_exec.rb

package info (click to toggle)
ruby-facets 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,824 kB
  • sloc: ruby: 25,483; xml: 90; makefile: 20
file content (44 lines) | stat: -rw-r--r-- 1,072 bytes parent folder | download
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
#require 'facets/kernel/instance_exec.rb'
require 'facets/one_nine.rb'
require 'test/unit'

class TestInstanceEvalWithArgs < Test::Unit::TestCase

  class Dummy
    def f
      :dummy_value
    end
  end

  def test_instance_exec
    # Create a block that returns the value of an argument
    # and a value of a method call to +self+.
    block = lambda { |a| [a, f] }
    assert_equal [:arg_value, :dummy_value],
      Dummy.new.instance_exec(:arg_value, &block)
  end

  def test_instance_exec_with_frozen_obj
    block = lambda { |a| [a, f] }
    obj = Dummy.new
    obj.freeze
    assert_equal [:arg_value, :dummy_value], obj.instance_exec(:arg_value, &block)
  end

  def test_instance_exec_nested
    i = 0
    obj = Dummy.new
    block = lambda do |arg|
      [arg] + instance_exec(1){|a| [f, a] }
    end
    assert_equal([:arg_value, :dummy_value, 1], obj.instance_exec(:arg_value, &block))
  end

  def test_instance_exec_with_immediate_value
    obj = 1
    block = lambda { |a| [a,self] }
    assert_equal(["A", 1], obj.instance_exec("A", &block))
  end

end