def test_imports(): def func(): import foo, bar import foo.bar as b from foo.bar import hello as h, bye as bb from ..foo.zoo import bar from . import foo from .foo import bar from __globals__ import test_name # this should not trigger variable definition test_name = 2 # this instead should do it test_foo = True __all__ = ['test_name', 'test_foo'] expected = ('var test_foo;\n' "import * as foo from 'foo';\n" "import * as bar from 'bar';\n" "import * as b from 'foo/bar';\n" "import {hello as h, bye as bb} from 'foo/bar';\n" "import {bar} from '../foo/zoo';\n" "import * as foo from './foo';\n" "import {bar} from './foo';\n" 'test_name = 2;\n' 'test_foo = true;\n' 'export {test_name};\n' 'export {test_foo};\n') assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_list_in(): def list_in(): return [ 1 in [10, 11], 'foo' in 'barfoobar', 11 in [10, 11] ] assert list_in() == eval_object(list_in, 'list_in();') expected = ( 'var _pj;\n' 'function _pj_snippets(container) {\n' ' function _in(left, right) {\n' ' if (((right instanceof Array) || ((typeof right) === "string"))) {\n' ' return (right.indexOf(left) > (- 1));\n' ' } else {\n' ' return (left in right);\n' ' }\n' ' }\n' ' container["_in"] = _in;\n' ' return container;\n' '}\n' '_pj = {};\n' '_pj_snippets(_pj);\n' 'function list_in() {\n' ' return [_pj._in(1, [10, 11]), _pj._in("foo", "barfoobar"), _pj._in(11, ' '[10, 11])];\n' '}\n' ) assert translate_object(list_in)[0] == expected
def test_imports(): def func(): import foo, bar import foo.bar as b from foo.bar import hello as h, bye as bb from ..foo.zoo import bar from . import foo from .foo import bar from __globals__ import test_name # this should not trigger variable definition test_name = 2 # this instead should do it test_foo = True __all__ = ['test_name', 'test_foo'] expected = ('var test_foo;\n' "import * as foo from 'foo';\n" "import * as bar from 'bar';\n" "import * as b from 'foo/bar';\n" "import {hello as h, bye as bb} from 'foo/bar';\n" "import {bar} from '../../foo/zoo';\n" "import * as foo from './foo';\n" "import {bar} from './foo';\n" 'test_name = 2;\n' 'test_foo = true;\n' 'export {test_name};\n' 'export {test_foo};\n') assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_exception(): def func(): class MyError(Exception): pass class MySecondError(MyError): """A stupid error""" expected = ( 'function MyError(message) {\n' ' this.name = "MyError";\n' ' this.message = (message || "Custom error MyError");\n' ' if (((typeof Error.captureStackTrace) === "function")) {\n' ' Error.captureStackTrace(this, this.constructor);\n' ' } else {\n' ' this.stack = new Error(message).stack;\n' ' }\n' '}\n' 'MyError.prototype = Object.create(Error.prototype);\n' 'MyError.prototype.constructor = MyError;\n' 'class MySecondError extends MyError {\n' '}\n' ) assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_try_except_complex(): def func(): value = 0 class MyError(Exception): pass class MySecondError(MyError): """A stupid error""" try: value += 1 raise MyError("Something bad happened") value += 1 except MySecondError: value += 20 except MyError: value += 30 except: value += 40 finally: value += 1 expected = ( 'var value;\n' 'value = 0;\n' 'function MyError(message) {\n' ' this.name = "MyError";\n' ' this.message = (message || "Custom error MyError");\n' ' if (((typeof Error.captureStackTrace) === "function")) {\n' ' Error.captureStackTrace(this, this.constructor);\n' ' } else {\n' ' this.stack = new Error(message).stack;\n' ' }\n' '}\n' 'MyError.prototype = Object.create(Error.prototype);\n' 'MyError.prototype.constructor = MyError;\n' 'class MySecondError extends MyError {\n' '}\n' 'try {\n' ' value += 1;\n' ' throw new MyError("Something bad happened");\n' ' value += 1;\n' '} catch(e) {\n' ' if ((e instanceof MySecondError)) {\n' ' value += 20;\n' ' } else {\n' ' if ((e instanceof MyError)) {\n' ' value += 30;\n' ' } else {\n' ' value += 40;\n' ' }\n' ' }\n' '} finally {\n' ' value += 1;\n' '}\n') assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_str(): def func(): str(x) expected = 'x.toString();\n' assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_kwargs(): def func(): def with_kwargs(a, **kwargs): pass with_kwargs(1, foo=2, bar=3) expected = ('function with_kwargs(a, kwargs = {}) {\n}\n' 'with_kwargs(1, {foo: 2, bar: 3});\n') assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_special_methods(): class Foo3: @classmethod def foo(self): return 'bar' expected = ('class Foo3 {\n' ' static foo() {\n' ' return "bar";\n' ' }\n' '}\n') assert translate_object(Foo3, enable_es6=True)[0] == expected
def test_globals(): def func(): def simple_alert(): window.alert('Hi there!') el = document.querySelector('button') el.addEventListener('click', simple_alert) expected = ('var el;\n' 'function simple_alert() {\n' ' window.alert("Hi there!");\n' '}\n' 'el = document.querySelector("button");\n' 'el.addEventListener("click", simple_alert);\n') assert translate_object(func, body_only=True)[0] == expected
def test_special_methods(): class Foo3: @classmethod def foo(self): return 'bar' expected = ( 'class Foo3 {\n' ' static foo() {\n' ' return "bar";\n' ' }\n' '}\n' ) assert translate_object(Foo3, enable_es6=True)[0] == expected
def test_inner_method_func(): def func(): class Example: def foo(self): def bar(): pass expected = ('class Example {\n' ' foo() {\n' ' var bar;\n' ' bar = () => {\n' ' };\n' ' }\n' '}\n') assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_try_except_simple(): def func(): class MyError(Exception): pass class MySecondError(MyError): """A stupid error""" try: value = 0 raise MySecondError('This is an error') except MySecondError: value = 1 expected = ( 'var value;\n' 'function MyError(message) {\n' ' this.name = "MyError";\n' ' this.message = (message || "Custom error MyError");\n' ' if (((typeof Error.captureStackTrace) === "function")) {\n' ' Error.captureStackTrace(this, this.constructor);\n' ' } else {\n' ' this.stack = new Error(message).stack;\n' ' }\n' '}\n' 'MyError.prototype = Object.create(Error.prototype);\n' 'MyError.prototype.constructor = MyError;\n' 'class MySecondError extends MyError {\n' '}\n' 'try {\n' ' value = 0;\n' ' throw new MySecondError("This is an error");\n' '} catch(e) {\n' ' if ((e instanceof MySecondError)) {\n' ' value = 1;\n' ' } else {\n' ' throw e;\n' ' }\n' '}\n' ) assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_slices(): def func(): foo = 'foofoo' foo[1:] foo[:-1] foo[3:-1] foo[2] expected = ('function func() {\n' ' var foo;\n' ' foo = "foofoo";\n' ' foo.slice(1);\n' ' foo.slice(0, (- 1));\n' ' foo.slice(3, (- 1));\n' ' foo[2];\n' '}\n') assert translate_object(func)[0] == expected
def test_properties(): class Foo: @property def bar(self): return self._bar @bar.setter def bar(self, value): self._bar = value expected = ('class Foo {\n' ' get bar() {\n' ' return this._bar;\n' ' }\n' ' set bar(value) {\n' ' this._bar = value;\n' ' }\n' '}\n') assert translate_object(Foo, enable_es6=True)[0] == expected
def test_special_methods(): # it seems that if name this class the same of the previous test, # inspect.getsourcelines will found that instead of this... class Foo2: def __len__(self): return 5 def __str__(self): return 'bar' expected = ('class Foo2 {\n' ' get length() {\n' ' return 5;\n' ' }\n' ' toString() {\n' ' return "bar";\n' ' }\n' '}\n') assert translate_object(Foo2, enable_es6=True)[0] == expected
def test_inner_method_func(): def func(): class Example: def foo(self): def bar(): pass expected = ( 'class Example {\n' ' foo() {\n' ' var bar;\n' ' bar = () => {\n' ' };\n' ' }\n' '}\n' ) assert translate_object(func, body_only=True, enable_es6=True)[0] == expected
def test_slices(): def func(): foo = 'foofoo' foo[1:] foo[:-1] foo[3:-1] foo[2] expected = ( 'function func() {\n' ' var foo;\n' ' foo = "foofoo";\n' ' foo.slice(1);\n' ' foo.slice(0, (- 1));\n' ' foo.slice(3, (- 1));\n' ' foo[2];\n' '}\n' ) assert translate_object(func)[0] == expected
def test_init_local_def_with_return(): """An __init__ with a function that returns something should not raise any concern.""" class Foo4: def __init__(self): def bar(): return 10 self.bar = bar expected = ( 'class Foo4 {\n' ' constructor() {\n' ' var bar;\n' ' bar = () => {\n' ' return 10;\n' ' };\n' ' this.bar = bar;\n' ' }\n' '}\n' ) assert translate_object(Foo4, enable_es6=True)[0] == expected
def test_special_methods(): # it seems that if name this class the same of the previous test, # inspect.getsourcelines will found that instead of this... class Foo2: def __len__(self): return 5 def __str__(self): return 'bar' expected = ( 'class Foo2 {\n' ' get length() {\n' ' return 5;\n' ' }\n' ' toString() {\n' ' return "bar";\n' ' }\n' '}\n' ) assert translate_object(Foo2, enable_es6=True)[0] == expected
def test_properties(): class Foo: @property def bar(self): return self._bar @bar.setter def bar(self, value): self._bar = value expected = ( 'class Foo {\n' ' get bar() {\n' ' return this._bar;\n' ' }\n' ' set bar(value) {\n' ' this._bar = value;\n' ' }\n' '}\n' ) assert translate_object(Foo, enable_es6=True)[0] == expected
def test_translate_object(self, name, py_code, options, expected): dump = translate_object(py_code, **options)[0] assert dump.rstrip() == expected.rstrip()
def test_translate_object_unsupported(self, name, py_code, options, expected): from metapensiero.pj.processor.exceptions import UnsupportedSyntaxError with pytest.raises(UnsupportedSyntaxError): translate_object(py_code, **options)[0]