Sha256: 25426f8588f2e800c8970a53f8a612e7b628749f66ce678753eef769e6413027

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

RSpec.describe FiniteMachine::Transition, '#check_conditions' do
  it "verifies all conditions pass" do
    context = double(:context)
    exec_conditions = 0
    ok_condition = -> { exec_conditions += 1; return true }
    fail_condition = -> { exec_conditions += 1; return false }

    transition = described_class.new(context, :event_name,
                                     if: [ok_condition, fail_condition])

    expect(transition.check_conditions).to eql(false)
    expect(exec_conditions).to eq(2)
  end

  it "verifies 'if' and 'unless' conditions" do
    context = double(:context)
    exec_conditions = 0
    ok_condition = -> { exec_conditions += 1; return true }
    fail_condition = -> { exec_conditions += 1; return false }

    transition = described_class.new(context, :event_name,
                                     if: [ok_condition],
                                     unless: [fail_condition])

    expect(transition.check_conditions).to eql(true)
    expect(exec_conditions).to eq(2)
  end

  it "verifies condition with arguments" do
    context = double(:context)
    condition = -> (_, arg) { arg == 1 }

    transition = described_class.new(context, :event_name,
                                     if: [condition])

    expect(transition.check_conditions(2)).to eql(false)
    expect(transition.check_conditions(1)).to eql(true)
  end

  it "verifies condition on target" do
    stub_const("Car", Class.new do
      def engine_on?
        true
      end
    end)
    context = Car.new
    condition = -> (car) { car.engine_on? }

    transition = described_class.new(context, :event_name, if: condition)

    expect(transition.check_conditions).to eql(true)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finite_machine-0.12.1 spec/unit/transition/check_conditions_spec.rb
finite_machine-0.12.0 spec/unit/transition/check_conditions_spec.rb