Sha256: db00f0e2f66226274b67f9a092077454d4e20b6c1720ce2e1a7f443880f4bdf6

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe FiniteMachine::Callable, '#call' do

  before(:each) {
    Car = Class.new do
      attr_reader :result

      def turn_engine_on
        @result = 'turn_engine_on'
        @engine_on = true
      end

      def set_engine(value)
        @result = "set_engine(#{value})"
        @engine = value.to_sym == :on
      end

      def turn_engine_off
        @result = 'turn_engine_off'
        @engine_on = false
      end

      def engine_on?
        @result = 'engine_on'
        !!@engine_on
      end
    end
  }

  let(:called) { [] }

  let(:target) { Car.new }

  let(:instance) { described_class.new(object) }

  context 'when string' do
    let(:object) {  'engine_on?' }

    it 'executes method on target' do
      instance.call(target)
      expect(target.result).to eql('engine_on')
    end
  end

  context 'when string' do
    let(:object) {  'set_engine(:on)' }

    it 'executes method with arguments' do
      instance.call(target)
      expect(target.result).to eql('set_engine(on)')
    end
  end

  context 'when symbol' do
    let(:object) {  :engine_on? }

    it 'executes method on target' do
      instance.call(target)
      expect(target.result).to eql('engine_on')
    end
  end

  context 'when proc without args' do
    let(:object) {  proc { |a| called << "block_with(#{a})" } }

    it 'passes arguments' do
      instance.call(target)
      expect(called).to eql(["block_with(#{target})"])
    end
  end

  context 'when proc with args' do
    let(:object) {  proc { |a,b| called << "block_with(#{a},#{b})" } }

    it 'passes arguments' do
      instance.call(target, :red)
      expect(called).to eql(["block_with(#{target},red)"])
    end
  end

  context 'when unknown' do
    let(:object) { Object.new }

    it 'raises error' do
      expect { instance.call(target) }.to raise_error(ArgumentError)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
finite_machine-0.2.0 spec/unit/callable/call_spec.rb