Sha256: 41fdaf891ffdf64346e0f2bc3e7f261b3b3edd4dc853fc2abc887836d728345e

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'test_helper'

module Hooks
  class On < StatePattern::State
    def press
      transition_to(Off)
      stateful.messages << "#{stateful.button_name} is off"
    end

    def enter
      stateful.messages << "Entered the On state"
    end

    def exit
      stateful.messages << "Exited the On state"
    end

  private 
    def private_method
    end
  end

  class Off < StatePattern::State
    def press
      transition_to(On)
      stateful.messages << "#{stateful.button_name} is on"
    end

    def enter
      stateful.messages << "Entered the Off state"
    end

    def exit
      stateful.messages << "Exited the Off state"
    end
  end

  class Button
    include StatePattern
    set_initial_state Off

    def button_name
      "Button"
    end

    def messages
      @messages ||= []
    end
  end
end

Expectations do
  expect ["Entered the Off state", "Exited the Off state", "Entered the On state", "Button is on"] do
    button = Hooks::Button.new
    button.press
    button.messages
  end

  expect ["Entered the Off state", "Exited the Off state", "Entered the On state", "Button is on", "Exited the On state", "Entered the Off state", "Button is off"] do
    button = Hooks::Button.new
    button.press
    button.press
    button.messages
  end

  expect false do
    Hooks::Button.new.respond_to?(:enter)
  end

  expect false do
    Hooks::Button.new.respond_to?(:exit)
  end

  expect false do
    Hooks::Button.new.respond_to?(:private_method)
  end

  expect true do
    Hooks::Button.new.respond_to?(:press)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
state_pattern-2.0.2 ./test/hook_test.rb
state_pattern-2.0.1 ./test/hook_test.rb
state_pattern-2.0.0 ./test/hook_test.rb