glebtv_state_machine 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (310) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +2 -0
  5. data/.travis.yml +72 -0
  6. data/.yardopts +5 -0
  7. data/Appraisals +491 -0
  8. data/CHANGELOG.md +502 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE +20 -0
  11. data/README.md +1244 -0
  12. data/Rakefile +42 -0
  13. data/examples/AutoShop_state.png +0 -0
  14. data/examples/Car_state.png +0 -0
  15. data/examples/Gemfile +5 -0
  16. data/examples/Gemfile.lock +14 -0
  17. data/examples/TrafficLight_state.png +0 -0
  18. data/examples/Vehicle_state.png +0 -0
  19. data/examples/auto_shop.rb +13 -0
  20. data/examples/car.rb +21 -0
  21. data/examples/doc/AutoShop.html +2856 -0
  22. data/examples/doc/AutoShop_state.png +0 -0
  23. data/examples/doc/Car.html +919 -0
  24. data/examples/doc/Car_state.png +0 -0
  25. data/examples/doc/TrafficLight.html +2230 -0
  26. data/examples/doc/TrafficLight_state.png +0 -0
  27. data/examples/doc/Vehicle.html +7921 -0
  28. data/examples/doc/Vehicle_state.png +0 -0
  29. data/examples/doc/_index.html +136 -0
  30. data/examples/doc/class_list.html +47 -0
  31. data/examples/doc/css/common.css +1 -0
  32. data/examples/doc/css/full_list.css +55 -0
  33. data/examples/doc/css/style.css +322 -0
  34. data/examples/doc/file_list.html +46 -0
  35. data/examples/doc/frames.html +13 -0
  36. data/examples/doc/index.html +136 -0
  37. data/examples/doc/js/app.js +205 -0
  38. data/examples/doc/js/full_list.js +173 -0
  39. data/examples/doc/js/jquery.js +16 -0
  40. data/examples/doc/method_list.html +734 -0
  41. data/examples/doc/top-level-namespace.html +105 -0
  42. data/examples/merb-rest/controller.rb +51 -0
  43. data/examples/merb-rest/model.rb +28 -0
  44. data/examples/merb-rest/view_edit.html.erb +24 -0
  45. data/examples/merb-rest/view_index.html.erb +23 -0
  46. data/examples/merb-rest/view_new.html.erb +13 -0
  47. data/examples/merb-rest/view_show.html.erb +17 -0
  48. data/examples/rails-rest/controller.rb +43 -0
  49. data/examples/rails-rest/migration.rb +7 -0
  50. data/examples/rails-rest/model.rb +23 -0
  51. data/examples/rails-rest/view__form.html.erb +34 -0
  52. data/examples/rails-rest/view_edit.html.erb +6 -0
  53. data/examples/rails-rest/view_index.html.erb +25 -0
  54. data/examples/rails-rest/view_new.html.erb +5 -0
  55. data/examples/rails-rest/view_show.html.erb +19 -0
  56. data/examples/traffic_light.rb +9 -0
  57. data/examples/vehicle.rb +33 -0
  58. data/gemfiles/active_model_3.0.0.gemfile +7 -0
  59. data/gemfiles/active_model_3.0.0.gemfile.lock +35 -0
  60. data/gemfiles/active_model_3.0.5.gemfile +7 -0
  61. data/gemfiles/active_model_3.0.5.gemfile.lock +35 -0
  62. data/gemfiles/active_model_3.1.1.gemfile +7 -0
  63. data/gemfiles/active_model_3.1.1.gemfile.lock +36 -0
  64. data/gemfiles/active_model_3.2.1.gemfile +7 -0
  65. data/gemfiles/active_model_3.2.12.gemfile +7 -0
  66. data/gemfiles/active_model_3.2.12.gemfile.lock +36 -0
  67. data/gemfiles/active_model_3.2.13.rc1.gemfile +7 -0
  68. data/gemfiles/active_model_3.2.13.rc1.gemfile.lock +36 -0
  69. data/gemfiles/active_model_4.0.0.gemfile +9 -0
  70. data/gemfiles/active_model_4.0.0.gemfile.lock +78 -0
  71. data/gemfiles/active_record_2.0.0.gemfile +9 -0
  72. data/gemfiles/active_record_2.0.0.gemfile.lock +39 -0
  73. data/gemfiles/active_record_2.0.5.gemfile +9 -0
  74. data/gemfiles/active_record_2.0.5.gemfile.lock +39 -0
  75. data/gemfiles/active_record_2.1.0.gemfile +9 -0
  76. data/gemfiles/active_record_2.1.0.gemfile.lock +39 -0
  77. data/gemfiles/active_record_2.1.2.gemfile +9 -0
  78. data/gemfiles/active_record_2.1.2.gemfile.lock +39 -0
  79. data/gemfiles/active_record_2.2.3.gemfile +9 -0
  80. data/gemfiles/active_record_2.2.3.gemfile.lock +39 -0
  81. data/gemfiles/active_record_2.3.12.gemfile +9 -0
  82. data/gemfiles/active_record_2.3.12.gemfile.lock +39 -0
  83. data/gemfiles/active_record_2.3.5.gemfile +9 -0
  84. data/gemfiles/active_record_2.3.5.gemfile.lock +39 -0
  85. data/gemfiles/active_record_3.0.0.gemfile +9 -0
  86. data/gemfiles/active_record_3.0.0.gemfile.lock +51 -0
  87. data/gemfiles/active_record_3.0.5.gemfile +9 -0
  88. data/gemfiles/active_record_3.0.5.gemfile.lock +50 -0
  89. data/gemfiles/active_record_3.1.1.gemfile +9 -0
  90. data/gemfiles/active_record_3.1.1.gemfile.lock +51 -0
  91. data/gemfiles/active_record_3.2.12.gemfile +9 -0
  92. data/gemfiles/active_record_3.2.12.gemfile.lock +51 -0
  93. data/gemfiles/active_record_3.2.13.rc1.gemfile +9 -0
  94. data/gemfiles/active_record_3.2.13.rc1.gemfile.lock +51 -0
  95. data/gemfiles/active_record_4.0.0.gemfile +11 -0
  96. data/gemfiles/active_record_4.0.0.gemfile.lock +83 -0
  97. data/gemfiles/data_mapper_0.10.2.gemfile +13 -0
  98. data/gemfiles/data_mapper_0.10.2.gemfile.lock +56 -0
  99. data/gemfiles/data_mapper_0.9.11.gemfile +13 -0
  100. data/gemfiles/data_mapper_0.9.11.gemfile.lock +71 -0
  101. data/gemfiles/data_mapper_0.9.4.gemfile +12 -0
  102. data/gemfiles/data_mapper_0.9.4.gemfile.lock +70 -0
  103. data/gemfiles/data_mapper_0.9.7.gemfile +13 -0
  104. data/gemfiles/data_mapper_0.9.7.gemfile.lock +67 -0
  105. data/gemfiles/data_mapper_1.0.0.gemfile +12 -0
  106. data/gemfiles/data_mapper_1.0.0.gemfile.lock +63 -0
  107. data/gemfiles/data_mapper_1.0.1.gemfile +12 -0
  108. data/gemfiles/data_mapper_1.0.1.gemfile.lock +63 -0
  109. data/gemfiles/data_mapper_1.0.2.gemfile +12 -0
  110. data/gemfiles/data_mapper_1.0.2.gemfile.lock +63 -0
  111. data/gemfiles/data_mapper_1.1.0.gemfile +12 -0
  112. data/gemfiles/data_mapper_1.1.0.gemfile.lock +61 -0
  113. data/gemfiles/data_mapper_1.2.0.gemfile +12 -0
  114. data/gemfiles/data_mapper_1.2.0.gemfile.lock +61 -0
  115. data/gemfiles/default.gemfile +7 -0
  116. data/gemfiles/default.gemfile.lock +27 -0
  117. data/gemfiles/graphviz_0.9.17.gemfile +7 -0
  118. data/gemfiles/graphviz_0.9.17.gemfile.lock +29 -0
  119. data/gemfiles/graphviz_0.9.21.gemfile +7 -0
  120. data/gemfiles/graphviz_0.9.21.gemfile.lock +29 -0
  121. data/gemfiles/graphviz_1.0.0.gemfile +7 -0
  122. data/gemfiles/graphviz_1.0.0.gemfile.lock +29 -0
  123. data/gemfiles/graphviz_1.0.3.gemfile +7 -0
  124. data/gemfiles/graphviz_1.0.3.gemfile.lock +29 -0
  125. data/gemfiles/graphviz_1.0.8.gemfile +7 -0
  126. data/gemfiles/graphviz_1.0.8.gemfile.lock +29 -0
  127. data/gemfiles/mongo_mapper_0.10.0.gemfile +8 -0
  128. data/gemfiles/mongo_mapper_0.10.0.gemfile.lock +47 -0
  129. data/gemfiles/mongo_mapper_0.11.2.gemfile +9 -0
  130. data/gemfiles/mongo_mapper_0.11.2.gemfile.lock +48 -0
  131. data/gemfiles/mongo_mapper_0.12.0.gemfile +9 -0
  132. data/gemfiles/mongo_mapper_0.12.0.gemfile.lock +48 -0
  133. data/gemfiles/mongo_mapper_0.5.5.gemfile +8 -0
  134. data/gemfiles/mongo_mapper_0.5.5.gemfile.lock +36 -0
  135. data/gemfiles/mongo_mapper_0.5.8.gemfile +8 -0
  136. data/gemfiles/mongo_mapper_0.5.8.gemfile.lock +36 -0
  137. data/gemfiles/mongo_mapper_0.6.0.gemfile +8 -0
  138. data/gemfiles/mongo_mapper_0.6.0.gemfile.lock +36 -0
  139. data/gemfiles/mongo_mapper_0.6.10.gemfile +8 -0
  140. data/gemfiles/mongo_mapper_0.6.10.gemfile.lock +36 -0
  141. data/gemfiles/mongo_mapper_0.7.0.gemfile +8 -0
  142. data/gemfiles/mongo_mapper_0.7.0.gemfile.lock +36 -0
  143. data/gemfiles/mongo_mapper_0.7.5.gemfile +8 -0
  144. data/gemfiles/mongo_mapper_0.7.5.gemfile.lock +39 -0
  145. data/gemfiles/mongo_mapper_0.8.0.gemfile +10 -0
  146. data/gemfiles/mongo_mapper_0.8.0.gemfile.lock +43 -0
  147. data/gemfiles/mongo_mapper_0.8.3.gemfile +10 -0
  148. data/gemfiles/mongo_mapper_0.8.3.gemfile.lock +43 -0
  149. data/gemfiles/mongo_mapper_0.8.4.gemfile +8 -0
  150. data/gemfiles/mongo_mapper_0.8.4.gemfile.lock +42 -0
  151. data/gemfiles/mongo_mapper_0.8.6.gemfile +8 -0
  152. data/gemfiles/mongo_mapper_0.8.6.gemfile.lock +42 -0
  153. data/gemfiles/mongo_mapper_0.9.0.gemfile +7 -0
  154. data/gemfiles/mongo_mapper_0.9.0.gemfile.lock +45 -0
  155. data/gemfiles/mongoid_2.0.0.gemfile +9 -0
  156. data/gemfiles/mongoid_2.0.0.gemfile.lock +49 -0
  157. data/gemfiles/mongoid_2.1.4.gemfile +9 -0
  158. data/gemfiles/mongoid_2.1.4.gemfile.lock +47 -0
  159. data/gemfiles/mongoid_2.2.4.gemfile +9 -0
  160. data/gemfiles/mongoid_2.2.4.gemfile.lock +47 -0
  161. data/gemfiles/mongoid_2.3.3.gemfile +9 -0
  162. data/gemfiles/mongoid_2.3.3.gemfile.lock +47 -0
  163. data/gemfiles/mongoid_2.4.0.gemfile +9 -0
  164. data/gemfiles/mongoid_2.4.0.gemfile.lock +47 -0
  165. data/gemfiles/mongoid_2.4.10.gemfile +9 -0
  166. data/gemfiles/mongoid_2.4.10.gemfile.lock +47 -0
  167. data/gemfiles/mongoid_2.5.2.gemfile +9 -0
  168. data/gemfiles/mongoid_2.5.2.gemfile.lock +47 -0
  169. data/gemfiles/mongoid_2.6.0.gemfile +9 -0
  170. data/gemfiles/mongoid_2.6.0.gemfile.lock +47 -0
  171. data/gemfiles/mongoid_3.0.0.gemfile +8 -0
  172. data/gemfiles/mongoid_3.0.0.gemfile.lock +45 -0
  173. data/gemfiles/mongoid_3.0.22.gemfile +8 -0
  174. data/gemfiles/mongoid_3.0.22.gemfile.lock +45 -0
  175. data/gemfiles/mongoid_3.1.0.gemfile +8 -0
  176. data/gemfiles/mongoid_3.1.0.gemfile.lock +45 -0
  177. data/gemfiles/sequel_2.11.0.gemfile +9 -0
  178. data/gemfiles/sequel_2.11.0.gemfile.lock +33 -0
  179. data/gemfiles/sequel_2.12.0.gemfile +9 -0
  180. data/gemfiles/sequel_2.12.0.gemfile.lock +33 -0
  181. data/gemfiles/sequel_2.8.0.gemfile +9 -0
  182. data/gemfiles/sequel_2.8.0.gemfile.lock +33 -0
  183. data/gemfiles/sequel_3.0.0.gemfile +9 -0
  184. data/gemfiles/sequel_3.0.0.gemfile.lock +33 -0
  185. data/gemfiles/sequel_3.10.0.gemfile +9 -0
  186. data/gemfiles/sequel_3.10.0.gemfile.lock +33 -0
  187. data/gemfiles/sequel_3.13.0.gemfile +9 -0
  188. data/gemfiles/sequel_3.13.0.gemfile.lock +33 -0
  189. data/gemfiles/sequel_3.14.0.gemfile +9 -0
  190. data/gemfiles/sequel_3.14.0.gemfile.lock +33 -0
  191. data/gemfiles/sequel_3.23.0.gemfile +9 -0
  192. data/gemfiles/sequel_3.23.0.gemfile.lock +33 -0
  193. data/gemfiles/sequel_3.24.0.gemfile +9 -0
  194. data/gemfiles/sequel_3.24.0.gemfile.lock +33 -0
  195. data/gemfiles/sequel_3.29.0.gemfile +9 -0
  196. data/gemfiles/sequel_3.29.0.gemfile.lock +33 -0
  197. data/gemfiles/sequel_3.34.0.gemfile +9 -0
  198. data/gemfiles/sequel_3.34.0.gemfile.lock +33 -0
  199. data/gemfiles/sequel_3.35.0.gemfile +9 -0
  200. data/gemfiles/sequel_3.35.0.gemfile.lock +33 -0
  201. data/gemfiles/sequel_3.4.0.gemfile +9 -0
  202. data/gemfiles/sequel_3.4.0.gemfile.lock +33 -0
  203. data/gemfiles/sequel_3.44.0.gemfile +9 -0
  204. data/gemfiles/sequel_3.44.0.gemfile.lock +33 -0
  205. data/glebtv_state_machine.gemspec +22 -0
  206. data/init.rb +1 -0
  207. data/lib/glebtv_state_machine.rb +1 -0
  208. data/lib/state_machine.rb +8 -0
  209. data/lib/state_machine/assertions.rb +36 -0
  210. data/lib/state_machine/branch.rb +225 -0
  211. data/lib/state_machine/callback.rb +236 -0
  212. data/lib/state_machine/core.rb +12 -0
  213. data/lib/state_machine/core_ext.rb +2 -0
  214. data/lib/state_machine/core_ext/class/state_machine.rb +5 -0
  215. data/lib/state_machine/error.rb +13 -0
  216. data/lib/state_machine/eval_helpers.rb +87 -0
  217. data/lib/state_machine/event.rb +257 -0
  218. data/lib/state_machine/event_collection.rb +141 -0
  219. data/lib/state_machine/extensions.rb +149 -0
  220. data/lib/state_machine/graph.rb +92 -0
  221. data/lib/state_machine/helper_module.rb +17 -0
  222. data/lib/state_machine/initializers.rb +4 -0
  223. data/lib/state_machine/initializers/merb.rb +1 -0
  224. data/lib/state_machine/initializers/rails.rb +25 -0
  225. data/lib/state_machine/integrations.rb +121 -0
  226. data/lib/state_machine/integrations/active_model.rb +585 -0
  227. data/lib/state_machine/integrations/active_model/locale.rb +11 -0
  228. data/lib/state_machine/integrations/active_model/observer.rb +33 -0
  229. data/lib/state_machine/integrations/active_model/observer_update.rb +42 -0
  230. data/lib/state_machine/integrations/active_model/versions.rb +31 -0
  231. data/lib/state_machine/integrations/active_record.rb +548 -0
  232. data/lib/state_machine/integrations/active_record/locale.rb +20 -0
  233. data/lib/state_machine/integrations/active_record/versions.rb +123 -0
  234. data/lib/state_machine/integrations/base.rb +100 -0
  235. data/lib/state_machine/integrations/data_mapper.rb +511 -0
  236. data/lib/state_machine/integrations/data_mapper/observer.rb +210 -0
  237. data/lib/state_machine/integrations/data_mapper/versions.rb +85 -0
  238. data/lib/state_machine/integrations/mongo_mapper.rb +389 -0
  239. data/lib/state_machine/integrations/mongo_mapper/locale.rb +4 -0
  240. data/lib/state_machine/integrations/mongo_mapper/versions.rb +89 -0
  241. data/lib/state_machine/integrations/mongoid.rb +465 -0
  242. data/lib/state_machine/integrations/mongoid/locale.rb +4 -0
  243. data/lib/state_machine/integrations/mongoid/versions.rb +81 -0
  244. data/lib/state_machine/integrations/sequel.rb +486 -0
  245. data/lib/state_machine/integrations/sequel/versions.rb +95 -0
  246. data/lib/state_machine/machine.rb +2292 -0
  247. data/lib/state_machine/machine_collection.rb +86 -0
  248. data/lib/state_machine/macro_methods.rb +522 -0
  249. data/lib/state_machine/matcher.rb +123 -0
  250. data/lib/state_machine/matcher_helpers.rb +54 -0
  251. data/lib/state_machine/node_collection.rb +222 -0
  252. data/lib/state_machine/path.rb +120 -0
  253. data/lib/state_machine/path_collection.rb +90 -0
  254. data/lib/state_machine/state.rb +297 -0
  255. data/lib/state_machine/state_collection.rb +112 -0
  256. data/lib/state_machine/state_context.rb +138 -0
  257. data/lib/state_machine/transition.rb +470 -0
  258. data/lib/state_machine/transition_collection.rb +245 -0
  259. data/lib/state_machine/version.rb +3 -0
  260. data/lib/state_machine/yard.rb +8 -0
  261. data/lib/state_machine/yard/handlers.rb +12 -0
  262. data/lib/state_machine/yard/handlers/base.rb +32 -0
  263. data/lib/state_machine/yard/handlers/event.rb +25 -0
  264. data/lib/state_machine/yard/handlers/machine.rb +344 -0
  265. data/lib/state_machine/yard/handlers/state.rb +25 -0
  266. data/lib/state_machine/yard/handlers/transition.rb +47 -0
  267. data/lib/state_machine/yard/templates.rb +3 -0
  268. data/lib/state_machine/yard/templates/default/class/html/setup.rb +30 -0
  269. data/lib/state_machine/yard/templates/default/class/html/state_machines.erb +12 -0
  270. data/lib/tasks/state_machine.rake +1 -0
  271. data/lib/tasks/state_machine.rb +30 -0
  272. data/lib/yard-state_machine.rb +2 -0
  273. data/test/files/en.yml +17 -0
  274. data/test/files/switch.rb +15 -0
  275. data/test/functional/state_machine_test.rb +1066 -0
  276. data/test/test_helper.rb +7 -0
  277. data/test/unit/assertions_test.rb +40 -0
  278. data/test/unit/branch_test.rb +969 -0
  279. data/test/unit/callback_test.rb +704 -0
  280. data/test/unit/error_test.rb +43 -0
  281. data/test/unit/eval_helpers_test.rb +270 -0
  282. data/test/unit/event_collection_test.rb +398 -0
  283. data/test/unit/event_test.rb +1196 -0
  284. data/test/unit/graph_test.rb +98 -0
  285. data/test/unit/helper_module_test.rb +17 -0
  286. data/test/unit/integrations/active_model_test.rb +1245 -0
  287. data/test/unit/integrations/active_record_test.rb +2551 -0
  288. data/test/unit/integrations/base_test.rb +104 -0
  289. data/test/unit/integrations/data_mapper_test.rb +2194 -0
  290. data/test/unit/integrations/mongo_mapper_test.rb +2026 -0
  291. data/test/unit/integrations/mongoid_test.rb +2309 -0
  292. data/test/unit/integrations/sequel_test.rb +1896 -0
  293. data/test/unit/integrations_test.rb +83 -0
  294. data/test/unit/invalid_event_test.rb +20 -0
  295. data/test/unit/invalid_parallel_transition_test.rb +18 -0
  296. data/test/unit/invalid_transition_test.rb +115 -0
  297. data/test/unit/machine_collection_test.rb +603 -0
  298. data/test/unit/machine_test.rb +3407 -0
  299. data/test/unit/matcher_helpers_test.rb +37 -0
  300. data/test/unit/matcher_test.rb +155 -0
  301. data/test/unit/node_collection_test.rb +362 -0
  302. data/test/unit/path_collection_test.rb +266 -0
  303. data/test/unit/path_test.rb +485 -0
  304. data/test/unit/state_collection_test.rb +352 -0
  305. data/test/unit/state_context_test.rb +441 -0
  306. data/test/unit/state_machine_test.rb +31 -0
  307. data/test/unit/state_test.rb +1101 -0
  308. data/test/unit/transition_collection_test.rb +2168 -0
  309. data/test/unit/transition_test.rb +1558 -0
  310. metadata +438 -0
@@ -0,0 +1,7 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start { add_filter '/test/' }
4
+ end
5
+
6
+ require 'test/unit'
7
+ require 'state_machine'
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class AssertionsBaseTest < Test::Unit::TestCase
4
+ include StateMachine::Assertions
5
+
6
+ def default_test
7
+ end
8
+ end
9
+
10
+ class AssertValidKeysTest < AssertionsBaseTest
11
+ def test_should_not_raise_exception_if_key_is_valid
12
+ assert_nothing_raised { assert_valid_keys({:name => 'foo', :value => 'bar'}, :name, :value, :force) }
13
+ end
14
+
15
+ def test_should_raise_exception_if_key_is_invalid
16
+ exception = assert_raise(ArgumentError) { assert_valid_keys({:name => 'foo', :value => 'bar', :invalid => true}, :name, :value, :force) }
17
+ assert_equal 'Invalid key(s): invalid', exception.message
18
+ end
19
+ end
20
+
21
+ class AssertExclusiveKeysTest < AssertionsBaseTest
22
+ def test_should_not_raise_exception_if_no_keys_found
23
+ assert_nothing_raised { assert_exclusive_keys({:on => :park}, :only, :except) }
24
+ end
25
+
26
+ def test_should_not_raise_exception_if_one_key_found
27
+ assert_nothing_raised { assert_exclusive_keys({:only => :parked}, :only, :except) }
28
+ assert_nothing_raised { assert_exclusive_keys({:except => :parked}, :only, :except) }
29
+ end
30
+
31
+ def test_should_raise_exception_if_two_keys_found
32
+ exception = assert_raise(ArgumentError) { assert_exclusive_keys({:only => :parked, :except => :parked}, :only, :except) }
33
+ assert_equal 'Conflicting keys: only, except', exception.message
34
+ end
35
+
36
+ def test_should_raise_exception_if_multiple_keys_found
37
+ exception = assert_raise(ArgumentError) { assert_exclusive_keys({:only => :parked, :except => :parked, :on => :park}, :only, :except, :with) }
38
+ assert_equal 'Conflicting keys: only, except', exception.message
39
+ end
40
+ end
@@ -0,0 +1,969 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class BranchTest < Test::Unit::TestCase
4
+ def setup
5
+ @branch = StateMachine::Branch.new(:from => :parked, :to => :idling)
6
+ end
7
+
8
+ def test_should_not_raise_exception_if_implicit_option_specified
9
+ assert_nothing_raised { StateMachine::Branch.new(:invalid => :valid) }
10
+ end
11
+
12
+ def test_should_not_have_an_if_condition
13
+ assert_nil @branch.if_condition
14
+ end
15
+
16
+ def test_should_not_have_an_unless_condition
17
+ assert_nil @branch.unless_condition
18
+ end
19
+
20
+ def test_should_have_a_state_requirement
21
+ assert_equal 1, @branch.state_requirements.length
22
+ end
23
+
24
+ def test_should_raise_an_exception_if_invalid_match_option_specified
25
+ exception = assert_raise(ArgumentError) { @branch.match(Object.new, :invalid => true) }
26
+ assert_equal 'Invalid key(s): invalid', exception.message
27
+ end
28
+ end
29
+
30
+ class BranchWithNoRequirementsTest < Test::Unit::TestCase
31
+ def setup
32
+ @object = Object.new
33
+ @branch = StateMachine::Branch.new
34
+ end
35
+
36
+ def test_should_use_all_matcher_for_event_requirement
37
+ assert_equal StateMachine::AllMatcher.instance, @branch.event_requirement
38
+ end
39
+
40
+ def test_should_use_all_matcher_for_from_state_requirement
41
+ assert_equal StateMachine::AllMatcher.instance, @branch.state_requirements.first[:from]
42
+ end
43
+
44
+ def test_should_use_all_matcher_for_to_state_requirement
45
+ assert_equal StateMachine::AllMatcher.instance, @branch.state_requirements.first[:to]
46
+ end
47
+
48
+ def test_should_match_empty_query
49
+ assert @branch.matches?(@object, {})
50
+ end
51
+
52
+ def test_should_match_non_empty_query
53
+ assert @branch.matches?(@object, :to => :idling, :from => :parked, :on => :ignite)
54
+ end
55
+
56
+ def test_should_include_all_requirements_in_match
57
+ match = @branch.match(@object, {})
58
+
59
+ assert_equal @branch.state_requirements.first[:from], match[:from]
60
+ assert_equal @branch.state_requirements.first[:to], match[:to]
61
+ assert_equal @branch.event_requirement, match[:on]
62
+ end
63
+ end
64
+
65
+ class BranchWithFromRequirementTest < Test::Unit::TestCase
66
+ def setup
67
+ @object = Object.new
68
+ @branch = StateMachine::Branch.new(:from => :parked)
69
+ end
70
+
71
+ def test_should_use_a_whitelist_matcher
72
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
73
+ end
74
+
75
+ def test_should_match_if_not_specified
76
+ assert @branch.matches?(@object, :to => :idling)
77
+ end
78
+
79
+ def test_should_match_if_included
80
+ assert @branch.matches?(@object, :from => :parked)
81
+ end
82
+
83
+ def test_should_not_match_if_not_included
84
+ assert !@branch.matches?(@object, :from => :idling)
85
+ end
86
+
87
+ def test_should_not_match_if_nil
88
+ assert !@branch.matches?(@object, :from => nil)
89
+ end
90
+
91
+ def test_should_ignore_to
92
+ assert @branch.matches?(@object, :from => :parked, :to => :idling)
93
+ end
94
+
95
+ def test_should_ignore_on
96
+ assert @branch.matches?(@object, :from => :parked, :on => :ignite)
97
+ end
98
+
99
+ def test_should_be_included_in_known_states
100
+ assert_equal [:parked], @branch.known_states
101
+ end
102
+
103
+ def test_should_include_requirement_in_match
104
+ match = @branch.match(@object, :from => :parked)
105
+ assert_equal @branch.state_requirements.first[:from], match[:from]
106
+ end
107
+ end
108
+
109
+ class BranchWithMultipleFromRequirementsTest < Test::Unit::TestCase
110
+ def setup
111
+ @object = Object.new
112
+ @branch = StateMachine::Branch.new(:from => [:idling, :parked])
113
+ end
114
+
115
+ def test_should_match_if_included
116
+ assert @branch.matches?(@object, :from => :idling)
117
+ end
118
+
119
+ def test_should_not_match_if_not_included
120
+ assert !@branch.matches?(@object, :from => :first_gear)
121
+ end
122
+
123
+ def test_should_be_included_in_known_states
124
+ assert_equal [:idling, :parked], @branch.known_states
125
+ end
126
+ end
127
+
128
+ class BranchWithFromMatcherRequirementTest < Test::Unit::TestCase
129
+ def setup
130
+ @object = Object.new
131
+ @branch = StateMachine::Branch.new(:from => StateMachine::BlacklistMatcher.new([:idling, :parked]))
132
+ end
133
+
134
+ def test_should_match_if_included
135
+ assert @branch.matches?(@object, :from => :first_gear)
136
+ end
137
+
138
+ def test_should_not_match_if_not_included
139
+ assert !@branch.matches?(@object, :from => :idling)
140
+ end
141
+
142
+ def test_include_values_in_known_states
143
+ assert_equal [:idling, :parked], @branch.known_states
144
+ end
145
+ end
146
+
147
+ class BranchWithToRequirementTest < Test::Unit::TestCase
148
+ def setup
149
+ @object = Object.new
150
+ @branch = StateMachine::Branch.new(:to => :idling)
151
+ end
152
+
153
+ def test_should_use_a_whitelist_matcher
154
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
155
+ end
156
+
157
+ def test_should_match_if_not_specified
158
+ assert @branch.matches?(@object, :from => :parked)
159
+ end
160
+
161
+ def test_should_match_if_included
162
+ assert @branch.matches?(@object, :to => :idling)
163
+ end
164
+
165
+ def test_should_not_match_if_not_included
166
+ assert !@branch.matches?(@object, :to => :parked)
167
+ end
168
+
169
+ def test_should_not_match_if_nil
170
+ assert !@branch.matches?(@object, :to => nil)
171
+ end
172
+
173
+ def test_should_ignore_from
174
+ assert @branch.matches?(@object, :to => :idling, :from => :parked)
175
+ end
176
+
177
+ def test_should_ignore_on
178
+ assert @branch.matches?(@object, :to => :idling, :on => :ignite)
179
+ end
180
+
181
+ def test_should_be_included_in_known_states
182
+ assert_equal [:idling], @branch.known_states
183
+ end
184
+
185
+ def test_should_include_requirement_in_match
186
+ match = @branch.match(@object, :to => :idling)
187
+ assert_equal @branch.state_requirements.first[:to], match[:to]
188
+ end
189
+ end
190
+
191
+ class BranchWithMultipleToRequirementsTest < Test::Unit::TestCase
192
+ def setup
193
+ @object = Object.new
194
+ @branch = StateMachine::Branch.new(:to => [:idling, :parked])
195
+ end
196
+
197
+ def test_should_match_if_included
198
+ assert @branch.matches?(@object, :to => :idling)
199
+ end
200
+
201
+ def test_should_not_match_if_not_included
202
+ assert !@branch.matches?(@object, :to => :first_gear)
203
+ end
204
+
205
+ def test_should_be_included_in_known_states
206
+ assert_equal [:idling, :parked], @branch.known_states
207
+ end
208
+ end
209
+
210
+ class BranchWithToMatcherRequirementTest < Test::Unit::TestCase
211
+ def setup
212
+ @object = Object.new
213
+ @branch = StateMachine::Branch.new(:to => StateMachine::BlacklistMatcher.new([:idling, :parked]))
214
+ end
215
+
216
+ def test_should_match_if_included
217
+ assert @branch.matches?(@object, :to => :first_gear)
218
+ end
219
+
220
+ def test_should_not_match_if_not_included
221
+ assert !@branch.matches?(@object, :to => :idling)
222
+ end
223
+
224
+ def test_include_values_in_known_states
225
+ assert_equal [:idling, :parked], @branch.known_states
226
+ end
227
+ end
228
+
229
+ class BranchWithOnRequirementTest < Test::Unit::TestCase
230
+ def setup
231
+ @object = Object.new
232
+ @branch = StateMachine::Branch.new(:on => :ignite)
233
+ end
234
+
235
+ def test_should_use_a_whitelist_matcher
236
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.event_requirement
237
+ end
238
+
239
+ def test_should_match_if_not_specified
240
+ assert @branch.matches?(@object, :from => :parked)
241
+ end
242
+
243
+ def test_should_match_if_included
244
+ assert @branch.matches?(@object, :on => :ignite)
245
+ end
246
+
247
+ def test_should_not_match_if_not_included
248
+ assert !@branch.matches?(@object, :on => :park)
249
+ end
250
+
251
+ def test_should_not_match_if_nil
252
+ assert !@branch.matches?(@object, :on => nil)
253
+ end
254
+
255
+ def test_should_ignore_to
256
+ assert @branch.matches?(@object, :on => :ignite, :to => :parked)
257
+ end
258
+
259
+ def test_should_ignore_from
260
+ assert @branch.matches?(@object, :on => :ignite, :from => :parked)
261
+ end
262
+
263
+ def test_should_not_be_included_in_known_states
264
+ assert_equal [], @branch.known_states
265
+ end
266
+
267
+ def test_should_include_requirement_in_match
268
+ match = @branch.match(@object, :on => :ignite)
269
+ assert_equal @branch.event_requirement, match[:on]
270
+ end
271
+ end
272
+
273
+ class BranchWithMultipleOnRequirementsTest < Test::Unit::TestCase
274
+ def setup
275
+ @object = Object.new
276
+ @branch = StateMachine::Branch.new(:on => [:ignite, :park])
277
+ end
278
+
279
+ def test_should_match_if_included
280
+ assert @branch.matches?(@object, :on => :ignite)
281
+ end
282
+
283
+ def test_should_not_match_if_not_included
284
+ assert !@branch.matches?(@object, :on => :shift_up)
285
+ end
286
+ end
287
+
288
+ class BranchWithOnMatcherRequirementTest < Test::Unit::TestCase
289
+ def setup
290
+ @object = Object.new
291
+ @branch = StateMachine::Branch.new(:on => StateMachine::BlacklistMatcher.new([:ignite, :park]))
292
+ end
293
+
294
+ def test_should_match_if_included
295
+ assert @branch.matches?(@object, :on => :shift_up)
296
+ end
297
+
298
+ def test_should_not_match_if_not_included
299
+ assert !@branch.matches?(@object, :on => :ignite)
300
+ end
301
+ end
302
+
303
+ class BranchWithExceptFromRequirementTest < Test::Unit::TestCase
304
+ def setup
305
+ @object = Object.new
306
+ @branch = StateMachine::Branch.new(:except_from => :parked)
307
+ end
308
+
309
+ def test_should_use_a_blacklist_matcher
310
+ assert_instance_of StateMachine::BlacklistMatcher, @branch.state_requirements.first[:from]
311
+ end
312
+
313
+ def test_should_match_if_not_included
314
+ assert @branch.matches?(@object, :from => :idling)
315
+ end
316
+
317
+ def test_should_not_match_if_included
318
+ assert !@branch.matches?(@object, :from => :parked)
319
+ end
320
+
321
+ def test_should_match_if_nil
322
+ assert @branch.matches?(@object, :from => nil)
323
+ end
324
+
325
+ def test_should_ignore_to
326
+ assert @branch.matches?(@object, :from => :idling, :to => :parked)
327
+ end
328
+
329
+ def test_should_ignore_on
330
+ assert @branch.matches?(@object, :from => :idling, :on => :ignite)
331
+ end
332
+
333
+ def test_should_be_included_in_known_states
334
+ assert_equal [:parked], @branch.known_states
335
+ end
336
+ end
337
+
338
+ class BranchWithMultipleExceptFromRequirementsTest < Test::Unit::TestCase
339
+ def setup
340
+ @object = Object.new
341
+ @branch = StateMachine::Branch.new(:except_from => [:idling, :parked])
342
+ end
343
+
344
+ def test_should_match_if_not_included
345
+ assert @branch.matches?(@object, :from => :first_gear)
346
+ end
347
+
348
+ def test_should_not_match_if_included
349
+ assert !@branch.matches?(@object, :from => :idling)
350
+ end
351
+
352
+ def test_should_be_included_in_known_states
353
+ assert_equal [:idling, :parked], @branch.known_states
354
+ end
355
+ end
356
+
357
+ class BranchWithExceptFromMatcherRequirementTest < Test::Unit::TestCase
358
+ def test_should_raise_an_exception
359
+ exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:except_from => StateMachine::AllMatcher.instance) }
360
+ assert_equal ':except_from option cannot use matchers; use :from instead', exception.message
361
+ end
362
+ end
363
+
364
+ class BranchWithExceptToRequirementTest < Test::Unit::TestCase
365
+ def setup
366
+ @object = Object.new
367
+ @branch = StateMachine::Branch.new(:except_to => :idling)
368
+ end
369
+
370
+ def test_should_use_a_blacklist_matcher
371
+ assert_instance_of StateMachine::BlacklistMatcher, @branch.state_requirements.first[:to]
372
+ end
373
+
374
+ def test_should_match_if_not_included
375
+ assert @branch.matches?(@object, :to => :parked)
376
+ end
377
+
378
+ def test_should_not_match_if_included
379
+ assert !@branch.matches?(@object, :to => :idling)
380
+ end
381
+
382
+ def test_should_match_if_nil
383
+ assert @branch.matches?(@object, :to => nil)
384
+ end
385
+
386
+ def test_should_ignore_from
387
+ assert @branch.matches?(@object, :to => :parked, :from => :idling)
388
+ end
389
+
390
+ def test_should_ignore_on
391
+ assert @branch.matches?(@object, :to => :parked, :on => :ignite)
392
+ end
393
+
394
+ def test_should_be_included_in_known_states
395
+ assert_equal [:idling], @branch.known_states
396
+ end
397
+ end
398
+
399
+ class BranchWithMultipleExceptToRequirementsTest < Test::Unit::TestCase
400
+ def setup
401
+ @object = Object.new
402
+ @branch = StateMachine::Branch.new(:except_to => [:idling, :parked])
403
+ end
404
+
405
+ def test_should_match_if_not_included
406
+ assert @branch.matches?(@object, :to => :first_gear)
407
+ end
408
+
409
+ def test_should_not_match_if_included
410
+ assert !@branch.matches?(@object, :to => :idling)
411
+ end
412
+
413
+ def test_should_be_included_in_known_states
414
+ assert_equal [:idling, :parked], @branch.known_states
415
+ end
416
+ end
417
+
418
+ class BranchWithExceptToMatcherRequirementTest < Test::Unit::TestCase
419
+ def test_should_raise_an_exception
420
+ exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:except_to => StateMachine::AllMatcher.instance) }
421
+ assert_equal ':except_to option cannot use matchers; use :to instead', exception.message
422
+ end
423
+ end
424
+
425
+ class BranchWithExceptOnRequirementTest < Test::Unit::TestCase
426
+ def setup
427
+ @object = Object.new
428
+ @branch = StateMachine::Branch.new(:except_on => :ignite)
429
+ end
430
+
431
+ def test_should_use_a_blacklist_matcher
432
+ assert_instance_of StateMachine::BlacklistMatcher, @branch.event_requirement
433
+ end
434
+
435
+ def test_should_match_if_not_included
436
+ assert @branch.matches?(@object, :on => :park)
437
+ end
438
+
439
+ def test_should_not_match_if_included
440
+ assert !@branch.matches?(@object, :on => :ignite)
441
+ end
442
+
443
+ def test_should_match_if_nil
444
+ assert @branch.matches?(@object, :on => nil)
445
+ end
446
+
447
+ def test_should_ignore_to
448
+ assert @branch.matches?(@object, :on => :park, :to => :idling)
449
+ end
450
+
451
+ def test_should_ignore_from
452
+ assert @branch.matches?(@object, :on => :park, :from => :parked)
453
+ end
454
+
455
+ def test_should_not_be_included_in_known_states
456
+ assert_equal [], @branch.known_states
457
+ end
458
+ end
459
+
460
+ class BranchWithExceptOnMatcherRequirementTest < Test::Unit::TestCase
461
+ def test_should_raise_an_exception
462
+ exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:except_on => StateMachine::AllMatcher.instance) }
463
+ assert_equal ':except_on option cannot use matchers; use :on instead', exception.message
464
+ end
465
+ end
466
+
467
+ class BranchWithMultipleExceptOnRequirementsTest < Test::Unit::TestCase
468
+ def setup
469
+ @object = Object.new
470
+ @branch = StateMachine::Branch.new(:except_on => [:ignite, :park])
471
+ end
472
+
473
+ def test_should_match_if_not_included
474
+ assert @branch.matches?(@object, :on => :shift_up)
475
+ end
476
+
477
+ def test_should_not_match_if_included
478
+ assert !@branch.matches?(@object, :on => :ignite)
479
+ end
480
+ end
481
+
482
+ class BranchWithConflictingFromRequirementsTest < Test::Unit::TestCase
483
+ def test_should_raise_an_exception
484
+ exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:from => :parked, :except_from => :parked) }
485
+ assert_equal 'Conflicting keys: from, except_from', exception.message
486
+ end
487
+ end
488
+
489
+ class BranchWithConflictingToRequirementsTest < Test::Unit::TestCase
490
+ def test_should_raise_an_exception
491
+ exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:to => :idling, :except_to => :idling) }
492
+ assert_equal 'Conflicting keys: to, except_to', exception.message
493
+ end
494
+ end
495
+
496
+ class BranchWithConflictingOnRequirementsTest < Test::Unit::TestCase
497
+ def test_should_raise_an_exception
498
+ exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:on => :ignite, :except_on => :ignite) }
499
+ assert_equal 'Conflicting keys: on, except_on', exception.message
500
+ end
501
+ end
502
+
503
+ class BranchWithDifferentRequirementsTest < Test::Unit::TestCase
504
+ def setup
505
+ @object = Object.new
506
+ @branch = StateMachine::Branch.new(:from => :parked, :to => :idling, :on => :ignite)
507
+ end
508
+
509
+ def test_should_match_empty_query
510
+ assert @branch.matches?(@object)
511
+ end
512
+
513
+ def test_should_match_if_all_requirements_match
514
+ assert @branch.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
515
+ end
516
+
517
+ def test_should_not_match_if_from_not_included
518
+ assert !@branch.matches?(@object, :from => :idling)
519
+ end
520
+
521
+ def test_should_not_match_if_to_not_included
522
+ assert !@branch.matches?(@object, :to => :parked)
523
+ end
524
+
525
+ def test_should_not_match_if_on_not_included
526
+ assert !@branch.matches?(@object, :on => :park)
527
+ end
528
+
529
+ def test_should_be_nil_if_unmatched
530
+ assert_nil @branch.match(@object, :from => :parked, :to => :idling, :on => :park)
531
+ end
532
+
533
+ def test_should_include_all_known_states
534
+ assert_equal [:parked, :idling], @branch.known_states
535
+ end
536
+
537
+ def test_should_not_duplicate_known_statse
538
+ branch = StateMachine::Branch.new(:except_from => :idling, :to => :idling, :on => :ignite)
539
+ assert_equal [:idling], branch.known_states
540
+ end
541
+ end
542
+
543
+ class BranchWithNilRequirementsTest < Test::Unit::TestCase
544
+ def setup
545
+ @object = Object.new
546
+ @branch = StateMachine::Branch.new(:from => nil, :to => nil)
547
+ end
548
+
549
+ def test_should_match_empty_query
550
+ assert @branch.matches?(@object)
551
+ end
552
+
553
+ def test_should_match_if_all_requirements_match
554
+ assert @branch.matches?(@object, :from => nil, :to => nil)
555
+ end
556
+
557
+ def test_should_not_match_if_from_not_included
558
+ assert !@branch.matches?(@object, :from => :parked)
559
+ end
560
+
561
+ def test_should_not_match_if_to_not_included
562
+ assert !@branch.matches?(@object, :to => :idling)
563
+ end
564
+
565
+ def test_should_include_all_known_states
566
+ assert_equal [nil], @branch.known_states
567
+ end
568
+ end
569
+
570
+ class BranchWithImplicitRequirementTest < Test::Unit::TestCase
571
+ def setup
572
+ @branch = StateMachine::Branch.new(:parked => :idling, :on => :ignite)
573
+ end
574
+
575
+ def test_should_create_an_event_requirement
576
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.event_requirement
577
+ assert_equal [:ignite], @branch.event_requirement.values
578
+ end
579
+
580
+ def test_should_use_a_whitelist_from_matcher
581
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
582
+ end
583
+
584
+ def test_should_use_a_whitelist_to_matcher
585
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
586
+ end
587
+ end
588
+
589
+ class BranchWithMultipleImplicitRequirementsTest < Test::Unit::TestCase
590
+ def setup
591
+ @object = Object.new
592
+ @branch = StateMachine::Branch.new(:parked => :idling, :idling => :first_gear, :on => :ignite)
593
+ end
594
+
595
+ def test_should_create_multiple_state_requirements
596
+ assert_equal 2, @branch.state_requirements.length
597
+ end
598
+
599
+ def test_should_not_match_event_as_state_requirement
600
+ assert !@branch.matches?(@object, :from => :on, :to => :ignite)
601
+ end
602
+
603
+ def test_should_match_if_from_included_in_any
604
+ assert @branch.matches?(@object, :from => :parked)
605
+ assert @branch.matches?(@object, :from => :idling)
606
+ end
607
+
608
+ def test_should_not_match_if_from_not_included_in_any
609
+ assert !@branch.matches?(@object, :from => :first_gear)
610
+ end
611
+
612
+ def test_should_match_if_to_included_in_any
613
+ assert @branch.matches?(@object, :to => :idling)
614
+ assert @branch.matches?(@object, :to => :first_gear)
615
+ end
616
+
617
+ def test_should_not_match_if_to_not_included_in_any
618
+ assert !@branch.matches?(@object, :to => :parked)
619
+ end
620
+
621
+ def test_should_match_if_all_options_match
622
+ assert @branch.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
623
+ assert @branch.matches?(@object, :from => :idling, :to => :first_gear, :on => :ignite)
624
+ end
625
+
626
+ def test_should_not_match_if_any_options_do_not_match
627
+ assert !@branch.matches?(@object, :from => :parked, :to => :idling, :on => :park)
628
+ assert !@branch.matches?(@object, :from => :parked, :to => :first_gear, :on => :park)
629
+ end
630
+
631
+ def test_should_include_all_known_states
632
+ assert_equal [:first_gear, :idling, :parked], @branch.known_states.sort_by {|state| state.to_s}
633
+ end
634
+
635
+ def test_should_not_duplicate_known_statse
636
+ branch = StateMachine::Branch.new(:parked => :idling, :first_gear => :idling)
637
+ assert_equal [:first_gear, :idling, :parked], branch.known_states.sort_by {|state| state.to_s}
638
+ end
639
+ end
640
+
641
+ class BranchWithImplicitFromRequirementMatcherTest < Test::Unit::TestCase
642
+ def setup
643
+ @matcher = StateMachine::BlacklistMatcher.new(:parked)
644
+ @branch = StateMachine::Branch.new(@matcher => :idling)
645
+ end
646
+
647
+ def test_should_not_convert_from_to_whitelist_matcher
648
+ assert_equal @matcher, @branch.state_requirements.first[:from]
649
+ end
650
+
651
+ def test_should_convert_to_to_whitelist_matcher
652
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
653
+ end
654
+ end
655
+
656
+ class BranchWithImplicitToRequirementMatcherTest < Test::Unit::TestCase
657
+ def setup
658
+ @matcher = StateMachine::BlacklistMatcher.new(:idling)
659
+ @branch = StateMachine::Branch.new(:parked => @matcher)
660
+ end
661
+
662
+ def test_should_convert_from_to_whitelist_matcher
663
+ assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
664
+ end
665
+
666
+ def test_should_not_convert_to_to_whitelist_matcher
667
+ assert_equal @matcher, @branch.state_requirements.first[:to]
668
+ end
669
+ end
670
+
671
+ class BranchWithImplicitAndExplicitRequirementsTest < Test::Unit::TestCase
672
+ def setup
673
+ @branch = StateMachine::Branch.new(:parked => :idling, :from => :parked)
674
+ end
675
+
676
+ def test_should_create_multiple_requirements
677
+ assert_equal 2, @branch.state_requirements.length
678
+ end
679
+
680
+ def test_should_create_implicit_requirements_for_implicit_options
681
+ assert(@branch.state_requirements.any? do |state_requirement|
682
+ state_requirement[:from].values == [:parked] && state_requirement[:to].values == [:idling]
683
+ end)
684
+ end
685
+
686
+ def test_should_create_implicit_requirements_for_explicit_options
687
+ assert(@branch.state_requirements.any? do |state_requirement|
688
+ state_requirement[:from].values == [:from] && state_requirement[:to].values == [:parked]
689
+ end)
690
+ end
691
+ end
692
+
693
+ class BranchWithIfConditionalTest < Test::Unit::TestCase
694
+ def setup
695
+ @object = Object.new
696
+ end
697
+
698
+ def test_should_have_an_if_condition
699
+ branch = StateMachine::Branch.new(:if => lambda {true})
700
+ assert_not_nil branch.if_condition
701
+ end
702
+
703
+ def test_should_match_if_true
704
+ branch = StateMachine::Branch.new(:if => lambda {true})
705
+ assert branch.matches?(@object)
706
+ end
707
+
708
+ def test_should_not_match_if_false
709
+ branch = StateMachine::Branch.new(:if => lambda {false})
710
+ assert !branch.matches?(@object)
711
+ end
712
+
713
+ def test_should_be_nil_if_unmatched
714
+ branch = StateMachine::Branch.new(:if => lambda {false})
715
+ assert_nil branch.match(@object)
716
+ end
717
+ end
718
+
719
+ class BranchWithMultipleIfConditionalsTest < Test::Unit::TestCase
720
+ def setup
721
+ @object = Object.new
722
+ end
723
+
724
+ def test_should_match_if_all_are_true
725
+ branch = StateMachine::Branch.new(:if => [lambda {true}, lambda {true}])
726
+ assert branch.match(@object)
727
+ end
728
+
729
+ def test_should_not_match_if_any_are_false
730
+ branch = StateMachine::Branch.new(:if => [lambda {true}, lambda {false}])
731
+ assert !branch.match(@object)
732
+
733
+ branch = StateMachine::Branch.new(:if => [lambda {false}, lambda {true}])
734
+ assert !branch.match(@object)
735
+ end
736
+ end
737
+
738
+ class BranchWithUnlessConditionalTest < Test::Unit::TestCase
739
+ def setup
740
+ @object = Object.new
741
+ end
742
+
743
+ def test_should_have_an_unless_condition
744
+ branch = StateMachine::Branch.new(:unless => lambda {true})
745
+ assert_not_nil branch.unless_condition
746
+ end
747
+
748
+ def test_should_match_if_false
749
+ branch = StateMachine::Branch.new(:unless => lambda {false})
750
+ assert branch.matches?(@object)
751
+ end
752
+
753
+ def test_should_not_match_if_true
754
+ branch = StateMachine::Branch.new(:unless => lambda {true})
755
+ assert !branch.matches?(@object)
756
+ end
757
+
758
+ def test_should_be_nil_if_unmatched
759
+ branch = StateMachine::Branch.new(:unless => lambda {true})
760
+ assert_nil branch.match(@object)
761
+ end
762
+ end
763
+
764
+ class BranchWithMultipleUnlessConditionalsTest < Test::Unit::TestCase
765
+ def setup
766
+ @object = Object.new
767
+ end
768
+
769
+ def test_should_match_if_all_are_false
770
+ branch = StateMachine::Branch.new(:unless => [lambda {false}, lambda {false}])
771
+ assert branch.match(@object)
772
+ end
773
+
774
+ def test_should_not_match_if_any_are_true
775
+ branch = StateMachine::Branch.new(:unless => [lambda {true}, lambda {false}])
776
+ assert !branch.match(@object)
777
+
778
+ branch = StateMachine::Branch.new(:unless => [lambda {false}, lambda {true}])
779
+ assert !branch.match(@object)
780
+ end
781
+ end
782
+
783
+ class BranchWithConflictingConditionalsTest < Test::Unit::TestCase
784
+ def setup
785
+ @object = Object.new
786
+ end
787
+
788
+ def test_should_match_if_if_is_true_and_unless_is_false
789
+ branch = StateMachine::Branch.new(:if => lambda {true}, :unless => lambda {false})
790
+ assert branch.match(@object)
791
+ end
792
+
793
+ def test_should_not_match_if_if_is_false_and_unless_is_true
794
+ branch = StateMachine::Branch.new(:if => lambda {false}, :unless => lambda {true})
795
+ assert !branch.match(@object)
796
+ end
797
+
798
+ def test_should_not_match_if_if_is_false_and_unless_is_false
799
+ branch = StateMachine::Branch.new(:if => lambda {false}, :unless => lambda {false})
800
+ assert !branch.match(@object)
801
+ end
802
+
803
+ def test_should_not_match_if_if_is_true_and_unless_is_true
804
+ branch = StateMachine::Branch.new(:if => lambda {true}, :unless => lambda {true})
805
+ assert !branch.match(@object)
806
+ end
807
+ end
808
+
809
+ class BranchWithoutGuardsTest < Test::Unit::TestCase
810
+ def setup
811
+ @object = Object.new
812
+ end
813
+
814
+ def test_should_match_if_if_is_false
815
+ branch = StateMachine::Branch.new(:if => lambda {false})
816
+ assert branch.matches?(@object, :guard => false)
817
+ end
818
+
819
+ def test_should_match_if_if_is_true
820
+ branch = StateMachine::Branch.new(:if => lambda {true})
821
+ assert branch.matches?(@object, :guard => false)
822
+ end
823
+
824
+ def test_should_match_if_unless_is_false
825
+ branch = StateMachine::Branch.new(:unless => lambda {false})
826
+ assert branch.matches?(@object, :guard => false)
827
+ end
828
+
829
+ def test_should_match_if_unless_is_true
830
+ branch = StateMachine::Branch.new(:unless => lambda {true})
831
+ assert branch.matches?(@object, :guard => false)
832
+ end
833
+ end
834
+
835
+ begin
836
+ # Load library
837
+ require 'graphviz'
838
+
839
+ class BranchDrawingTest < Test::Unit::TestCase
840
+ def setup
841
+ @machine = StateMachine::Machine.new(Class.new)
842
+ states = [:parked, :idling]
843
+
844
+ @graph = StateMachine::Graph.new('test')
845
+ states.each {|state| @graph.add_nodes(state.to_s)}
846
+
847
+ @branch = StateMachine::Branch.new(:from => :idling, :to => :parked)
848
+ @branch.draw(@graph, :park, states)
849
+ @edge = @graph.get_edge_at_index(0)
850
+ end
851
+
852
+ def test_should_create_edges
853
+ assert_equal 1, @graph.edge_count
854
+ end
855
+
856
+ def test_should_use_from_state_from_start_node
857
+ assert_equal 'idling', @edge.node_one(false)
858
+ end
859
+
860
+ def test_should_use_to_state_for_end_node
861
+ assert_equal 'parked', @edge.node_two(false)
862
+ end
863
+
864
+ def test_should_use_event_name_as_label
865
+ assert_equal 'park', @edge['label'].to_s.gsub('"', '')
866
+ end
867
+ end
868
+
869
+ class BranchDrawingWithFromRequirementTest < Test::Unit::TestCase
870
+ def setup
871
+ @machine = StateMachine::Machine.new(Class.new)
872
+ states = [:parked, :idling, :first_gear]
873
+
874
+ @graph = StateMachine::Graph.new('test')
875
+ states.each {|state| @graph.add_nodes(state.to_s)}
876
+
877
+ @branch = StateMachine::Branch.new(:from => [:idling, :first_gear], :to => :parked)
878
+ @branch.draw(@graph, :park, states)
879
+ end
880
+
881
+ def test_should_generate_edges_for_each_valid_from_state
882
+ [:idling, :first_gear].each_with_index do |from_state, index|
883
+ edge = @graph.get_edge_at_index(index)
884
+ assert_equal from_state.to_s, edge.node_one(false)
885
+ assert_equal 'parked', edge.node_two(false)
886
+ end
887
+ end
888
+ end
889
+
890
+ class BranchDrawingWithExceptFromRequirementTest < Test::Unit::TestCase
891
+ def setup
892
+ @machine = StateMachine::Machine.new(Class.new)
893
+ states = [:parked, :idling, :first_gear]
894
+
895
+ @graph = StateMachine::Graph.new('test')
896
+ states.each {|state| @graph.add_nodes(state.to_s)}
897
+
898
+ @branch = StateMachine::Branch.new(:except_from => :parked, :to => :parked)
899
+ @branch.draw(@graph, :park, states)
900
+ end
901
+
902
+ def test_should_generate_edges_for_each_valid_from_state
903
+ %w(idling first_gear).each_with_index do |from_state, index|
904
+ edge = @graph.get_edge_at_index(index)
905
+ assert_equal from_state, edge.node_one(false)
906
+ assert_equal 'parked', edge.node_two(false)
907
+ end
908
+ end
909
+ end
910
+
911
+ class BranchDrawingWithoutFromRequirementTest < Test::Unit::TestCase
912
+ def setup
913
+ @machine = StateMachine::Machine.new(Class.new)
914
+ states = [:parked, :idling, :first_gear]
915
+
916
+ @graph = StateMachine::Graph.new('test')
917
+ states.each {|state| @graph.add_nodes(state.to_s)}
918
+
919
+ @branch = StateMachine::Branch.new(:to => :parked)
920
+ @branch.draw(@graph, :park, states)
921
+ end
922
+
923
+ def test_should_generate_edges_for_each_valid_from_state
924
+ %w(parked idling first_gear).each_with_index do |from_state, index|
925
+ edge = @graph.get_edge_at_index(index)
926
+ assert_equal from_state, edge.node_one(false)
927
+ assert_equal 'parked', edge.node_two(false)
928
+ end
929
+ end
930
+ end
931
+
932
+ class BranchDrawingWithoutToRequirementTest < Test::Unit::TestCase
933
+ def setup
934
+ @machine = StateMachine::Machine.new(Class.new)
935
+
936
+ graph = StateMachine::Graph.new('test')
937
+ graph.add_nodes('parked')
938
+
939
+ @branch = StateMachine::Branch.new(:from => :parked)
940
+ @branch.draw(graph, :park, [:parked])
941
+ @edge = graph.get_edge_at_index(0)
942
+ end
943
+
944
+ def test_should_create_loopback_edge
945
+ assert_equal 'parked', @edge.node_one(false)
946
+ assert_equal 'parked', @edge.node_two(false)
947
+ end
948
+ end
949
+
950
+ class BranchDrawingWithNilStateTest < Test::Unit::TestCase
951
+ def setup
952
+ @machine = StateMachine::Machine.new(Class.new)
953
+
954
+ graph = StateMachine::Graph.new('test')
955
+ graph.add_nodes('parked')
956
+
957
+ @branch = StateMachine::Branch.new(:from => :idling, :to => nil)
958
+ @branch.draw(graph, :park, [nil, :idling])
959
+ @edge = graph.get_edge_at_index(0)
960
+ end
961
+
962
+ def test_should_generate_edges_for_each_valid_from_state
963
+ assert_equal 'idling', @edge.node_one(false)
964
+ assert_equal 'nil', @edge.node_two(false)
965
+ end
966
+ end
967
+ rescue LoadError
968
+ $stderr.puts 'Skipping GraphViz StateMachine::Branch tests. `gem install ruby-graphviz` >= v0.9.17 and try again.'
969
+ end unless ENV['TRAVIS']