def setUp(self): self.u1 = User('fred') self.u2 = User('tony') c1 = EmuChoice('start*8', [self.u1]) c2 = EmuChoice('up*1', [self.u2]) c3 = EmuChoice('down*6 up*3') self.choices = Choices(c1, c2, c3)
def setUp(self): self.vm1 = InputManager(threshold=1, on_decision=decision) self.vm2 = InputManager(threshold=2, on_decision=decision) self.u1 = User('bob') self.u2 = User('frank') self.c1 = EmuChoice('start*9') self.c2 = EmuChoice('A*3')
def test_equals(self): fail_msg = 'EmuChoice constructor failed' n1 = 'down*7' v1 = [self.u1, self.u2] n2 = 'start*3' v2 = [self.u2, self.u3] c1 = EmuChoice(n1, v1) c2 = EmuChoice(n1, v2) c3 = EmuChoice(n2, v1) c4 = EmuChoice(n2, v2) c5 = EmuChoice(n1, v1) self.assertEqual(c1, c1, fail_msg) self.assertEqual(c2, c2, fail_msg) self.assertEqual(c3, c3, fail_msg) self.assertEqual(c4, c4, fail_msg) self.assertEqual(c1, c5, fail_msg) self.assertEqual(c1.name, n1, fail_msg) self.assertEqual(c2.name, n1, fail_msg) self.assertEqual(c3.name, n2, fail_msg) self.assertEqual(c4.name, n2, fail_msg) self.assertNotEqual(c1, c2, fail_msg) self.assertNotEqual(c2, c3, fail_msg) self.assertNotEqual(c3, c4, fail_msg) self.assertNotEqual(c1, c4, fail_msg)
def test_serialize(self): fail_msg = 'User serialize failed' user = User('bob') choice = EmuChoice('attack*1', [user]) ser = choice.serialize() deser = EmuChoice.deserialize(ser) self.assertEqual(choice, deser, fail_msg)
def test_vote(self): fail_msg = 'EmuChoice vote failed' choice = EmuChoice('run*1') self.assertFalse(choice.voters, fail_msg) choice.add_vote(self.u1) self.assertEqual(choice.votes, 1, fail_msg) self.assertIn(self.u1, choice.voters, fail_msg)
def test_serialize(self): c1 = EmuChoice('start*9') c2 = EmuChoice('A*6') c3 = EmuChoice('down*1') choices = Choices(c1, c2, c3) ser = choices.serialize() deser = Choices.deserialize(ser) self.assertEqual(choices, deser, 'Choices serialize failed')
def cast_vote(self, user, choice_name): ''' Cast vote towards choice_name for the given user and return True if decision is reached (i.e., vote count for choice exceeds threshold after vote). :param user: User casting the vote. :param choice_name: Name of choice to vote for. ''' choice = EmuChoice(choice_name) if choice.name not in self._choices: self._choices.add_choice(choice_name) self._choices.remove_vote(user) self._choices.add_vote(user, choice.name) choice = self._choices.get_choice(choice.name) exceeded = choice.votes >= self.threshold if self._vote: self._vote(self) if exceeded: self._decision(choice) self._choices.clear_votes() return exceeded
def add_choice(self, choice_name): ''' Adds choice to list. :param choice_name: Name of choice to add. ''' choice = EmuChoice(choice_name) self._choices[choice.name] = choice return True
def deserialize(serialized): fields = json.loads(serialized) fields = (EmuChoice.deserialize(choice) for choice in fields) return Choices(*fields)
def test_equals(self): fail_msg = 'Choices equals failed' choices = Choices(EmuChoice('start*9')) self.assertEqual(self.choices, self.choices, fail_msg) self.assertNotEqual(self.choices, choices, fail_msg)
def setUp(self): self.buttons = EmuChoice('start*9') self.cheat = EmuChoice('attack')