class MethodDelegationTest

Public Instance Methods

checkCallOriginal(delegator, method, originalResult, newMethod) click to toggle source
# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 168
def checkCallOriginal(delegator, method, originalResult, newMethod)
  if newMethod
    assert_raise NoMethodError do
      result = delegator.send(method)
    end
  else
    result = delegator.send(method)
    assert_equal originalResult, result
  end
end
checkDelegation(delegator, method, originalResult, newMethod=false) click to toggle source
# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 96
def checkDelegation(delegator, method, originalResult, newMethod=false)
  delegate1 = TestDelegate.new
  delegate2 = TestDelegate.new
  
  Util::MethodDelegation.registerDelegate(delegate1, delegator, method)
  Util::MethodDelegation.registerDelegate(delegate2, delegator, method)
  
  assert delegator.respond_to?(:_methodDelegates)
  if newMethod
    assert !delegator.respond_to?("#{method}_delegate_original".to_sym)
  else
    assert delegator.respond_to?("#{method}_delegate_original".to_sym)
  end

  # check delegator parameter    

  delegate1.mode = :delegatorId
  assert_equal delegator.object_id, delegator.send(method)
  
  delegate1.callcount = 0
  delegate2.callcount = 0
  
  delegate1.mode = :return7
  # delegate1 returns a value

  assert_equal 7, delegator.send(method)
  assert_equal 1, delegate1.callcount
  # delegate2 is not called

  assert_equal 0, delegate2.callcount
  
  delegate1.mode = :nothing
  # delegate1 just exits and thus returns nil

  assert_equal nil, delegator.send(method)
  assert_equal 2, delegate1.callcount
  # delegate2 is not called

  assert_equal 0, delegate2.callcount
  
  delegate1.mode = :continue
  delegate2.mode = :return7
  # delegate1 is called but continues

  # delegate2 returns a value

  assert_equal 7, delegator.send(method)
  assert_equal 3, delegate1.callcount
  assert_equal 1, delegate2.callcount
  
  delegate1.mode = :continue
  delegate2.mode = :continue
  # both delegates continue, the original method returns its value

  checkCallOriginal(delegator, method, originalResult, newMethod)
  # both delegates are called though

  assert_equal 4, delegate1.callcount
  assert_equal 2, delegate2.callcount
  
  # calling unregister with a non existing method has no effect

  Util::MethodDelegation.unregisterDelegate(delegate1, delegator, "xxx")
  Util::MethodDelegation.unregisterDelegate(delegate1, delegator, method)
  
  checkCallOriginal(delegator, method, originalResult, newMethod)
  # delegate1 not called any more

  assert_equal 4, delegate1.callcount
  # delegate2 is still called

  assert_equal 3, delegate2.callcount
  
  Util::MethodDelegation.unregisterDelegate(delegate2, delegator, method)
  
  checkCallOriginal(delegator, method, originalResult, newMethod)
  # both delegates not called any more

  assert_equal 4, delegate1.callcount
  assert_equal 3, delegate2.callcount
  
  # after all delegates were unregistered, singleton class should be clean

  assert !delegator.respond_to?(:_methodDelegates)
end
test_const_missing() click to toggle source
# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 83
def test_const_missing
  surroundingModule = Module.nesting.first
  Util::MethodDelegation.registerDelegate(ConstPathElement, surroundingModule, "const_missing")
  
  assert_equal "SomeArbitraryConst", SomeArbitraryConst.to_s
  assert_equal "AnotherConst::A::B::C", AnotherConst::A::B::C.to_s
  
  Util::MethodDelegation.unregisterDelegate(ConstPathElement, surroundingModule, "const_missing")
  assert_raise NameError do 
    SomeArbitraryConst
  end
end
test_method_defined_in_class() click to toggle source
# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 63
def test_method_defined_in_class
  # delegator is a String

  delegator = "Delegator1"
  checkDelegation(delegator, "to_s", "Delegator1")
end
test_method_defined_in_singleton() click to toggle source

missing: check with multiple params and block param

# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 51
def test_method_defined_in_singleton
  # delegator is an Array

  delegator = []
  # delegating method is a method defined in the singleton class

  class << delegator
    def methodInSingleton
      "result from method in singleton"
    end
  end
  checkDelegation(delegator, "methodInSingleton", "result from method in singleton")
end
test_method_defined_in_superclass() click to toggle source
# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 69
def test_method_defined_in_superclass
  # delegator is an instance of a new anonymous class

  delegator = Class.new.new
  # delegating method is +object_id+ which is defined in the superclass

  checkDelegation(delegator, "class", delegator.class)
end
test_new_method() click to toggle source
# File lib/puppet/vendor/rgen/test/method_delegation_test.rb, line 76
def test_new_method
  # delegator is an String

  delegator = "Delegator2"
  # delegating method is a new method which does not exist on String

  checkDelegation(delegator, "artificialMethod", delegator.object_id, true)
end