def test_unparseable(self): with self.assertRaises(ValueError) as e: L.GameState('xyzzy') self.assertEqual(str(e.exception), "text did not parse as a game state") with self.assertRaises(ValueError) as e: L.GameState('Foo (b1) r3-y1\nxyzzy\n') self.assertEqual(str(e.exception), "text did not parse as a game state")
def test_trivial(self): s = L.GameState('Foo (0,b3r1) g3-\n' + 'Bar(1,b3y2) -r1y2') self.assertEqual(str(s), 'Foo (0, r1b3) g3-\nBar (1, y2b3) -r1y2\n') self.assertEqual(s.toString(), 'Foo (0, r1b3) g3-\nBar (1, y2b3) -r1y2\n') self.assertEqual(repr(s), "libannotate.GameState('Foo (0, r1b3) g3-\\nBar (1, y2b3) -r1y2\\n')") s = L.GameState('Foo (0,b3r1) g3-\n' + 'Bar(1,b3y2) -r1y2\n') self.assertEqual(str(s), 'Foo (0, r1b3) g3-\nBar (1, y2b3) -r1y2\n') self.assertEqual(s.toString(), 'Foo (0, r1b3) g3-\nBar (1, y2b3) -r1y2\n') self.assertEqual(repr(s), "libannotate.GameState('Foo (0, r1b3) g3-\\nBar (1, y2b3) -r1y2\\n')")
def test_nonmutating_apply(self): s1 = L.GameState('Foo (0,b3r1) g3-\n' + 'Bar(1,b3y2) -r1y2') self.assertEqual(s1.toString(), 'Foo (0, r1b3) g3-\nBar (1, y2b3) -r1y2\n') m = L.WholeMove('build g1') s2 = s1.copyApply(0, m) self.assertEqual(s1.toString(), 'Foo (0, r1b3) g3-\nBar (1, y2b3) -r1y2\n') self.assertEqual(s2.toString(), 'Foo (0, r1b3) g1g3-\nBar (1, y2b3) -r1y2\n')
def test_gameIsOver(self): s = L.GameState(""" Player1 (0, g3y2) g3- Blueberry (b3) y1y1g1- Timbuktu (g2) -r1g1 Player2 (1, y2b1) r3-y2y1 """) m = L.WholeMove('move y1 from Blueberry to Player2; catastrophe yellow at Player2') self.assertEqual(s.gameIsOver(), False) s.apply(0, m) self.assertEqual(s.gameIsOver(), True)
def test_getBestMove(self): s = L.GameState(""" Player1 (0, g3y2) g3- Blueberry (b3) y1y1g1- Timbuktu (g2) -r1g1 Player2 (1, y2b1) r3-y2y1 """) m = s.getBestMove(0) self.assertEqual(m.toString(), 'move y1 from Blueberry to Player2; catastrophe yellow at Player2') with self.assertRaises(ValueError) as e: s.getBestMove(2) self.assertEqual(str(e.exception), "attacker must be 0 or 1")
def test_nonmutating_apply_exceptions(self): s = L.GameState('Foo (0,b3r1) g3-\n' + 'Bar(1,b3y2) -r1y2') with self.assertRaises(TypeError) as e: s.copyApply() self.assertEqual(str(e.exception), "function takes exactly 2 arguments (0 given)") with self.assertRaises(TypeError) as e: s.copyApply(0, 'pass') self.assertEqual(str(e.exception), "argument 2 must be libannotate.WholeMove, not str") with self.assertRaises(TypeError) as e: s.copyApply(L.WholeMove('pass'), 0) self.assertEqual(str(e.exception), "an integer is required (got type libannotate.WholeMove)") with self.assertRaises(ValueError) as e: s.copyApply(2, L.WholeMove('pass')) self.assertEqual(str(e.exception), "attacker must be 0 or 1")
def test_in_universe_exceptions(self): s = L.GameState('Foo (0,b3r1) g3-\n' + 'Bar(1,b3y2) -g1y2') with self.assertRaises(ValueError) as e: s.apply(0, L.WholeMove('sac g3 at Foo')) self.assertEqual(str(e.exception), "The move as parsed was disallowed by the rule against self-destruction.") with self.assertRaises(ValueError) as e: s.apply(0, L.WholeMove('build y1 at Nonexistent')) self.assertEqual(str(e.exception), "The move as parsed referred to a nonexistent star system.") with self.assertRaises(ValueError) as e: s.apply(1, L.WholeMove('move g1 from Bar to Foo (b1)')) self.assertEqual(str(e.exception), "The move as parsed tried to create a new star system with the same name as an existing one.") with self.assertRaises(ValueError) as e: s.apply(1, L.WholeMove('build at Bar')) self.assertEqual(str(e.exception), "The move as parsed was incomplete or ambiguous.") with self.assertRaises(ValueError) as e: s.apply(0, L.WholeMove('build y1 at Foo')) self.assertEqual(str(e.exception), "The move as parsed was disallowed by the rules.")
def test_lack_of_default_constructor(self): with self.assertRaises(TypeError) as e: L.GameState() self.assertEqual(str(e.exception), "function takes exactly 1 argument (0 given)")
def test_mutating_apply(self): s = L.GameState('Foo (0,b3r1) g3-\n' + 'Bar(1,b3y2) -r1y2') m = L.WholeMove('build g1') result = s.apply(0, m) self.assertEqual(result, None) self.assertEqual(s.toString(), 'Foo (0, r1b3) g1g3-\nBar (1, y2b3) -r1y2\n')
def test_impossible(self): with self.assertRaises(ValueError) as e: L.GameState('Foo (0, b1) b1b1-\nBar (1, r3) -b1\n') self.assertEqual(str(e.exception), "text did not parse as a game state")