state_machines 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.idea/.name +1 -0
  4. data/.idea/.rakeTasks +7 -0
  5. data/.idea/cssxfire.xml +9 -0
  6. data/.idea/encodings.xml +5 -0
  7. data/.idea/misc.xml +5 -0
  8. data/.idea/modules.xml +12 -0
  9. data/.idea/scopes/scope_settings.xml +5 -0
  10. data/.idea/state_machine2.iml +34 -0
  11. data/.idea/vcs.xml +9 -0
  12. data/.idea/workspace.xml +1156 -0
  13. data/.rspec +3 -0
  14. data/.travis.yml +8 -0
  15. data/Gemfile +4 -0
  16. data/LICENSE.txt +23 -0
  17. data/README.md +29 -0
  18. data/Rakefile +1 -0
  19. data/lib/state_machines/assertions.rb +40 -0
  20. data/lib/state_machines/branch.rb +187 -0
  21. data/lib/state_machines/callback.rb +220 -0
  22. data/lib/state_machines/core.rb +25 -0
  23. data/lib/state_machines/core_ext/class/state_machine.rb +5 -0
  24. data/lib/state_machines/core_ext.rb +2 -0
  25. data/lib/state_machines/error.rb +13 -0
  26. data/lib/state_machines/eval_helpers.rb +87 -0
  27. data/lib/state_machines/event.rb +246 -0
  28. data/lib/state_machines/event_collection.rb +141 -0
  29. data/lib/state_machines/extensions.rb +148 -0
  30. data/lib/state_machines/helper_module.rb +17 -0
  31. data/lib/state_machines/integrations/base.rb +100 -0
  32. data/lib/state_machines/integrations.rb +113 -0
  33. data/lib/state_machines/machine.rb +2234 -0
  34. data/lib/state_machines/machine_collection.rb +84 -0
  35. data/lib/state_machines/macro_methods.rb +520 -0
  36. data/lib/state_machines/matcher.rb +123 -0
  37. data/lib/state_machines/matcher_helpers.rb +54 -0
  38. data/lib/state_machines/node_collection.rb +221 -0
  39. data/lib/state_machines/path.rb +120 -0
  40. data/lib/state_machines/path_collection.rb +90 -0
  41. data/lib/state_machines/state.rb +276 -0
  42. data/lib/state_machines/state_collection.rb +112 -0
  43. data/lib/state_machines/state_context.rb +138 -0
  44. data/lib/state_machines/transition.rb +470 -0
  45. data/lib/state_machines/transition_collection.rb +245 -0
  46. data/lib/state_machines/version.rb +3 -0
  47. data/lib/state_machines/yard.rb +8 -0
  48. data/lib/state_machines.rb +3 -0
  49. data/spec/errors/default_spec.rb +14 -0
  50. data/spec/errors/with_message_spec.rb +39 -0
  51. data/spec/helpers/helper_spec.rb +14 -0
  52. data/spec/internal/app/models/auto_shop.rb +31 -0
  53. data/spec/internal/app/models/car.rb +19 -0
  54. data/spec/internal/app/models/model_base.rb +6 -0
  55. data/spec/internal/app/models/motorcycle.rb +9 -0
  56. data/spec/internal/app/models/traffic_light.rb +47 -0
  57. data/spec/internal/app/models/vehicle.rb +123 -0
  58. data/spec/machine_spec.rb +3167 -0
  59. data/spec/matcher_helpers_spec.rb +39 -0
  60. data/spec/matcher_spec.rb +157 -0
  61. data/spec/models/auto_shop_spec.rb +41 -0
  62. data/spec/models/car_spec.rb +90 -0
  63. data/spec/models/motorcycle_spec.rb +44 -0
  64. data/spec/models/traffic_light_spec.rb +56 -0
  65. data/spec/models/vehicle_spec.rb +580 -0
  66. data/spec/node_collection_spec.rb +371 -0
  67. data/spec/path_collection_spec.rb +271 -0
  68. data/spec/path_spec.rb +488 -0
  69. data/spec/spec_helper.rb +6 -0
  70. data/spec/state_collection_spec.rb +352 -0
  71. data/spec/state_context_spec.rb +442 -0
  72. data/spec/state_machine_spec.rb +29 -0
  73. data/spec/state_spec.rb +970 -0
  74. data/spec/support/migration_helpers.rb +50 -0
  75. data/spec/support/models.rb +6 -0
  76. data/spec/transition_collection_spec.rb +2199 -0
  77. data/spec/transition_spec.rb +1558 -0
  78. data/state_machines.gemspec +23 -0
  79. metadata +194 -0
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe StateMachines::MatcherHelpers do
4
+ context 'All' do
5
+ include StateMachines::MatcherHelpers
6
+
7
+ before(:each) do
8
+ @matcher = all
9
+ end
10
+
11
+ it 'should_build_an_all_matcher' do
12
+ assert_equal StateMachines::AllMatcher.instance, @matcher
13
+ end
14
+ end
15
+
16
+ context 'Any' do
17
+ include StateMachines::MatcherHelpers
18
+
19
+ before(:each) do
20
+ @matcher = any
21
+ end
22
+
23
+ it 'should_build_an_all_matcher' do
24
+ assert_equal StateMachines::AllMatcher.instance, @matcher
25
+ end
26
+ end
27
+
28
+ context 'Same' do
29
+ include StateMachines::MatcherHelpers
30
+
31
+ before(:each) do
32
+ @matcher = same
33
+ end
34
+
35
+ it 'should_build_a_loopback_matcher' do
36
+ assert_equal StateMachines::LoopbackMatcher.instance, @matcher
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,157 @@
1
+ require 'spec_helper'
2
+
3
+ describe StateMachines::Matcher do
4
+ context 'ByDefault' do
5
+ before(:each) do
6
+ @matcher = StateMachines::Matcher.new
7
+ end
8
+
9
+ it 'should_have_no_values' do
10
+ assert_equal [], @matcher.values
11
+ end
12
+
13
+ it 'should_filter_all_values' do
14
+ assert_equal [], @matcher.filter([:parked, :idling])
15
+ end
16
+ end
17
+
18
+ context 'WithValue' do
19
+ before(:each) do
20
+ @matcher = StateMachines::Matcher.new(nil)
21
+ end
22
+
23
+ it 'should_have_values' do
24
+ assert_equal [nil], @matcher.values
25
+ end
26
+
27
+ it 'should_filter_unknown_values' do
28
+ assert_equal [nil], @matcher.filter([nil, :parked])
29
+ end
30
+ end
31
+
32
+ context 'WithMultipleValues' do
33
+ before(:each) do
34
+ @matcher = StateMachines::Matcher.new([:parked, :idling])
35
+ end
36
+
37
+ it 'should_have_values' do
38
+ assert_equal [:parked, :idling], @matcher.values
39
+ end
40
+
41
+ it 'should_filter_unknown_values' do
42
+ assert_equal [:parked], @matcher.filter([:parked, :first_gear])
43
+ end
44
+ end
45
+
46
+ context 'AllMatcher' do
47
+ before(:each) do
48
+ @matcher = StateMachines::AllMatcher.instance
49
+ end
50
+
51
+ it 'should_have_no_values' do
52
+ assert_equal [], @matcher.values
53
+ end
54
+
55
+ it 'should_always_match' do
56
+ [nil, :parked, :idling].each { |value| assert @matcher.matches?(value) }
57
+ end
58
+
59
+ it 'should_not_filter_any_values' do
60
+ assert_equal [:parked, :idling], @matcher.filter([:parked, :idling])
61
+ end
62
+
63
+ it 'should_generate_blacklist_matcher_after_subtraction' do
64
+ matcher = @matcher - [:parked, :idling]
65
+ assert_instance_of StateMachines::BlacklistMatcher, matcher
66
+ assert_equal [:parked, :idling], matcher.values
67
+ end
68
+
69
+ it 'should_have_a_description' do
70
+ assert_equal 'all', @matcher.description
71
+ end
72
+ end
73
+
74
+ context 'WhitelistMatcher' do
75
+ before(:each) do
76
+ @matcher = StateMachines::WhitelistMatcher.new([:parked, :idling])
77
+ end
78
+
79
+ it 'should_have_values' do
80
+ assert_equal [:parked, :idling], @matcher.values
81
+ end
82
+
83
+ it 'should_filter_unknown_values' do
84
+ assert_equal [:parked, :idling], @matcher.filter([:parked, :idling, :first_gear])
85
+ end
86
+
87
+ it 'should_match_known_values' do
88
+ assert @matcher.matches?(:parked)
89
+ end
90
+
91
+ it 'should_not_match_unknown_values' do
92
+ assert !@matcher.matches?(:first_gear)
93
+ end
94
+
95
+ it 'should_have_a_description' do
96
+ assert_equal '[:parked, :idling]', @matcher.description
97
+
98
+ matcher = StateMachines::WhitelistMatcher.new([:parked])
99
+ assert_equal ':parked', matcher.description
100
+ end
101
+ end
102
+
103
+ context 'BlacklistMatcher' do
104
+ before(:each) do
105
+ @matcher = StateMachines::BlacklistMatcher.new([:parked, :idling])
106
+ end
107
+
108
+ it 'should_have_values' do
109
+ assert_equal [:parked, :idling], @matcher.values
110
+ end
111
+
112
+ it 'should_filter_known_values' do
113
+ assert_equal [:first_gear], @matcher.filter([:parked, :idling, :first_gear])
114
+ end
115
+
116
+ it 'should_match_unknown_values' do
117
+ assert @matcher.matches?(:first_gear)
118
+ end
119
+
120
+ it 'should_not_match_known_values' do
121
+ assert !@matcher.matches?(:parked)
122
+ end
123
+
124
+ it 'should_have_a_description' do
125
+ assert_equal 'all - [:parked, :idling]', @matcher.description
126
+
127
+ matcher = StateMachines::BlacklistMatcher.new([:parked])
128
+ assert_equal 'all - :parked', matcher.description
129
+ end
130
+ end
131
+
132
+ context 'LoopbackMatcher' do
133
+ before(:each) do
134
+ @matcher = StateMachines::LoopbackMatcher.instance
135
+ end
136
+
137
+ it 'should_have_no_values' do
138
+ assert_equal [], @matcher.values
139
+ end
140
+
141
+ it 'should_filter_all_values' do
142
+ assert_equal [], @matcher.filter([:parked, :idling])
143
+ end
144
+
145
+ it 'should_match_if_from_context_is_same' do
146
+ assert @matcher.matches?(:parked, :from => :parked)
147
+ end
148
+
149
+ it 'should_not_match_if_from_context_is_different' do
150
+ assert !@matcher.matches?(:parked, :from => :idling)
151
+ end
152
+
153
+ it 'should_have_a_description' do
154
+ assert_equal 'same', @matcher.description
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe AutoShop do
4
+
5
+ let(:auto_shop) { AutoShop.new }
6
+
7
+ it 'should_be_in_available_state' do
8
+ assert_equal 'available', auto_shop.state
9
+ end
10
+
11
+ it 'should_allow_tow_vehicle' do
12
+ assert auto_shop.tow_vehicle
13
+ end
14
+
15
+ it 'should_not_allow_fix_vehicle' do
16
+ assert !auto_shop.fix_vehicle
17
+ end
18
+
19
+ context 'Busy' do
20
+ before(:each) do
21
+ auto_shop.tow_vehicle
22
+ end
23
+
24
+ it 'should_be_in_busy_state' do
25
+ assert_equal 'busy', auto_shop.state
26
+ end
27
+
28
+ it 'should_have_incremented_number_of_customers' do
29
+ assert_equal 1, auto_shop.num_customers
30
+ end
31
+
32
+ it 'should_not_allow_tow_vehicle' do
33
+ assert !auto_shop.tow_vehicle
34
+ end
35
+
36
+ it 'should_allow_fix_vehicle' do
37
+ assert auto_shop.fix_vehicle
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Car do
4
+ let(:car) { Car.new }
5
+
6
+ it 'should_be_in_parked_state' do
7
+ assert_equal 'parked', car.state
8
+ end
9
+
10
+ it 'should_not_have_the_seatbelt_on' do
11
+ assert !car.seatbelt_on
12
+ end
13
+
14
+ it 'should_not_allow_park' do
15
+ assert !car.park
16
+ end
17
+
18
+ it 'should_allow_ignite' do
19
+ assert car.ignite
20
+ assert_equal 'idling', car.state
21
+ end
22
+
23
+ it 'should_not_allow_idle' do
24
+ assert !car.idle
25
+ end
26
+
27
+ it 'should_not_allow_shift_up' do
28
+ assert !car.shift_up
29
+ end
30
+
31
+ it 'should_not_allow_shift_down' do
32
+ assert !car.shift_down
33
+ end
34
+
35
+ it 'should_not_allow_crash' do
36
+ assert !car.crash
37
+ end
38
+
39
+ it 'should_not_allow_repair' do
40
+ assert !car.repair
41
+ end
42
+
43
+ it 'should_allow_reverse' do
44
+ assert car.reverse
45
+ end
46
+
47
+
48
+ context 'backing up' do
49
+ before(:each) do
50
+ car.reverse
51
+ end
52
+
53
+ it 'should_be_in_backing_up_state' do
54
+ assert_equal 'backing_up', car.state
55
+ end
56
+
57
+ it 'should_allow_park' do
58
+ assert car.park
59
+ end
60
+
61
+ it 'should_not_allow_ignite' do
62
+ assert !car.ignite
63
+ end
64
+
65
+ it 'should_allow_idle' do
66
+ assert car.idle
67
+ end
68
+
69
+ it 'should_allow_shift_up' do
70
+ assert car.shift_up
71
+ end
72
+
73
+ it 'should_not_allow_shift_down' do
74
+ assert !car.shift_down
75
+ end
76
+
77
+ it 'should_not_allow_crash' do
78
+ assert !car.crash
79
+ end
80
+
81
+ it 'should_not_allow_repair' do
82
+ assert !car.repair
83
+ end
84
+
85
+ it 'should_not_allow_reverse' do
86
+ assert !car.reverse
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Motorcycle do
4
+ let(:motorcycle) { Motorcycle.new }
5
+
6
+ it 'should_be_in_idling_state' do
7
+ assert_equal 'idling', motorcycle.state
8
+ end
9
+
10
+ it 'should_allow_park' do
11
+ assert motorcycle.park
12
+ end
13
+
14
+ it 'should_not_allow_ignite' do
15
+ assert !motorcycle.ignite
16
+ end
17
+
18
+ it 'should_allow_shift_up' do
19
+ assert motorcycle.shift_up
20
+ end
21
+
22
+ it 'should_not_allow_shift_down' do
23
+ assert !motorcycle.shift_down
24
+ end
25
+
26
+ it 'should_not_allow_crash' do
27
+ assert !motorcycle.crash
28
+ end
29
+
30
+ it 'should_not_allow_repair' do
31
+ assert !motorcycle.repair
32
+ end
33
+
34
+ it 'should_inherit_decibels_from_superclass' do
35
+ motorcycle.park
36
+ assert_equal 0.0, motorcycle.decibels
37
+ end
38
+
39
+ it 'should_use_decibels_defined_in_state' do
40
+ motorcycle.shift_up
41
+ assert_equal 1.0, motorcycle.decibels
42
+ end
43
+
44
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe TrafficLight do
4
+ let(:light) { TrafficLight.new }
5
+ context 'Stop' do
6
+ before(:each) do
7
+ light.state = 'stop'
8
+ end
9
+
10
+ it 'should_use_stop_color' do
11
+ assert_equal 'red', light.color
12
+ end
13
+
14
+ it 'should_pass_arguments_through' do
15
+ assert_equal 'RED', light.color(:upcase!)
16
+ end
17
+
18
+ it 'should_pass_block_through' do
19
+ color = light.color { |value| value.upcase! }
20
+ assert_equal 'RED', color
21
+ end
22
+
23
+ it 'should_use_stop_capture_violations' do
24
+ assert_equal true, light.capture_violations?
25
+ end
26
+ end
27
+
28
+ context 'Proceed' do
29
+ before(:each) do
30
+ light.state = 'proceed'
31
+ end
32
+
33
+ it 'should_use_proceed_color' do
34
+ assert_equal 'green', light.color
35
+ end
36
+
37
+ it 'should_use_proceed_capture_violations' do
38
+ assert_equal false, light.capture_violations?
39
+ end
40
+ end
41
+
42
+ context 'Caution' do
43
+ before(:each) do
44
+ light.state = 'caution'
45
+ end
46
+
47
+ it 'should_use_caution_color' do
48
+ assert_equal 'yellow', light.color
49
+ end
50
+
51
+ it 'should_use_caution_capture_violations' do
52
+ assert_equal true, light.capture_violations?
53
+ end
54
+ end
55
+
56
+ end