Sha256: 32ad34fdf0c4d71c5a527b0420582cef8cad1380f49df9d3978d2f30e50c379e

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

class FailHandlingStateMachine
  class Error < StandardError; end
  class RunError < StandardError; end

  include SimplyFSM

  state_machine :activity, fail: :on_any_fail do
    state :sleeping, initial: true
    state :running
    state :cleaning

    event :sleep, transitions: { from: %i[running cleaning], to: :sleeping }
    event :clean, transitions: { from: :running, to: :cleaning }
    event :run,
          fail: ->(_event) { raise RunError, "Cannot run" },
          transitions: { from: :sleeping, to: :running }
  end

  def on_any_fail(event_name)
    raise Error, "Cannot do: #{event_name}"
  end
end

RSpec.describe FailHandlingStateMachine do
  describe "#sleep" do
    it "error if already sleeping" do
      expect { subject.sleep }.to raise_error(FailHandlingStateMachine::Error)
    end
  end

  describe "#run" do
    it "custom error if already running" do
      subject.run
      expect { subject.run }.to raise_error(FailHandlingStateMachine::RunError)
    end

    it "custom error if cleaning" do
      subject.run
      subject.clean
      expect { subject.run }.to raise_error(FailHandlingStateMachine::RunError)
    end
  end

  describe "#clean" do
    it "error if sleeping" do
      expect { subject.clean }.to raise_error(FailHandlingStateMachine::Error)
    end

    it "error if already cleaning" do
      subject.run
      subject.clean
      expect { subject.clean }.to raise_error(FailHandlingStateMachine::Error)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simply_fsm-0.3.0 spec/unit/fail_events_spec.rb
simply_fsm-0.2.3 spec/unit/fail_events_spec.rb
simply_fsm-0.2.1 spec/unit/fail_events_spec.rb
simply_fsm-0.2.0 spec/unit/fail_events_spec.rb