def setUp(self): self.command = Command('command1', lambda arg_list: arg_list, [string_lexeme, string_lexeme]) self.command2 = Command('command2', lambda arg_list: arg_list[::-1], [string_lexeme, number_lexeme]) self.interpreter = Interpreter([self.command, self.command2]) self.interpreter2 = Interpreter([self.command, self.command2], trim_strings=False) self.interpreter3 = Interpreter([self.command, self.command2], disallowed_chars=r'[a-z]')
def test_interpreter_repr_success(self): command = Command('command1', lambda arg_list: arg_list, [string_lexeme, string_lexeme]) command2 = Command('command2', lambda arg_list: arg_list[::-1], [string_lexeme, number_lexeme]) interpreter = Interpreter([command, command2]) self.assertEqual( repr(interpreter), '<Interpreter commands=' + repr(interpreter.commands) + ' command_char=' + repr(interpreter.command_char) + ' trim_strings=' + repr(interpreter.trim_strings) + ' disallowed_chars=' + repr(interpreter.disallowed_chars) + '>')
def test_command_repr_success(self): command = Command('teleport-to', lambda arg_list: arg_list, [string_lexeme, integer_lexeme]) self.assertEqual( repr(command), '<Command command_word=' + repr(command.command_word) + ' func=' + repr(command.func) + ' expects=' + repr(command.expects) + '>')
class TestCommandRemoveCW(unittest.TestCase): def setUp(self): self.command = Command('teleport-to', lambda arg_list: arg_list, [string_lexeme, string_lexeme]) def test_command_remove_CW_success(self): self.assertEqual( self.command.try_remove_command_word('teleport-to u1 u2'), (True, 'u1 u2')) self.assertEqual( self.command.try_remove_command_word(' teleport-to u1 u2 '), (True, 'u1 u2 ')) def test_command_remove_CW_string_not_str(self): self.assertRaises(TypeError, self.command.try_remove_command_word, 12) def test_command_remove_CW_string_less_than_CW(self): self.assertFalse(self.command.try_remove_command_word('telep')) self.assertFalse(self.command.try_remove_command_word(' telep')) def test_command_remove_CW_no_CW(self): self.assertFalse(self.command.try_remove_command_word('teleport-ta')) self.assertFalse( self.command.try_remove_command_word(' teleport-ta'))
from comengine import string_lexeme, integer_lexeme, Lexeme, Command, Interpreter from random import randint def random_number(arg_list): return randint(0, 100) capital_letters_lexeme = Lexeme('[A-Z]+') # one or more capital letters teleport = Command('tp', lambda arg_list: print('Teleported', arg_list[0], 'to', arg_list[1]), [string_lexeme, string_lexeme]) say = Command('say', lambda arg_list: print('User #', arg_list[0], 'said', arg_list[1]), [integer_lexeme, string_lexeme]) sum = Command('sum', lambda arg_list: int(arg_list[0]) + int(arg_list[1]), [integer_lexeme, integer_lexeme]) rand = Command('rand', random_number) scream = Command('scream', lambda arg_list: print('AARGH!', arg_list[0]), [capital_letters_lexeme]) interpreter = Interpreter([teleport, say, sum, rand, scream]) result1 = interpreter.interpret('/tp player1 player2')
def test_command_creation_success(self): Command('command', lambda arg_list: True) Command('command', lambda arg_list: True, [string_lexeme, integer_lexeme])
def setUp(self): self.command = Command('teleport-to', lambda arg_list: arg_list, [string_lexeme, string_lexeme])
def test_command_creation_func_type(self): Command('command', type)
def test_command_creation_func_partial(self): f = functools.partial(int, base=2) Command('command', f)
def test_command_creation_func_builtin(self): Command('command', open)
def setUp(self): self.command = Command('command1', lambda arg_list: arg_list) self.command2 = Command('command2', lambda arg_list: arg_list[::-1])