PageRenderTime 78ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/node_collection_test.rb

https://github.com/johnsonzes/state_machine
Ruby | 217 lines | 173 code | 44 blank | 0 comment | 0 complexity | 8e21952f3a96716675cf8b6fc6a80c91 MD5 | raw file
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class NodeCollectionByDefaultTest < Test::Unit::TestCase
  3. def setup
  4. @machine = StateMachine::Machine.new(Class.new)
  5. @collection = StateMachine::NodeCollection.new(@machine)
  6. end
  7. def test_should_not_have_any_nodes
  8. assert_equal 0, @collection.length
  9. end
  10. def test_should_have_a_machine
  11. assert_equal @machine, @collection.machine
  12. end
  13. def test_should_index_by_name
  14. @collection << object = Struct.new(:name).new(:parked)
  15. assert_equal object, @collection[:parked]
  16. end
  17. end
  18. class NodeCollectionTest < Test::Unit::TestCase
  19. def setup
  20. @machine = StateMachine::Machine.new(Class.new)
  21. @collection = StateMachine::NodeCollection.new(@machine)
  22. end
  23. def test_should_raise_exception_if_invalid_option_specified
  24. exception = assert_raise(ArgumentError) { StateMachine::NodeCollection.new(@machine, :invalid => true) }
  25. assert_equal 'Invalid key(s): invalid', exception.message
  26. end
  27. def test_should_raise_exception_on_lookup_if_invalid_index_specified
  28. exception = assert_raise(ArgumentError) { @collection[:something, :invalid] }
  29. assert_equal 'Invalid index: :invalid', exception.message
  30. end
  31. def test_should_raise_exception_on_fetch_if_invalid_index_specified
  32. exception = assert_raise(ArgumentError) { @collection.fetch(:something, :invalid) }
  33. assert_equal 'Invalid index: :invalid', exception.message
  34. end
  35. end
  36. class NodeCollectionAfterBeingCopiedTest < Test::Unit::TestCase
  37. def setup
  38. machine = StateMachine::Machine.new(Class.new)
  39. @collection = StateMachine::NodeCollection.new(machine)
  40. @collection << @parked = Struct.new(:name).new(:parked)
  41. @copied_collection = @collection.dup
  42. @copied_collection << @idling = Struct.new(:name).new(:idling)
  43. end
  44. def test_should_not_modify_the_original_list
  45. assert_equal 1, @collection.length
  46. assert_equal 2, @copied_collection.length
  47. end
  48. def test_should_not_modify_the_indices
  49. assert_nil @collection[:idling]
  50. assert_equal @idling, @copied_collection[:idling]
  51. end
  52. def test_should_copy_each_node
  53. assert_not_same @parked, @copied_collection[:parked]
  54. end
  55. end
  56. class NodeCollectionWithoutIndicesTest < Test::Unit::TestCase
  57. def setup
  58. machine = StateMachine::Machine.new(Class.new)
  59. @collection = StateMachine::NodeCollection.new(machine, :index => {})
  60. end
  61. def test_should_allow_adding_node
  62. @collection << Object.new
  63. assert_equal 1, @collection.length
  64. end
  65. def test_should_not_allow_keys_retrieval
  66. exception = assert_raise(ArgumentError) { @collection.keys }
  67. assert_equal 'No indices configured', exception.message
  68. end
  69. def test_should_not_allow_lookup
  70. @collection << object = Object.new
  71. exception = assert_raise(ArgumentError) { @collection[0] }
  72. assert_equal 'No indices configured', exception.message
  73. end
  74. def test_should_not_allow_fetching
  75. @collection << object = Object.new
  76. exception = assert_raise(ArgumentError) { @collection.fetch(0) }
  77. assert_equal 'No indices configured', exception.message
  78. end
  79. end
  80. class NodeCollectionWithIndicesTest < Test::Unit::TestCase
  81. def setup
  82. machine = StateMachine::Machine.new(Class.new)
  83. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  84. @object = Struct.new(:name, :value).new(:parked, 1)
  85. @collection << @object
  86. end
  87. def test_should_use_first_index_by_default_on_key_retrieval
  88. assert_equal [:parked], @collection.keys
  89. end
  90. def test_should_allow_customizing_index_for_key_retrieval
  91. assert_equal [1], @collection.keys(:value)
  92. end
  93. def test_should_use_first_index_by_default_on_lookup
  94. assert_equal @object, @collection[:parked]
  95. assert_nil @collection[1]
  96. end
  97. def test_should_allow_customizing_index_on_lookup
  98. assert_equal @object, @collection[1, :value]
  99. assert_nil @collection[:parked, :value]
  100. end
  101. def test_should_use_first_index_by_default_on_fetch
  102. assert_equal @object, @collection.fetch(:parked)
  103. exception = assert_raise(IndexError) { @collection.fetch(1) }
  104. assert_equal '1 is an invalid name', exception.message
  105. end
  106. def test_should_allow_customizing_index_on_fetch
  107. assert_equal @object, @collection.fetch(1, :value)
  108. exception = assert_raise(IndexError) { @collection.fetch(:parked, :value) }
  109. assert_equal ':parked is an invalid value', exception.message
  110. end
  111. end
  112. class NodeCollectionWithNodesTest < Test::Unit::TestCase
  113. def setup
  114. @machine = StateMachine::Machine.new(Class.new)
  115. @collection = StateMachine::NodeCollection.new(@machine)
  116. @klass = Struct.new(:name, :machine)
  117. @parked = @klass.new(:parked, @machine)
  118. @idling = @klass.new(:idling, @machine)
  119. @collection << @parked
  120. @collection << @idling
  121. end
  122. def test_should_be_able_to_enumerate
  123. order = []
  124. @collection.each {|object| order << object}
  125. assert_equal [@parked, @idling], order
  126. end
  127. def test_should_be_able_to_concatenate_multiple_nodes
  128. @first_gear = @klass.new(:first_gear, @machine)
  129. @second_gear = @klass.new(:second_gear, @machine)
  130. @collection.concat([@first_gear, @second_gear])
  131. order = []
  132. @collection.each {|object| order << object}
  133. assert_equal [@parked, @idling, @first_gear, @second_gear], order
  134. end
  135. def test_should_be_able_to_access_by_index
  136. assert_equal @parked, @collection.at(0)
  137. assert_equal @idling, @collection.at(1)
  138. end
  139. def test_should_deep_copy_machine_changes
  140. new_machine = StateMachine::Machine.new(Class.new)
  141. @collection.machine = new_machine
  142. assert_equal new_machine, @collection.machine
  143. assert_equal new_machine, @parked.machine
  144. assert_equal new_machine, @idling.machine
  145. end
  146. end
  147. class NodeCollectionAfterUpdateTest < Test::Unit::TestCase
  148. def setup
  149. machine = StateMachine::Machine.new(Class.new)
  150. @collection = StateMachine::NodeCollection.new(machine, :index => [:name, :value])
  151. @klass = Struct.new(:name, :value)
  152. @parked = @klass.new(:parked, 1)
  153. @idling = @klass.new(:idling, 2)
  154. @collection << @parked << @idling
  155. @parked.name = :parking
  156. @parked.value = 0
  157. @collection.update(@parked)
  158. end
  159. def test_should_not_change_the_index
  160. assert_equal @parked, @collection.at(0)
  161. end
  162. def test_should_not_duplicate_in_the_collection
  163. assert_equal 2, @collection.length
  164. end
  165. def test_should_add_each_indexed_key
  166. assert_equal @parked, @collection[:parking]
  167. assert_equal @parked, @collection[0, :value]
  168. end
  169. def test_should_remove_each_old_indexed_key
  170. assert_nil @collection[:parked]
  171. assert_nil @collection[1, :value]
  172. end
  173. end