def parse(self, msg): """ Parse a message, return relevant output. @param msg: unparsed message @type msg: str @return: message to print or None (if there's no output to print) @rtype: str|None """ # split message into first word + rest of line try: command, args = msg.split(None, 1) except ValueError: command, args = msg, "" if command in self.COMMANDS: stat = self.COMMANDS[command](args) if state.is_done(stat): return stat.value elif state.is_next(stat): print(stat.value) return None else: return None for func in self.FILTERS: stat = func(msg) if state.is_done(stat): return stat.value elif state.is_replace(stat): msg = stat.value return msg
def parse(self, 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 self.COMMANDS: ret = self.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 self.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(plugins.calculator.cmd_calc(None)))
def test_incorrect_parenthesis(self): self.assertTrue(state.is_next(plugins.calculator.cmd_calc("2 * ((1 + 2)"))) self.assertEqual(plugins.calculator.cmd_calc("2 * ((1 + 2)").value, "2 * ((1 + 2)")
def test_division_by_zero(self): self.assertTrue(state.is_next(plugins.calculator.cmd_calc("1 / 0"))) self.assertEqual(plugins.calculator.cmd_calc("1 / 0").value, "1 / 0") self.assertTrue(state.is_next(plugins.calculator.cmd_calc("1.0 / 0"))) self.assertEqual(plugins.calculator.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(plugins.calculator.cmd_calc("foo +1")))
def test_minus_minus(self): self.assertTrue(state.is_next(plugins.karma.f_karma("bar--"))) self.assertEqual(plugins.karma.KARMA["bar"], -1)
def test_plus_plus(self): self.assertTrue(state.is_next(plugins.karma.f_karma("foo++"))) self.assertEqual(plugins.karma.KARMA["foo"], 1)
def test_triple(self): old_count = plugins.wordcount.WORDCOUNT self.assertTrue(state.is_next(plugins.wordcount.f_word_count("foo bar baz"))) self.assertEqual(plugins.wordcount.WORDCOUNT, old_count + 3)
def test_single(self): old_count = plugins.wordcount.WORDCOUNT self.assertTrue(state.is_next(plugins.wordcount.f_word_count("foo"))) self.assertEqual(plugins.wordcount.WORDCOUNT, old_count + 1)
def test_plus_plus(self): self.assertTrue(state.is_next(plugins.karma.f_karma('foo++'))) self.assertEqual(plugins.karma.KARMA['foo'], 1)
def test_double(self): old_count = plugins.wordcount.WORDCOUNT self.assertTrue(state.is_next(plugins.wordcount.f_word_count('foo bar'))) self.assertEqual(plugins.wordcount.WORDCOUNT, old_count + 2)