Guest User

Untitled

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. from mock import Mock
  2. from placidity.interpreter import Interpreter
  3. from placidity.utils import Operations
  4.  
  5. class AbstractTestInterpreter:
  6.  
  7. def setup_method(self, method):
  8. interpreter = Interpreter()
  9. self.interpret = interpreter.interpret
  10.  
  11. class TestInterpreter(AbstractTestInterpreter):
  12. def test_execute_commands(self):
  13. def execute_with_commands(commands):
  14. assert commands == [command, ]
  15. return 'executed command'
  16.  
  17. def execute_with_variables(variables):
  18. assert variables == {}
  19. return 'executed command'
  20.  
  21. execute_methods = (execute_with_commands, execute_with_variables, )
  22.  
  23. command = self.create_command('command')
  24. interpreter = Interpreter(command)
  25.  
  26. for execute_method in execute_methods:
  27. command.execute = execute_method
  28.  
  29. assert interpreter.interpret('command') == 'executed command'
  30. command.matches.assert_called_with('command')
  31.  
  32. def test_execute_with_multiple_commands(self):
  33. def execute_with_commands(commands):
  34. assert commands == [command1, command2, ]
  35. return 'executed command'
  36.  
  37. def execute_with_variables(variables):
  38. assert variables == {}
  39. return 'executed command'
  40.  
  41. command1 = self.create_command('foo', execute_with_commands)
  42. command2 = self.create_command('bar', execute_with_variables)
  43. interpreter = Interpreter([command1, command2, ])
  44.  
  45. assert interpreter.interpret('foo') == 'executed command'
  46.  
  47. def create_command(self, name, execute_method=None):
  48. command = Mock()
  49. command.aliases = name
  50. command.matches = Mock()
  51. command.matches.return_value = True
  52.  
  53. if execute_method:
  54. command.execute = execute_method
  55.  
  56. return command
  57.  
  58. class TestSetVariable(AbstractTestInterpreter):
  59.  
  60. def test_set(self):
  61. self.interpret('a=6')
  62. assert self.interpret('a') == 6
  63.  
  64. def test_set_expression(self):
  65. self.interpret('a=4*3')
  66. assert self.interpret('a') == 12
  67.  
  68. def test_set_variable(self):
  69. self.interpret('a=8')
  70. self.interpret('b=a')
  71. assert self.interpret('b') == 8
  72.  
  73. def test_variable_in_expression(self):
  74. self.interpret('a=12')
  75. assert self.interpret('a+3') == 15
  76.  
  77. class TestUnsetVariable(AbstractTestInterpreter):
  78.  
  79. def test_unset_variable(self):
  80. assert self.interpret('a') == 'null'
  81.  
  82. def test_variable_in_expression(self):
  83. assert self.interpret('a+3') == 'null'
  84.  
  85. class TestMath(AbstractTestInterpreter):
  86. operations = Operations(('1+1', 2), ('5-1', 4), ('3*5', 15), ('12/4', 3), )
  87.  
  88. def test_operations(self):
  89. for operation in self.operations:
  90. assert self.interpret(operation.expression) == operation.result
Add Comment
Please, Sign In to add comment