test/test_zyps.rb in zyps-0.6.3 vs test/test_zyps.rb in zyps-0.7.0
- old
+ new
@@ -15,10 +15,12 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'zyps'
+require 'zyps/actions'
+require 'zyps/conditions'
require 'test/unit'
include Zyps
@@ -32,18 +34,11 @@
class TestGameObject < Test::Unit::TestCase
def test_constraints
#Test at initialization.
- object = GameObject.new(
- "", #Name.
- Location.new,
- Color.new,
- Vector.new,
- 0,
- -1 #Size.
- )
+ object = GameObject.new(:size => -1)
assert_equal(0, object.size)
#Test accessors.
object = GameObject.new
object.size = -1
assert_equal(0, object.size)
@@ -110,18 +105,18 @@
def test_explicit_initialization
behavior = Behavior.new
creature = Creature.new(
- "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]
+ :name => "name",
+ :location => Location.new(10, -3),
+ :color => Color.new(0.5, 0.6, 0.7),
+ :vector => Vector.new(1.5, 225),
+ :age => 2.001,
+ :size => 5.0, #Size.
+ :tags => ["predator", "blue team"],
+ :behaviors => [behavior]
)
assert_equal("name", creature.name)
assert_equal(10, creature.location.x)
assert_equal(-3, creature.location.y)
assert_equal(0.5, creature.color.red)
@@ -156,13 +151,13 @@
def setup
#Create an environment and add creatures.
@environment = Environment.new
- @environment.objects << Creature.new('1')
- @environment.objects << Creature.new('2')
- @environment.objects << Creature.new('3')
+ @environment.objects << Creature.new(:name => '1')
+ @environment.objects << Creature.new(:name => '2')
+ @environment.objects << Creature.new(:name => '3')
end
#An action that keeps a log of the actor and target.
@@ -170,13 +165,15 @@
attr_reader :interactions
def initialize
#Interactions will be logged here.
@interactions = []
end
- def do(actor, target)
+ def do(actor, targets)
#Log the interaction.
- @interactions << "#{actor.name} targeting #{target.name}"
+ targets.each do |target|
+ @interactions << "#{actor.name} targeting #{target.name}"
+ end
end
end
def test_interactions
@@ -205,13 +202,13 @@
attr_reader :interactions
def initialize
#Interactions will be logged here.
@interactions = []
end
- def act(target)
+ def act(environment)
#Log the interaction.
- @interactions << "Environment targeting #{target.name}"
+ environment.objects.each {|target| @interactions << "Environment targeting #{target.name}"}
end
end
def test_environmental_factors
@@ -229,12 +226,12 @@
end
#A condition that is false unless actor and target have specific names.
class NameCondition < Condition
- def met?(actor, target)
- return true if actor.name == '1' and target.name == '2'
+ def select(actor, targets)
+ targets.find_all {|target| actor.name == '1' and target.name == '2'}
end
end
def test_conditions
@@ -253,34 +250,10 @@
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
@@ -484,72 +457,72 @@
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)
+ GameObject.new(:location => Location.new(0, 0), :size =>0.196), #Radius = 0.25
+ GameObject.new(:location => Location.new(1, 0), :size =>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)
+ GameObject.new(:location => Location.new(0, 0), :size =>0.785), #Radius = 0.5
+ GameObject.new(:location => Location.new(1, 0), :size =>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)
+ GameObject.new(:location => Location.new(0, 0), :size =>1.766), #Radius = 0.75
+ GameObject.new(:location => Location.new(1, 0), :size =>1.766)
))
#Objects in same place.
assert(Utility.collided?(
- GameObject.new("", Location.new(0, 0)),
- GameObject.new("", Location.new(0, 0))
+ GameObject.new(:location => Location.new(0, 0)),
+ GameObject.new(:location => Location.new(0, 0))
))
end
end
class TestBehavior < Test::Unit::TestCase
- #Always true.
+ #True for all targets.
class TrueCondition < Condition
- def met?(actor, target)
- true
+ def select(actor, targets)
+ targets #Select all targets.
end
end
- #Always false.
+ #False for all targets.
class FalseCondition < Condition
- def met?(actor, target)
- false
+ def select(actor, targets)
+ [] #Select no targets.
end
end
def setup
- @actor = Creature.new('actor')
- @target = Creature.new('target')
- @other = Creature.new('other')
- @environment = Environment.new
- @environment.objects << @actor << @target << @other
+ @actor = Creature.new(:name => 'actor')
+ @target = Creature.new(:name => 'target')
+ @other = Creature.new(:name => 'other')
+ @targets = []
+ @targets << @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)
+ def start(actor, targets)
super
@start_count += 1
end
- def do(actor, target)
+ def do(actor, targets)
@do_count += 1
end
- def stop(actor, target)
+ def stop(actor, targets)
super
@stop_count += 1
end
end
@@ -560,25 +533,25 @@
behavior.conditions << TrueCondition.new
action = MockAction.new
behavior.actions << action
#Perform the behavior.
- behavior.perform(@actor, @environment)
+ behavior.perform(@actor, @targets)
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)
+ behavior.perform(@actor, @targets)
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)
+ behavior.perform(@actor, @targets)
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
@@ -595,6 +568,6 @@
assert_not_same(original.conditions, copy.conditions, "Copy's condition list should not be same object.")
assert_not_same(original.conditions[0], copy.conditions[0], "Conditions in list should not be same objects.")
end
-end
\ No newline at end of file
+end