Exemple #1
0
    def test_tear_down(self):
        """
        It should call __exit__
        """
        vm = VM(platform)

        with patch.object(vm, '__exit__', autospec=True) as r:
            vm.tear_down()
            r.assert_called_once_with()
Exemple #2
0
    def test_tear_down(self):
        """
        It should call __exit__
        """
        vm = VM(platform)

        with patch.object(vm, '__exit__', autospec=True) as r:
            vm.tear_down()
            r.assert_called_once_with()
Exemple #3
0
    def test_set_up(self):
        """
        It should call __enter__
        """
        vm = VM(platform)

        with patch.object(vm, '__enter__', autospec=True) as r:
            r.return_value = 'foo'
            self.assertEqual(vm.set_up(), 'foo')
            r.assert_called_once_with()
Exemple #4
0
    def test_set_up(self):
        """
        It should call __enter__
        """
        vm = VM(platform)

        with patch.object(vm, '__enter__', autospec=True) as r:
            r.return_value = 'foo'
            self.assertEqual(vm.set_up(), 'foo')
            r.assert_called_once_with()
Exemple #5
0
    def test_is_alive(self):
        """
        It should be alive within a with context
        """
        vm = VM(platform)
        self.assertFalse(vm.is_alive())

        with VM(platform) as vm:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())
Exemple #6
0
    def test_assert_on_re_create(self):
        """
        It should allow to re create a vm
        """
        vm = VM(platform)

        with vm as _:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())

        with vm as _:
            self.assertTrue(vm.is_alive())
Exemple #7
0
    def test_assert_on_re_create(self):
        """
        It should allow to re create a vm
        """
        vm = VM(platform)

        with vm as _:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())

        with vm as _:
            self.assertTrue(vm.is_alive())
Exemple #8
0
    def test_assert_on_re_enter(self):
        """
        It should fail to re enter
        """
        vm = VM(platform)

        with vm as _:
            self.assertRaises(AssertionError, vm.__enter__)
Exemple #9
0
    def test_assert_on_re_exit(self):
        """
        It should fail to re exit
        """
        vm = VM(platform)

        self.assertRaises(AssertionError, vm.__exit__)

        with vm as _:
            pass

        self.assertRaises(AssertionError, vm.__exit__)
Exemple #10
0
    def test_with_exceptions(self):
        """
        It should raise V8 exceptions
        """
        code_error = vm_module.lib.E_V8_JS_ERROR
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            r.v8cffi_vm_new = Mock(return_value=code_error)
            r.E_V8_OK = code_ok

            self.assertRaises(exceptions.V8JSError, VM(platform).__enter__)
Exemple #11
0
    def test_is_alive(self):
        """
        It should be alive within a with context
        """
        vm = VM(platform)
        self.assertFalse(vm.is_alive())

        with VM(platform) as vm:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())
Exemple #12
0
    def test_allocate_and_free(self):
        """
        It should allocate and free the C VM
        """
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            vm = VM(platform)

            vm_new = Mock(return_value=code_ok)
            r.v8cffi_vm_new = vm_new
            r.E_V8_OK = code_ok
            vm.__enter__()
            self.assertTrue(vm_new.called)

            vm_free = Mock()
            r.v8cffi_vm_free = vm_free
            vm.__exit__()
            self.assertTrue(vm_free.called)

            self.assertFalse(vm.is_alive())
            self.assertRaises(AssertionError, vm.__exit__)
Exemple #13
0
    def test_allocate_and_free(self):
        """
        It should allocate and free the C VM
        """
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            vm = VM(platform)

            vm_new = Mock(return_value=code_ok)
            r.v8cffi_vm_new = vm_new
            r.E_V8_OK = code_ok
            vm.__enter__()
            self.assertTrue(vm_new.called)

            vm_free = Mock()
            r.v8cffi_vm_free = vm_free
            vm.__exit__()
            self.assertTrue(vm_free.called)

            self.assertFalse(vm.is_alive())
            self.assertRaises(AssertionError, vm.__exit__)
Exemple #14
0
class VMTest(unittest.TestCase):
    def setUp(self):
        self.vm = VM(platform)
        self.vm.set_up()

    def tearDown(self):
        self.vm.tear_down()

    def test_keep_platform(self):
        """
        It should keep a reference to the platform
        """
        self.assertEqual(platform, self.vm._platform)

    def test_with(self):
        """
        It should support with statement
        """
        with VM(platform) as vm:
            self.assertIsInstance(vm, VM)

    def test_is_alive(self):
        """
        It should be alive within a with context
        """
        vm = VM(platform)
        self.assertFalse(vm.is_alive())

        with VM(platform) as vm:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())

    def test_create_context(self):
        """
        It should create a Context
        """
        self.assertIsInstance(self.vm.create_context(), context.Context)

        with patch('v8cffi.context.Context', autospec=True) as r:
            r.return_value = None
            self.vm.create_context()
            r.assert_called_once_with(self.vm)

    def test_get_c_vm(self):
        """
        It should return the C VM
        """
        self.assertEqual(self.vm._c_vm, self.vm.get_c_vm())

    def test_set_up(self):
        """
        It should call __enter__
        """
        vm = VM(platform)

        with patch.object(vm, '__enter__', autospec=True) as r:
            r.return_value = 'foo'
            self.assertEqual(vm.set_up(), 'foo')
            r.assert_called_once_with()

    def test_tear_down(self):
        """
        It should call __exit__
        """
        vm = VM(platform)

        with patch.object(vm, '__exit__', autospec=True) as r:
            vm.tear_down()
            r.assert_called_once_with()

    def test_allocate_and_free(self):
        """
        It should allocate and free the C VM
        """
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            vm = VM(platform)

            vm_new = Mock(return_value=code_ok)
            r.v8cffi_vm_new = vm_new
            r.E_V8_OK = code_ok
            vm.__enter__()
            self.assertTrue(vm_new.called)

            vm_free = Mock()
            r.v8cffi_vm_free = vm_free
            vm.__exit__()
            self.assertTrue(vm_free.called)

            self.assertFalse(vm.is_alive())
            self.assertRaises(AssertionError, vm.__exit__)

    def test_with_exceptions(self):
        """
        It should raise V8 exceptions
        """
        code_error = vm_module.lib.E_V8_JS_ERROR
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            r.v8cffi_vm_new = Mock(return_value=code_error)
            r.E_V8_OK = code_ok

            self.assertRaises(exceptions.V8JSError, VM(platform).__enter__)

    def test_assert_on_re_enter(self):
        """
        It should fail to re enter
        """
        vm = VM(platform)

        with vm as _:
            self.assertRaises(AssertionError, vm.__enter__)

    def test_assert_on_re_exit(self):
        """
        It should fail to re exit
        """
        vm = VM(platform)

        self.assertRaises(AssertionError, vm.__exit__)

        with vm as _:
            pass

        self.assertRaises(AssertionError, vm.__exit__)

    def test_assert_on_re_create(self):
        """
        It should allow to re create a vm
        """
        vm = VM(platform)

        with vm as _:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())

        with vm as _:
            self.assertTrue(vm.is_alive())
Exemple #15
0
 def test_with(self):
     """
     It should support with statement
     """
     with VM(platform) as vm:
         self.assertIsInstance(vm, VM)
Exemple #16
0
class ContextTest(unittest.TestCase):

    def setUp(self):
        self.vm = VM(platform)
        self.vm.set_up()

    def tearDown(self):
        self.vm.tear_down()

    def test_keep_vm(self):
        """
        It should keep a reference to the VM
        """
        ctx = context.Context(self.vm)
        self.assertIsInstance(ctx._vm, VM)

    def test_with(self):
        """
        It should support with statement
        """
        with context.Context(self.vm) as ctx:
            self.assertIsInstance(ctx, context.Context)

    def test_set_up(self):
        """
        It should call __enter__
        """
        ctx = context.Context(self.vm)

        with patch.object(ctx, '__enter__', autospec=True) as r:
            r.return_value = 'foo'
            self.assertEqual(ctx.set_up(), 'foo')
            r.assert_called_once_with()

    def test_tear_down(self):
        """
        It should call __exit__
        """
        ctx = context.Context(self.vm)

        with patch.object(ctx, '__exit__', autospec=True) as r:
            ctx.tear_down()
            r.assert_called_once_with()

    def test_load_libs(self):
        """
        It should run the script file content on V8
        """
        script = b'var foo = "foo";'

        with js_file(script) as path:
            with context.Context(self.vm) as ctx:
                with patch.object(ctx, 'run_script', autospec=True) as r:
                    ctx.load_libs([path])
                    r.assert_called_once_with(script, identifier=path)

    def test_run_script(self):
        """
        It should run the script on V8
        """
        script_foo = b'var foo = "foo!";'
        script_bar = 'var bar = "bar!";'
        script_special = 'var txt = "áéíóú";'

        with context.Context(self.vm) as ctx:
            ctx.run_script(script_foo)
            ctx.run_script(script_bar)
            ctx.run_script(script_special)
            self.assertEqual("foo!", ctx.run_script(b'foo'))
            self.assertEqual("bar!", ctx.run_script('bar'))
            self.assertEqual("áéíóú", ctx.run_script('txt'))
            self.assertRaises(exceptions.V8JSError, ctx.run_script, 'baz')
            self.assertRaises(exceptions.V8JSError, ctx.run_script, 'function[]();')

        with context.Context(self.vm) as ctx:
            self.assertRaises(exceptions.V8JSError, ctx.run_script, 'foo')

    def test_builtin_libs(self):
        """
        It should pre-load builtin libraries
        """
        with context.Context(self.vm) as ctx:
            self.assertEqual("20", ctx.run_script('Math.max(10, 20);'))

    def test_run_script_trace_back(self):
        """
        It should run the script on V8\
        and get a useful traceback
        """
        def get_exception_message(ctx, script):
            try:
                return ctx.run_script(script)
            except exceptions.V8JSError as ex:
                return six.text_type(ex)

        script_oops = (
            'function oops() {\n'
            '  thereMayBeErrors();\n'
            '  var my_var_2;\n'
            '}')
        script_oops2 = (
            'function oops2() {\n'
            '  thereMayBeMoreErrors();\n'
            '  var my_var_2;\n'
            '}')
        var_a = 'var a;'
        script_long = (
            'function oops3() {\n' +
            var_a * 100 + 'thereMayBeMoreErrors();' + var_a * 100 + '\n'
            '}')

        # todo: trim source line when too long
        with context.Context(self.vm) as ctx:
            ctx.run_script(script_oops, identifier='my_file_áéíóú.js')
            ctx.run_script(script_oops2, identifier='my_other_file.js')
            ctx.run_script(script_long)
            self.assertEqual(
                'my_file_áéíóú.js:2\n'
                '      thereMayBeErrors();\n'
                '      ^\n'
                'ReferenceError: thereMayBeErrors is not defined\n'
                '    at oops (my_file_áéíóú.js:2:3)\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'oops()'))
            self.assertEqual(
                'my_other_file.js:2\n'
                '      thereMayBeMoreErrors();\n'
                '      ^\n'
                'ReferenceError: thereMayBeMoreErrors is not defined\n'
                '    at oops2 (my_other_file.js:2:3)\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'oops2()'))
            self.assertEqual(
                '<anonymous>:2\n'
                '    ~Line too long to display.\n'
                'ReferenceError: thereMayBeMoreErrors is not defined\n'
                '    at oops3 (<anonymous>:2:601)\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'oops3()'))
            self.assertEqual(
                '<anonymous>:1\n'
                '    nonExistentFunc();\n'
                '    ^\n'
                'ReferenceError: nonExistentFunc is not defined\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'nonExistentFunc();'))
            self.assertEqual(
                '<anonymous>:1\n'
                '    function[]();\n'
                '            ^\n'
                'SyntaxError: Unexpected token [',
                get_exception_message(ctx, 'function[]();'))
            # Has no .stack property
            self.assertEqual(
                '<anonymous>:2\n'
                '      throw "myException";\n'
                '      ^\n'
                'myException',
                get_exception_message(
                    ctx,
                    '(function() {\n'
                    '  throw "myException";\n'
                    '})();'))
Exemple #17
0
class VMTest(unittest.TestCase):

    def setUp(self):
        self.vm = VM(platform)
        self.vm.set_up()

    def tearDown(self):
        self.vm.tear_down()

    def test_keep_platform(self):
        """
        It should keep a reference to the platform
        """
        self.assertEqual(platform, self.vm._platform)

    def test_with(self):
        """
        It should support with statement
        """
        with VM(platform) as vm:
            self.assertIsInstance(vm, VM)

    def test_is_alive(self):
        """
        It should be alive within a with context
        """
        vm = VM(platform)
        self.assertFalse(vm.is_alive())

        with VM(platform) as vm:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())

    def test_create_context(self):
        """
        It should create a Context
        """
        self.assertIsInstance(self.vm.create_context(), context.Context)

        with patch('v8cffi.context.Context', autospec=True) as r:
            r.return_value = None
            self.vm.create_context()
            r.assert_called_once_with(self.vm)

    def test_get_c_vm(self):
        """
        It should return the C VM
        """
        self.assertEqual(self.vm._c_vm, self.vm.get_c_vm())

    def test_set_up(self):
        """
        It should call __enter__
        """
        vm = VM(platform)

        with patch.object(vm, '__enter__', autospec=True) as r:
            r.return_value = 'foo'
            self.assertEqual(vm.set_up(), 'foo')
            r.assert_called_once_with()

    def test_tear_down(self):
        """
        It should call __exit__
        """
        vm = VM(platform)

        with patch.object(vm, '__exit__', autospec=True) as r:
            vm.tear_down()
            r.assert_called_once_with()

    def test_allocate_and_free(self):
        """
        It should allocate and free the C VM
        """
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            vm = VM(platform)

            vm_new = Mock(return_value=code_ok)
            r.v8cffi_vm_new = vm_new
            r.E_V8_OK = code_ok
            vm.__enter__()
            self.assertTrue(vm_new.called)

            vm_free = Mock()
            r.v8cffi_vm_free = vm_free
            vm.__exit__()
            self.assertTrue(vm_free.called)

            self.assertFalse(vm.is_alive())
            self.assertRaises(AssertionError, vm.__exit__)

    def test_with_exceptions(self):
        """
        It should raise V8 exceptions
        """
        code_error = vm_module.lib.E_V8_JS_ERROR
        code_ok = vm_module.lib.E_V8_OK

        with patch('v8cffi.vm.lib', autospec=True) as r:
            r.v8cffi_vm_new = Mock(return_value=code_error)
            r.E_V8_OK = code_ok

            self.assertRaises(exceptions.V8JSError, VM(platform).__enter__)

    def test_assert_on_re_enter(self):
        """
        It should fail to re enter
        """
        vm = VM(platform)

        with vm as _:
            self.assertRaises(AssertionError, vm.__enter__)

    def test_assert_on_re_exit(self):
        """
        It should fail to re exit
        """
        vm = VM(platform)

        self.assertRaises(AssertionError, vm.__exit__)

        with vm as _:
            pass

        self.assertRaises(AssertionError, vm.__exit__)

    def test_assert_on_re_create(self):
        """
        It should allow to re create a vm
        """
        vm = VM(platform)

        with vm as _:
            self.assertTrue(vm.is_alive())

        self.assertFalse(vm.is_alive())

        with vm as _:
            self.assertTrue(vm.is_alive())
Exemple #18
0
 def setUp(self):
     self.vm = VM(platform)
     self.vm.set_up()
Exemple #19
0
 def setUp(self):
     self.vm = VM(platform)
     self.vm.set_up()
Exemple #20
0
class ContextTest(unittest.TestCase):
    def setUp(self):
        self.vm = VM(platform)
        self.vm.set_up()

    def tearDown(self):
        self.vm.tear_down()

    def test_keep_vm(self):
        """
        It should keep a reference to the VM
        """
        ctx = context.Context(self.vm)
        self.assertIsInstance(ctx._vm, VM)

    def test_with(self):
        """
        It should support with statement
        """
        with context.Context(self.vm) as ctx:
            self.assertIsInstance(ctx, context.Context)

    def test_set_up(self):
        """
        It should call __enter__
        """
        ctx = context.Context(self.vm)

        with patch.object(ctx, '__enter__', autospec=True) as r:
            r.return_value = 'foo'
            self.assertEqual(ctx.set_up(), 'foo')
            r.assert_called_once_with()

    def test_tear_down(self):
        """
        It should call __exit__
        """
        ctx = context.Context(self.vm)

        with patch.object(ctx, '__exit__', autospec=True) as r:
            ctx.tear_down()
            r.assert_called_once_with()

    def test_load_libs(self):
        """
        It should run the script file content on V8
        """
        script = b'var foo = "foo";'

        with js_file(script) as path:
            with context.Context(self.vm) as ctx:
                with patch.object(ctx, 'run_script', autospec=True) as r:
                    ctx.load_libs([path])
                    r.assert_called_once_with(script, identifier=path)

    def test_run_script(self):
        """
        It should run the script on V8
        """
        script_foo = b'var foo = "foo!";'
        script_bar = 'var bar = "bar!";'
        script_special = 'var txt = "áéíóú";'

        with context.Context(self.vm) as ctx:
            ctx.run_script(script_foo)
            ctx.run_script(script_bar)
            ctx.run_script(script_special)
            self.assertEqual("foo!", ctx.run_script(b'foo'))
            self.assertEqual("bar!", ctx.run_script('bar'))
            self.assertEqual("áéíóú", ctx.run_script('txt'))
            self.assertRaises(exceptions.V8JSError, ctx.run_script, 'baz')
            self.assertRaises(exceptions.V8JSError, ctx.run_script,
                              'function[]();')

        with context.Context(self.vm) as ctx:
            self.assertRaises(exceptions.V8JSError, ctx.run_script, 'foo')

    def test_builtin_libs(self):
        """
        It should pre-load builtin libraries
        """
        with context.Context(self.vm) as ctx:
            self.assertEqual("20", ctx.run_script('Math.max(10, 20);'))

    def test_run_script_trace_back(self):
        """
        It should run the script on V8\
        and get a useful traceback
        """
        def get_exception_message(ctx, script):
            try:
                return ctx.run_script(script)
            except exceptions.V8JSError as ex:
                return six.text_type(ex)

        script_oops = ('function oops() {\n'
                       '  thereMayBeErrors();\n'
                       '  var my_var_2;\n'
                       '}')
        script_oops2 = ('function oops2() {\n'
                        '  thereMayBeMoreErrors();\n'
                        '  var my_var_2;\n'
                        '}')
        var_a = 'var a;'
        script_long = ('function oops3() {\n' + var_a * 100 +
                       'thereMayBeMoreErrors();' + var_a * 100 + '\n'
                       '}')

        # todo: trim source line when too long
        with context.Context(self.vm) as ctx:
            ctx.run_script(script_oops, identifier='my_file_áéíóú.js')
            ctx.run_script(script_oops2, identifier='my_other_file.js')
            ctx.run_script(script_long)
            self.assertEqual(
                'my_file_áéíóú.js:2\n'
                '      thereMayBeErrors();\n'
                '      ^\n'
                'ReferenceError: thereMayBeErrors is not defined\n'
                '    at oops (my_file_áéíóú.js:2:3)\n'
                '    at <anonymous>:1:1', get_exception_message(ctx, 'oops()'))
            self.assertEqual(
                'my_other_file.js:2\n'
                '      thereMayBeMoreErrors();\n'
                '      ^\n'
                'ReferenceError: thereMayBeMoreErrors is not defined\n'
                '    at oops2 (my_other_file.js:2:3)\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'oops2()'))
            self.assertEqual(
                '<anonymous>:2\n'
                '    ~Line too long to display.\n'
                'ReferenceError: thereMayBeMoreErrors is not defined\n'
                '    at oops3 (<anonymous>:2:601)\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'oops3()'))
            self.assertEqual(
                '<anonymous>:1\n'
                '    nonExistentFunc();\n'
                '    ^\n'
                'ReferenceError: nonExistentFunc is not defined\n'
                '    at <anonymous>:1:1',
                get_exception_message(ctx, 'nonExistentFunc();'))
            self.assertEqual(
                '<anonymous>:1\n'
                '    function[]();\n'
                '            ^\n'
                'SyntaxError: Unexpected token [',
                get_exception_message(ctx, 'function[]();'))
            # Has no .stack property
            self.assertEqual(
                '<anonymous>:2\n'
                '      throw "myException";\n'
                '      ^\n'
                'myException',
                get_exception_message(
                    ctx, '(function() {\n'
                    '  throw "myException";\n'
                    '})();'))