aasm 2.3.1 → 2.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.
- data/.gitignore +1 -0
- data/CHANGELOG.md +15 -0
- data/LICENSE +1 -1
- data/README.md +40 -13
- data/Rakefile +2 -2
- data/lib/aasm/aasm.rb +19 -31
- data/lib/aasm/base.rb +56 -0
- data/lib/aasm/supporting_classes.rb +1 -0
- data/lib/aasm/version.rb +1 -1
- data/spec/{functional → models}/conversation.rb +0 -2
- data/{test/models → spec/models/not_auto_loaded}/process.rb +1 -0
- data/spec/models/process_with_new_dsl.rb +31 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/spec_helpers/models_spec_helper.rb +184 -0
- data/spec/unit/aasm_spec.rb +16 -89
- data/spec/unit/active_record_persistence_spec.rb +195 -191
- data/spec/unit/auth_machine_spec.rb +66 -0
- data/spec/{functional → unit}/conversation_spec.rb +2 -3
- data/spec/unit/event_spec.rb +0 -9
- data/spec/unit/memory_leak_spec.rb +34 -0
- data/spec/unit/new_dsl_spec.rb +28 -0
- data/spec/unit/state_spec.rb +13 -11
- metadata +23 -25
- data/test/functional/auth_machine_test.rb +0 -148
- data/test/test_helper.rb +0 -43
- data/test/unit/aasm_test.rb +0 -0
- data/test/unit/event_test.rb +0 -54
- data/test/unit/state_machine_test.rb +0 -37
- data/test/unit/state_test.rb +0 -69
- data/test/unit/state_transition_test.rb +0 -75
data/test/test_helper.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler'
|
3
|
-
begin
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
rescue Bundler::BundlerError => e
|
6
|
-
$stderr.puts e.message
|
7
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
-
exit e.status_code
|
9
|
-
end
|
10
|
-
|
11
|
-
require 'ostruct'
|
12
|
-
require 'rubygems'
|
13
|
-
|
14
|
-
begin
|
15
|
-
gem 'minitest'
|
16
|
-
rescue Gem::LoadError
|
17
|
-
puts 'minitest gem not found'
|
18
|
-
end
|
19
|
-
|
20
|
-
begin
|
21
|
-
require 'minitest/autorun'
|
22
|
-
puts 'using minitest'
|
23
|
-
rescue LoadError
|
24
|
-
require 'test/unit'
|
25
|
-
puts 'using test/unit'
|
26
|
-
end
|
27
|
-
|
28
|
-
require 'rr'
|
29
|
-
require 'shoulda'
|
30
|
-
|
31
|
-
class Test::Unit::TestCase
|
32
|
-
include RR::Adapters::TestUnit
|
33
|
-
end
|
34
|
-
|
35
|
-
begin
|
36
|
-
require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying'
|
37
|
-
require 'ruby-debug/completion'
|
38
|
-
rescue LoadError
|
39
|
-
end
|
40
|
-
|
41
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
42
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
43
|
-
require 'aasm'
|
data/test/unit/aasm_test.rb
DELETED
File without changes
|
data/test/unit/event_test.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class EventTest < Test::Unit::TestCase
|
4
|
-
def new_event
|
5
|
-
@event = AASM::SupportingClasses::Event.new(@name, {:success => @success}) do
|
6
|
-
transitions :to => :closed, :from => [:open, :received]
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'event' do
|
11
|
-
setup do
|
12
|
-
@name = :close_order
|
13
|
-
@success = :success_callback
|
14
|
-
end
|
15
|
-
|
16
|
-
should 'set the name' do
|
17
|
-
assert_equal @name, new_event.name
|
18
|
-
end
|
19
|
-
|
20
|
-
should 'set the success option' do
|
21
|
-
assert_equal @success, new_event.success
|
22
|
-
end
|
23
|
-
|
24
|
-
should 'create StateTransitions' do
|
25
|
-
mock(AASM::SupportingClasses::StateTransition).new({:to => :closed, :from => :open})
|
26
|
-
mock(AASM::SupportingClasses::StateTransition).new({:to => :closed, :from => :received})
|
27
|
-
new_event
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'when firing' do
|
31
|
-
should 'raise an AASM::InvalidTransition error if the transitions are empty' do
|
32
|
-
event = AASM::SupportingClasses::Event.new(:event)
|
33
|
-
|
34
|
-
obj = OpenStruct.new
|
35
|
-
obj.aasm_current_state = :open
|
36
|
-
|
37
|
-
assert_raise AASM::InvalidTransition do
|
38
|
-
event.fire(obj)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
should 'return the state of the first matching transition it finds' do
|
43
|
-
event = AASM::SupportingClasses::Event.new(:event) do
|
44
|
-
transitions :to => :closed, :from => [:open, :received]
|
45
|
-
end
|
46
|
-
|
47
|
-
obj = OpenStruct.new
|
48
|
-
obj.aasm_current_state = :open
|
49
|
-
|
50
|
-
assert_equal :closed, event.fire(obj)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
-
|
3
|
-
class StateMachineTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "state machines" do
|
6
|
-
|
7
|
-
should "be created without memory leak" do
|
8
|
-
assert_equal 1, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine
|
9
|
-
assert_number_of_objects AASM::SupportingClasses::State, 5 # AuthMachine
|
10
|
-
assert_number_of_objects AASM::SupportingClasses::Event, 11 # AuthMachine
|
11
|
-
assert_number_of_objects AASM::SupportingClasses::StateTransition, 19 # AuthMachine
|
12
|
-
|
13
|
-
load File.expand_path(File.dirname(__FILE__) + '/../models/process.rb')
|
14
|
-
assert_equal 2, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + Process
|
15
|
-
assert_number_of_objects Models::Process, 0
|
16
|
-
assert_number_of_objects AASM::SupportingClasses::State, 8 # AuthMachine + Process
|
17
|
-
assert_number_of_objects AASM::SupportingClasses::Event, 13 # AuthMachine + Process
|
18
|
-
assert_number_of_objects AASM::SupportingClasses::StateTransition, 21 # AuthMachine + Process
|
19
|
-
|
20
|
-
Models.send(:remove_const, "Process") if Models.const_defined?("Process")
|
21
|
-
load File.expand_path(File.dirname(__FILE__) + '/../models/process.rb')
|
22
|
-
assert_equal 2, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + Process
|
23
|
-
assert_number_of_objects AASM::SupportingClasses::State, 8 # AuthMachine + Process
|
24
|
-
assert_number_of_objects AASM::SupportingClasses::Event, 13 # AuthMachine + Process
|
25
|
-
assert_number_of_objects AASM::SupportingClasses::StateTransition, 21 # AuthMachine + Process
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def assert_number_of_objects(clazz, num)
|
33
|
-
count = ObjectSpace.each_object(clazz) {}
|
34
|
-
assert_equal num, count, "#{num} expected, but we had #{count} #{clazz}"
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
data/test/unit/state_test.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class StateTest < Test::Unit::TestCase
|
4
|
-
def new_state(options={})
|
5
|
-
AASM::SupportingClasses::State.new(@name, @options.merge(options))
|
6
|
-
end
|
7
|
-
|
8
|
-
context 'state' do
|
9
|
-
setup do
|
10
|
-
@name = :astate
|
11
|
-
@options = { :crazy_custom_key => 'key' }
|
12
|
-
end
|
13
|
-
|
14
|
-
should 'set the name' do
|
15
|
-
assert_equal :astate, new_state.name
|
16
|
-
end
|
17
|
-
|
18
|
-
should 'set the display_name from name' do
|
19
|
-
assert_equal "Astate", new_state.display_name
|
20
|
-
end
|
21
|
-
|
22
|
-
should 'set the display_name from options' do
|
23
|
-
assert_equal "A State", new_state(:display => "A State").display_name
|
24
|
-
end
|
25
|
-
|
26
|
-
should 'set the options and expose them as options' do
|
27
|
-
assert_equal @options, new_state.options
|
28
|
-
end
|
29
|
-
|
30
|
-
should 'equal a symbol of the same name' do
|
31
|
-
assert_equal new_state, :astate
|
32
|
-
end
|
33
|
-
|
34
|
-
should 'equal a state of the same name' do
|
35
|
-
assert_equal new_state, new_state
|
36
|
-
end
|
37
|
-
|
38
|
-
should 'send a message to the record for an action if the action is present as a symbol' do
|
39
|
-
state = new_state(:entering => :foo)
|
40
|
-
mock(record = Object.new).foo
|
41
|
-
state.call_action(:entering, record)
|
42
|
-
end
|
43
|
-
|
44
|
-
should 'send a message to the record for an action if the action is present as a string' do
|
45
|
-
state = new_state(:entering => 'foo')
|
46
|
-
mock(record = Object.new).foo
|
47
|
-
state.call_action(:entering, record)
|
48
|
-
end
|
49
|
-
|
50
|
-
should 'call a proc with the record as its argument for an action if the action is present as a proc' do
|
51
|
-
state = new_state(:entering => Proc.new {|r| r.foobar})
|
52
|
-
mock(record = Object.new).foobar
|
53
|
-
state.call_action(:entering, record)
|
54
|
-
end
|
55
|
-
|
56
|
-
should 'send a message to the record for each action if the action is present as an array' do
|
57
|
-
state = new_state(:entering => [:a, :b, 'c', lambda {|r| r.foobar}])
|
58
|
-
|
59
|
-
record = Object.new
|
60
|
-
mock(record).a
|
61
|
-
mock(record).b
|
62
|
-
mock(record).c
|
63
|
-
mock(record).foobar
|
64
|
-
|
65
|
-
state.call_action(:entering, record)
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class StateTransitionTest < Test::Unit::TestCase
|
4
|
-
context 'state transition' do
|
5
|
-
setup do
|
6
|
-
@opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
7
|
-
@st = AASM::SupportingClasses::StateTransition.new(@opts)
|
8
|
-
end
|
9
|
-
|
10
|
-
should 'set from, to, and opts attr readers' do
|
11
|
-
assert_equal @opts[:from], @st.from
|
12
|
-
assert_equal @opts[:to], @st.to
|
13
|
-
assert_equal @opts, @st.options
|
14
|
-
end
|
15
|
-
|
16
|
-
should 'pass equality check if from and to are the same' do
|
17
|
-
obj = OpenStruct.new
|
18
|
-
obj.from = @opts[:from]
|
19
|
-
obj.to = @opts[:to]
|
20
|
-
|
21
|
-
assert_equal @st, obj
|
22
|
-
end
|
23
|
-
|
24
|
-
should 'fail equality check if from is not the same' do
|
25
|
-
obj = OpenStruct.new
|
26
|
-
obj.from = 'blah'
|
27
|
-
obj.to = @opts[:to]
|
28
|
-
|
29
|
-
assert_not_equal @st, obj
|
30
|
-
end
|
31
|
-
|
32
|
-
should 'fail equality check if to is not the same' do
|
33
|
-
obj = OpenStruct.new
|
34
|
-
obj.from = @opts[:from]
|
35
|
-
obj.to = 'blah'
|
36
|
-
|
37
|
-
assert_not_equal @st, obj
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'when performing guard checks' do
|
41
|
-
should 'return true if there is no guard' do
|
42
|
-
opts = {:from => 'foo', :to => 'bar'}
|
43
|
-
st = AASM::SupportingClasses::StateTransition.new(opts)
|
44
|
-
assert st.perform(nil)
|
45
|
-
end
|
46
|
-
|
47
|
-
should 'call the method on the object if guard is a symbol' do
|
48
|
-
opts = {:from => 'foo', :to => 'bar', :guard => :test_guard}
|
49
|
-
st = AASM::SupportingClasses::StateTransition.new(opts)
|
50
|
-
|
51
|
-
mock(obj = Object.new).test_guard
|
52
|
-
|
53
|
-
st.perform(obj)
|
54
|
-
end
|
55
|
-
|
56
|
-
should 'call the method on the object if guard is a string' do
|
57
|
-
opts = {:from => 'foo', :to => 'bar', :guard => 'test_guard'}
|
58
|
-
st = AASM::SupportingClasses::StateTransition.new(opts)
|
59
|
-
|
60
|
-
mock(obj = Object.new).test_guard
|
61
|
-
|
62
|
-
st.perform(obj)
|
63
|
-
end
|
64
|
-
|
65
|
-
should 'call the proc passing the object if guard is a proc' do
|
66
|
-
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test_guard}}
|
67
|
-
st = AASM::SupportingClasses::StateTransition.new(opts)
|
68
|
-
|
69
|
-
mock(obj = Object.new).test_guard
|
70
|
-
|
71
|
-
st.perform(obj)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|