def test_multiplication(self): self.assertTrue(state.is_done(ircbot.cmd_calc("1 * 1"))) self.assertEqual(ircbot.cmd_calc("1 * 1").value, "1") self.assertTrue(state.is_done(ircbot.cmd_calc("-1 * 1"))) self.assertEqual(ircbot.cmd_calc("-1 * 1").value, "-1") self.assertTrue(state.is_done(ircbot.cmd_calc("0.1 * 0.1"))) self.assertEqual(ircbot.cmd_calc("0.1 * 0.1").value, "0.01")
def test_division(self): self.assertTrue(state.is_done(ircbot.cmd_calc("1 / 1"))) self.assertEqual(ircbot.cmd_calc("1 / 1").value, "1") self.assertTrue(state.is_done(ircbot.cmd_calc("1 / 2"))) self.assertEqual(ircbot.cmd_calc("1 / 2").value, "0") self.assertTrue(state.is_done(ircbot.cmd_calc("1.0 / 2"))) self.assertEqual(ircbot.cmd_calc("1.0 / 2").value, "0.5")
def test_addition(self): self.assertTrue(state.is_done(ircbot.cmd_calc("1 + 1"))) self.assertEqual(ircbot.cmd_calc("1 + 1").value, "2") self.assertTrue(state.is_done(ircbot.cmd_calc("-1 + 1"))) self.assertEqual(ircbot.cmd_calc("-1 + 1").value, "0") self.assertTrue(state.is_done(ircbot.cmd_calc("0.5 + 0.5"))) self.assertEqual(ircbot.cmd_calc("0.5 + 0.5").value, "1.0")
def test_subtraction(self): self.assertTrue(state.is_done(ircbot.cmd_calc("1 - 1"))) self.assertEqual(ircbot.cmd_calc("1 - 1").value, "0") self.assertTrue(state.is_done(ircbot.cmd_calc("-1 - 1"))) self.assertEqual(ircbot.cmd_calc("-1 - 1").value, "-2") self.assertTrue(state.is_done(ircbot.cmd_calc("0.6 - 0.5"))) self.assertEqual(ircbot.cmd_calc("0.6 - 0.5").value, "0.1")
def parse(msg): """ Parses given input. @param msg: input message to parse @type msg: str @return: return message processed with associated commands and filters @rtype: str """ try: cmd, args = msg.split(" ", 1) except ValueError: cmd, args = msg.rstrip(), "" if cmd in COMMANDS: ret = COMMANDS[cmd](args) if state.is_done(ret): return ret.value elif state.is_next(ret): pass else: raise Exception("Illegal state of command.") for filter in FILTERS: ret = filter(msg) if state.is_done(ret): return ret.value if state.is_replace(ret): msg = ret.value return msg
def test_custom(self): ircbot.WORDCOUNT = 5 self.assertEqual(ircbot.WORDCOUNT, 5) self.assertTrue(state.is_done(ircbot.cmd_word_count())) self.assertEqual(ircbot.cmd_word_count(None).value, 'Actual word count is ' + str(ircbot.WORDCOUNT) + ' words.')
def test_correct_parenthesis(self): self.assertTrue(state.is_done(ircbot.cmd_calc("2 * (1 + 2)"))) self.assertEqual(ircbot.cmd_calc("2 * (1 + 2)").value, "6")
def test_single_negative_number_float(self): self.assertTrue(state.is_done(ircbot.cmd_calc("-1.0"))) self.assertEqual(ircbot.cmd_calc("-1.0").value, "-1.0")
def test_not_state(self): self.assertFalse(state.is_done(object()))
def test_not_done(self): self.assertFalse(state.is_done(state.replace(None))) self.assertFalse(state.is_done(state.next(None)))
def test_done(self): self.assertTrue(state.is_done(state.done(None)))
def test_single_positive_number(self): self.assertTrue(state.is_done(ircbot.cmd_calc("1"))) self.assertEquals(ircbot.cmd_calc("1").value, "1")
def test_default(self): self.assertTrue(state.is_done(ircbot.cmd_karma('foo'))) self.assertEqual(ircbot.cmd_karma('foo').value, "'foo' has no karma.") ircbot.KARMA = { 'foo': 5 } self.assertTrue(state.is_done(ircbot.cmd_karma('foo'))) self.assertEqual(ircbot.cmd_karma('foo').value, "'foo' has 5 points of karma.")
def test_minus_minus(self): self.assertTrue(state.is_done(ircbot.f_karma('bar--'))) self.assertEqual(ircbot.KARMA['bar'], -1)
def test_plus_plus(self): self.assertTrue(state.is_done(ircbot.f_karma('foo++'))) self.assertEqual(ircbot.KARMA['foo'], 1)