<commit_before># encoding: utf-8

require 'spec_helper'

describe FiniteMachine, 'events' do

  it "allows for hash rocket syntax to describe transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow, :green  => :yellow
        event :stop, :yellow => :red
      }
    end

    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
  end

  it "allows for (:from | :to) key pairs to describe transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow, from: :green, to: :yellow
        event :stop, from: :yellow, to: :red
      }
    end

    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
  end

  it "permits no-op event without 'to' transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :noop,  from: :green
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
        event :ready, from: :red,    to: :yellow
        event :go,    from: :yellow, to: :green
      }
    end

    expect(fsm.current).to eql(:green)

    expect(fsm.can?(:noop)).to be_true
    expect(fsm.can?(:slow)).to be_true

    fsm.noop
    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:yellow)

    expect(fsm.cannot?(:noop)).to be_true
    expect(fsm.cannot?(:slow)).to be_true
  end

  it "permits event from any state with :any 'from'" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
        event :ready, from: :red,    to: :yellow
        event :go,    from: :yellow, to: :green
        event :run,   from: :any,    to: :green
      }
    end

    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.go
    expect(fsm.current).to eql(:green)
    fsm.run
    expect(fsm.current).to eql(:green)
  end

  it "permits event from any state without 'from'" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
        event :ready, from: :red,    to: :yellow
        event :go,    from: :yellow, to: :green
        event :run,                  to: :green
      }
    end

    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.go
    expect(fsm.current).to eql(:green)
    fsm.run
    expect(fsm.current).to eql(:green)
  end

  it "raises error on invalid transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
      }
    end

    expect(fsm.current).to eql(:green)

    expect { fsm.stop }.to raise_error(FiniteMachine::TransitionError, /state 'green'/)
  end

  context 'when multiple from states' do
    it "allows for array from key" do
      fsm = FiniteMachine.define do
        initial :green

        events {
          event :slow,  :green            => :yellow
          event :stop,  [:green, :yellow] => :red
          event :ready, :red              => :yellow
          event :go,    [:yellow, :red]   => :green
        }
      end

      expect(fsm.current).to eql(:green)

      expect(fsm.can?(:slow)).to be_true
      expect(fsm.can?(:stop)).to be_true
      expect(fsm.cannot?(:ready)).to be_true
      expect(fsm.cannot?(:go)).to be_true

      fsm.slow;  expect(fsm.current).to eql(:yellow)
      fsm.stop;  expect(fsm.current).to eql(:red)
      fsm.ready; expect(fsm.current).to eql(:yellow)<commit_msg>Fix spec for new error type.<commit_after># encoding: utf-8

require 'spec_helper'

describe FiniteMachine, 'events' do

  it "allows for hash rocket syntax to describe transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow, :green  => :yellow
        event :stop, :yellow => :red
      }
    end

    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
  end

  it "allows for (:from | :to) key pairs to describe transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow, from: :green, to: :yellow
        event :stop, from: :yellow, to: :red
      }
    end

    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
  end

  it "permits no-op event without 'to' transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :noop,  from: :green
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
        event :ready, from: :red,    to: :yellow
        event :go,    from: :yellow, to: :green
      }
    end

    expect(fsm.current).to eql(:green)

    expect(fsm.can?(:noop)).to be_true
    expect(fsm.can?(:slow)).to be_true

    fsm.noop
    expect(fsm.current).to eql(:green)
    fsm.slow
    expect(fsm.current).to eql(:yellow)

    expect(fsm.cannot?(:noop)).to be_true
    expect(fsm.cannot?(:slow)).to be_true
  end

  it "permits event from any state with :any 'from'" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
        event :ready, from: :red,    to: :yellow
        event :go,    from: :yellow, to: :green
        event :run,   from: :any,    to: :green
      }
    end

    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.go
    expect(fsm.current).to eql(:green)
    fsm.run
    expect(fsm.current).to eql(:green)
  end

  it "permits event from any state without 'from'" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
        event :ready, from: :red,    to: :yellow
        event :go,    from: :yellow, to: :green
        event :run,                  to: :green
      }
    end

    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.stop
    expect(fsm.current).to eql(:red)
    fsm.run
    expect(fsm.current).to eql(:green)

    fsm.slow
    expect(fsm.current).to eql(:yellow)
    fsm.go
    expect(fsm.current).to eql(:green)
    fsm.run
    expect(fsm.current).to eql(:green)
  end

  it "raises error on invalid transition" do
    fsm = FiniteMachine.define do
      initial :green

      events {
        event :slow,  from: :green,  to: :yellow
        event :stop,  from: :yellow, to: :red
      }
    end

    expect(fsm.current).to eql(:green)

    expect { fsm.stop }.to raise_error(FiniteMachine::InvalidStateError, /state 'green'/)
  end

  context 'when multiple from states' do
    it "allows for array from key" do
      fsm = FiniteMachine.define do
        initial :green

        events {
          event :slow,  :green            => :yellow
          event :stop,  [:green, :yellow] => :red
          event :ready, :red              => :yellow
          event :go,    [:yellow, :red]   => :green
        }
      end

      expect(fsm.current).to eql(:green)

      expect(fsm.can?(:slow)).to be_true
      expect(fsm.can?(:stop)).to be_true
      expect(fsm.cannot?(:ready)).to be_true
      expect(fsm.cannot?(:go)).to be_true

      fsm.slow;  expect(fsm.current).to eql(:yellow)
      fsm.stop;  expect(fsm.current).to eql(:red)
      fsm.ready; expect(fsm.current).to eql(:yellow)
{'fraction_non_alphanumeric': 0.13149777219239117, 'fraction_numerical': 0.00022849308808408546, 'mean_word_length': 2.245828698553949, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4859094', 'n_tokens_mistral': 3235, 'n_tokens_neox': 3027, 'n_words': 762}