def test_create_empty(self): bag = LetterBag() self.assertTrue(bag.contentCount() == 0, "empty bag contentCount() is zero") try: l = bag.remove('c') self.assertTrue(False, "able to remove letter from empty bag") except Exception, e: self.assertTrue(True, "exception trying to remove from empty bag")
def test_create_empty(self): bag = LetterBag() self.assertTrue( bag.contentCount() == 0, "empty bag contentCount() is zero") try: l = bag.remove('c') self.assertTrue(False, "able to remove letter from empty bag") except Exception, e: self.assertTrue(True, "exception trying to remove from empty bag")
def testShortGame(self): self.setUp() repository = GameStateRepository() joe = User.create('joe', '*****@*****.**') steve = User.create('steve', '*****@*****.**') v = PrintView() game = GameState.create() game.addPlayer(joe) game.addPlayer(steve) for p in game.players: p.bag = LetterBag.fromString('catgegae') game.start() self.assertTrue(game.leader().score == 0, "leader score==0") repository.register(game) id = repository.id(game) self.assertTrue(id != None, 'registering with repository set game id') game2 = repository.findById(id) self.assertTrue(game2.leader().score == 0, "leader score==0") game2.playWord(joe,0,0,True,'cat') repository.update(game2) game3 = repository.findById(id) # same id retrieves game2 update self.assertTrue(game3.leader().score == 4, "same id retrieves update") self.tearDown()
def test_removeRandom(self): letters = 'thequickfoxtheolddog' total = len(letters) bag = LetterBag.fromString(letters) self.assertTrue(bag.contentCount() == total, '{} letters in {}'.format(total, letters)) bag2 = bag.removeRandom(7) expectedRemaining = total - 7 self.assertTrue(bag.contentCount() == expectedRemaining, '{} left after removing 7'.format(expectedRemaining)) bag3 = bag.removeRandom(expectedRemaining) self.assertTrue(bag.contentCount() == 0, 'none left after removing all') bag2.addAll(bag3) for i in range(total): bag2.remove(letters[i]) self.assertTrue(bag2.contentCount() == 0, 'found all letters in bag2')
def testScoreOnlyWhenAddingLetters(self): joe = self.joe steve = self.steve v = PrintView() game = self.game for p in game.players: p.bag = LetterBag.fromString('catgegae') self.assertTrue(game.leader().score == 0, "leader score==0") game.playWord(joe, 0, 0, True, 'cat') self.assertTrue(game.scoreForUser(joe) == 4, "score for cat is 4") try: game.playWord(steve, 0, 0, True, 'cat') self.assertTrue( False, 'playing word already played should raise exception') except ValueError, e: self.assertTrue( True, 'playing word already played raised error: {}'.format(e))
def testShortGame(self): joe = self.joe steve = self.steve v = PrintView() game = self.game for p in game.players: p.bag = LetterBag.fromString('catgegae') self.assertTrue(game.leader().score == 0, "leader score==0") game.playWord(joe, 0, 0, True, 'cat') game.playWord(steve, 0, 0, False, 'cage') game.playWord(joe, 0, 2, True, 'gag') game.playWord(steve, 2, 2, False, 'gate') game.skipTurn(joe) game.skipTurn(steve) v.showGameState(game) self.assertTrue(game.gameOver(), "game is over") self.assertTrue(game.leader().player.name == 'steve', "steve is the winner") self.assertTrue(game.leader().score == 13, "winner score is 13")
def testScoreOnlyWhenAddingLetters(self): joe = self.joe steve = self.steve v = PrintView() game = self.game for p in game.players: p.bag = LetterBag.fromString('catgegae') self.assertTrue(game.leader().score == 0, "leader score==0") game.playWord(joe,0,0,True,'cat') self.assertTrue(game.scoreForUser(joe) == 4, "score for cat is 4") try: game.playWord(steve,0,0,True,'cat') self.assertTrue( False, 'playing word already played should raise exception') except ValueError, e: self.assertTrue( True, 'playing word already played raised error: {}'.format(e))
def test_removeRandom(self): letters = 'thequickfoxtheolddog' total = len(letters) bag = LetterBag.fromString(letters) self.assertTrue( bag.contentCount() == total, '{} letters in {}'.format(total,letters)) bag2 = bag.removeRandom(7) expectedRemaining = total - 7 self.assertTrue( bag.contentCount() == expectedRemaining, '{} left after removing 7'.format(expectedRemaining)) bag3 = bag.removeRandom(expectedRemaining) self.assertTrue( bag.contentCount() == 0, 'none left after removing all') bag2.addAll(bag3) for i in range(total): bag2.remove(letters[i]) self.assertTrue( bag2.contentCount() == 0, 'found all letters in bag2')
def testShortGame(self): joe = self.joe steve = self.steve v = PrintView() game = self.game for p in game.players: p.bag = LetterBag.fromString('catgegae') self.assertTrue(game.leader().score == 0, "leader score==0") game.playWord(joe,0,0,True,'cat') game.playWord(steve,0,0,False,'cage') game.playWord(joe,0,2,True,'gag') game.playWord(steve,2,2,False,'gate') game.skipTurn(joe) game.skipTurn(steve) v.showGameState(game) self.assertTrue(game.gameOver(), "game is over") self.assertTrue( game.leader().player.name == 'steve', "steve is the winner") self.assertTrue(game.leader().score == 13, "winner score is 13")
def test_letterAtIndex(self): bag = LetterBag.fromString('aaaccez') self.assertTrue(bag.letterAtIndex(0) == 'a', "bag has an 'a' at 0") self.assertTrue(bag.letterAtIndex(2) == 'a', "bag has an 'a' at 2") self.assertTrue(bag.letterAtIndex(6) == 'z', "bag has an 'z' at 6") self.assertTrue(bag.letterAtIndex(5) == 'e', "bag has an 'e' at 5")
def test_simple_example(self): bag = LetterBag() bag.add('a') self.assertTrue(bag.contentCount() == 1, "size is one after adding one") bag.add('z') self.assertTrue(bag.contentCount() == 2, "2 after adding second") bag.add('z') self.assertTrue(bag.contentCount() == 3, "3 after adding third") try: l = bag.remove('c') self.assertTrue(False, "able to remove letter not in bag") except Exception, e: self.assertTrue(True, "exception trying to remove letter not in bag")
def restoreTransients(self, state): """set transient values from persistent fields""" state.bagOfLetters = LetterBag.fromString(state.letters) state.boardContent = list(state.board) # list support random access return state
def test_simple_example(self): bag = LetterBag() bag.add('a') self.assertTrue( bag.contentCount() == 1, "size is one after adding one") bag.add('z') self.assertTrue(bag.contentCount() == 2, "2 after adding second") bag.add('z') self.assertTrue(bag.contentCount() == 3, "3 after adding third") try: l = bag.remove('c') self.assertTrue(False, "able to remove letter not in bag") except Exception, e: self.assertTrue( True, "exception trying to remove letter not in bag")
def restoreTransients(self, state): """set (derive) transient values from persistent fields""" state.game = state.gameKey.get() state.player = state.userKey.get() state.bag = LetterBag.fromString(state.letters) return state
def test_letterAtIndex(self): bag = LetterBag.fromString('aaaccez') self.assertTrue( bag.letterAtIndex(0) == 'a', "bag has an 'a' at 0") self.assertTrue( bag.letterAtIndex(2) == 'a', "bag has an 'a' at 2") self.assertTrue( bag.letterAtIndex(6) == 'z', "bag has an 'z' at 6") self.assertTrue( bag.letterAtIndex(5) == 'e', "bag has an 'e' at 5")