Sha256: 3af4e564c567d09db47546fae2fe7115a600f29643f3b46a3aca508be2b099aa

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require 'test_helper'

describe Casting, '.delegating' do
  it 'delegates missing methods to object delegates' do
    client = test_person
    client.extend(Casting::Client)
    client.delegate_missing_methods

    attendant = test_person
    attendant.extend(TestPerson::Greeter)

    assert_raises(NoMethodError){
      client.greet
    }
    Casting.delegating(client => attendant) do
      assert_equal 'hello', client.greet
    end
    assert_raises(NoMethodError){
      client.greet
    }
  end
end

describe Casting::Delegation do

  it 'calls a method defined on another object of the same type' do
    client = test_person
    attendant = test_person
    attendant.extend(TestPerson::Greeter)
    delegation = Casting::Delegation.new('greet', client).to(attendant)
    assert_equal 'hello', delegation.call
  end

  it 'passes arguments to a delegated method' do
    client = test_person
    attendant = test_person
    attendant.extend(TestPerson::Verbose)
    delegation = Casting::Delegation.new('verbose', client).to(attendant).with('arg1','arg2')
    assert_equal 'arg1,arg2', delegation.call
  end

  it 'delegates when given a module' do
    client = test_person
    delegation = Casting::Delegation.new('greet', client).to(TestPerson::Greeter)
    assert_equal 'hello', delegation.call
  end

  it 'does not delegate when given a class' do
    client = test_person
    assert_raises(TypeError){
      Casting::Delegation.new('class_defined', client).to(Unrelated)
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
casting-0.7.2 test/casting_19_test.rb
casting-0.7.1 test/casting_19_test.rb
casting-0.7.0 test/casting_19_test.rb