Skip to content

Commit

Permalink
Improving test coverage a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Sep 17, 2016
1 parent b6b2183 commit 4878c4d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 8 deletions.
10 changes: 10 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[run]
source = src/pyspacewar
omit = */tests/*

[report]
exclude_lines =
pragma: nocover
except ImportError:
except NameError:
if __name__ == .__main__.:
14 changes: 14 additions & 0 deletions src/pyspacewar/tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def move(self, dt):
print "Tick (%s)" % dt


class Controller(object):
"""Fake controller."""

def control(self):
print "Controlling the world"


def doctest_PythonTimeSource():
"""Tests for PythonTimeSource
Expand Down Expand Up @@ -235,6 +242,7 @@ def doctest_Game_wait_for_tick():
>>> g = Game()
>>> ts = g.time_source = TimeSourceStub()
>>> g.world.add(Ticker())
>>> g.controllers.append(Controller())
Initial call to g.wait_for_tick remembers the current time. All other
calls wait the necessary amount. Each call also causes an update in the
Expand All @@ -244,14 +252,17 @@ def doctest_Game_wait_for_tick():
True
>>> g.wait_for_tick()
Tick (2.0)
Controlling the world
Waiting for 11
True
>>> g.wait_for_tick()
Tick (2.0)
Controlling the world
Waiting for 21
True
>>> g.wait_for_tick()
Tick (2.0)
Controlling the world
Waiting for 31
True
Expand All @@ -260,11 +271,13 @@ def doctest_Game_wait_for_tick():
>>> ts.counter += 5
>>> g.wait_for_tick()
Tick (2.0)
Controlling the world
Waiting for 41
True
>>> ts.counter += 105
>>> g.wait_for_tick()
Tick (2.0)
Controlling the world
Waiting for 51
False
Expand All @@ -278,6 +291,7 @@ def doctest_Game_wait_for_tick():
>>> g.wait_for_tick()
Tick (2.0)
Controlling the world
Waiting for 61
False
Expand Down
105 changes: 99 additions & 6 deletions src/pyspacewar/tests/test_world.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def collision(self, other):
print "%s collides with %s" % (self.name, other.name)


def effect(msg):
def callback(*args):
print(msg)
return callback


def doctest_World():
"""Tests for basic World functions
Expand Down Expand Up @@ -78,15 +84,15 @@ def doctest_World():
Some objects have mass and thus attract other objects (even those that have
no mass)
>>> w.add(Object('planet', mass=100))
>>> w.add(Object('planet', mass=100, radius=10))
>>> w.update(0.1)
planet attracts brick1 for 0.1 time units
planet attracts brick2 for 0.1 time units
Moving brick1 for 0.1 time units
Moving brick2 for 0.1 time units
Moving planet for 0.1 time units
>>> w.add(Object('sun', mass=1e20))
>>> w.add(Object('sun', mass=1e20, radius=20))
>>> w.update(0.1)
planet attracts brick1 for 0.1 time units
planet attracts brick2 for 0.1 time units
Expand Down Expand Up @@ -121,6 +127,10 @@ def doctest_World_collision_detection_in_update():
o1 collides with o3
o3 collides with o1
>>> w.remove(o1)
>>> w.remove(o2)
>>> w.remove(o3)
"""


Expand Down Expand Up @@ -405,6 +415,7 @@ def doctest_Ship_collision():
>>> from world import Ship, Vector, Planet, Debris, Missile, World
>>> ship = Ship(position=Vector(3, 5), velocity=Vector(10, 10))
>>> ship.world = World()
>>> ship.hit_effect = effect('Ouch!')
Debris is easily deflected
Expand All @@ -415,6 +426,7 @@ def doctest_Ship_collision():
Missiles cause more damage, but no bouncing
>>> ship.collision(Missile())
Ouch!
>>> print ship.health
0.4
>>> print ship.velocity
Expand All @@ -430,11 +442,10 @@ def doctest_Ship_collision():
When health falls under 0, the ship dies
>>> def croak(who):
... print "Ship died"
>>> ship.die = croak
>>> ship.die = effect('Ship died')
>>> ship.collision(Missile())
Ouch!
Ship died
>>> print ship.health
-0.25
Expand All @@ -443,6 +454,7 @@ def doctest_Ship_collision():
>>> ship.dead = True
>>> ship.collision(Missile())
Ouch!
>>> print ship.health
-0.85
Expand Down Expand Up @@ -504,20 +516,31 @@ def doctest_Ship_death():
>>> ship.left_thrust
0
A dying ship emits a sound effect
>>> ship = Ship()
>>> ship.world = World()
>>> ship.explode_effect = effect('Boom')
>>> ship.die()
Boom
"""


def doctest_Ship_rebirth():
"""Tests for Ship.respawn.
>>> from world import Ship, Vector
>>> from world import Ship, Vector, World
>>> ship = Ship(Vector(10, 20), velocity=Vector(1, 2), direction=90)
>>> ship.dead = True
>>> ship.health = -0.3
>>> ship.world = World()
>>> ship.respawn_effect = effect('Woohoo!')
A dead ship can come back to life (to make the game more interesting)
>>> ship.respawn()
Woohoo!
>>> ship.dead
False
>>> ship.health
Expand All @@ -532,7 +555,9 @@ def doctest_Ship_launch():
>>> from world import Ship, Vector, World
>>> ship = Ship(velocity=Vector(10, 20), direction=90)
>>> ship.world = World()
>>> ship.launch_effect = effect('Fooom!')
>>> ship.launch()
Fooom!
>>> len(ship.world.objects)
1
Expand Down Expand Up @@ -609,6 +634,63 @@ def doctest_Missile_explode():
"""


def doctest_Missile_collision():
"""Tests for Missile.collision.
>>> from world import Missile, World, Planet
>>> missile = Missile()
>>> world = World()
>>> world.add(missile)
>>> moon = Planet()
>>> missile.collision(moon)
>>> missile.dead
True
>>> missile in world.objects
False
"""


def doctest_Object_collision():
"""Tests for Object.collision.
>>> from world import Object, World, Vector
>>> asteroid = Object(Vector(3, 4), velocity=Vector(0.1, 0.2),
... mass=14, radius=2)
>>> tincan = Object(Vector(0.5, 4), velocity=Vector(1.5, -0.3),
... mass=1, radius=1)
>>> tincan.world = World()
>>> tincan.bounce_effect = effect('Bump!')
>>> tincan.collision(asteroid)
Bump!
"""


def doctest_Object_bounce():
"""Tests for Object.bounce.
>>> from world import Object, World, Vector
>>> asteroid = Object(Vector(3, 4), velocity=Vector(0.1, 0.2),
... mass=14, radius=2)
>>> tincan = Object(Vector(0.5, 4), velocity=Vector(1.5, -0.3),
... mass=1, radius=1)
>>> tincan.world = World()
>>> tincan.bounce_effect = effect('Bump!')
>>> tincan.bounce(asteroid)
Bump!
>>> print tincan.velocity
(-1.350, -0.270)
>>> print tincan.position
(0.000, 4.000)
"""


def doctest_Object_add_debris():
"""Tests for Object.add_debris.
Expand Down Expand Up @@ -659,6 +741,17 @@ def doctest_Debris():
"""


def doctest_Planet():
"""Tests for Planet.
>>> from world import Planet, Vector
>>> sun = Planet(position=Vector(10, 15), mass=200)
>>> moon = Planet(position=Vector(20, 35), mass=10)
>>> moon.gravitate(sun, 0.1) # nothing happens
>>> moon.collision(sun) # nothing happens
"""

def test_suite():
path = os.path.join(os.path.dirname(__file__), os.path.pardir)
if path not in sys.path:
Expand Down
2 changes: 1 addition & 1 deletion src/pyspacewar/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_git_revision(unknown='', format=' (git %s)'):
p = subprocess.Popen(['git', 'describe', '--always'],
cwd=package_root,
stdout=subprocess.PIPE)
except OSError:
except OSError: # pragma: nocover
return unknown
else:
return format % p.communicate()[0].strip()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ whitelist_externals = coverage
deps =
coverage
commands =
coverage run --source=src/pyspacewar test.py
coverage run test.py
coverage report

0 comments on commit 4878c4d

Please sign in to comment.