test/test_zyps.rb in zyps-0.4.1 vs test/test_zyps.rb in zyps-0.5.1

- old
+ new

@@ -123,10 +123,11 @@ #Create an environment and add creatures. @environment = Environment.new @environment.objects << Creature.new('1') @environment.objects << Creature.new('2') + @environment.objects << Creature.new('3') end #An action that keeps a log of the actor and target. @@ -142,15 +143,17 @@ end end def test_interactions - #Set up behavior that will log interactions. - behavior = Behavior.new + #Set up behaviors that will log interactions. log = LogAction.new - behavior.actions << log - @environment.objects.each {|creature| creature.behaviors << behavior} + @environment.objects.each do |creature| + behavior = Behavior.new + behavior.actions << log + creature.behaviors << behavior + end #Have environment elements interact. @environment.interact #Look for expected interactions (each should only occur once). @@ -209,18 +212,40 @@ @environment.objects.each {|creature| creature.behaviors << behavior} #Have environment elements interact. @environment.interact - #Creature '1' should NOT have been acted on. - assert(log.interactions.find_all{|i| i == "2 targeting 1"}.length == 0) - #Creature '2' SHOULD have been acted on. - assert(log.interactions.find_all{|i| i == "1 targeting 2"}.length == 1) + assert_equal(0, log.interactions.find_all{|i| i == "2 targeting 1"}.length, "Creature '1' should NOT have been acted on.") + assert_equal(1, log.interactions.find_all{|i| i == "1 targeting 2"}.length, "Creature '2' SHOULD have been acted on.") end + #Test that creatures can switch targets when one is removed from the environment. + def test_object_removal + + #Set up behaviors that will log interactions. + log = LogAction.new + @environment.objects.each do |creature| + behavior = Behavior.new + behavior.actions << log + creature.behaviors << behavior + end + + #Have environment elements interact. + @environment.interact + #Remove the original target from the environment. + @environment.objects.delete_at(1) + #Interact again. + @environment.interact + + #Look for expected interactions (each should only occur once). + assert_equal(1, log.interactions.find_all{|i| i == "1 targeting 3"}.length) + + end + + end class TestColor < Test::Unit::TestCase @@ -443,7 +468,85 @@ assert(Utility.collided?( GameObject.new("", Location.new(0, 0)), GameObject.new("", Location.new(0, 0)) )) end + +end + +#TODO: Rewrite to pass environment. +class TestBehavior < Test::Unit::TestCase + + #Always true. + class TrueCondition < Condition + def test(actor, target) + true + end + end + #Always false. + class FalseCondition < Condition + def test(actor, target) + false + end + end + + + def setup + @actor = Creature.new('actor') + @target = Creature.new('target') + @other = Creature.new('other') + @environment = Environment.new + @environment.objects << @actor << @target << @other + end + + + #Tracks number of times its start, stop, and do methods are called. + class MockAction < Action + attr_accessor :start_count, :stop_count, :do_count + def initialize + @start_count, @stop_count, @do_count = 0, 0, 0 + end + def start(actor, target) + super + @start_count += 1 + end + def do(actor, target) + @do_count += 1 + end + def stop(actor, target) + super + @stop_count += 1 + end + end + + def test_actions + + #Set up a behavior with a true condition and a mock action. + behavior = Behavior.new + behavior.conditions << TrueCondition.new + action = MockAction.new + behavior.actions << action + + #Perform the behavior. + behavior.perform(@actor, @environment) + assert_equal(1, action.start_count, "start() should have been called on the mock action.") + assert_equal(1, action.do_count, "do() should have been called.") + assert_equal(0, action.stop_count, "stop() should NOT have been called.") + + #Perform the behavior again. + behavior.perform(@actor, @environment) + assert_equal(1, action.start_count, "start() should NOT have been called.") + assert_equal(2, action.do_count, "do() should have been called.") + assert_equal(0, action.stop_count, "stop() should NOT have been called.") + + #Add a false condition to the behavior. + behavior.conditions << FalseCondition.new + #Perform the behavior. + behavior.perform(@actor, @environment) + assert_equal(1, action.start_count, "start() should NOT have been called.") + assert_equal(2, action.do_count, "do() should NOT have been called, because conditions are no longer true.") + assert_equal(1, action.stop_count, "stop() SHOULD have been called.") + + end + end \ No newline at end of file