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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
require_relative 'test_helper'
class MachineWithInternationalizationTest < BaseTestCase
def setup
I18n.backend = I18n::Backend::Simple.new
# Initialize the backend
StateMachines::Machine.new(new_model)
@model = new_model
end
def test_should_use_defaults
I18n.backend.store_translations(:en, {
:activerecord => {:errors => {:messages => {:invalid_transition => "cannot #{interpolation_key('event')}"}}}
})
machine = StateMachines::Machine.new(@model)
machine.state :parked, :idling
machine.event :ignite
record = @model.new(:state => 'idling')
machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
assert_equal ['State cannot transition via "ignite"'], record.errors.full_messages
end
def test_should_allow_customized_error_key
I18n.backend.store_translations(:en, {
:activerecord => {:errors => {:messages => {:bad_transition => "cannot #{interpolation_key('event')}"}}}
})
machine = StateMachines::Machine.new(@model, :messages => {:invalid_transition => :bad_transition})
machine.state :parked, :idling
record = @model.new(:state => 'idling')
machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
assert_equal ['State cannot ignite'], record.errors.full_messages
end
def test_should_allow_customized_error_string
machine = StateMachines::Machine.new(@model, :messages => {:invalid_transition => "cannot #{interpolation_key('event')}"})
machine.state :parked, :idling
record = @model.new(:state => 'idling')
machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
assert_equal ['State cannot ignite'], record.errors.full_messages
end
def test_should_allow_customized_state_key_scoped_to_class_and_machine
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:'foo' => {:state => {:states => {:parked => 'shutdown'}}}}}
})
machine = StateMachines::Machine.new(@model)
machine.state :parked
assert_equal 'shutdown', machine.state(:parked).human_name
end
def test_should_allow_customized_state_key_scoped_to_class
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:'foo' => {:states => {:parked => 'shutdown'}}}}
})
machine = StateMachines::Machine.new(@model)
machine.state :parked
assert_equal 'shutdown', machine.state(:parked).human_name
end
def test_should_allow_customized_state_key_scoped_to_machine
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:state => {:states => {:parked => 'shutdown'}}}}
})
machine = StateMachines::Machine.new(@model)
machine.state :parked
assert_equal 'shutdown', machine.state(:parked).human_name
end
def test_should_allow_customized_state_key_unscoped
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:states => {:parked => 'shutdown'}}}
})
machine = StateMachines::Machine.new(@model)
machine.state :parked
assert_equal 'shutdown', machine.state(:parked).human_name
end
def test_should_support_nil_state_key
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:states => {:nil => 'empty'}}}
})
machine = StateMachines::Machine.new(@model)
assert_equal 'empty', machine.state(nil).human_name
end
def test_should_allow_customized_event_key_scoped_to_class_and_machine
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:'foo' => {:state => {:events => {:park => 'stop'}}}}}
})
machine = StateMachines::Machine.new(@model)
machine.event :park
assert_equal 'stop', machine.event(:park).human_name
end
def test_should_allow_customized_event_key_scoped_to_class
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:'foo' => {:events => {:park => 'stop'}}}}
})
machine = StateMachines::Machine.new(@model)
machine.event :park
assert_equal 'stop', machine.event(:park).human_name
end
def test_should_allow_customized_event_key_scoped_to_machine
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:state => {:events => {:park => 'stop'}}}}
})
machine = StateMachines::Machine.new(@model)
machine.event :park
assert_equal 'stop', machine.event(:park).human_name
end
def test_should_allow_customized_event_key_unscoped
I18n.backend.store_translations(:en, {
:activerecord => {:state_machines => {:events => {:park => 'stop'}}}
})
machine = StateMachines::Machine.new(@model)
machine.event :park
assert_equal 'stop', machine.event(:park).human_name
end
def test_should_only_add_locale_once_in_load_path
assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_record/locale\.rb$} }.length
# Create another ActiveRecord model that will trigger the i18n feature
new_model
assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_record/locale\.rb$} }.length
end
def test_should_prefer_other_locales_first
@original_load_path = I18n.load_path
I18n.backend = I18n::Backend::Simple.new
I18n.load_path = [File.dirname(__FILE__) + '/files/en.yml']
machine = StateMachines::Machine.new(@model)
machine.state :parked, :idling
machine.event :ignite
record = @model.new(:state => 'idling')
machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
assert_equal ['State cannot transition'], record.errors.full_messages
ensure
I18n.load_path = @original_load_path
end
private
def interpolation_key(key)
"%{#{key}}"
end
end
|