state_flow 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/base_spec.rb ADDED
@@ -0,0 +1,457 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ describe StateFlow::Base do
5
+ describe "state" do
6
+
7
+ before(:each) do
8
+ @target = Class.new do
9
+ include ::SelectableAttr::Base
10
+ include ::StateFlow
11
+
12
+ selectable_attr(:status) do
13
+ entry '01', :foo, "FOO"
14
+ entry '02', :bar, "BAR"
15
+ entry '03', :baz, "BAZ"
16
+ end
17
+ end
18
+ end
19
+
20
+ describe "valid arguments" do
21
+ describe "state(:<state_name>)" do
22
+ before(:each) do
23
+ @flow = @target.state_flow(:status) do
24
+ state :foo
25
+ state :bar, :lock => true
26
+ end
27
+ end
28
+
29
+ it "inspect" do
30
+ klass_name =
31
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
32
+ " @klass=#{@target.name.inspect}" <<
33
+ ' @entries=[' <<
34
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::Action>>, ' <<
35
+ '<StateFlow::Entry @key=:bar @action=<StateFlow::Action @lock=true>>' <<
36
+ ']>'
37
+ end
38
+
39
+ it "check" do
40
+ @flow[:foo].key.should == :foo
41
+ @flow[:foo].events.should == []
42
+ @flow[:foo].success_key.should == nil
43
+ @flow[:foo].options.should == {}
44
+ @flow[:bar].key.should == :bar
45
+ @flow[:bar].events.should == []
46
+ @flow[:bar].success_key.should == nil
47
+ @flow[:bar].action.lock.should == true
48
+ @flow[:bar].action.lock.should == true
49
+ end
50
+ end
51
+
52
+ describe "state(:<state_name> => nil)" do
53
+ before(:each) do
54
+ @flow = @target.state_flow(:status) do
55
+ state :foo => nil
56
+ state :bar => nil, :lock => true
57
+ end
58
+ end
59
+
60
+ it "inspect" do
61
+ klass_name =
62
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
63
+ " @klass=#{@target.name.inspect}" <<
64
+ ' @entries=[' <<
65
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::Action>>, ' <<
66
+ '<StateFlow::Entry @key=:bar @action=<StateFlow::Action @lock=true>>' <<
67
+ ']>'
68
+ end
69
+
70
+ it "check" do
71
+ @flow[:foo].key.should == :foo
72
+ @flow[:foo].events.should == []
73
+ @flow[:foo].success_key.should == nil
74
+ @flow[:foo].options.should == {}
75
+ @flow[:bar].key.should == :bar
76
+ @flow[:bar].events.should == []
77
+ @flow[:bar].success_key.should == nil
78
+ @flow[:bar].action.lock.should == true
79
+ end
80
+ end
81
+
82
+ describe "state(:<state_name> => :<new_state_name>) with :if/:unless" do
83
+ before(:each) do
84
+ proc_obj = Proc.new{|record| true} # インスタンス変数に格納するとなぜかうまく動かないんです・・・?
85
+ @proc_inspect = proc_obj.inspect
86
+ @flow = @target.state_flow(:status) do
87
+ state :foo => :bar
88
+ state :bar => :baz, :if => proc_obj
89
+ state({:baz => :foo}, {:unless => :some_method?})
90
+ end
91
+ end
92
+
93
+ it "inspect" do
94
+ klass_name =
95
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
96
+ " @klass=#{@target.name.inspect}" <<
97
+ ' @entries=[' <<
98
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::Action @success_key=:bar>>, ' <<
99
+ "<StateFlow::Entry @key=:bar @action=<StateFlow::Action @success_key=:baz @if=#{@proc_inspect}>>, " <<
100
+ '<StateFlow::Entry @key=:baz @action=<StateFlow::Action @success_key=:foo @unless=:some_method?>>' <<
101
+ ']>'
102
+ end
103
+
104
+ it "check" do
105
+ @flow[:foo].key.should == :foo
106
+ @flow[:foo].events.should == []
107
+ @flow[:foo].success_key.should == :bar
108
+ @flow[:foo].options.should == {}
109
+ @flow[:bar].key.should == :bar
110
+ @flow[:bar].events.should == []
111
+ @flow[:bar].success_key.should == :baz
112
+ @flow[:bar].action.if.class.should == Proc
113
+ @flow[:baz].key.should == :baz
114
+ @flow[:baz].events.should == []
115
+ @flow[:baz].success_key.should == :foo
116
+ @flow[:baz].action.unless.should == :some_method?
117
+ end
118
+ end
119
+
120
+
121
+ describe "state(:<state_name> => :<new_state_name>)" do
122
+ before(:each) do
123
+ @flow = @target.state_flow(:status) do
124
+ state :foo => :bar
125
+ state :bar => :baz, :lock => true
126
+ state({:baz => :foo}, {:lock => true})
127
+ end
128
+ end
129
+
130
+ it "inspect" do
131
+ klass_name =
132
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
133
+ " @klass=#{@target.name.inspect}" <<
134
+ ' @entries=[' <<
135
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::Action @success_key=:bar>>, ' <<
136
+ '<StateFlow::Entry @key=:bar @action=<StateFlow::Action @success_key=:baz @lock=true>>, ' <<
137
+ '<StateFlow::Entry @key=:baz @action=<StateFlow::Action @success_key=:foo @lock=true>>' <<
138
+ ']>'
139
+ end
140
+
141
+ it "check" do
142
+ @flow[:foo].key.should == :foo
143
+ @flow[:foo].events.should == []
144
+ @flow[:foo].success_key.should == :bar
145
+ @flow[:foo].options.should == {}
146
+ @flow[:bar].key.should == :bar
147
+ @flow[:bar].events.should == []
148
+ @flow[:bar].success_key.should == :baz
149
+ @flow[:bar].action.lock.should == true
150
+ @flow[:baz].key.should == :baz
151
+ @flow[:baz].events.should == []
152
+ @flow[:baz].success_key.should == :foo
153
+ @flow[:baz].action.lock.should == true
154
+ end
155
+ end
156
+
157
+ describe "state(:<state_name> => :<new_state_name>, :failure => <:failure_state>)" do
158
+ before(:each) do
159
+ @flow = @target.state_flow(:status) do
160
+ state :foo => :bar, :failure => :baz
161
+ state :bar => :baz, :lock => true
162
+ state({:baz => :foo}, {:lock => true})
163
+ end
164
+ end
165
+
166
+ it "inspect" do
167
+ klass_name =
168
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
169
+ " @klass=#{@target.name.inspect}" <<
170
+ ' @entries=[' <<
171
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::Action @success_key=:bar @failure_key=:baz>>, ' <<
172
+ '<StateFlow::Entry @key=:bar @action=<StateFlow::Action @success_key=:baz @lock=true>>, ' <<
173
+ '<StateFlow::Entry @key=:baz @action=<StateFlow::Action @success_key=:foo @lock=true>>' <<
174
+ ']>'
175
+ end
176
+
177
+ it "check" do
178
+ @flow[:foo].key.should == :foo
179
+ @flow[:foo].events.should == []
180
+ @flow[:foo].success_key.should == :bar
181
+ @flow[:foo].failure_key.should == :baz
182
+ @flow[:foo].options.should == {}
183
+ @flow[:bar].key.should == :bar
184
+ @flow[:bar].events.should == []
185
+ @flow[:bar].success_key.should == :baz
186
+ @flow[:bar].action.lock.should == true
187
+ @flow[:baz].key.should == :baz
188
+ @flow[:baz].events.should == []
189
+ @flow[:baz].success_key.should == :foo
190
+ @flow[:baz].action.lock.should == true
191
+ end
192
+ end
193
+
194
+ describe "state(:<state_name> => action(:<method_name>))" do
195
+ before(:each) do
196
+ @flow = @target.state_flow(:status) do
197
+ state :foo => action(:hoge)
198
+ state :bar => action(:hoge), :lock => true
199
+ state({:baz => action(:hoge)}, {:lock => true})
200
+ end
201
+ end
202
+
203
+ it "inspect" do
204
+ klass_name =
205
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
206
+ " @klass=#{@target.name.inspect}" <<
207
+ ' @entries=[' <<
208
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::NamedAction @name=:hoge>>, ' <<
209
+ '<StateFlow::Entry @key=:bar @action=<StateFlow::NamedAction @name=:hoge @lock=true>>, ' <<
210
+ '<StateFlow::Entry @key=:baz @action=<StateFlow::NamedAction @name=:hoge @lock=true>>' <<
211
+ ']>'
212
+ end
213
+
214
+ it "check" do
215
+ @flow[:foo].key.should == :foo
216
+ @flow[:foo].events.should == []
217
+ @flow[:foo].action.name.should == :hoge
218
+ @flow[:foo].success_key.should == nil
219
+ @flow[:foo].options.should == {}
220
+ @flow[:bar].key.should == :bar
221
+ @flow[:bar].events.should == []
222
+ @flow[:bar].action.name.should == :hoge
223
+ @flow[:bar].success_key.should == nil
224
+ @flow[:bar].action.lock.should == true
225
+ @flow[:baz].key.should == :baz
226
+ @flow[:baz].events.should == []
227
+ @flow[:baz].action.name.should == :hoge
228
+ @flow[:baz].success_key.should == nil
229
+ @flow[:baz].action.lock.should == true
230
+ end
231
+ end
232
+
233
+ describe "state(:<state_name> => {action(:<method_name>) => :<new_state_name>})" do
234
+ before(:each) do
235
+ @flow = @target.state_flow(:status) do
236
+ begin
237
+ state :foo => {action(:hoge) => :bar}
238
+ state :bar => {action(:hoge) => :baz}, :lock => true
239
+ state :baz => {action(:hoge) => :foo, :lock => true}
240
+ rescue Exception
241
+ puts $!.backtrace.join("\n ")
242
+ raise
243
+ end
244
+ end
245
+ end
246
+
247
+ it "inspect" do
248
+ klass_name =
249
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
250
+ " @klass=#{@target.name.inspect}" <<
251
+ ' @entries=[' <<
252
+ '<StateFlow::Entry @key=:foo @action=<StateFlow::NamedAction @name=:hoge @success_key=:bar>>, ' <<
253
+ '<StateFlow::Entry @key=:bar @action=<StateFlow::NamedAction @name=:hoge @success_key=:baz @lock=true>>, ' <<
254
+ '<StateFlow::Entry @key=:baz @action=<StateFlow::NamedAction @name=:hoge @success_key=:foo @lock=true>>' <<
255
+ ']>'
256
+ end
257
+
258
+ it "check" do
259
+ @flow[:foo].key.should == :foo
260
+ @flow[:foo].events.should == []
261
+ @flow[:foo].action.name.should == :hoge
262
+ @flow[:foo].action.success_key.should == :bar
263
+ @flow[:foo].success_key.should == :bar
264
+ @flow[:foo].options.should == {}
265
+ @flow[:bar].key.should == :bar
266
+ @flow[:bar].events.should == []
267
+ @flow[:bar].action.name.should == :hoge
268
+ @flow[:bar].action.success_key.should == :baz
269
+ @flow[:bar].success_key.should == :baz
270
+ @flow[:bar].action.lock.should == true
271
+ @flow[:baz].key.should == :baz
272
+ @flow[:baz].events.should == []
273
+ @flow[:baz].action.name.should == :hoge
274
+ @flow[:baz].action.success_key.should == :foo
275
+ @flow[:baz].success_key.should == :foo
276
+ @flow[:baz].action.lock.should == true
277
+ end
278
+ end
279
+
280
+ describe "with_events" do
281
+ def check_flow_with_event
282
+ @flow[:foo].key.should == :foo
283
+ @flow[:foo].events.should_not == []
284
+ @flow[:foo].event_for(:event1).should_not be_nil
285
+ @flow[:foo].event_for(:event1).success_key.should == :bar
286
+ @flow[:foo].event_for(:event2).success_key.should == :baz
287
+ @flow[:foo].action.should be_nil
288
+ @flow[:foo].success_key.should == nil
289
+ @flow[:foo].options.should == {}
290
+ @flow[:bar].key.should == :bar
291
+ @flow[:bar].event_for(:event1).should_not be_nil
292
+ @flow[:bar].event_for(:event1).success_key.should == :baz
293
+ @flow[:bar].event_for(:event2).success_key.should == :foo
294
+ @flow[:bar].event_for(:event1).action.lock.should == true
295
+ @flow[:bar].event_for(:event2).action.lock.should == true
296
+ @flow[:bar].action.should be_nil
297
+ @flow[:bar].success_key.should == nil
298
+ @flow[:baz].key.should == :baz
299
+ @flow[:baz].event_for(:event1).should_not be_nil
300
+ @flow[:baz].event_for(:event1).success_key.should == :foo
301
+ @flow[:baz].event_for(:event2).success_key.should == :bar
302
+ @flow[:baz].event_for(:event1).action.lock.should == true
303
+ @flow[:baz].event_for(:event2).action.lock.should == true
304
+ @flow[:baz].action.should be_nil
305
+ @flow[:baz].success_key.should == nil
306
+ end
307
+
308
+ def check_inspect_with_event
309
+ klass_name = @target.name
310
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
311
+ " @klass=#{@target.name.inspect}" <<
312
+ ' @entries=[' <<
313
+ '<StateFlow::Entry @key=:foo @events=[' <<
314
+ '<StateFlow::Event @name=:event1 @action=<StateFlow::Action @success_key=:bar>>, ' <<
315
+ '<StateFlow::Event @name=:event2 @action=<StateFlow::Action @success_key=:baz>>' <<
316
+ ']>, ' <<
317
+ '<StateFlow::Entry @key=:bar @events=[' <<
318
+ '<StateFlow::Event @name=:event1 @action=<StateFlow::Action @success_key=:baz @lock=true>>, ' <<
319
+ '<StateFlow::Event @name=:event2 @action=<StateFlow::Action @success_key=:foo @lock=true>>' <<
320
+ ']>, ' <<
321
+ '<StateFlow::Entry @key=:baz @events=[' <<
322
+ '<StateFlow::Event @name=:event1 @action=<StateFlow::Action @success_key=:foo @lock=true>>, ' <<
323
+ '<StateFlow::Event @name=:event2 @action=<StateFlow::Action @success_key=:bar @lock=true>>' <<
324
+ ']>' <<
325
+ ']>'
326
+ end
327
+
328
+ describe "state(:<state_name> => {event(:<event_name1>) => :<new_state_name>, event(:<event_name2>) => :<new_state_name>})" do
329
+ before(:each) do
330
+ @flow = @target.state_flow(:status) do
331
+ state :foo => {event(:event1) => :bar, event(:event2) => :baz}
332
+ state :bar => {event(:event1) => :baz, event(:event2) => :foo}, :lock => true
333
+ state :baz => {event(:event1) => :foo, event(:event2) => :bar, :lock => true}
334
+ end
335
+ end
336
+ it("check") { check_flow_with_event }
337
+ it("inspect") {check_inspect_with_event }
338
+ end
339
+
340
+ describe "state( :<state_name> => [ {event(:<event_name1>) => :<new_state_name>, event(:<event_name2>) => :<new_state_name>} ] )" do
341
+ before(:each) do
342
+ @flow = @target.state_flow(:status) do
343
+ state :foo => [{event(:event1) => :bar, event(:event2) => :baz}]
344
+ state :bar => [{event(:event1) => :baz, event(:event2) => :foo}], :lock => true
345
+ state :baz => [{event(:event1) => :foo, event(:event2) => :bar, :lock => true}]
346
+ end
347
+ end
348
+ it("check") { check_flow_with_event }
349
+ it("inspect") {check_inspect_with_event }
350
+ end
351
+
352
+ describe "state( :<state_name> => [ {event(:<event_name1>) => :<new_state_name>}, {event(:<event_name2>) => :<new_state_name>} ] )" do
353
+ before(:each) do
354
+ @flow = @target.state_flow(:status) do
355
+ state :foo => [{event(:event1) => :bar}, {event(:event2) => :baz}]
356
+ state :bar => [{event(:event1) => :baz}, {event(:event2) => :foo}], :lock => true
357
+ state :baz => [{event(:event1) => :foo, :lock => true}, {event(:event2) => :bar, :lock => true}]
358
+ end
359
+ end
360
+ it("check") { check_flow_with_event }
361
+ it("inspect") {check_inspect_with_event }
362
+ end
363
+ end
364
+
365
+ describe "with event and actions" do
366
+ def check_flow_with_event_and_actions
367
+ @flow[:foo].key.should == :foo
368
+ @flow[:foo].events.should_not == []
369
+ @flow[:foo].event_for(:event1).should_not be_nil
370
+ @flow[:foo].event_for(:event1).success_key.should == :bar
371
+ @flow[:foo].event_for(:event2).success_key.should == :baz
372
+ @flow[:foo].event_for(:event1).action.name.should == :hoge
373
+ @flow[:foo].event_for(:event2).action.name.should == :hage
374
+ @flow[:foo].event_for(:event1).action.success_key.should == :bar
375
+ @flow[:foo].event_for(:event2).action.success_key.should == :baz
376
+ @flow[:foo].action.should be_nil
377
+ @flow[:foo].success_key.should == nil
378
+ @flow[:foo].options.should == {}
379
+ @flow[:bar].key.should == :bar
380
+ @flow[:bar].event_for(:event1).should_not be_nil
381
+ @flow[:bar].event_for(:event1).success_key.should == :baz
382
+ @flow[:bar].event_for(:event2).success_key.should == :foo
383
+ @flow[:bar].event_for(:event1).action.name.should == :hoge
384
+ @flow[:bar].event_for(:event2).action.name.should == :hage
385
+ @flow[:bar].event_for(:event1).action.success_key.should == :baz
386
+ @flow[:bar].event_for(:event2).action.success_key.should == :foo
387
+ @flow[:bar].event_for(:event1).action.lock.should == true
388
+ @flow[:bar].event_for(:event2).action.lock.should == true
389
+ @flow[:bar].action.should be_nil
390
+ @flow[:bar].success_key.should == nil
391
+ @flow[:baz].key.should == :baz
392
+ @flow[:baz].event_for(:event1).should_not be_nil
393
+ @flow[:baz].event_for(:event1).success_key.should == :foo
394
+ @flow[:baz].event_for(:event2).success_key.should == :bar
395
+ @flow[:baz].event_for(:event1).action.name.should == :hoge
396
+ @flow[:baz].event_for(:event2).action.name.should == :hage
397
+ @flow[:baz].event_for(:event1).action.success_key.should == :foo
398
+ @flow[:baz].event_for(:event2).action.success_key.should == :bar
399
+ @flow[:baz].event_for(:event1).action.lock.should == true
400
+ @flow[:baz].event_for(:event2).action.lock.should == true
401
+ @flow[:baz].action.should be_nil
402
+ @flow[:baz].success_key.should == nil
403
+ end
404
+
405
+ def check_inspect_with_event_and_actions
406
+ klass_name = @target.name
407
+ @flow.inspect.should == '<StateFlow::Base @attr_name=:status @attr_key_name=:status_key' <<
408
+ " @klass=#{@target.name.inspect}" <<
409
+ ' @entries=[' <<
410
+ '<StateFlow::Entry @key=:foo @events=[' <<
411
+ '<StateFlow::Event @name=:event1 @action=<StateFlow::NamedAction @name=:hoge @success_key=:bar>>, ' <<
412
+ '<StateFlow::Event @name=:event2 @action=<StateFlow::NamedAction @name=:hage @success_key=:baz>>' <<
413
+ ']>, ' <<
414
+ '<StateFlow::Entry @key=:bar @events=[' <<
415
+ '<StateFlow::Event @name=:event1 @action=<StateFlow::NamedAction @name=:hoge @success_key=:baz @lock=true>>, ' <<
416
+ '<StateFlow::Event @name=:event2 @action=<StateFlow::NamedAction @name=:hage @success_key=:foo @lock=true>>' <<
417
+ ']>, ' <<
418
+ '<StateFlow::Entry @key=:baz @events=[' <<
419
+ '<StateFlow::Event @name=:event1 @action=<StateFlow::NamedAction @name=:hoge @success_key=:foo @lock=true>>, ' <<
420
+ '<StateFlow::Event @name=:event2 @action=<StateFlow::NamedAction @name=:hage @success_key=:bar @lock=true>>' <<
421
+ ']>' <<
422
+ ']>'
423
+ end
424
+
425
+ describe "state( :<state_name> => { event(:<event_name1>) => { action(:<method_name1>) => :<new_state_name>}, event(:<event_name2>) => {action(:<method_name1>) => :<new_state_name>} }" do
426
+ before(:each) do
427
+ @flow = @target.state_flow(:status) do
428
+ state :foo => [{event(:event1) => {action(:hoge) => :bar}, event(:event2) => {action(:hage) => :baz}}]
429
+ state :bar => [{event(:event1) => {action(:hoge) => :baz}, event(:event2) => {action(:hage) => :foo}}], :lock => true
430
+ state :baz => [{event(:event1) => {action(:hoge) => :foo}, event(:event2) => {action(:hage) => :bar}, :lock => true}]
431
+ end
432
+ end
433
+ it("check") { check_flow_with_event_and_actions }
434
+ it("inspect") {check_inspect_with_event_and_actions }
435
+ end
436
+
437
+ describe "state( :<state_name> => [ { event(:<event_name1>) => { action(:<method_name1>) => :<new_state_name>}, event(:<event_name2>) => {action(:<method_name1>) => :<new_state_name>} } ]" do
438
+ before(:each) do
439
+ @flow = @target.state_flow(:status) do
440
+ state :foo => [{event(:event1) => {action(:hoge) => :bar}, event(:event2) => {action(:hage) => :baz}}]
441
+ state :bar => [{event(:event1) => {action(:hoge) => :baz}, event(:event2) => {action(:hage) => :foo}}], :lock => true
442
+ state :baz => [{event(:event1) => {action(:hoge) => :foo, :lock => true}, event(:event2) => {action(:hage) => :bar, :lock => true}}]
443
+ end
444
+ end
445
+ it("check") { check_flow_with_event_and_actions }
446
+ it("inspect") {check_inspect_with_event_and_actions }
447
+ end
448
+
449
+ end
450
+ end
451
+
452
+ describe "invalid arguments" do
453
+ end
454
+
455
+ end
456
+
457
+ end