Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a9d9ca16
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a9d9ca16
编写于
6月 28, 2008
作者:
R
rick
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
converted tests for more complex state transitions
上级
74cb0569
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
142 addition
and
123 deletion
+142
-123
activemodel/lib/active_model/state_machine/event.rb
activemodel/lib/active_model/state_machine/event.rb
+3
-4
activemodel/lib/active_model/state_machine/machine.rb
activemodel/lib/active_model/state_machine/machine.rb
+22
-4
activemodel/test/state_machine/event_test.rb
activemodel/test/state_machine/event_test.rb
+3
-3
activemodel/test/state_machine/machine_test.rb
activemodel/test/state_machine/machine_test.rb
+3
-1
activemodel/test/state_machine_test.rb
activemodel/test/state_machine_test.rb
+111
-111
未找到文件。
activemodel/lib/active_model/state_machine/event.rb
浏览文件 @
a9d9ca16
...
...
@@ -3,9 +3,8 @@ module StateMachine
class
Event
attr_reader
:name
,
:success
def
initialize
(
name
,
options
=
{},
&
block
)
@name
,
@transitions
=
name
,
[]
machine
=
options
.
delete
(
:machine
)
def
initialize
(
machine
,
name
,
options
=
{},
&
block
)
@machine
,
@name
,
@transitions
=
machine
,
name
,
[]
if
machine
machine
.
klass
.
send
(
:define_method
,
"
#{
name
.
to_s
}
!"
)
do
|*
args
|
machine
.
fire_event
(
name
,
self
,
true
,
*
args
)
...
...
@@ -19,7 +18,7 @@ def initialize(name, options = {}, &block)
end
def
fire
(
obj
,
to_state
=
nil
,
*
args
)
transitions
=
@transitions
.
select
{
|
t
|
t
.
from
==
obj
.
current_state
}
transitions
=
@transitions
.
select
{
|
t
|
t
.
from
==
obj
.
current_state
(
@machine
?
@machine
.
name
:
nil
)
}
raise
InvalidTransition
if
transitions
.
size
==
0
next_state
=
nil
...
...
activemodel/lib/active_model/state_machine/machine.rb
浏览文件 @
a9d9ca16
...
...
@@ -19,12 +19,21 @@ def update(options = {}, &block)
self
end
def
fire_event
(
name
,
record
,
persist
,
*
args
)
state_index
[
record
.
current_state
].
call_action
(
:exit
,
record
)
if
new_state
=
@events
[
name
].
fire
(
record
,
*
args
)
def
fire_event
(
event
,
record
,
persist
,
*
args
)
state_index
[
record
.
current_state
(
@name
)
].
call_action
(
:exit
,
record
)
if
new_state
=
@events
[
event
].
fire
(
record
,
*
args
)
state_index
[
new_state
].
call_action
(
:enter
,
record
)
if
record
.
respond_to?
(
event_fired_callback
)
record
.
send
(
event_fired_callback
,
record
.
current_state
,
new_state
)
end
record
.
current_state
(
@name
,
new_state
)
else
if
record
.
respond_to?
(
event_failed_callback
)
record
.
send
(
event_failed_callback
,
event
)
end
false
end
end
...
...
@@ -37,13 +46,22 @@ def events_for(state)
events
=
@events
.
values
.
select
{
|
event
|
event
.
transitions_from_state?
(
state
)
}
events
.
map!
{
|
event
|
event
.
name
}
end
private
def
state
(
name
,
options
=
{})
@states
<<
(
state_index
[
name
]
||=
State
.
new
(
name
,
:machine
=>
self
)).
update
(
options
)
end
def
event
(
name
,
options
=
{},
&
block
)
(
@events
[
name
]
||=
Event
.
new
(
name
,
:machine
=>
self
)).
update
(
options
,
&
block
)
(
@events
[
name
]
||=
Event
.
new
(
self
,
name
)).
update
(
options
,
&
block
)
end
def
event_fired_callback
@event_fired_callback
||=
(
@name
==
:default
?
''
:
"
#{
@name
}
_"
)
+
'event_fired'
end
def
event_failed_callback
@event_failed_callback
||=
(
@name
==
:default
?
''
:
"
#{
@name
}
_"
)
+
'event_failed'
end
end
end
...
...
activemodel/test/state_machine/event_test.rb
浏览文件 @
a9d9ca16
...
...
@@ -7,7 +7,7 @@ def setup
end
def
new_event
@event
=
ActiveModel
::
StateMachine
::
Event
.
new
(
@name
,
{
:success
=>
@success
})
do
@event
=
ActiveModel
::
StateMachine
::
Event
.
new
(
nil
,
@name
,
{
:success
=>
@success
})
do
transitions
:to
=>
:closed
,
:from
=>
[
:open
,
:received
]
end
end
...
...
@@ -31,7 +31,7 @@ def new_event
class
EventBeingFiredTest
<
ActiveModel
::
TestCase
test
'should raise an AASM::InvalidTransition error if the transitions are empty'
do
event
=
ActiveModel
::
StateMachine
::
Event
.
new
(
:event
)
event
=
ActiveModel
::
StateMachine
::
Event
.
new
(
nil
,
:event
)
assert_raises
ActiveModel
::
StateMachine
::
InvalidTransition
do
event
.
fire
(
nil
)
...
...
@@ -39,7 +39,7 @@ class EventBeingFiredTest < ActiveModel::TestCase
end
test
'should return the state of the first matching transition it finds'
do
event
=
ActiveModel
::
StateMachine
::
Event
.
new
(
:event
)
do
event
=
ActiveModel
::
StateMachine
::
Event
.
new
(
nil
,
:event
)
do
transitions
:to
=>
:closed
,
:from
=>
[
:open
,
:received
]
end
...
...
activemodel/test/state_machine/machine_test.rb
浏览文件 @
a9d9ca16
...
...
@@ -36,6 +36,8 @@ class StateMachineMachineTest < ActiveModel::TestCase
end
test
"finds events for given state"
do
assert_equal
[
:shutdown
,
:timeout
],
MachineTestSubject
.
state_machine
.
events_for
(
:open
)
events
=
MachineTestSubject
.
state_machine
.
events_for
(
:open
)
assert
events
.
include?
(
:shutdown
)
assert
events
.
include?
(
:timeout
)
end
end
\ No newline at end of file
activemodel/test/state_machine_test.rb
浏览文件 @
a9d9ca16
...
...
@@ -186,48 +186,50 @@ def subj.aasm_write_state
# foo.aasm_current_state
# end
#end
#describe AASM, '- event callbacks' do
# it 'should call aasm_event_fired if defined and successful for bang fire' do
# foo = Foo.new
# def foo.aasm_event_fired(from, to)
# end
#
# foo.should_receive(:aasm_event_fired)
#
# foo.close!
# end
#
# it 'should call aasm_event_fired if defined and successful for non-bang fire' do
# foo = Foo.new
# def foo.aasm_event_fired(from, to)
# end
#
# foo.should_receive(:aasm_event_fired)
#
# foo.close
# end
#
# it 'should call aasm_event_failed if defined and transition failed for bang fire' do
# foo = Foo.new
# def foo.aasm_event_failed(event)
# end
#
# foo.should_receive(:aasm_event_failed)
#
# foo.null!
# end
#
# it 'should call aasm_event_failed if defined and transition failed for non-bang fire' do
# foo = Foo.new
# def foo.aasm_event_failed(event)
# end
#
# foo.should_receive(:aasm_event_failed)
#
# foo.null
# end
#end
uses_mocha
'StateMachineEventCallbacksTest'
do
class
StateMachineEventCallbacksTest
<
ActiveModel
::
TestCase
test
'should call aasm_event_fired if defined and successful for bang fire'
do
subj
=
StateMachineSubject
.
new
def
subj
.
aasm_event_fired
(
from
,
to
)
end
subj
.
expects
(
:event_fired
)
subj
.
close!
end
test
'should call aasm_event_fired if defined and successful for non-bang fire'
do
subj
=
StateMachineSubject
.
new
def
subj
.
aasm_event_fired
(
from
,
to
)
end
subj
.
expects
(
:event_fired
)
subj
.
close
end
test
'should call aasm_event_failed if defined and transition failed for bang fire'
do
subj
=
StateMachineSubject
.
new
def
subj
.
event_failed
(
event
)
end
subj
.
expects
(
:event_failed
)
subj
.
null!
end
test
'should call aasm_event_failed if defined and transition failed for non-bang fire'
do
subj
=
StateMachineSubject
.
new
def
subj
.
aasm_event_failed
(
event
)
end
subj
.
expects
(
:event_failed
)
subj
.
null
end
end
end
uses_mocha
'StateMachineStateActionsTest'
do
class
StateMachineStateActionsTest
<
ActiveModel
::
TestCase
...
...
@@ -254,72 +256,70 @@ class StateMachineInheritanceTest < ActiveModel::TestCase
assert_equal
StateMachineSubject
.
state_machine
.
events
,
StateMachineSubjectSubclass
.
state_machine
.
events
end
end
#
#
#class ChetanPatil
# include AASM
# aasm_initial_state :sleeping
# aasm_state :sleeping
# aasm_state :showering
# aasm_state :working
# aasm_state :dating
#
# aasm_event :wakeup do
# transitions :from => :sleeping, :to => [:showering, :working]
# end
#
# aasm_event :dress do
# transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes
# transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) }
# end
#
# def wear_clothes(shirt_color, trouser_type)
# end
#end
#
#
#describe ChetanPatil do
# it 'should transition to specified next state (sleeping to showering)' do
# cp = ChetanPatil.new
# cp.wakeup! :showering
#
# cp.aasm_current_state.should == :showering
# end
#
# it 'should transition to specified next state (sleeping to working)' do
# cp = ChetanPatil.new
# cp.wakeup! :working
#
# cp.aasm_current_state.should == :working
# end
#
# it 'should transition to default (first or showering) state' do
# cp = ChetanPatil.new
# cp.wakeup!
#
# cp.aasm_current_state.should == :showering
# end
#
# it 'should transition to default state when on_transition invoked' do
# cp = ChetanPatil.new
# cp.dress!(nil, 'purple', 'dressy')
#
# cp.aasm_current_state.should == :working
# end
#
# it 'should call on_transition method with args' do
# cp = ChetanPatil.new
# cp.wakeup! :showering
#
# cp.should_receive(:wear_clothes).with('blue', 'jeans')
# cp.dress! :working, 'blue', 'jeans'
# end
#
# it 'should call on_transition proc' do
# cp = ChetanPatil.new
# cp.wakeup! :showering
#
# cp.should_receive(:wear_clothes).with('purple', 'slacks')
# cp.dress!(:dating, 'purple', 'slacks')
# end
#end
\ No newline at end of file
class
StateMachineSubject
state_machine
:chetan_patil
,
:initial
=>
:sleeping
do
state
:sleeping
state
:showering
state
:working
state
:dating
event
:wakeup
do
transitions
:from
=>
:sleeping
,
:to
=>
[
:showering
,
:working
]
end
event
:dress
do
transitions
:from
=>
:sleeping
,
:to
=>
:working
,
:on_transition
=>
:wear_clothes
transitions
:from
=>
:showering
,
:to
=>
[
:working
,
:dating
],
:on_transition
=>
Proc
.
new
{
|
obj
,
*
args
|
obj
.
wear_clothes
(
*
args
)
}
end
end
def
wear_clothes
(
shirt_color
,
trouser_type
)
end
end
class
StateMachineWithComplexTransitionsTest
<
ActiveModel
::
TestCase
def
setup
@subj
=
StateMachineSubject
.
new
end
test
'transitions to specified next state (sleeping to showering)'
do
@subj
.
wakeup!
:showering
assert_equal
:showering
,
@subj
.
current_state
(
:chetan_patil
)
end
test
'transitions to specified next state (sleeping to working)'
do
@subj
.
wakeup!
:working
assert_equal
:working
,
@subj
.
current_state
(
:chetan_patil
)
end
test
'transitions to default (first or showering) state'
do
@subj
.
wakeup!
assert_equal
:showering
,
@subj
.
current_state
(
:chetan_patil
)
end
test
'transitions to default state when on_transition invoked'
do
@subj
.
dress!
(
nil
,
'purple'
,
'dressy'
)
assert_equal
:working
,
@subj
.
current_state
(
:chetan_patil
)
end
uses_mocha
"StateMachineWithComplexTransitionsTest on_transition tests"
do
test
'calls on_transition method with args'
do
@subj
.
wakeup!
:showering
@subj
.
expects
(
:wear_clothes
).
with
(
'blue'
,
'jeans'
)
@subj
.
dress!
:working
,
'blue'
,
'jeans'
end
test
'calls on_transition proc'
do
@subj
.
wakeup!
:showering
@subj
.
expects
(
:wear_clothes
).
with
(
'purple'
,
'slacks'
)
@subj
.
dress!
(
:dating
,
'purple'
,
'slacks'
)
end
end
end
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录