state_machines-yard 0.1.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +12 -4
  3. data/.github/workflows/release.yml +8 -6
  4. data/.release-please-manifest.json +3 -0
  5. data/.rubocop.yml +38 -0
  6. data/CHANGELOG.md +54 -0
  7. data/Gemfile +12 -1
  8. data/README.md +45 -0
  9. data/Rakefile +21 -3
  10. data/lib/state_machines/yard/description.rb +77 -0
  11. data/lib/state_machines/yard/handlers/actions.rb +113 -0
  12. data/lib/state_machines/yard/handlers/base.rb +145 -20
  13. data/lib/state_machines/yard/handlers/callback.rb +56 -0
  14. data/lib/state_machines/yard/handlers/event.rb +16 -12
  15. data/lib/state_machines/yard/handlers/machine.rb +258 -253
  16. data/lib/state_machines/yard/handlers/requirements.rb +92 -0
  17. data/lib/state_machines/yard/handlers/state.rb +16 -12
  18. data/lib/state_machines/yard/handlers/transition.rb +59 -30
  19. data/lib/state_machines/yard/handlers.rb +3 -1
  20. data/lib/state_machines/yard/renderer.rb +62 -0
  21. data/lib/state_machines/yard/templates/default/class/html/setup.rb +14 -7
  22. data/lib/state_machines/yard/templates/default/class/html/state_machines.erb +33 -0
  23. data/lib/state_machines/yard/templates.rb +2 -0
  24. data/lib/state_machines/yard/version.rb +3 -1
  25. data/lib/state_machines/yard.rb +7 -0
  26. data/release-please-config.json +11 -0
  27. data/state_machines-yard.gemspec +16 -8
  28. data/test/description_test.rb +138 -0
  29. data/test/handlers/base_test.rb +43 -18
  30. data/test/handlers/callback_test.rb +168 -0
  31. data/test/handlers/event_test.rb +32 -19
  32. data/test/handlers/guards_test.rb +180 -0
  33. data/test/handlers/machine_test.rb +100 -16
  34. data/test/handlers/review_test.rb +192 -0
  35. data/test/handlers/state_test.rb +20 -13
  36. data/test/handlers/transition_test.rb +154 -19
  37. data/test/integration_test.rb +47 -36
  38. data/test/templates_test.rb +39 -16
  39. data/test/test_helper.rb +24 -10
  40. metadata +103 -20
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class BaseHandlerTest < Minitest::Test
@@ -11,7 +13,7 @@ class BaseHandlerTest < Minitest::Test
11
13
  cleanup_yard
12
14
  end
13
15
 
14
- def test_extract_node_name_with_symbol
16
+ def test_symbol_state_name
15
17
  code = <<-RUBY
16
18
  class TestClass
17
19
  state_machine :status do
@@ -22,17 +24,18 @@ class BaseHandlerTest < Minitest::Test
22
24
  RUBY
23
25
 
24
26
  parse_code(code)
25
-
26
- # We can't directly test the extract_node_name method since it's private
27
- # But we can test that symbol nodes were correctly processed
27
+
28
+ # The name was read from the symbol literal
28
29
  test_class = YARD::Registry.at('TestClass')
29
- refute_nil test_class, "TestClass should be registered"
30
-
30
+
31
+ refute_nil test_class, 'TestClass should be registered'
32
+
31
33
  parked_method = YARD::Registry.at('TestClass#parked?')
32
- refute_nil parked_method, "parked? method should be registered"
34
+
35
+ refute_nil parked_method, 'parked? method should be registered'
33
36
  end
34
37
 
35
- def test_extract_node_name_with_string
38
+ def test_string_state_name
36
39
  code = <<-RUBY
37
40
  class TestClass
38
41
  state_machine :status do
@@ -43,16 +46,18 @@ class BaseHandlerTest < Minitest::Test
43
46
  RUBY
44
47
 
45
48
  parse_code(code)
46
-
49
+
47
50
  # Test that string nodes were correctly processed
48
51
  test_class = YARD::Registry.at('TestClass')
49
- refute_nil test_class, "TestClass should be registered"
50
-
52
+
53
+ refute_nil test_class, 'TestClass should be registered'
54
+
51
55
  moving_method = YARD::Registry.at('TestClass#moving?')
52
- refute_nil moving_method, "moving? method should be registered"
56
+
57
+ refute_nil moving_method, 'moving? method should be registered'
53
58
  end
54
59
 
55
- def test_extract_node_names_with_array
60
+ def test_several_state_names
56
61
  code = <<-RUBY
57
62
  class TestClass
58
63
  state_machine :status do
@@ -63,14 +68,34 @@ class BaseHandlerTest < Minitest::Test
63
68
  RUBY
64
69
 
65
70
  parse_code(code)
66
-
71
+
67
72
  # Test that array nodes were correctly processed
68
73
  test_class = YARD::Registry.at('TestClass')
69
- refute_nil test_class, "TestClass should be registered"
70
-
71
- %w(parked idling moving).each do |state|
74
+
75
+ refute_nil test_class, 'TestClass should be registered'
76
+
77
+ %w[parked idling moving].each do |state|
72
78
  method = YARD::Registry.at("TestClass##{state}?")
79
+
73
80
  refute_nil method, "#{state}? method should be registered"
74
81
  end
75
82
  end
76
- end
83
+
84
+ # YARD's parser ends a statement before a trailing rational or imaginary
85
+ # literal and past a trailing negative number; each call is read whole
86
+ def test_a_call_ending_in_any_number_is_read_whole
87
+ machine = parse_machines(<<~RUBY)['Vehicle'][:status]
88
+ class Vehicle # é
89
+ state_machine :status, initial: :parked do
90
+ state :parked, value: 1r
91
+ state :idling, value: 2i
92
+ state :stalled, value: -1
93
+ event(:ignite) { transition parked: :idling, if: 3r }
94
+ end
95
+ end
96
+ RUBY
97
+
98
+ assert_equal(%w[1r 2i -1], machine.states.map { it.value(false).inspect })
99
+ assert_equal(:'3r', machine.events[:ignite].branches.first.if_condition)
100
+ end
101
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'state_machines/yard/renderer'
5
+
6
+ # The documented machine carries each transition callback with its methods,
7
+ # the transitions it runs on and its own conditions, read from literals only
8
+ class CallbackHandlerTest < Minitest::Test
9
+ include TestHelpers
10
+
11
+ def setup
12
+ setup_yard
13
+ end
14
+
15
+ def teardown
16
+ cleanup_yard
17
+ end
18
+
19
+ def machine(body)
20
+ code = <<~RUBY
21
+ class Vehicle
22
+ state_machine :status, initial: :parked do
23
+ #{body}
24
+ event(:ignite) { transition parked: :idling }
25
+ event(:park) { transition idling: :parked }
26
+ end
27
+ end
28
+ RUBY
29
+ parse_machines(code)['Vehicle'][:status]
30
+ end
31
+
32
+ def callbacks(body)
33
+ machine(body).callbacks.values.flatten.map do |callback|
34
+ branch = callback.branch
35
+ requirement = branch.state_requirements.first
36
+ { type: callback.type, methods: callback.callables,
37
+ from: [requirement[:from].class, requirement[:from].values],
38
+ to: [requirement[:to].class, requirement[:to].values],
39
+ on: branch.event_requirement.values, if: branch.if_condition, unless: branch.unless_condition,
40
+ state_guards: branch.state_guards }
41
+ end
42
+ end
43
+
44
+ def test_each_kind_is_documented
45
+ found = callbacks(<<~RUBY)
46
+ before_transition do: :check_mirrors
47
+ after_transition do: :log
48
+ around_transition do: :time
49
+ after_failure do: :alert
50
+ RUBY
51
+
52
+ assert_equal(%i[before around after failure], found.map { it[:type] })
53
+ assert_equal([[:check_mirrors], [:time], [:log], [:alert]], found.map { it[:methods] })
54
+ end
55
+
56
+ def test_requirements_are_read_as_for_a_transition
57
+ found = callbacks(<<~RUBY).to_h { [it[:methods].first, it] }
58
+ before_transition from: :parked, to: [:idling, nil], on: :ignite, do: :a
59
+ after_transition parked: :idling, do: :b
60
+ after_transition except_to: :parked, except_on: :park, do: :c
61
+ before_transition all - [:parked] => same, do: :d
62
+ RUBY
63
+
64
+ assert_equal([StateMachines::WhitelistMatcher, [:parked]], found[:a][:from])
65
+ assert_equal([StateMachines::WhitelistMatcher, [:idling, nil]], found[:a][:to])
66
+ assert_equal([:ignite], found[:a][:on])
67
+ assert_equal([[StateMachines::WhitelistMatcher, [:parked]], [StateMachines::WhitelistMatcher, [:idling]]],
68
+ [found[:b][:from], found[:b][:to]])
69
+ assert_equal([[StateMachines::AllMatcher, []], [StateMachines::BlacklistMatcher, [:parked]], [:park]],
70
+ [found[:c][:from], found[:c][:to], found[:c][:on]])
71
+ assert_equal([[StateMachines::BlacklistMatcher, [:parked]], [StateMachines::LoopbackMatcher, []]],
72
+ [found[:d][:from], found[:d][:to]])
73
+ end
74
+
75
+ def test_conditions_and_state_guards_are_kept
76
+ callback = callbacks(<<~RUBY).first
77
+ before_transition on: :ignite, do: :log, if: [:logging?, 'verbose'], unless: -> { quiet? },
78
+ if_any_state: { alarm: :off, gate: nil }
79
+ RUBY
80
+
81
+ assert_equal([:logging?, 'verbose'], callback[:if])
82
+ assert_equal(:'-> { quiet? }', callback[:unless])
83
+ assert_equal({ if_any_state: { alarm: :off, gate: nil } }, callback[:state_guards])
84
+ end
85
+
86
+ def test_methods_are_read_as_the_core_reads_them
87
+ callback = callbacks(<<~RUBY).first
88
+ before_transition :first, 'second(self)', on: :ignite, do: [:third, -> { fourth }, lambda { fifth }, notifier] do |vehicle|
89
+ vehicle.sixth
90
+ end
91
+ RUBY
92
+
93
+ assert_equal([:first, 'second(self)', :third, 'lambda@(stdin):3', 'lambda@(stdin):3', 'notifier',
94
+ 'lambda@(stdin):3'], callback[:methods])
95
+ end
96
+
97
+ def test_a_method_in_parentheses_is_that_method
98
+ callback = callbacks('before_transition on: :park, do: [(:lock), ((-> { beep })), begin :wait end]').first
99
+
100
+ assert_equal([:lock, 'lambda@(stdin):3', :wait], callback[:methods])
101
+ end
102
+
103
+ def test_a_block_argument_is_a_method
104
+ assert_equal([:lock, '&:beep'], callbacks('before_transition on: :park, do: :lock, &:beep').first[:methods])
105
+ end
106
+
107
+ # The core reads do: with Array(), so do: nil adds no method, and &nil
108
+ # passes no block; a callback left with no method is refused
109
+ def test_nil_adds_no_method
110
+ found = callbacks(<<~RUBY)
111
+ before_transition on: :park, do: :check, &nil
112
+ before_transition :lock, on: :park, do: (nil), &(nil)
113
+ after_transition on: :park, &nil
114
+ after_transition on: :park, do: nil
115
+ after_transition on: :park, do: ()
116
+ RUBY
117
+
118
+ assert_equal([[:check], [:lock]], found.map { it[:methods] })
119
+ end
120
+
121
+ # & calls to_proc: a literal without one raises TypeError, so the core
122
+ # never registers the declaration; a Symbol's and a Hash's are Procs
123
+ def test_a_block_argument_without_to_proc_leaves_the_declaration_out
124
+ values = ['false', 'true', '1', '1.5', '1r', '2i', '"bad"', "\"a\#{b}\"", '`ls`', '__FILE__', '__LINE__',
125
+ '__ENCODING__', '[]', '%i[a]', '1..2', '/x/', '(false)', 'begin 1 end', '((true))']
126
+ body = values.map { "before_transition on: :park, do: :check, &#{it}" }
127
+ body.push('before_transition on: :park, do: :kept, &:beep', 'before_transition on: :park, do: :kept, &({ a: 1 })')
128
+
129
+ assert_equal([[:kept, '&:beep'], [:kept, 'lambda']], callbacks(body.join("\n")).map { it[:methods] })
130
+ end
131
+
132
+ def test_options_on_how_a_callback_runs_are_not_requirements
133
+ callback = callbacks('before_transition on: :park, do: :lock, bind_to_object: true, terminator: nil').first
134
+
135
+ assert_equal([StateMachines::AllMatcher, []], callback[:from])
136
+ assert_equal([:lock], callback[:methods])
137
+ end
138
+
139
+ def test_a_callback_the_core_refuses_or_written_as_code_is_left_out
140
+ found = callbacks(<<~RUBY)
141
+ after_failure from: :parked, do: :alert
142
+ after_transition to: STATES, do: :log
143
+ before_transition from: :parked
144
+ before_transition from: :parked, do: :kept
145
+ RUBY
146
+
147
+ assert_equal([[:kept]], found.map { it[:methods] })
148
+ end
149
+
150
+ def test_the_mermaid_diagram_shows_the_callbacks
151
+ machine = machine('before_transition on: :park, do: :lock')
152
+
153
+ mermaid = StateMachines::Yard::Renderer.render_mermaid(machine)[:mermaid]
154
+
155
+ assert_includes(mermaid, 'before lock')
156
+ end
157
+
158
+ def test_a_renderer_that_fails_leaves_the_diagram_out
159
+ machine = machine('')
160
+ machine.instance_variable_set(:@events, nil)
161
+ previous = ENV.fetch('STATE_MACHINES_RENDERER', nil)
162
+ ENV['STATE_MACHINES_RENDERER'] = 'mermaid'
163
+
164
+ assert_nil(StateMachines::Yard::Renderer.render(machine))
165
+ ensure
166
+ ENV['STATE_MACHINES_RENDERER'] = previous
167
+ end
168
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class EventHandlerTest < Minitest::Test
@@ -29,26 +31,32 @@ class EventHandlerTest < Minitest::Test
29
31
  RUBY
30
32
 
31
33
  parse_code(code)
32
-
34
+
33
35
  vehicle_class = YARD::Registry.at('Vehicle')
34
- refute_nil vehicle_class, "Vehicle class should be registered"
35
-
36
+
37
+ refute_nil vehicle_class, 'Vehicle class should be registered'
38
+
36
39
  # Check for methods that should be generated for events
37
40
  can_ignite_method = YARD::Registry.at('Vehicle#can_ignite?')
38
- refute_nil can_ignite_method, "can_ignite? method should be registered"
41
+
42
+ refute_nil can_ignite_method, 'can_ignite? method should be registered'
39
43
  assert_match(/Checks whether :ignite can be fired/, can_ignite_method.docstring.to_s)
40
-
44
+
41
45
  ignite_method = YARD::Registry.at('Vehicle#ignite')
42
- refute_nil ignite_method, "ignite method should be registered"
46
+
47
+ refute_nil ignite_method, 'ignite method should be registered'
43
48
  assert_match(/Fires the :ignite event/, ignite_method.docstring.to_s)
44
-
49
+
45
50
  ignite_bang_method = YARD::Registry.at('Vehicle#ignite!')
46
- refute_nil ignite_bang_method, "ignite! method should be registered"
51
+
52
+ refute_nil ignite_bang_method, 'ignite! method should be registered'
47
53
  assert_match(/Fires the :ignite event, raising an exception if it fails/, ignite_bang_method.docstring.to_s)
48
-
54
+
49
55
  ignite_transition_method = YARD::Registry.at('Vehicle#ignite_transition')
50
- refute_nil ignite_transition_method, "ignite_transition method should be registered"
51
- assert_match(/Gets the next transition that would be performed if :ignite were to be fired/, ignite_transition_method.docstring.to_s)
56
+
57
+ refute_nil ignite_transition_method, 'ignite_transition method should be registered'
58
+ assert_match(/Gets the next transition that would be performed if :ignite were to be fired/,
59
+ ignite_transition_method.docstring.to_s)
52
60
  end
53
61
 
54
62
  def test_event_handler_with_multiple_events
@@ -62,23 +70,28 @@ class EventHandlerTest < Minitest::Test
62
70
  RUBY
63
71
 
64
72
  parse_code(code)
65
-
73
+
66
74
  vehicle_class = YARD::Registry.at('Vehicle')
67
- refute_nil vehicle_class, "Vehicle class should be registered"
68
-
75
+
76
+ refute_nil vehicle_class, 'Vehicle class should be registered'
77
+
69
78
  # Check for methods that should be generated for each event
70
- %w(ignite drive park).each do |event|
79
+ %w[ignite drive park].each do |event|
71
80
  can_method = YARD::Registry.at("Vehicle#can_#{event}?")
81
+
72
82
  refute_nil can_method, "can_#{event}? method should be registered"
73
-
83
+
74
84
  event_method = YARD::Registry.at("Vehicle##{event}")
85
+
75
86
  refute_nil event_method, "#{event} method should be registered"
76
-
87
+
77
88
  bang_method = YARD::Registry.at("Vehicle##{event}!")
89
+
78
90
  refute_nil bang_method, "#{event}! method should be registered"
79
-
91
+
80
92
  transition_method = YARD::Registry.at("Vehicle##{event}_transition")
93
+
81
94
  refute_nil transition_method, "#{event}_transition method should be registered"
82
95
  end
83
96
  end
84
- end
97
+ end
@@ -0,0 +1,180 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'state_machines/yard/renderer'
5
+
6
+ # The documented machine carries the guards and requirements the core runs,
7
+ # read from literals only
8
+ class GuardsTest < Minitest::Test
9
+ include TestHelpers
10
+
11
+ def setup
12
+ setup_yard
13
+ end
14
+
15
+ def teardown
16
+ cleanup_yard
17
+ end
18
+
19
+ def machine(body, options = 'initial: :parked')
20
+ code = <<~RUBY
21
+ module Fleet
22
+ class Vehicle
23
+ state_machine :status, #{options} do
24
+ #{body}
25
+ end
26
+ end
27
+ end
28
+ RUBY
29
+ parse_machines(code)['Fleet::Vehicle'][:status]
30
+ end
31
+
32
+ def branches(body)
33
+ machine("event :go do\n#{body}\nend").events[:go].branches
34
+ end
35
+
36
+ def test_every_state_guard_kind_keeps_its_group
37
+ kinds = %i[if_state unless_state if_all_states unless_all_states if_any_state unless_any_state]
38
+ body = kinds.map { "transition parked: :moving, #{it}: { gate: :open, alarm: nil }" }.join("\n")
39
+
40
+ guards = branches(body).map(&:state_guards)
41
+
42
+ assert_equal(kinds.map { { it => { gate: :open, alarm: nil } } }, guards)
43
+ end
44
+
45
+ def test_state_guards_with_string_keys_and_names
46
+ branch = branches("transition parked: :moving, if_state: { 'gate' => 'open' }").first
47
+
48
+ assert_equal({ if_state: { 'gate' => 'open' } }, branch.state_guards)
49
+ end
50
+
51
+ def test_array_conditions_keep_each_condition
52
+ branch = branches("transition parked: :moving, if: [:a?, 'b && c'], unless: %i[d? e?]").first
53
+
54
+ assert_equal([:a?, 'b && c'], branch.if_condition)
55
+ assert_equal(%i[d? e?], branch.unless_condition)
56
+ end
57
+
58
+ def test_a_string_condition_is_the_code_it_holds
59
+ branch = branches(%q(transition parked: :moving, if: "speed > 0 && gear != 'R'")).first
60
+
61
+ assert_equal("speed > 0 && gear != 'R'", branch.if_condition)
62
+ end
63
+
64
+ def test_a_condition_written_as_code_is_documented_by_its_source
65
+ first, second = branches(<<~RUBY)
66
+ transition parked: :moving, if: -> { warm? }
67
+ transition parked: :idling, unless: [:cold?, engine.stalled?]
68
+ RUBY
69
+
70
+ assert_equal(:'-> { warm? }', first.if_condition)
71
+ assert_equal(%i[cold? engine.stalled?], second.unless_condition)
72
+ end
73
+
74
+ def test_a_dynamic_condition_is_never_read_as_a_state_matcher
75
+ branch = branches('transition parked: :moving, if: all').first
76
+
77
+ assert_equal(:all, branch.if_condition)
78
+ end
79
+
80
+ def test_false_and_nil_are_no_condition
81
+ first, second = branches(<<~RUBY)
82
+ transition parked: :moving, if: false
83
+ transition idling: :moving, unless: nil
84
+ RUBY
85
+
86
+ refute(first.if_condition)
87
+ assert_nil(second.unless_condition)
88
+ end
89
+
90
+ def test_a_transition_with_a_state_guard_written_as_code_is_left_out
91
+ branches = branches(<<~RUBY)
92
+ transition parked: :moving, if_state: GATE_OPEN
93
+ transition parked: :idling
94
+ RUBY
95
+
96
+ requirements = branches.map { it.state_requirements.first }
97
+
98
+ assert_equal([[[:parked], [:idling]]], requirements.map { |req| [req[:from].values, req[:to].values] })
99
+ end
100
+
101
+ def test_a_transition_the_core_refuses_is_left_out
102
+ assert_empty(branches('transition parked: :moving, if_state: {}'))
103
+ end
104
+
105
+ def test_requirements_written_as_code_leave_the_transition_out
106
+ assert_empty(branches('transition STATES => :parked'))
107
+ end
108
+
109
+ def test_matchers_and_names
110
+ first, second, third = branches(<<~RUBY)
111
+ transition all - [:moving, nil] => :parked
112
+ transition any => same
113
+ transition :"hard stop" => nil
114
+ RUBY
115
+
116
+ assert_instance_of(StateMachines::BlacklistMatcher, first.state_requirements.first[:from])
117
+ assert_equal([:moving, nil], first.state_requirements.first[:from].values)
118
+ assert_instance_of(StateMachines::AllMatcher, second.state_requirements.first[:from])
119
+ assert_instance_of(StateMachines::LoopbackMatcher, second.state_requirements.first[:to])
120
+ assert_equal([:'hard stop', nil], third.known_states)
121
+ end
122
+
123
+ def test_machine_options_are_read
124
+ machine = machine('', "initial: :parked, attribute: :status_code, namespace: 'car'")
125
+
126
+ assert_equal([:parked], machine.states.select(&:initial?).map(&:name))
127
+ assert_equal(:status_code, machine.attribute)
128
+ assert_equal('car', machine.namespace)
129
+ end
130
+
131
+ def test_an_initial_state_written_as_code_is_dynamic
132
+ machine = machine('state :parked', 'initial: -> { :parked }')
133
+
134
+ assert_predicate(machine, :dynamic_initial_state?)
135
+ assert_empty(machine.states.select(&:initial?))
136
+ end
137
+
138
+ def test_state_and_event_options_are_not_names
139
+ machine = machine(<<~RUBY)
140
+ state :parked, value: 1, human_name: 'Parked'
141
+ state :idling, value: Time.now
142
+ event :"odd name", human_name: 'Odd' do
143
+ transition parked: :idling
144
+ end
145
+ RUBY
146
+
147
+ # A value written as code is shown as written, never as the default
148
+ assert_equal([[:parked, '1', 'Parked'], [:idling, 'Time.now', 'idling']],
149
+ machine.states.map { [it.name, it.value(false).inspect, it.human_name] })
150
+ assert_equal([[:'odd name', 'Odd']], machine.events.map { [it.name, it.human_name] })
151
+ end
152
+
153
+ def test_the_mermaid_diagram_shows_the_guards
154
+ machine = machine(<<~RUBY)
155
+ event :drive do
156
+ transition parked: :moving, if: :engine_started?, if_any_state: { gate: :open, alarm: :off }
157
+ end
158
+ RUBY
159
+
160
+ mermaid = StateMachines::Yard::Renderer.render_mermaid(machine)[:mermaid]
161
+
162
+ assert_includes(mermaid, 'drive [if engine_started? #38;#38; if_any_state (gate=open, alarm=off)]')
163
+ end
164
+
165
+ def test_the_documented_machine_is_owned_by_the_documented_class
166
+ assert_equal('Fleet::Vehicle', machine('').owner_class.name)
167
+ end
168
+
169
+ def test_the_graphviz_diagram_is_a_png_drawn_by_graphviz
170
+ previous = StateMachines::Machine.renderer
171
+ StateMachines::Machine.renderer = StateMachines::STDIORenderer
172
+ machine = machine('event(:drive) { transition parked: :moving }')
173
+
174
+ image = StateMachines::Yard::Renderer.render_graphviz(machine)[:image]
175
+
176
+ assert_equal("\x89PNG".b, image.byteslice(0, 4))
177
+ ensure
178
+ StateMachines::Machine.renderer = previous
179
+ end
180
+ end
@@ -1,8 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class MachineHandlerTest < Minitest::Test
4
6
  include TestHelpers
5
7
 
8
+ RENDERER_VARIABLE = 'STATE_MACHINES_RENDERER'
9
+ DIAGRAM_RENDERER = 'mermaid'
10
+ NIL_STATE_ID = 'nil_state'
11
+
12
+ NAMESPACED_MACHINES = <<~RUBY
13
+ module Air
14
+ class Craft
15
+ state_machine initial: :grounded do
16
+ event :launch do
17
+ transition grounded: :airborne
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ module Sea
24
+ class Craft
25
+ state_machine initial: :docked do
26
+ event :launch do
27
+ transition docked: :sailing
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ module Air
34
+ class Glider < Craft
35
+ state_machine do
36
+ state :towed
37
+ end
38
+ end
39
+ end
40
+ RUBY
41
+
6
42
  def setup
7
43
  setup_yard
8
44
  end
@@ -11,6 +47,49 @@ class MachineHandlerTest < Minitest::Test
11
47
  cleanup_yard
12
48
  end
13
49
 
50
+ def test_classes_sharing_a_short_name_do_not_share_an_inherited_machine
51
+ previous_renderer = ENV.fetch(RENDERER_VARIABLE, nil)
52
+ ENV[RENDERER_VARIABLE] = DIAGRAM_RENDERER
53
+ begin
54
+ parse_code(NAMESPACED_MACHINES)
55
+ ensure
56
+ ENV[RENDERER_VARIABLE] = previous_renderer
57
+ end
58
+
59
+ diagram = YARD::Registry.at('Air::Glider')['state_machines'][:state][DIAGRAM_RENDERER.to_sym]
60
+ documented_states = (diagram.scan(/^\s+(\S+) : /).flatten - [NIL_STATE_ID]).sort
61
+
62
+ eval(NAMESPACED_MACHINES, TOPLEVEL_BINDING) # rubocop:disable Security/Eval -- the fixture, run to compare
63
+ actual_states = Air::Glider.state_machines[:state].states.filter_map(&:name).map(&:to_s).sort
64
+
65
+ assert_equal actual_states, documented_states
66
+ end
67
+
68
+ def test_a_subclass_machine_documents_the_methods_that_read_its_state
69
+ parse_code(<<~RUBY)
70
+ class Vehicle
71
+ state_machine :status, initial: :parked do
72
+ event(:ignite) { transition parked: :idling }
73
+ end
74
+ end
75
+
76
+ class Car < Vehicle
77
+ state_machine :status do
78
+ state :towed
79
+ end
80
+ end
81
+ RUBY
82
+
83
+ %w[Car#status? Car#status_name Car#human_status_name Car#towed?].each do |path|
84
+ refute_nil YARD::Registry.at(path), "#{path} should be documented"
85
+ end
86
+ # Listed and fired through the superclass's machine
87
+ assert_nil YARD::Registry.at('Car#status_events')
88
+ assert_nil YARD::Registry.at('Car#ignite')
89
+ # The subclass's states stay its own
90
+ assert_nil YARD::Registry.at('Vehicle#towed?')
91
+ end
92
+
14
93
  def test_machine_handler_registers_state_machine
15
94
  code = <<-RUBY
16
95
  class Vehicle
@@ -22,16 +101,18 @@ class MachineHandlerTest < Minitest::Test
22
101
  RUBY
23
102
 
24
103
  parse_code(code)
25
-
104
+
26
105
  vehicle_class = YARD::Registry.at('Vehicle')
27
- refute_nil vehicle_class, "Vehicle class should be registered"
28
- assert has_attribute?(vehicle_class, 'state_machines'), "Vehicle class should have state_machines attribute"
29
- assert_equal 1, vehicle_class['state_machines'].size, "Vehicle should have one state machine"
30
- assert vehicle_class['state_machines'].key?(:status), "Vehicle should have a status state machine"
31
-
106
+
107
+ refute_nil vehicle_class, 'Vehicle class should be registered'
108
+ assert attribute?(vehicle_class, 'state_machines'), 'Vehicle class should have state_machines attribute'
109
+ assert_equal 1, vehicle_class['state_machines'].size, 'Vehicle should have one state machine'
110
+ assert vehicle_class['state_machines'].key?(:status), 'Vehicle should have a status state machine'
111
+
32
112
  machine = vehicle_class['state_machines'][:status]
33
- assert_equal :status, machine[:name], "Machine name should be :status"
34
- assert_equal "This is a state machine for vehicle status", machine[:description], "Machine description should match"
113
+
114
+ assert_equal :status, machine[:name], 'Machine name should be :status'
115
+ assert_equal 'This is a state machine for vehicle status', machine[:description], 'Machine description should match'
35
116
  end
36
117
 
37
118
  def test_machine_handler_with_default_name
@@ -45,10 +126,11 @@ class MachineHandlerTest < Minitest::Test
45
126
  RUBY
46
127
 
47
128
  parse_code(code)
48
-
129
+
49
130
  vehicle_class = YARD::Registry.at('Vehicle')
50
- refute_nil vehicle_class, "Vehicle class should be registered"
51
- assert vehicle_class['state_machines'].key?(:state), "Vehicle should have a default state machine"
131
+
132
+ refute_nil vehicle_class, 'Vehicle class should be registered'
133
+ assert vehicle_class['state_machines'].key?(:state), 'Vehicle should have a default state machine'
52
134
  end
53
135
 
54
136
  def test_machine_handler_with_options
@@ -62,11 +144,13 @@ class MachineHandlerTest < Minitest::Test
62
144
  RUBY
63
145
 
64
146
  parse_code(code)
65
-
147
+
66
148
  vehicle_class = YARD::Registry.at('Vehicle')
67
- refute_nil vehicle_class, "Vehicle class should be registered"
68
-
149
+
150
+ refute_nil vehicle_class, 'Vehicle class should be registered'
151
+
69
152
  machine = vehicle_class['state_machines'][:status]
70
- refute_nil machine, "Status state machine should be registered"
153
+
154
+ refute_nil machine, 'Status state machine should be registered'
71
155
  end
72
- end
156
+ end