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_none(self): self.assertTrue(state.is_next(ircbot.cmd_calc(None)))
def test_incorrect_parenthesis(self): self.assertTrue(state.is_next(ircbot.cmd_calc("2 * ((1 + 2)"))) self.assertEqual(ircbot.cmd_calc("2 * ((1 + 2)").value, "2 * ((1 + 2)")
def test_division_by_zero(self): self.assertTrue(state.is_next(ircbot.cmd_calc("1 / 0"))) self.assertEqual(ircbot.cmd_calc("1 / 0").value, "1 / 0") self.assertTrue(state.is_next(ircbot.cmd_calc("1.0 / 0"))) self.assertEqual(ircbot.cmd_calc("1.0 / 0").value, "1.0 / 0")
def test_not_state(self): self.assertFalse(state.is_next(object()))
def test_not_next(self): self.assertFalse(state.is_next(state.done(None))) self.assertFalse(state.is_next(state.replace(None)))
def test_next(self): self.assertTrue(state.is_next(state.next(None)))
def test_not_math(self): self.assertTrue(state.is_next(ircbot.cmd_calc("foo +1")))
def test_minus_minus(self): self.assertTrue(state.is_next(ircbot.f_karma('bar--'))) self.assertEqual(ircbot.KARMA['bar'], -1)
def test_plus_plus(self): self.assertTrue(state.is_next(ircbot.f_karma('foo++'))) self.assertEqual(ircbot.KARMA['foo'], 1)
def test_triple(self): old_count = ircbot.WORDCOUNT self.assertTrue(state.is_next(ircbot.f_word_count('foo bar baz'))) self.assertEqual(ircbot.WORDCOUNT, old_count + 3)
def test_single(self): old_count = ircbot.WORDCOUNT self.assertTrue(state.is_next(ircbot.f_word_count('foo'))) self.assertEqual(ircbot.WORDCOUNT, old_count + 1)