Sha256: 78fca86e16e585726cf2aad918bedbd50d6fdfa1f89ed4bf7222d607c9b59d56

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

require 'test_helper'

class Sending
  extend Surrounded::Context
  
  initialize :one, :two
  
  forwarding [:hello, :goodbye] => :one
  forward_trigger :two, :ping
  forward_trigger :two, :argumentative
  forwards :two, :blockhead
  
  role :one do
    def hello
      'hello'
    end
    
    def goodbye
      'goodbye'
    end
  end
  
  role :two do
    def ping
      one.hello
    end

    def argumentative(yes, no)
      [yes, no].join(' and ')
    end

    def blockhead
      yield
    end
  end
end

describe Surrounded::Context, 'forwarding triggers' do
  let(:user){ User.new("Jim") }
  let(:other_user){ User.new("Guille") }
  let(:context){ Sending.new(one: user, two: other_user) }
  
  it 'forwards multiple configured instance methods as triggers' do
    assert_equal 'hello', context.hello
    assert_equal 'goodbye', context.goodbye
  end
  
  it 'forwards individual configured instance methods as triggers' do
    assert_equal 'hello', context.ping
  end
  
  it 'does not forward __id__' do
    error = assert_raises(ArgumentError){
      Sending.class_eval do
        forward_trigger :one, :__id__
      end
    }
    assert_match(/you may not forward '__id__`/i, error.message)
  end
  
  it 'does not forward __send__' do
    error = assert_raises(ArgumentError){
      Sending.class_eval do
        forward_trigger :one, :__send__
      end
    }
    assert_match(/you may not forward '__send__/i, error.message)
  end

  it 'passes arguments' do
    assert_equal 'YES and NO', context.argumentative('YES','NO')
  end

  it 'passes blocks' do
    result = context.blockhead do
      "Put them in the iron maiden. Excellent!"
    end
    assert_equal "Put them in the iron maiden. Excellent!", result
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
surrounded-1.1.0 test/context_forwarding_test.rb
surrounded-1.0.0 test/context_forwarding_test.rb