File: machine_with_internationalization_test.rb

package info (click to toggle)
ruby-state-machines-activemodel 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 276 kB
  • sloc: ruby: 860; makefile: 5
file content (187 lines) | stat: -rw-r--r-- 6,810 bytes parent folder | download | duplicates (3)
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
181
182
183
184
185
186
187
require_relative 'test_helper'
require 'i18n'

class MachineWithInternationalizationTest < BaseTestCase
  def setup
    I18n.backend = I18n::Backend::Simple.new
    @model = new_model { include ActiveModel::Validations }
  end

  def test_should_use_defaults
    I18n.backend.store_translations(:en,
                                    activemodel: { errors: { messages: { invalid_transition: 'cannot %{event}' } } }
                                    )

    machine = StateMachines::Machine.new(@model, action: :save)
    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,
                                    activemodel: { errors: { messages: { bad_transition: 'cannot %{event}' } } }
                                    )

    machine = StateMachines::Machine.new(@model, action: :save, messages: { invalid_transition: :bad_transition })
    machine.state :parked, :idling

    record = @model.new
    record.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, action: :save, messages: { invalid_transition: 'cannot %{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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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,
                                    activemodel: { 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_model/locale\.rb$} }.length

    # Create another ActiveModel model that will triger the i18n feature
    new_model

    assert_equal 1, I18n.load_path.select { |path| path =~ %r{active_model/locale\.rb$} }.length
  end

#  def test_should_add_locale_to_beginning_of_load_path
    #@original_load_path = I18n.load_path
    #I18n.backend = I18n::Backend::Simple.new

    #app_locale = File.dirname(__FILE__) + '/files/en.yml'
    #default_locale = File.dirname(__FILE__) + '/../lib/state_machines/integrations/active_model/locale.rb'
    #I18n.load_path = [app_locale]

    #StateMachines::Machine.new(@model)

    #assert_equal [default_locale, app_locale].map { |path| File.expand_path(path) }, I18n.load_path.map { |path| File.expand_path(path) }
  #ensure
    #I18n.load_path = @original_load_path
  #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 ignite'], record.errors.full_messages
  ensure
    I18n.load_path = @original_load_path
  end
end