예제 #1
0
class DecompilerTestCase(unittest.TestCase):
    def setUp(self):
        self.compiler = EveScriptCompiler()
        self.decompiler = EveScriptDecompiler()

    def tearDown(self):
        pass

    def assertDecompile(self, ast):
        """Assert the decompiled script can be compiled to the same AST."""
        decomp = self.decompiler.decompile(ast)
        recomp = self.compiler.compile(decomp)
        self.assertDictEqual(ast, recomp)

    def test_simple_script(self):
        ast = self.compiler.compile(simple_script)
        self.assertDecompile(ast)

    def test_complex_expr(self):
        ast = self.compiler.compile(complex_expr)
        self.assertDecompile(ast)

    def test_multi_conditions(self):
        ast = self.compiler.compile(multi_conditions)
        self.assertDecompile(ast)

    def test_multi_actions(self):
        ast = self.compiler.compile(multi_actions)
        self.assertDecompile(ast)

    def test_standalone_action(self):
        ast = self.compiler.compile(standalone_action)
        self.assertDecompile(ast)

    def test_nested_if(self):
        ast = self.compiler.compile(nested_if)
        self.assertDecompile(ast)
예제 #2
0
class ExecutorTestCase(unittest.TestCase):
    def setUp(self):
        for mock in [
                *list(actions.values()), *list(operators.values()),
                *list(variables.values())
        ]:
            mock.reset_mock()

        self.compiler = EveScriptCompiler()
        self.executor = EveScriptExecutor({
            'actions': actions,
            'operators': operators,
            'variables': variables,
        })

    def tearDown(self):
        pass

    def test_simple_script(self):
        variables['$var1'].return_value = 1

        ast = self.compiler.compile(simple_script)
        self.executor.run_script(ast)

        variables['$var1'].assert_called()
        actions['say'].assert_called_with('$var1 > 0')

    def test_multi_actions(self):
        variables['$var1'].return_value = 1
        variables['$var2'].return_value = -1

        ast = self.compiler.compile(multi_actions)
        self.executor.run_script(ast)

        variables['$var1'].assert_called()
        variables['$var2'].assert_called()
        actions['say'].assert_called_with('$var1 > 0')
        actions['play'].assert_called_with('$var2 < 0')

    def test_complex_expr(self):
        variables['$var2'].return_value = -1
        variables['$var3'].return_value = 0
        operators['match'].return_value = True

        ast = self.compiler.compile(complex_expr)
        self.executor.run_script(ast)

        variables['$var1'].assert_called()
        variables['$var2'].assert_called()
        variables['$var3'].assert_called()
        operators['match'].assert_called()
        actions['say'].assert_called_with('success')

    def test_complex_expr_fail(self):
        variables['$var2'].return_value = 1
        variables['$var3'].return_value = 0
        operators['match'].return_value = False

        ast = self.compiler.compile(complex_expr)
        self.executor.run_script(ast)

        variables['$var1'].assert_called()
        variables['$var2'].assert_called()
        variables['$var3'].assert_called()
        operators['match'].assert_called()
        actions['say'].assert_not_called()

    def test_invalid_action(self):
        variables['$var1'].return_value = 1
        ast = self.compiler.compile(ivnalid_action)

        with self.assertRaises(InvalidAction):
            self.executor.run_script(ast)

    def test_invalid_variable(self):
        variables['$var1'].return_value = 1
        ast = self.compiler.compile(ivnalid_variable)

        with self.assertRaises(InvalidVariable):
            self.executor.run_script(ast)

    def test_invalid_operator(self):
        variables['$var1'].return_value = 1
        ast = self.compiler.compile(ivnalid_operator)

        with self.assertRaises(InvalidOperator):
            self.executor.run_script(ast)
예제 #3
0
class CompilerTestCase(unittest.TestCase):
    def setUp(self):
        self.compiler = EveScriptCompiler()

    def tearDown(self):
        pass

    def test_simple_script(self):
        ast = self.compiler.compile(simple_script)
        self.assertEqual(ast, simple_script_ast)

    def test_constant_expr(self):
        ast = self.compiler.compile(constant_expr)
        self.assertEqual(ast, constant_expr_ast)

    def test_complex_expr(self):
        ast = self.compiler.compile(complex_expr)
        self.assertEqual(ast['statements'][0]['if'], complex_expr_ast)

    def test_multi_conditions(self):
        ast = self.compiler.compile(multi_conditions)
        self.assertEqual(ast, multi_conditions_ast)

    def test_multi_actions(self):
        ast = self.compiler.compile(multi_actions)
        self.assertEqual(ast, multi_actions_ast)

    def test_zero_actions(self):
        ast = self.compiler.compile(zero_actions)
        self.assertEqual(ast, zero_actions_ast)

    def test_empty_param_action(self):
        ast = self.compiler.compile(empty_param_action)
        self.assertEqual(ast, empty_param_action_ast)

    def test_syntax_error(self):
        with self.assertRaises(antlr4.error.Errors.ParseCancellationException):
            self.compiler.compile(syntax_error, raise_exceptions=True)

    def test_comment(self):
        ast = self.compiler.compile(comment, True)
        self.assertEqual(ast, comment_ast)

    def test_standalone_action(self):
        ast = self.compiler.compile(standalone_action, True)
        self.assertEqual(ast, standalone_action_ast)

    def test_nested_if(self):
        ast = self.compiler.compile(nested_if, True)
        self.assertEqual(ast, nested_if_ast)

    def test_if_else(self):
        ast = self.compiler.compile(if_else, True)
        self.assertEqual(ast, if_else_ast)
예제 #4
0
import os
import sys

from defs.actions import ACTIONS
from defs.operators import OPERATORS
from defs.variables import VARIABLES

cwd = os.path.dirname(__file__)

try:
    from evescript.compiler import EveScriptCompiler
    from evescript.executor import EveScriptExecutor
except ModuleNotFoundError:
    sys.path.append(os.path.normpath(os.path.join(cwd, '..', 'src')))
    from evescript.compiler import EveScriptCompiler
    from evescript.executor import EveScriptExecutor

with open('script.es') as f:
    script = f.read()

compiler = EveScriptCompiler()
compiled_script = compiler.compile(script)

executor = EveScriptExecutor({
    'actions': ACTIONS,
    'operators': OPERATORS,
    'variables': VARIABLES,
})

executor.run_script(compiled_script)