Sha256: cb1ea11c509935f59698d305e81efe423bfeb8acf4275190ceac583993d97671

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

require "test_helper"

describe MicroFSM do
  let(:fsm) {
    MicroFSM.new(:pending)
      .when(:confirm, pending: :confirmed)
      .when(:ignore, pending: :ignored)
      .when(:reset, confirmed: :pending, ignored: :pending)
  }

  def test_class
    assert_kind_of MicroFSM, fsm
  end

  def test_defines_initial_state
    assert_equal :pending, fsm.state
  end

  def test_changes_the_state
    assert fsm.trigger?(:confirm)
    assert fsm.trigger(:confirm)
    assert_equal :confirmed, fsm.state
  end

  def test_preserves_the_state_if_transition_is_not_possible
    refute fsm.trigger?(:reset)
    refute fsm.trigger(:reset)
    assert_equal :pending, fsm.state
  end

  def test_multiple_transitions
    fsm.trigger(:confirm)
    assert_equal :confirmed, fsm.state

    fsm.trigger(:reset)
    assert_equal :pending, fsm.state

    fsm.trigger(:ignore)
    assert_equal :ignored, fsm.state

    fsm.trigger(:reset)
    assert_equal :pending, fsm.state
  end

  def test_raises_an_error_if_an_invalid_event_is_triggered
    assert_raises(MicroFSM::InvalidEvent) do
      fsm.trigger(:random_event)
    end
  end

  def test_raises_error_if_trigger_from_incompatible_state
    assert_raises(MicroFSM::InvalidState) do
      fsm.trigger!(:reset)
    end
  end

  def test_raises_an_error_if_a_transition_is_overwritten
    fsm = MicroFSM.new(:init)

    fsm.when(:trigger, init: :started)
    assert_raises(MicroFSM::InvalidTransition) do
      fsm.when(:trigger, init: :wrong)
    end
  end

  def test_state_assign
    new_state = :unknown
    refute_equal new_state, fsm.state
    fsm.state = new_state
    assert_equal new_state, fsm.state
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
microfsm-0.1.7 test/microfsm_test.rb
microfsm-0.1.6 test/microfsm_test.rb
microfsm-0.1.4 test/microfsm_test.rb