Attachment 'nimtest.py'
Download
Toggle line numbers
1 #!/usr/bin/env python
2
3 import unittest
4 from nim import *
5
6 class GameTests(unittest.TestCase):
7 def testEmptyGame(self):
8 game = Game([])
9 self.assertEquals(len(game.board), 0)
10
11 def testGameWithOnePileOfOneMatch(self):
12 game = Game([1])
13 self.assertEquals(len(game.board), 1)
14 self.assertEquals(game.board[0], 1)
15
16 def testGameWithTwoPiles(self):
17 game = Game([1, 1])
18 self.assertEquals(len(game.board), 2)
19
20 def testTakeOneMatchFromOnePile(self):
21 game = Game([1])
22 win = game.takeMatches(matches=1, pile=0)
23 self.assertEquals(game.board[0], 0)
24 self.assertEquals(win, True)
25 self.assertEquals(len(game.board), 1)
26
27 def testTakeOneMatchFromTwoPiles(self):
28 game = Game([1, 6])
29 win = game.takeMatches(matches=1, pile=0)
30 self.assertEquals(game.board[1], 6)
31 self.assertEquals(win, False)
32 self.assertEquals(len(game.board), 2)
33
34 def testTakeTwoMatchesWhenTheresOnlyOne(self):
35 game = Game([1])
36 self.assertRaises(NotEnoughMatchesError, game.takeMatches, matches=2, pile=0)
37
38 def testTakeNegativeMatches(self):
39 game = Game([1])
40 try:
41 win = game.takeMatches(matches=-1, pile=0)
42 self.fail()
43 except MustTakePositiveMatchesError, e:
44 # Expected
45 pass
46
47 def testTakeZeroMatches(self):
48 game = Game([1])
49 try:
50 win = game.takeMatches(matches=0, pile=0)
51 self.fail()
52 except MustTakePositiveMatchesError, e:
53 # Expected
54 pass
55
56
57
58 if __name__ == "__main__":
59 unittest.main()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.