brew/Library/Homebrew/test/test_object.rb
Jack Nagel 2503cedf2c Object#instance_exec for Ruby 1.8.6
Not thread safe! But I don't think we care.

We want to evaluate the env DSL block in the context of ENV for asthetic
reasons, but we also want access to methods on the requirement instance.
We can use #instance_exec to pass the requirement itself into the block:

  class Foo < Requirement
    env do |req|
      append 'PATH', req.some_path
    end

    def some_path
      which 'something'
    end
  end

Also add a simplified version of Object#instance_exec for Ruby 1.8.6.
2013-01-21 17:24:11 -06:00

21 lines
496 B
Ruby

require 'testing_env'
require 'extend/object'
class InstanceExecTests < Test::Unit::TestCase
def test_evaluates_in_context_of_receiver
assert_equal 1, [1].instance_exec { first }
end
def test_passes_arguments_to_block
assert_equal 2, [1].instance_exec(1) { |x| first + x }
end
def test_does_not_persist_temporary_singleton_method
obj = Object.new
before = obj.methods
obj.instance_exec { methods }
after = obj.methods
assert_equal before, after
end
end