1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
# frozen_string_literal: true
require 'test_helper'
class MachineStateInitializationTest < StateMachinesTest
def setup
@klass = Class.new
@machine = StateMachines::Machine.new(@klass, :state, initial: :parked, initialize: false)
@object = @klass.new
@object.state = nil
end
def test_should_set_states_if_nil
@machine.initialize_state(@object)
assert_equal 'parked', @object.state
end
def test_should_set_states_if_empty
@object.state = ''
@machine.initialize_state(@object)
assert_equal 'parked', @object.state
end
def test_should_not_set_states_if_not_empty
@object.state = 'idling'
@machine.initialize_state(@object)
assert_equal 'idling', @object.state
end
def test_should_set_states_if_not_empty_and_forced
@object.state = 'idling'
@machine.initialize_state(@object, force: true)
assert_equal 'parked', @object.state
end
def test_should_not_set_state_if_nil_and_nil_is_valid_state
@machine.state :initial, value: nil
@machine.initialize_state(@object)
assert_nil @object.state
end
def test_should_write_to_hash_if_specified
@machine.initialize_state(@object, to: hash = {})
assert_equal({ 'state' => 'parked' }, hash)
end
def test_should_not_write_to_object_if_writing_to_hash
@machine.initialize_state(@object, to: {})
assert_nil @object.state
end
end
|