예제 #1
0
 def test_transform_ImportDeclaration(self):
     self.assertEqual(js2py("import { thing } from \"location\""),
                      "from location import thing\n")
     self.assertEqual(js2py("import { thing1, thing2 } from \"location\""),
                      "from location import thing1, thing2\n")
     self.assertEqual(js2py("import * as mymod from \"location\""),
                      "from location import * as mymod\n")
예제 #2
0
 def test_transform_ArrayExpression(self):
     self.assertEqual(js2py("[1,2,3,4]"), "[1, 2, 3, 4]\n")
     self.assertEqual(js2py("[a,b,3,4]"), "[a, b, 3, 4]\n")
     self.assertEqual(js2py("[a,b,3,4]"), "[a, b, 3, 4]\n")
     self.assertEqual(js2py("[]"), "[]\n")
     self.assertEqual(js2py("[[], [1], [1,2], [1,2,3]]"),
                      "[[], [1], [1, 2], [1, 2, 3]]\n")
예제 #3
0
 def test_transform_FunctionExpression(self):
     self.assertEqual(
         js2py("x = function() { console.log('hi'); }"),
         "def Anonymous_0():\n    console.log('hi')\n\n\nx = Anonymous_0\n")
     self.assertEqual(
         js2py("x = (function() { console.log('hi'); })()"),
         "def Anonymous_0():\n    console.log('hi')\n\n\nx = Anonymous_0()\n"
     )
예제 #4
0
 def test_or(self):
     with self.subTest('false or true'):
         self.assertEqual({'a': True}, js2py('a = a || true', {'a': False}))
     with self.subTest('true or true'):
         self.assertEqual({'a': True}, js2py('a = a || true', {'a': True}))
     with self.subTest('false or false'):
         self.assertEqual({'a': False}, js2py('a = a || false',
                                              {'a': False}))
예제 #5
0
 def test_TryCatchFinally(self):
     self.assertEqual(
         js2py("try { something() } catch (e) { }"),
         "try:\n    something()\nexcept Exception as e:\n    pass\n")
     self.assertEqual(
         js2py(
             "try { something() } catch (e) { } finally { something2() }"),
         "try:\n    something()\nexcept Exception as e:\n    pass\nfinally:\n    something2()\n"
     )
     self.assertEqual(
         js2py(
             "try { something() } catch (e) { something1() } finally { something2() }"
         ),
         "try:\n    something()\nexcept Exception as e:\n    something1()\nfinally:\n    something2()\n"
     )
예제 #6
0
 def test_transform_MethodDefinition(self):
     self.assertEqual(
         js2py(
             "class A {\n    constructor() { }\n    calcArea() { return 3*5 + a } }\n"
         ),
         "class A:\n\n    def __init__(self):\n        pass\n\n    def calcArea(self):\n        return 3 * 5 + a\n"
     )
예제 #7
0
 def test_transform_IfStatement(self):
     self.assertEqual(js2py("""if (1) { return 1 }"""),
                      """if 1:\n    return 1\n""")
     self.assertEqual(js2py("""if (1) {return 1} else { return 2 }"""),
                      """if 1:\n    return 1\nelse:\n    return 2\n""")
     self.assertEqual(
         js2py("""if (1) {return 1} else if (2) { return 2 }"""),
         """if 1:\n    return 1\nelif 2:\n    return 2\n""")
     self.assertEqual(
         js2py(
             """if (1) {return 1} else if (2) { return 2 } else { return 3 }"""
         ),
         """if 1:\n    return 1\nelif 2:\n    return 2\nelse:\n    return 3\n"""
     )
     self.assertEqual(js2py("""if (1) { foo(); foo(); return 1}"""),
                      """if 1:\n    foo()\n    foo()\n    return 1\n""")
예제 #8
0
 def test_string(self):
     params = [['a = "x"', {
         'a': 'b'
     }, {
         'a': 'x'
     }], ['if (a == "x") { a = "y"}', {
         'a': 'x'
     }, {
         'a': 'y'
     }], ['if (a == "x") { a = "y"}', {
         'a': 'm'
     }, {
         'a': 'm'
     }], [
         'if (a == "x") { a = "y"} else { a = "z"}', {
             'a': "b"
         }, {
             'a': "z"
         }
     ], [
         'if (a == "x") { a = "y"} else { a = "z"}', {
             'a': "x"
         }, {
             'a': "y"
         }
     ]]
     for p in params:
         with self.subTest(js=p[0], context=p[1], expected=p[2]):
             self.assertEqual(p[2], js2py(p[0], p[1]))
예제 #9
0
 def test_transform_ForOfStatement(self):
     self.assertEqual(
         js2py(
             """array1 = ['a', 'b', 'c']; for (element of array1) {  console.log(element); }"""
         ),
         """array1 = ['a', 'b', 'c']\nfor element in array1:\n    console.log(element)\n"""
     )
예제 #10
0
 def test_variable_addition(self):
     self.assertEqual({
         'a': 3,
         'b': 2
     }, js2py('a = a + b', {
         'a': 1,
         'b': 2
     }))
예제 #11
0
 def test_subtraction(self):
     self.assertEqual({
         'a': -1,
         'b': 2
     }, js2py('a = a - b', {
         'a': 1,
         'b': 2
     }))
예제 #12
0
    def test_postprocess(self):
        data = """function Box2( min, max ) {

	this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity );
	this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity );

}

Object.assign( Box2.prototype, {

	set: function ( min, max ) {

		this.min.copy( min );
		this.max.copy( max );

		return this;

	}});"""
        js2py(data, postprocess=True)
예제 #13
0
 def test_if(self):
     params = [['if (a>3) { a = 0}', {
         'a': 4
     }, {
         'a': 0
     }], ['if (a>3) { a = 0}', {
         'a': 3
     }, {
         'a': 3
     }], [
         'if (a>3 && b == 1) { a = 0}', {
             'a': 4,
             'b': 1
         }, {
             'a': 0,
             'b': 1
         }
     ], ['if (a>3 && b == 1) { a = 0}', {
         'a': 4,
         'b': 2
     }, {
         'a': 4,
         'b': 2
     }], [
         'if (a>3 || b == 1) { a = 0}', {
             'a': 3,
             'b': 1
         }, {
             'a': 0,
             'b': 1
         }
     ],
               [
                   'if (b == 1){ if (a > 3) { a = 0}}', {
                       'a': 4,
                       'b': 1
                   }, {
                       'a': 0,
                       'b': 1
                   }
               ],
               [
                   'if (b == 1){ if (a > 3) { a = 0}}', {
                       'a': 3,
                       'b': 1
                   }, {
                       'a': 3,
                       'b': 1
                   }
               ]]
     for p in params:
         with self.subTest(js=p[0], context=p[1], expected=p[2]):
             self.assertEqual(p[2], js2py(p[0], p[1]))
예제 #14
0
 def test_transform_UnaryExpression(self):
     self.assertEqual(js2py("-x"), "-x\n")
     self.assertEqual(js2py("~x"), "~x\n")
     self.assertEqual(js2py("+x"), "+x\n")
     self.assertEqual(js2py("-(x + 1)"), "-(x + 1)\n")
     self.assertEqual(js2py("delete a"), "del a\n")
     self.assertEqual(js2py("delete a[0]"), "del a[0]\n")
예제 #15
0
    def test_transform_SwitchStatement(self):
        self.assertEqual(
            js2py("""
switch (s) {
case 1:
    console.log(hi);
case 2:
case 3:
    console.log(hello);
}"""), "if s == 1:\n    console.log(hi)\nelif s == 2 or s == 3:\n    console.log(hello)\n"
        )
        self.assertEqual(
            js2py("""
    switch (s) {
    case 1:
        console.log(hi);
    case 2:
    case 3:
        console.log(hello);
    default:
        console.log(hey);
    }"""), "if s == 1:\n    console.log(hi)\nelif s == 2 or s == 3:\n    console.log(hello)\nconsole.log(hey)\n"
        )
예제 #16
0
 def test_else(self):
     params = [['if (a>3) { a = 0} else { a = 1}', {
         'a': 4
     }, {
         'a': 0
     }], ['if (a>3) { a = 0} else { a = 1}', {
         'a': 3
     }, {
         'a': 1
     }],
               [
                   'if (a>3) { a = 0} else { if (a == 3) {a = 2}}', {
                       'a': 3
                   }, {
                       'a': 2
                   }
               ]]
     for p in params:
         with self.subTest(js=p[0], context=p[1], expected=p[2]):
             self.assertEqual(p[2], js2py(p[0], p[1]))
예제 #17
0
 def test_transform_ForStatement(self):
     self.assertEqual(js2py("""for (i = 0; i < 5; i++) { print(i); }"""),
                      "i = 0\nwhile i < 5:\n    print(i)\n    i += 1\n")
     self.assertEqual(js2py("""for (i = 2; i < 5; i++) { print(i); }"""),
                      "i = 2\nwhile i < 5:\n    print(i)\n    i += 1\n")
     self.assertEqual(js2py("""for (i = 2; i < 5; i += 3) { print(i); }"""),
                      "i = 2\nwhile i < 5:\n    print(i)\n    i += 3\n")
     self.assertEqual(
         js2py("""for (i = 2, j=3; i < 5; i += 3) { print(i); }"""),
         "i = 2\nj = 3\nwhile i < 5:\n    print(i)\n    i += 3\n")
     self.assertEqual(
         js2py("""for (i = 2, j=3; i < 5; i += 3, j += 1) { print(i); }"""),
         "i = 2\nj = 3\nwhile i < 5:\n    print(i)\n    i += 3\n    j += 1\n"
     )
     self.assertEqual(
         js2py("""for (; i < 5; i += 3, j += 1) { print(i); }"""),
         "while i < 5:\n    print(i)\n    i += 3\n    j += 1\n")
예제 #18
0
 def test_assign_new(self):
     self.assertRaises(Exception, {'a': 1}, js2py('a = 1', {}))
예제 #19
0
 def test_transform_Literal(self):
     self.assertEqual(js2py("1"), "1\n")
     self.assertEqual(js2py("'1'"), '"""1"""\n')
     self.assertEqual(js2py("x = \"1\""), "x = '1'\n")
     self.assertEqual(js2py("null"), "None\n")
예제 #20
0
 def test_and(self):
     with self.subTest('false and true'):
         self.assertEqual({'a': False}, js2py('a = a && true',
                                              {'a': False}))
     with self.subTest('true and true'):
         self.assertEqual({'a': True}, js2py('a = a && true', {'a': True}))
예제 #21
0
 def test_empty(self):
     self.assertEqual({'a': 1}, js2py('', {'a': 1}))
예제 #22
0
 def test_transform_RegexLiteral(self):
     self.assertEqual(js2py(r"/thing/"), "re.compile('thing')\n")
예제 #23
0
 def test_with_properties(self):
     self.assertEqual({'a': {
         'x': -1
     }}, js2py('a.x = a.x - 1', {'a': {
         'x': 0
     }}))
예제 #24
0
 def test_mixed_addition(self):
     self.assertEqual({'a': 2}, js2py('a = a + 1', {'a': 1}))
예제 #25
0
 def test_transform_NewExpression(self):
     self.assertEqual(js2py(r"thing = new Dog()"), "thing = Dog()\n")
예제 #26
0
 def test_scalar_addition(self):
     self.assertEqual({'a': 3}, js2py('a = 1 + 2', {'a': 0}))
예제 #27
0
 def test_transform_CallExpression(self):
     self.assertEqual(js2py("print('hello')"), "print('hello')\n")
예제 #28
0
 def test_transform_Identifier(self):
     self.assertEqual(js2py("thing"), "thing\n")
예제 #29
0
 def test_assign_existing(self):
     self.assertEqual({'a': 1}, js2py('a = 1', {'a': 0}))
예제 #30
0
 def test_transform_StaticMemberExpression(self):
     self.assertEqual(js2py("console.log(1)"), "console.log(1)\n")