def test_no_return(self): def execute(): pass command = self.create_command('foo', execute_method=execute) interpreter = Interpreter(command) assert interpreter.interpret('foo') == None
def test_execute_command(self): def execute(): return 'executed command' command1 = self.create_command('foo', execute_method=execute) command2 = self.create_command('bar', execute_method=execute) interpreter = Interpreter([command1, command2, ]) assert interpreter.interpret('foo') == 'executed command'
def test_execute_with_multiple_commands(self): def execute_with_commands(commands): assert commands == [command1, command2, ] return 'executed command' def execute_with_variables(variables): assert variables == {} return 'executed command' command1 = self.create_command('foo', execute_with_commands) command2 = self.create_command('bar', execute_with_variables) interpreter = Interpreter([command1, command2, ]) assert interpreter.interpret('foo') == 'executed command'
def test_priority(self): def execute_1(): return 'foo' command1 = self.create_command('bar', 'high', execute_1) def execute_2(): return 'BYE' command2 = self.create_command('bar', 'normal', execute_2) def execute_3(): return 'BYEBYE' command3 = self.create_command('bar', 'low', execute_3) interpreter = Interpreter([command1, command2, command3]) assert interpreter.interpret('bar') == 'foo'
class TestInterpreter: def setup_method(self, method): self.interpreter = Interpreter() def test_sum(self): assert self.interpreter.interpret('1+1') == 2 def test_subtract(self): assert self.interpreter.interpret('5-1') == 4 def test_multiply(self): assert self.interpreter.interpret('3*5') == 15 def test_divide(self): assert self.interpreter.interpret('12/4') == 3
def test_context_owner_set(self): def execute_1(): return 'foo' command1 = self.create_command('foobar', execute_method=execute_1) def execute_2(expression): if expression == 'foobar': return None return 'bar' command2 = self.create_command('bar', execute_method=execute_2) interpreter = Interpreter([command1, command2]) interpreter.context.claim_for(command2) assert interpreter.interpret('foobar') == None assert interpreter.interpret('bar') == 'bar'
def test_execute_parameters(self): def no_parameters(): return 'executed command' def with_context(context): assert context.owner == None return 'executed command' def with_commands(commands): assert commands == [command, ] return 'executed command' def with_expression(expression): assert expression == 'command' return 'executed command' def with_variables(variables): assert variables == {} return 'executed command' def with_multiple_parameters(expression, commands, variables, context): assert context.owner == None assert commands == [command, ] assert expression == 'command' assert variables == {} return 'executed command' execute_methods = (no_parameters, with_context, with_commands, with_expression, with_variables, with_multiple_parameters, ) command = self.create_command('command') interpreter = Interpreter(command) for execute_method in execute_methods: command.execute = execute_method assert interpreter.interpret('command') == 'executed command', \ execute_method.__name__ + ' failed!' command.matches.assert_called_with('command')
def test_execute_commands(self): def execute_with_commands(commands): assert commands == [command, ] return 'executed command' def execute_with_variables(variables): assert variables == {} return 'executed command' execute_methods = (execute_with_commands, execute_with_variables, ) command = self.create_command('command') interpreter = Interpreter(command) for execute_method in execute_methods: command.execute = execute_method assert interpreter.interpret('command') == 'executed command' command.matches.assert_called_with('command')
def test_none(self): interpreter = Interpreter() assert interpreter.interpret(None) == None
def test_exception(self): interpreter = Interpreter() assert interpreter.interpret('foobar') == 'null'
def setup_method(self, method): self.interpreter = Interpreter()