PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/node_collection_test.rb

http://github.com/pluginaweek/state_machine
Ruby | 362 lines | 289 code | 73 blank | 0 comment | 0 complexity | e7efcd2c11ee2bd7b27f381bb13ca4ae MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class Node < Struct.new(:name, :value, :machine)
  3. def context
  4. yield
  5. end
  6. end
  7. class NodeCollectionByDefaultTest < Test::Unit::TestCase
  8. def setup
  9. @machine = StateMachine::Machine.new(Class.new)
  10. @collection = StateMachine::NodeCollection.new(@machine)
  11. end
  12. def test_should_not_have_any_nodes
  13. assert_equal 0, @collection.length
  14. end
  15. def test_should_have_a_machine
  16. assert_equal @machine, @collection.machine
  17. end
  18. def test_should_index_by_name
  19. @collection << object = Node.new(:parked)
  20. assert_equal object, @collection[:parked]
  21. end
  22. end
  23. class NodeCollectionTest < Test::Unit::TestCase
  24. def setup
  25. @machine = StateMachine::Machine.new(Class.new)
  26. @collection = StateMachine::NodeCollection.new(@machine)
  27. end
  28. def test_should_raise_exception_if_invalid_option_specified
  29. exception = assert_raise(ArgumentError) { StateMachine::NodeCollection.new(@machine, :invalid => true) }
  30. assert_equal 'Invalid key(s): invalid', exception.message
  31. end
  32. def test_should_raise_exception_on_lookup_if_invalid_index_specified
  33. exception = assert_raise(ArgumentError) { @collection[:something, :invalid] }
  34. assert_equal 'Invalid index: :invalid', exception.message
  35. end
  36. def test_should_raise_exception_on_fetch_if_invalid_index_specified
  37. exception = assert_raise(ArgumentError) { @collection.fetch(:something, :invalid) }
  38. assert_equal 'Invalid index: :invalid', exception.message
  39. end
  40. end
  41. class NodeCollectionAfterBeingCopiedTest < Test::Unit::TestCase
  42. def setup
  43. machine = StateMachine::Machine.new(Class.new)
  44. @collection = StateMachine::NodeCollection.new(machine)
  45. @collection << @parked = Node.new(:parked)
  46. @contexts_run = contexts_run = []
  47. @collection.context([:parked]) {contexts_run << :parked}
  48. @contexts_run.clear
  49. @copied_collection = @collection.dup
  50. @copied_collection << @idling = Node.new(:idling)
  51. @copied_collection.context([:first_gear]) {contexts_run << :first_gear}
  52. end
  53. def test_should_not_modify_the_original_list
  54. assert_equal 1, @collection.length
  55. assert_equal 2, @copied_collection.length
  56. end
  57. def test_should_not_modify_the_indices
  58. assert_nil @collection[:idling]
  59. assert_equal @idling, @copied_collection[:idling]
  60. end
  61. def test_should_copy_each_node
  62. assert_not_same @parked, @copied_collection[:parked]
  63. end
  64. def test_should_not_run_contexts
  65. assert_equal [], @contexts_run
  66. end
  67. def test_should_not_modify_contexts
  68. @collection << Node.new(:first_gear)
  69. assert_equal [], @contexts_run
  70. end
  71. def test_should_copy_contexts
  72. @copied_collection << Node.new(:parked)
  73. assert !@contexts_run.empty?
  74. end
  75. end
  76. class NodeCollectionWithoutIndicesTest < Test::Unit::TestCase
  77. def setup
  78. machine = StateMachine::Machine.new(Class.new)
  79. @collection = StateMachine::NodeCollection.new(machine, :index => {})
  80. end
  81. def test_should_allow_adding_node
  82. @collection << Object.new
  83. assert_equal 1, @collection.length
  84. end
  85. def test_should_not_allow_keys_retrieval
  86. exception = assert_raise(ArgumentError) { @collection.keys }
  87. assert_equal 'No indices configured', exception.message
  88. end
  89. def test_should_not_allow_lookup
  90. @collection << Object.new
  91. exception = assert_raise(ArgumentError) { @collection[0] }
  92. assert_equal 'No indices configured', exception.message
  93. end
  94. def test_should_not_allow_fetching
  95. @collection << Object.new
  96. exception = assert_raise(ArgumentError) { @collection.fetch(0) }
  97. assert_equal 'No indices configured', exception.message
  98. end
  99. end
  100. class NodeCollectionWithIndicesTest < Test::Unit::TestCase
  101. def setup
  102. machine = StateMachine::Machine.new(Class.new)
  103. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  104. @object = Node.new(:parked, 1)
  105. @collection << @object
  106. end
  107. def test_should_use_first_index_by_default_on_key_retrieval
  108. assert_equal [:parked], @collection.keys
  109. end
  110. def test_should_allow_customizing_index_for_key_retrieval
  111. assert_equal [1], @collection.keys(:value)
  112. end
  113. def test_should_use_first_index_by_default_on_lookup
  114. assert_equal @object, @collection[:parked]
  115. assert_nil @collection[1]
  116. end
  117. def test_should_allow_customizing_index_on_lookup
  118. assert_equal @object, @collection[1, :value]
  119. assert_nil @collection[:parked, :value]
  120. end
  121. def test_should_use_first_index_by_default_on_fetch
  122. assert_equal @object, @collection.fetch(:parked)
  123. exception = assert_raise(IndexError) { @collection.fetch(1) }
  124. assert_equal '1 is an invalid name', exception.message
  125. end
  126. def test_should_allow_customizing_index_on_fetch
  127. assert_equal @object, @collection.fetch(1, :value)
  128. exception = assert_raise(IndexError) { @collection.fetch(:parked, :value) }
  129. assert_equal ':parked is an invalid value', exception.message
  130. end
  131. end
  132. class NodeCollectionWithNodesTest < Test::Unit::TestCase
  133. def setup
  134. @machine = StateMachine::Machine.new(Class.new)
  135. @collection = StateMachine::NodeCollection.new(@machine)
  136. @parked = Node.new(:parked, nil, @machine)
  137. @idling = Node.new(:idling, nil, @machine)
  138. @collection << @parked
  139. @collection << @idling
  140. end
  141. def test_should_be_able_to_enumerate
  142. order = []
  143. @collection.each {|object| order << object}
  144. assert_equal [@parked, @idling], order
  145. end
  146. def test_should_be_able_to_concatenate_multiple_nodes
  147. @first_gear = Node.new(:first_gear, nil, @machine)
  148. @second_gear = Node.new(:second_gear, nil, @machine)
  149. @collection.concat([@first_gear, @second_gear])
  150. order = []
  151. @collection.each {|object| order << object}
  152. assert_equal [@parked, @idling, @first_gear, @second_gear], order
  153. end
  154. def test_should_be_able_to_access_by_index
  155. assert_equal @parked, @collection.at(0)
  156. assert_equal @idling, @collection.at(1)
  157. end
  158. def test_should_deep_copy_machine_changes
  159. new_machine = StateMachine::Machine.new(Class.new)
  160. @collection.machine = new_machine
  161. assert_equal new_machine, @collection.machine
  162. assert_equal new_machine, @parked.machine
  163. assert_equal new_machine, @idling.machine
  164. end
  165. end
  166. class NodeCollectionAfterUpdateTest < Test::Unit::TestCase
  167. def setup
  168. machine = StateMachine::Machine.new(Class.new)
  169. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  170. @parked = Node.new(:parked, 1)
  171. @idling = Node.new(:idling, 2)
  172. @collection << @parked << @idling
  173. @parked.name = :parking
  174. @parked.value = 0
  175. @collection.update(@parked)
  176. end
  177. def test_should_not_change_the_index
  178. assert_equal @parked, @collection.at(0)
  179. end
  180. def test_should_not_duplicate_in_the_collection
  181. assert_equal 2, @collection.length
  182. end
  183. def test_should_add_each_indexed_key
  184. assert_equal @parked, @collection[:parking]
  185. assert_equal @parked, @collection[0, :value]
  186. end
  187. def test_should_remove_each_old_indexed_key
  188. assert_nil @collection[:parked]
  189. assert_nil @collection[1, :value]
  190. end
  191. end
  192. class NodeCollectionWithStringIndexTest < Test::Unit::TestCase
  193. def setup
  194. machine = StateMachine::Machine.new(Class.new)
  195. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  196. @parked = Node.new(:parked, 1)
  197. @collection << @parked
  198. end
  199. def test_should_index_by_name
  200. assert_equal @parked, @collection[:parked]
  201. end
  202. def test_should_index_by_string_name
  203. assert_equal @parked, @collection['parked']
  204. end
  205. end
  206. class NodeCollectionWithSymbolIndexTest < Test::Unit::TestCase
  207. def setup
  208. machine = StateMachine::Machine.new(Class.new)
  209. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  210. @parked = Node.new('parked', 1)
  211. @collection << @parked
  212. end
  213. def test_should_index_by_name
  214. assert_equal @parked, @collection['parked']
  215. end
  216. def test_should_index_by_symbol_name
  217. assert_equal @parked, @collection[:parked]
  218. end
  219. end
  220. class NodeCollectionWithNumericIndexTest < Test::Unit::TestCase
  221. def setup
  222. machine = StateMachine::Machine.new(Class.new)
  223. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  224. @parked = Node.new(10, 1)
  225. @collection << @parked
  226. end
  227. def test_should_index_by_name
  228. assert_equal @parked, @collection[10]
  229. end
  230. def test_should_index_by_string_name
  231. assert_equal @parked, @collection['10']
  232. end
  233. def test_should_index_by_symbol_name
  234. assert_equal @parked, @collection[:'10']
  235. end
  236. end
  237. class NodeCollectionWithPredefinedContextsTest < Test::Unit::TestCase
  238. def setup
  239. machine = StateMachine::Machine.new(Class.new)
  240. @collection = StateMachine::NodeCollection.new(machine)
  241. @contexts_run = contexts_run = []
  242. @collection.context([:parked]) { contexts_run << :parked }
  243. @collection.context([:parked]) { contexts_run << :second_parked }
  244. end
  245. def test_should_run_contexts_in_the_order_defined
  246. @collection << Node.new(:parked)
  247. assert_equal [:parked, :second_parked], @contexts_run
  248. end
  249. def test_should_not_run_contexts_if_not_matched
  250. @collection << Node.new(:idling)
  251. assert_equal [], @contexts_run
  252. end
  253. end
  254. class NodeCollectionWithPostdefinedContextsTest < Test::Unit::TestCase
  255. def setup
  256. machine = StateMachine::Machine.new(Class.new)
  257. @collection = StateMachine::NodeCollection.new(machine)
  258. @collection << Node.new(:parked)
  259. end
  260. def test_should_run_context_if_matched
  261. contexts_run = []
  262. @collection.context([:parked]) { contexts_run << :parked }
  263. assert_equal [:parked], contexts_run
  264. end
  265. def test_should_not_run_contexts_if_not_matched
  266. contexts_run = []
  267. @collection.context([:idling]) { contexts_run << :idling }
  268. assert_equal [], contexts_run
  269. end
  270. end
  271. class NodeCollectionWithMatcherContextsTest < Test::Unit::TestCase
  272. def setup
  273. machine = StateMachine::Machine.new(Class.new)
  274. @collection = StateMachine::NodeCollection.new(machine)
  275. @collection << Node.new(:parked)
  276. end
  277. def test_should_always_run_all_matcher_context
  278. contexts_run = []
  279. @collection.context([StateMachine::AllMatcher.instance]) { contexts_run << :all }
  280. assert_equal [:all], contexts_run
  281. end
  282. def test_should_only_run_blacklist_matcher_if_not_matched
  283. contexts_run = []
  284. @collection.context([StateMachine::BlacklistMatcher.new([:parked])]) { contexts_run << :blacklist }
  285. assert_equal [], contexts_run
  286. @collection << Node.new(:idling)
  287. assert_equal [:blacklist], contexts_run
  288. end
  289. end