コード例 #1
0
 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]')
コード例 #2
0
    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) + '>')
コード例 #3
0
 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) + '>')
コード例 #4
0
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'))
コード例 #5
0
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')
コード例 #6
0
 def test_command_creation_success(self):
     Command('command', lambda arg_list: True)
     Command('command', lambda arg_list: True,
             [string_lexeme, integer_lexeme])
コード例 #7
0
 def setUp(self):
     self.command = Command('teleport-to', lambda arg_list: arg_list,
                            [string_lexeme, string_lexeme])
コード例 #8
0
 def test_command_creation_func_type(self):
     Command('command', type)
コード例 #9
0
 def test_command_creation_func_partial(self):
     f = functools.partial(int, base=2)
     Command('command', f)
コード例 #10
0
 def test_command_creation_func_builtin(self):
     Command('command', open)
コード例 #11
0
 def setUp(self):
     self.command = Command('command1', lambda arg_list: arg_list)
     self.command2 = Command('command2', lambda arg_list: arg_list[::-1])