def setUp(self): self.grid = Grid(5, 5) def newAnt(): return AntFemale(self.grid, NO_SPRITE, None) self.ant = AntQueen(self.grid, NO_SPRITE, life=10, nextPosition=lambda square: square[0], nextAnt=lambda: (newAnt, 4))
class AntQueenTestCase(unittest.TestCase): def setUp(self): self.grid = Grid(5, 5) def newAnt(): return AntFemale(self.grid, NO_SPRITE, None) self.ant = AntQueen(self.grid, NO_SPRITE, life=10, nextPosition=lambda square: square[0], nextAnt=lambda: (newAnt, 4)) def test01a(self): """ a queen that does not produce an ant does not lose life points """ self.ant._next = None self.assertTrue(self.ant._createNext() is None) self.assertEquals(10, self.ant._life) def test01b(self): """ a queen that produces an ant loses some life """ self.ant._next = lambda: AntFemale(self.grid, NO_SPRITE, None) self.ant._cost = 1 self.assertEquals(AntFemale, self.ant._createNext().__class__) self.assertEquals(9, self.ant._life) def test01c(self): """ a queen with not enough life left will not produce the ant """ self.ant._life = 3 self.ant._cost = 3 self.ant._next = lambda: AntFemale(self.grid, NO_SPRITE, None) self.assertTrue(self.ant._createNext() is None) self.assertEquals(3, self.ant._life) def test02a(self): """ a queen can produce ants on soil """ self.grid.put(2, 2, Soil(self.grid)) self.grid.put(2, 2, self.ant) self.assertTrue(self.ant._next is None) self.ant.prepareToMove() self.assertTrue(self.ant._next is not None) newAnt = self.ant.move() self.assertTrue(isinstance(newAnt, AntFemale)) self.assertTrue(self.grid.has(1, 1, newAnt)) def test02b(self): """ a queen can produce ants on grass """ self.grid.put(2, 2, Grass(self.grid)) self.grid.put(2, 2, self.ant) self.assertTrue(self.ant._next is None) self.ant.prepareToMove() self.assertTrue(self.ant._next is not None) newAnt = self.ant.move() self.assertTrue(isinstance(newAnt, AntFemale)) self.assertTrue(self.grid.has(1, 1, newAnt)) def test02c(self): """ a queen cannot produce ants on sand """ self.grid.put(2, 2, Sand(self.grid)) self.grid.put(2, 2, self.ant) self.assertTrue(self.ant._next is None) self.ant.prepareToMove() self.assertTrue(self.ant._next is None) newAnt = self.ant.move() self.assertTrue(newAnt is None) def test03(self): """ a dead queen produces nothing """ self.grid.put(2, 2, Grass(self.grid)) self.grid.put(2, 2, self.ant) self.assertTrue(self.ant._next is None) self.ant._life = 0 self.ant.prepareToMove() self.assertTrue(self.ant._next is None) newAnt = self.ant.move() self.assertTrue(newAnt is None)