-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathworkflow_from_json_test.rb
158 lines (125 loc) · 4.49 KB
/
workflow_from_json_test.rb
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
require File.join(File.dirname(__FILE__), 'test_helper')
require 'workflow'
class WorkflowFromJsonTest < Minitest::Test
test '#241 define a workflow from JSON' do
c = Class.new
c.class_eval do
include Workflow
attr_reader :events
def record(event)
@events ||= []
@events << event
end
def ran?(event)
@events.include? event
end
def clear_events
@events = []
end
def event_1(test)
# puts "running event_1 with arg '#{test}'"
record __method__
end
def before_transition(from, to, triggering_event, *args, **kwargs)
# puts "before_transition #{from} -> #{to}, #{triggering_event}, #{args}, #{kwargs}"
record __method__
end
def on_transition(from, to, triggering_event, *args, **kwargs)
# puts "on_transition #{from} -> #{to}, #{triggering_event}, #{args}, #{kwargs}"
record __method__
end
def after_transition(from, to, triggering_event, *args, **kwargs)
# puts "after_transition #{from} -> #{to}, #{triggering_event}, #{args}, #{kwargs}"
record __method__
end
def event_1_condition?
# puts "event_1_condition? -> true"
record __method__
true
end
def on_state_1_exit(new_state, event, *args)
# puts "on_state_1_exit(#{new_state}, #{event}, #{args})"
record __method__
end
def entering_state_2(prior_state, triggering_event, *args, **kwargs)
# puts "entering_state_2 #{prior_state}, #{triggering_event}, #{args}, #{kwargs}"
record __method__
end
def entering_state_3(prior_state, triggering_event, *args, **kwargs)
# puts "entering_state_3 #{prior_state}, #{triggering_event}, #{args}, #{kwargs}"
record __method__
end
def on_state_2_entry(new_state, event, *args)
# puts "on_state_2_entry(#{new_state}, #{event}, #{args})"
record __method__
end
end
ran_normal = []
hash = {
state_1: {
events: {
event_1: {
transition_to: :state_2,
if: :event_1_condition?,
meta: { e1: 1 }
},
},
meta: { a: 1 }
},
state_2: {
events: {
event_2: {
transition_to: :state_3,
}
},
on_entry: :entering_state_2,
meta: { a: 2 }
},
state_3: {
on_entry: "entering_state_3",
}
}
spec = Workflow::Specification.new do
hash.each_pair do |state_name, state_def|
state state_name, state_def
on_entry {|prior_state, triggering_event| send state_def[:on_entry], prior_state, triggering_event } if state_def.include?(:on_entry)
on_exit { ran_normal << :on_exit }
state_def[:events]&.each_pair do |event_name, event_def|
event event_name, event_def
end
end
before_transition do |from, to, triggering_event, *args, **kwargs|
ran_normal << :before_transition
before_transition(from, to, triggering_event, *args, **kwargs)
end
on_transition do |from, to, triggering_event, *args, **kwargs|
ran_normal << :on_transition
on_transition(from, to, triggering_event, *args, **kwargs)
end
after_transition do |from, to, triggering_event, *args, **kwargs|
ran_normal << :after_transition
after_transition(from, to, triggering_event, *args, **kwargs)
end
end
c.send :assign_workflow, spec
o = c.new
assert o.state_1?, "Should be state_1"
refute o.state_2?, "Should not be state_2"
o.event_1! "hello"
[:event_1_condition?, :event_1, :entering_state_2, :before_transition, :on_transition, :after_transition].each do |event|
assert o.ran?(event), "Should have run event #{event}"
end
refute o.state_1?, "Should not be state_1"
assert o.state_2?, "Should be state_2"
o.clear_events
o.event_2!
refute o.ran?(:event_1), "Should not have run event_1"
[:entering_state_3, :before_transition, :on_transition, :after_transition].each do |event|
assert o.ran?(event), "Should have run event #{event}"
end
assert ran_normal.include?(:before_transition), "Should have run before_transition proc"
assert ran_normal.include?(:on_transition), "Should have run on_transition proc"
assert ran_normal.include?(:after_transition), "Should have run after_transition proc"
assert ran_normal.include?(:on_exit), "Should have run on_exit proc"
end
end