PageRenderTime 205ms CodeModel.GetById 40ms app.highlight 110ms RepoModel.GetById 51ms app.codeStats 0ms

/lib/gemcache/ruby/1.9.1/gems/state_machine-1.1.2/test/unit/node_collection_test.rb

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