test/test_zyps.rb in zyps-0.3.1 vs test/test_zyps.rb in zyps-0.4.1
- old
+ new
@@ -18,13 +18,37 @@
require 'zyps'
require 'test/unit'
+#Redefine Clock to return a predictable time.
+class Clock
+ def elapsed_time; 0.1; end
+end
+
+
class TestGameObject < Test::Unit::TestCase
+ def test_constraints
+ #Test at initialization.
+ object = GameObject.new(
+ "", #Name.
+ Location.new,
+ Color.new,
+ Vector.new,
+ 0,
+ -1 #Size.
+ )
+ assert_equal(0, object.size)
+ #Test accessors.
+ object = GameObject.new
+ object.size = -1
+ assert_equal(0, object.size)
+ end
+
+
def test_move
#Set up moving object.
object = GameObject.new
object.location = Location.new(0, 0)
object.vector = Vector.new(1.4142, 45)
@@ -51,11 +75,11 @@
assert_equal(1, creature.color.green)
assert_equal(1, creature.color.blue)
assert_equal(0, creature.vector.speed)
assert_equal(0, creature.vector.pitch)
assert_equal(nil, creature.name)
- assert_in_delta(0, creature.age, 0.1)
+ assert_equal(1, creature.size)
assert_equal([], creature.tags)
assert_equal([], creature.behaviors)
#Identifiers should be unique.
assert_not_equal(creature.identifier, Creature.new.identifier)
end
@@ -67,10 +91,11 @@
"name",
Location.new(10, -3),
Color.new(0.5, 0.6, 0.7),
Vector.new(1.5, 225),
2.001, #Age.
+ 5.0, #Size.
["predator", "blue team"],
[behavior]
)
assert_equal("name", creature.name)
assert_equal(10, creature.location.x)
@@ -78,11 +103,11 @@
assert_equal(0.5, creature.color.red)
assert_equal(0.6, creature.color.green)
assert_equal(0.7, creature.color.blue)
assert_equal(1.5, creature.vector.speed)
assert_equal(225, creature.vector.pitch)
- assert_in_delta(2.001, creature.age, 0.01)
+ assert_equal(5.0, creature.size)
assert(creature.tags.include?("predator"))
assert(creature.tags.include?("blue team"))
assert(creature.behaviors.include?(behavior))
end
@@ -91,89 +116,162 @@
class TestEnvironment < Test::Unit::TestCase
+
def setup
- #Interactions will be logged here.
- @interactions = []
+ #Create an environment and add creatures.
+ @environment = Environment.new
+ @environment.objects << Creature.new('1')
+ @environment.objects << Creature.new('2')
- #Create creatures.
- creature1 = Creature.new('1')
- creature2 = Creature.new('2')
- creature_behavior = Behavior.new
- creature_behavior.actions << lambda {|creature, target| @interactions << "#{creature.name} targeting #{target.name}"}
- creature1.behaviors << creature_behavior
- creature2.behaviors << creature_behavior
-
- #Create an environment and add the creatures.
- @environment = Environment.new([creature1, creature2])
-
end
+ #An action that keeps a log of the actor and target.
+ class LogAction < Action
+ attr_reader :interactions
+ def initialize
+ #Interactions will be logged here.
+ @interactions = []
+ end
+ def do(actor, target)
+ #Log the interaction.
+ @interactions << "#{actor.name} targeting #{target.name}"
+ end
+ end
+
def test_interactions
+ #Set up behavior that will log interactions.
+ behavior = Behavior.new
+ log = LogAction.new
+ behavior.actions << log
+ @environment.objects.each {|creature| creature.behaviors << behavior}
+
#Have environment elements interact.
@environment.interact
#Look for expected interactions (each should only occur once).
- assert(@interactions.find_all{|i| i == "2 targeting 1"}.length == 1)
- assert(@interactions.find_all{|i| i == "1 targeting 2"}.length == 1)
- assert(@interactions.find_all{|i| i == "1 targeting 1"}.length == 0)
- assert(@interactions.find_all{|i| i == "2 targeting 2"}.length == 0)
+ assert(log.interactions.find_all{|i| i == "2 targeting 1"}.length == 1)
+ assert(log.interactions.find_all{|i| i == "1 targeting 2"}.length == 1)
+ assert(log.interactions.find_all{|i| i == "1 targeting 1"}.length == 0)
+ assert(log.interactions.find_all{|i| i == "2 targeting 2"}.length == 0)
end
+ #An environmental factor that logs its target.
+ class LogFactor < EnvironmentalFactor
+ attr_reader :interactions
+ def initialize
+ #Interactions will be logged here.
+ @interactions = []
+ end
+ def act(target)
+ #Log the interaction.
+ @interactions << "Environment targeting #{target.name}"
+ end
+ end
+
def test_environmental_factors
#Create an environmental factor.
- behavior = Behavior.new
- behavior.actions << lambda {|factor, target| @interactions << "Environment targeting #{target.name}"}
- @environment.environmental_factors << EnvironmentalFactor.new([behavior])
+ logger = LogFactor.new
+ @environment.environmental_factors << logger
#Have environment elements interact.
@environment.interact
#Look for expected interactions (each should only occur once).
- assert(@interactions.find_all{|i| i == "Environment targeting 1"}.length == 1)
- assert(@interactions.find_all{|i| i == "Environment targeting 2"}.length == 1)
+ assert(logger.interactions.find_all{|i| i == "Environment targeting 1"}.length == 1)
+ assert(logger.interactions.find_all{|i| i == "Environment targeting 2"}.length == 1)
end
+ #A condition that is false unless actor and target have specific names.
+ class NameCondition < Condition
+ def test(actor, target)
+ return true if actor.name == '1' and target.name == '2'
+ end
+ end
+
def test_conditions
- #Change behaviors to only occur if the target's name is '2'.
- @environment.objects.each do |creature|
- behavior = Behavior.new
- behavior.conditions << lambda do |creature, target|
- return true if creature.name == '1' and target.name == '2'
- end
- behavior.actions << lambda do |creature, target|
- @interactions << "#{creature.name} is targeting #{target.name}"
- end
- creature.behaviors << behavior
- end
-
+ #Set up behavior that will log interactions.
+ behavior = Behavior.new
+ log = LogAction.new
+ behavior.actions << log
+ name_checker = NameCondition.new
+ behavior.conditions << name_checker
+ @environment.objects.each {|creature| creature.behaviors << behavior}
+
#Have environment elements interact.
@environment.interact
- #Creature '1' should not have been acted on.
- assert(@interactions.find_all{|i| i == "2 is targeting 1"}.length == 0)
- #Creature '2' *should* have been acted on.
- assert(@interactions.find_all{|i| i == "1 is targeting 2"}.length == 1)
+ #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)
end
end
+class TestColor < Test::Unit::TestCase
+
+ def test_default_initialization
+ color = Color.new
+ assert_equal(1, color.red)
+ assert_equal(1, color.green)
+ assert_equal(1, color.blue)
+ end
+
+ def test_explicit_initialization
+ color = Color.new(0.25, 0.5, 0.75)
+ assert_equal(0.25, color.red)
+ assert_equal(0.5, color.green)
+ assert_equal(0.75, color.blue)
+ end
+
+ def test_constraints
+ #Test at initialization.
+ color = Color.new(-1, -1, -1)
+ assert_equal(0, color.red)
+ assert_equal(0, color.green)
+ assert_equal(0, color.blue)
+ color = Color.new(2, 2, 2)
+ assert_equal(1, color.red)
+ assert_equal(1, color.green)
+ assert_equal(1, color.blue)
+ #Test accessors.
+ color = Color.new
+ color.red = -1
+ assert_equal(0, color.red)
+ color.red = 2
+ assert_equal(1, color.red)
+ color.green = -1
+ assert_equal(0, color.green)
+ color.green = 2
+ assert_equal(1, color.green)
+ color.blue = -1
+ assert_equal(0, color.blue)
+ color.blue = 2
+ assert_equal(1, color.blue)
+ end
+
+
+end
+
+
+
class TestVector < Test::Unit::TestCase
def test_initialize
@@ -268,27 +366,10 @@
end
-class TestClock < Test::Unit::TestCase
-
-
- def test_elapsed_time
-
- #Create a clock, wait a moment, then see how much time has elapsed since its creation.
- clock = Clock.new
- sleep 0.1
- assert_in_delta(0.1, clock.elapsed_time, 0.02)
-
- end
-
-
-end
-
-
-
class TestUtility < Test::Unit::TestCase
def test_to_radians
@@ -340,20 +421,29 @@
assert_equal(330, Utility.find_reflection_angle(270, 210))
assert_equal(30, Utility.find_reflection_angle(270, 150))
end
- def test_inside_box?
- #Too far left.
- assert(! Utility.inside_box?(Location.new(1, 3), Location.new(2, 2), Location.new(4, 4)))
- #Too far right.
- assert(! Utility.inside_box?(Location.new(5, 3), Location.new(2, 2), Location.new(4, 4)))
- #Too far up.
- assert(! Utility.inside_box?(Location.new(3, 1), Location.new(2, 2), Location.new(4, 4)))
- #Too far down.
- assert(! Utility.inside_box?(Location.new(3, 5), Location.new(2, 2), Location.new(4, 4)))
- #Inside.
- assert(Utility.inside_box?(Location.new(3, 3), Location.new(2, 2), Location.new(4, 4)))
+ def test_collided?
+ #Objects apart.
+ assert(! Utility.collided?(
+ GameObject.new("", Location.new(0, 0), Color.new, Vector.new, 0, 0.196), #Radius = 0.25
+ GameObject.new("", Location.new(1, 0), Color.new, Vector.new, 0, 0.196)
+ ))
+ #Objects touching (not a collision).
+ assert(! Utility.collided?(
+ GameObject.new("", Location.new(0, 0), Color.new, Vector.new, 0, 0.785), #Radius = 0.5
+ GameObject.new("", Location.new(1, 0), Color.new, Vector.new, 0, 0.785)
+ ))
+ #Objects collided.
+ assert(Utility.collided?(
+ GameObject.new("", Location.new(0, 0), Color.new, Vector.new, 0, 1.766), #Radius = 0.75
+ GameObject.new("", Location.new(1, 0), Color.new, Vector.new, 0, 1.766)
+ ))
+ #Objects in same place.
+ assert(Utility.collided?(
+ GameObject.new("", Location.new(0, 0)),
+ GameObject.new("", Location.new(0, 0))
+ ))
end
-
end
\ No newline at end of file