def test_function_in_jsx_map_array(self):
        tree = parse_statement(
            "#frontend\n<input\n\tonChange=function(event)\n\t\tfoo()\n\tvalue=\"bar\"\n/>\n"
        )
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["frontend"],
            _wrap_front(
                "new Component(\"input\", {\n    onChange: async (event) => {\n        await foo();\n    },\n    value: \"bar\",\n\n}, []);\n"
            ))

        tree = parse_statement(
            "foo = {\n\tonChange: function(event)\n\t\tfoo()\n\tvalue: \"bar\"\n"
        )
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back(
                "var foo = {\n    \"onChange\": async (event) => {\n        await foo();\n    },\n    \"value\": \"bar\",\n}\n"
            ))

        tree = parse_statement(
            "foo = [\n\tfunction(event)\n\t\tfoo()\n\t\"bar\"\n]\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back(
                "var foo = [\n    async (event) => {\n        await foo();\n    },\n    \"bar\",\n\n];\n"
            ))
    def test_variables(self):
        tree = parse_statement("foo = 'abcd'\nbar = foo\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"],
                         _wrap_back("var foo = \"abcd\";\nvar bar = foo;\n"))

        tree = parse_statement("foo = 5.45\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"], _wrap_back("var foo = 5.45;\n"))

        tree = parse_statement("foo = 5\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"], _wrap_back("var foo = 5;\n"))

        tree = parse_statement("foo = 5\nfoo = foo ++\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"],
                         _wrap_back("var foo = 5;\nfoo = foo++;\n"))
    def test_function_definition(self):
        tree = parse_statement("function foo()\n    bar = baz\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back(
                "async function foo() {\n    var bar = baz;\n}\n\nmodule.exports = {\n\tfoo\n};\n"
            ))

        tree = parse_statement("function()\n    bar = baz\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"],
                         _wrap_back("async () => {\n    var bar = baz;\n}\n"))

        tree = parse_statement("function foo(bar, baz)\n    bar = baz\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back(
                "async function foo(bar, baz) {\n    var bar = baz;\n}\n\nmodule.exports = {\n\tfoo\n};\n"
            ))
    def test_boolean(self):
        tree = parse_statement("foo = false")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"], _wrap_back("var foo = false;\n"))

        tree = parse_statement("foo = true")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"], _wrap_back("var foo = true;\n"))
    def test_class_method_call(self):
        tree = parse_statement("foo = bar.baz(\n\n)\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(result["backend"],
                         _wrap_back("var foo = await bar.baz(\n\n);\n"))

        tree = parse_statement("foo = bar.baz.biz.buzz(\n\n)\n")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back("var foo = await bar.baz.biz.buzz(\n\n);\n"))
    def test_mysql(self):
        tree = parse_statement(
            "table = Table(\n\t\"table1\"\n\t[\n\t]\n\tTable.SOURCE_MYSQL\n)\n"
        )
        processed = process_tree(tree)
        output = generate(processed, "js")
        imports = output['external_imports']
        self.assertEqual(imports, [{"module": "mysql", "version": "2.18.1"}])

        tree = parse_statement(
            "table = Table(\n\t\"table1\"\n\t[\n\t]\n\tTable.SOURCE_FILE\n)\n")
        processed = process_tree(tree)
        output = generate(processed, "js")
        imports = output['external_imports']
        self.assertEqual(imports, [])
 def test_class_variables(self):
     tree = parse_statement("foo = bar.baz.biz")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(result["backend"],
                      _wrap_back("var foo = bar.baz.biz;\n"))
 def test_one_line_function(self):
     tree = parse_statement("foo = bar()")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(result["backend"],
                      _wrap_back("var foo = await bar();\n"))
 def test_function_call_and_platform(self):
     tree = parse_statement("foo()\n#frontend\nfoo()\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(result["backend"], _wrap_back("await foo();\n"))
     self.assertEqual(result["frontend"], _wrap_front("await foo();\n"))
 def test_else(self):
     tree = parse_statement("if foo == true\nelse\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(result["backend"],
                      _wrap_back("if (foo == true) {\n}\nelse {\n}\n"))
Esempio n. 11
0
 def test_maps(self):
     tree = parse_statement("foo = {\n\tabcd: 'foo'\n}\n")
     processed = process_tree(tree)
     self.assertEqual(processed, [
         statement(
             {
                 "type":
                 TYPES["VARIABLE_ASSIGNMENT"],
                 "name":
                 "foo",
                 "value":
                 statement({
                     "type": TYPES["MAP"],
                     "self_closes": False,
                 }, 0),
             }, 0),
         statement(
             {
                 "type": TYPES["MAP_ROW"],
                 "key": "abcd",
                 "value": statement("\"foo\"", 0),
             }, 4),
         statement({
             "type": TYPES["MAP_END"],
         }, 0),
     ])
 def test_labels(self):
     tree = parse_statement("#frontend\nfoo = bar\n")
     processed = process_tree(tree)
     result = generate(processed, "js", "frontend_label")
     result = result["code"]
     self.assertTrue(result["frontend"].startswith(
         "Modules[\"frontend_label\"] = (async ()"))
 def test_variable_chain(self):
     tree = parse_statement("this.foo.bar = baz")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(result["backend"],
                      _wrap_back("this.foo.bar = baz;\n"))
 def test_value_manipulation_with_function(self):
     tree = parse_statement("bar + foo(\n\tbaz\n)\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(result["backend"],
                      _wrap_back("bar + await foo(\n    baz,\n\n);\n"))
 def test_platforms_and_imports(self):
     tree = parse_statement(
         "#frontend\nprint(\n    foo\n)\n#backend\nprint(\n    foo\n)\n")
     processed = process_tree(tree)
     output = generate(processed, "js")
     result = output["code"]
     imports = output["internal_imports"]
     self.assertEqual(
         result["backend"],
         _wrap_back(
             "const {\n    print\n} = require(\"./stdlib_js_backend_common.js\");\n\nawait print(\n    foo,\n\n);\n"
         ))
     self.assertEqual(imports["backend"], [{
         "category": "backend",
         "extension": "js",
         "type": "stdlib",
         "lang": "js",
         "library": "common",
     }])
     self.assertEqual(result["frontend"],
                      _wrap_front("await print(\n    foo,\n\n);\n"))
     self.assertEqual(imports["frontend"], [{
         "extension": "js",
         "type": "stdlib",
         "lang": "js",
         "library": "common",
         "category": "frontend",
     }])
Esempio n. 16
0
    def test_functions(self):
        tree = parse_statement("""function abba(a, b, c)
    foo = bar
function(a,b,c)
    foo = bar
function abba()
    foo = bar
function()
    foo = bar
""")
        processed = process_tree(tree)
        self.assertEqual(processed, [
            statement(
                {
                    "type": TYPES["FUNCTION"],
                    "name": "abba",
                    "params": ["a", "b", "c"],
                }, 0),
            statement(
                {
                    "type": TYPES["VARIABLE_ASSIGNMENT"],
                    "name": "foo",
                    "value": statement("bar", 0),
                }, 4),
            statement(
                {
                    "type": TYPES["FUNCTION"],
                    "name": None,
                    "params": ["a", "b", "c"],
                }, 0),
            statement(
                {
                    "type": TYPES["VARIABLE_ASSIGNMENT"],
                    "name": "foo",
                    "value": statement("bar", 0),
                }, 4),
            statement(
                {
                    "type": TYPES["FUNCTION"],
                    "name": "abba",
                    "params": [],
                }, 0),
            statement(
                {
                    "type": TYPES["VARIABLE_ASSIGNMENT"],
                    "name": "foo",
                    "value": statement("bar", 0),
                }, 4),
            statement({
                "type": TYPES["FUNCTION"],
                "name": None,
                "params": [],
            }, 0),
            statement(
                {
                    "type": TYPES["VARIABLE_ASSIGNMENT"],
                    "name": "foo",
                    "value": statement("bar", 0),
                }, 4),
        ])
    def test_jsx(self):
        expected_imports = [{
            "category": "frontend",
            "extension": "js",
            "lang": "js",
            "library": "frontend",
            "type": "stdlib",
        }]
        tree = parse_statement("#frontend\n<div>\n</div>\n")
        processed = process_tree(tree)
        output = generate(processed, "js")
        result = output["code"]
        imports = output["internal_imports"]
        self.assertEqual(result["frontend"],
                         _wrap_front("new Component(\"div\", {}, [\n\n]);\n"))
        self.assertEqual(imports["frontend"], expected_imports)

        tree = parse_statement("#frontend\n<div/>\n")
        processed = process_tree(tree)
        output = generate(processed, "js")
        result = output["code"]
        imports = output["internal_imports"]
        self.assertEqual(result["frontend"],
                         _wrap_front("new Component(\"div\", {}, []);\n"))
        self.assertEqual(imports["frontend"], expected_imports)

        tree = parse_statement("#frontend\n<div\n\tfoo=\"bar\"\n>\n</div>\n")
        processed = process_tree(tree)
        output = generate(processed, "js")
        result = output["code"]
        imports = output["internal_imports"]
        self.assertEqual(
            result["frontend"],
            _wrap_front(
                "new Component(\"div\", {\n    foo: \"bar\",\n\n}, [\n\n]);\n")
        )
        self.assertEqual(imports["frontend"], expected_imports)

        tree = parse_statement("#frontend\n<input\n/>\n")
        processed = process_tree(tree)
        output = generate(processed, "js")
        result = output["code"]
        imports = output["internal_imports"]
        self.assertEqual(
            result["frontend"],
            _wrap_front("new Component(\"input\", {\n\n}, []);\n"))
        self.assertEqual(imports["frontend"], expected_imports)
 def test_maps(self):
     tree = parse_statement("foo = {\n\tabcd: 'foo'\n}\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back("var foo = {\n    \"abcd\": \"foo\",\n\n};\n"))
 def test_arrays(self):
     tree = parse_statement("foo = [\n\t'bar'\n\t'baz'\n]\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back("var foo = [\n    \"bar\",\n    \"baz\",\n\n];\n"))
 def test_for_as_array(self):
     tree = parse_statement("for foo as bar\n    baz = bar\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back("for (var bar of foo) {\n    var baz = bar;\n}\n"))
 def test_for_normal(self):
     tree = parse_statement("for i=0;i<5;i++\n    foo = i\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back("for (var i = 0;i < 5;i++) {\n    var foo = i;\n}\n"))
 def test_function_call(self):
     tree = parse_statement("func(\n    foo\n    bar\n    baz\n)\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back("await func(\n    foo,\n    bar,\n    baz,\n\n);\n"))
Esempio n. 23
0
    def test_value_manipulation(self):
        tree = parse_statement("bar + baz")
        processed = process_tree(tree)
        self.assertEqual(processed, [
            statement(
                {
                    "type": TYPES["VALUE_MANIPULATION"],
                    "values": [
                        statement("bar", 0),
                        "+",
                        statement("baz", 0),
                    ],
                }, 0),
        ])

        tree = parse_statement("bar    -    \"string\"")
        processed = process_tree(tree)
        self.assertEqual(processed, [
            statement(
                {
                    "type":
                    TYPES["VALUE_MANIPULATION"],
                    "values": [
                        statement("bar", 0),
                        "-",
                        statement("\"string\"", 0),
                    ],
                }, 0),
        ])

        tree = parse_statement("bar + baz + 'foo'")
        processed = process_tree(tree)
        self.assertEqual(processed, [
            statement(
                {
                    "type":
                    TYPES["VALUE_MANIPULATION"],
                    "values": [
                        statement("bar", 0),
                        "+",
                        statement("baz", 0),
                        "+",
                        statement("\"foo\"", 0),
                    ],
                }, 0),
        ])
 def test_for_as_object(self):
     tree = parse_statement("for foo as bar : baz\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back("for (var bar in foo) {\n    var baz = foo[bar];\n}\n"))
    def test_class_definition(self):
        tree = parse_statement("class Foo")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back("class Foo {\n}\n\nmodule.exports = {\n\tFoo\n};\n"))

        tree = parse_statement("class Foo extends Bar")
        processed = process_tree(tree)
        result = generate(processed, "js")
        result = result["code"]
        self.assertEqual(
            result["backend"],
            _wrap_back(
                "class Foo extends Bar {\n}\n\nmodule.exports = {\n\tFoo\n};\n"
            ))
 def test_while(self):
     tree = parse_statement("foo = 5\nwhile foo > bar\n    foo = bar\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back(
             "var foo = 5;\nwhile (foo > bar) {\n    foo = bar;\n}\n"))
Esempio n. 27
0
 def test_map_one_line(self):
     tree = parse_statement("{}")
     processed = process_tree(tree)
     self.assertEqual(processed, [
         statement({
             "type": TYPES["MAP"],
             "self_closes": True,
         }, 0),
     ])
 def test_conditionals(self):
     tree = parse_statement("foo = 10\nif foo == bar\n    foo = bar\n")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back(
             "var foo = 10;\nif (foo == bar) {\n    foo = bar;\n}\n"))
 def test_imports_frontend(self):
     tree = parse_statement("#frontend\nfoo = Bar()\n")
     processed = process_tree(tree)
     result = generate(processed, "js", None,
                       {"frontend": {
                           "classes": ["Bar"]
                       }})
     result = result["code"]
     self.assertEqual(result["frontend"],
                      _wrap_front("var foo = new Bar();\n"))
 def test_imports_with_chain(self):
     tree = parse_statement("Api.post()")
     processed = process_tree(tree)
     result = generate(processed, "js")
     result = result["code"]
     self.assertEqual(
         result["backend"],
         _wrap_back(
             "const {\n    Api\n} = require(\"./stdlib_js_backend_common.js\");\n\nawait Api.post();\n"
         ))