From c3f67c54bd97216a2ea8aa8057c51c38f8ecd5d7 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Tue, 9 Dec 2014 13:46:01 +0300 Subject: [PATCH] JS backend: added jsCode test cases --- compiler/testData/cli/js/jsCodeError.args | 3 + compiler/testData/cli/js/jsCodeError.kt | 2 + compiler/testData/cli/js/jsCodeError.out | 3 + .../cli/js/jsCodeNotLiteralError.args | 3 + .../testData/cli/js/jsCodeNotLiteralError.kt | 3 + .../testData/cli/js/jsCodeNotLiteralError.out | 3 + compiler/testData/cli/js/jsCodeWarning.args | 3 + compiler/testData/cli/js/jsCodeWarning.kt | 1 + compiler/testData/cli/js/jsCodeWarning.out | 2 + .../cli/KotlincExecutableTestGenerated.java | 18 +++ .../org/jetbrains/jet/cli/js/K2JsCliTest.java | 15 +++ .../k2js/test/semantics/JsCodeTest.java | 106 ++++++++++++++++++ .../testData/jsCode/cases/break.kt | 19 ++++ .../testData/jsCode/cases/catchScope.kt | 20 ++++ .../testData/jsCode/cases/continue.kt | 19 ++++ .../testData/jsCode/cases/doWhile.kt | 19 ++++ js/js.translator/testData/jsCode/cases/for.kt | 18 +++ .../testData/jsCode/cases/forIn.kt | 18 +++ .../testData/jsCode/cases/function.kt | 13 +++ js/js.translator/testData/jsCode/cases/if.kt | 18 +++ .../testData/jsCode/cases/invocation.kt | 9 ++ .../testData/jsCode/cases/label.kt | 59 ++++++++++ .../testData/jsCode/cases/labelNestedClash.kt | 30 +++++ .../jsCode/cases/labelSiblingClash.kt | 26 +++++ .../testData/jsCode/cases/literal.kt | 38 +++++++ .../testData/jsCode/cases/object.kt | 20 ++++ .../testData/jsCode/cases/objectScopes.kt | 15 +++ .../testData/jsCode/cases/operators.kt | 67 +++++++++++ .../testData/jsCode/cases/quotes.kt | 14 +++ .../testData/jsCode/cases/switch.kt | 27 +++++ .../testData/jsCode/cases/tryCatchFinally.kt | 27 +++++ .../testData/jsCode/cases/while.kt | 19 ++++ 32 files changed, 657 insertions(+) create mode 100644 compiler/testData/cli/js/jsCodeError.args create mode 100644 compiler/testData/cli/js/jsCodeError.kt create mode 100644 compiler/testData/cli/js/jsCodeError.out create mode 100644 compiler/testData/cli/js/jsCodeNotLiteralError.args create mode 100644 compiler/testData/cli/js/jsCodeNotLiteralError.kt create mode 100644 compiler/testData/cli/js/jsCodeNotLiteralError.out create mode 100644 compiler/testData/cli/js/jsCodeWarning.args create mode 100644 compiler/testData/cli/js/jsCodeWarning.kt create mode 100644 compiler/testData/cli/js/jsCodeWarning.out create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/semantics/JsCodeTest.java create mode 100644 js/js.translator/testData/jsCode/cases/break.kt create mode 100644 js/js.translator/testData/jsCode/cases/catchScope.kt create mode 100644 js/js.translator/testData/jsCode/cases/continue.kt create mode 100644 js/js.translator/testData/jsCode/cases/doWhile.kt create mode 100644 js/js.translator/testData/jsCode/cases/for.kt create mode 100644 js/js.translator/testData/jsCode/cases/forIn.kt create mode 100644 js/js.translator/testData/jsCode/cases/function.kt create mode 100644 js/js.translator/testData/jsCode/cases/if.kt create mode 100644 js/js.translator/testData/jsCode/cases/invocation.kt create mode 100644 js/js.translator/testData/jsCode/cases/label.kt create mode 100644 js/js.translator/testData/jsCode/cases/labelNestedClash.kt create mode 100644 js/js.translator/testData/jsCode/cases/labelSiblingClash.kt create mode 100644 js/js.translator/testData/jsCode/cases/literal.kt create mode 100644 js/js.translator/testData/jsCode/cases/object.kt create mode 100644 js/js.translator/testData/jsCode/cases/objectScopes.kt create mode 100644 js/js.translator/testData/jsCode/cases/operators.kt create mode 100644 js/js.translator/testData/jsCode/cases/quotes.kt create mode 100644 js/js.translator/testData/jsCode/cases/switch.kt create mode 100644 js/js.translator/testData/jsCode/cases/tryCatchFinally.kt create mode 100644 js/js.translator/testData/jsCode/cases/while.kt diff --git a/compiler/testData/cli/js/jsCodeError.args b/compiler/testData/cli/js/jsCodeError.args new file mode 100644 index 00000000000..ed43350e65f --- /dev/null +++ b/compiler/testData/cli/js/jsCodeError.args @@ -0,0 +1,3 @@ +$TESTDATA_DIR$/jsCodeError.kt +-output +$TEMP_DIR$/out.js \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeError.kt b/compiler/testData/cli/js/jsCodeError.kt new file mode 100644 index 00000000000..37b18b90d3f --- /dev/null +++ b/compiler/testData/cli/js/jsCodeError.kt @@ -0,0 +1,2 @@ +fun f1(): Unit = js("var = 10;") +fun f2(): Unit = js("""var = 10;""") \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeError.out b/compiler/testData/cli/js/jsCodeError.out new file mode 100644 index 00000000000..4b2b46ec2ed --- /dev/null +++ b/compiler/testData/cli/js/jsCodeError.out @@ -0,0 +1,3 @@ +ERROR: compiler/testData/cli/js/jsCodeError.kt: (1, 25) JavaScript: missing variable name +ERROR: compiler/testData/cli/js/jsCodeError.kt: (2, 27) JavaScript: missing variable name +COMPILATION_ERROR \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeNotLiteralError.args b/compiler/testData/cli/js/jsCodeNotLiteralError.args new file mode 100644 index 00000000000..be9f8c51b05 --- /dev/null +++ b/compiler/testData/cli/js/jsCodeNotLiteralError.args @@ -0,0 +1,3 @@ +$TESTDATA_DIR$/jsCodeNotLiteralError.kt +-output +$TEMP_DIR$/out.js \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeNotLiteralError.kt b/compiler/testData/cli/js/jsCodeNotLiteralError.kt new file mode 100644 index 00000000000..7a1d1117b15 --- /dev/null +++ b/compiler/testData/cli/js/jsCodeNotLiteralError.kt @@ -0,0 +1,3 @@ +val s = "1 + 1;" +fun two(): Int = js(s) +fun three(): Int = js("1" + "+ 2;") \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeNotLiteralError.out b/compiler/testData/cli/js/jsCodeNotLiteralError.out new file mode 100644 index 00000000000..fe966af2efa --- /dev/null +++ b/compiler/testData/cli/js/jsCodeNotLiteralError.out @@ -0,0 +1,3 @@ +ERROR: compiler/testData/cli/js/jsCodeNotLiteralError.kt: (2, 18) Argument must be string literal +ERROR: compiler/testData/cli/js/jsCodeNotLiteralError.kt: (3, 20) Argument must be string literal +COMPILATION_ERROR \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeWarning.args b/compiler/testData/cli/js/jsCodeWarning.args new file mode 100644 index 00000000000..868e777a7d2 --- /dev/null +++ b/compiler/testData/cli/js/jsCodeWarning.args @@ -0,0 +1,3 @@ +$TESTDATA_DIR$/jsCodeWarning.kt +-output +$TEMP_DIR$/out.js \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeWarning.kt b/compiler/testData/cli/js/jsCodeWarning.kt new file mode 100644 index 00000000000..7ee479ddb43 --- /dev/null +++ b/compiler/testData/cli/js/jsCodeWarning.kt @@ -0,0 +1 @@ +fun main(args: Array): Unit = js("var a = 08;") \ No newline at end of file diff --git a/compiler/testData/cli/js/jsCodeWarning.out b/compiler/testData/cli/js/jsCodeWarning.out new file mode 100644 index 00000000000..e6d5117c059 --- /dev/null +++ b/compiler/testData/cli/js/jsCodeWarning.out @@ -0,0 +1,2 @@ +WARNING: compiler/testData/cli/js/jsCodeWarning.kt: (1, 50) JavaScript: illegal octal literal digit 8; interpreting it as a decimal digit +OK \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java b/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java index 330441a5a72..dfa12fec5f9 100644 --- a/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/cli/KotlincExecutableTestGenerated.java @@ -156,6 +156,24 @@ public class KotlincExecutableTestGenerated extends AbstractKotlincExecutableTes JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cli/js"), Pattern.compile("^(.+)\\.args$"), false); } + @TestMetadata("jsCodeError.args") + public void testJsCodeError() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/js/jsCodeError.args"); + doJsTest(fileName); + } + + @TestMetadata("jsCodeNotLiteralError.args") + public void testJsCodeNotLiteralError() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/js/jsCodeNotLiteralError.args"); + doJsTest(fileName); + } + + @TestMetadata("jsCodeWarning.args") + public void testJsCodeWarning() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/js/jsCodeWarning.args"); + doJsTest(fileName); + } + @TestMetadata("jsExtraHelp.args") public void testJsExtraHelp() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/js/jsExtraHelp.args"); diff --git a/compiler/tests/org/jetbrains/jet/cli/js/K2JsCliTest.java b/compiler/tests/org/jetbrains/jet/cli/js/K2JsCliTest.java index 6eaaa3218d4..21a6001d6ec 100644 --- a/compiler/tests/org/jetbrains/jet/cli/js/K2JsCliTest.java +++ b/compiler/tests/org/jetbrains/jet/cli/js/K2JsCliTest.java @@ -90,4 +90,19 @@ public class K2JsCliTest extends CliBaseTest { Assert.assertFalse(new File(tmpdir.getTmpDir(), "out.js").isFile()); } + + @Test + public void jsCodeError() throws Exception { + executeCompilerCompareOutputJS(); + } + + @Test + public void jsCodeWarning() throws Exception { + executeCompilerCompareOutputJS(); + } + + @Test + public void jsCodeNotLiteralError() throws Exception { + executeCompilerCompareOutputJS(); + } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsCodeTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsCodeTest.java new file mode 100644 index 00000000000..aeca65d1ce9 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsCodeTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.test.semantics; + +import org.jetbrains.k2js.test.SingleFileTranslationWithDirectivesTest; + +public final class JsCodeTest extends SingleFileTranslationWithDirectivesTest { + + public JsCodeTest() { + super("jsCode/"); + } + + public void testQuotes() throws Exception { + checkFooBoxIsOk(); + } + + public void testLiteral() throws Exception { + checkFooBoxIsOk(); + } + + public void testFunction() throws Exception { + checkFooBoxIsOk(); + } + + public void testObject() throws Exception { + checkFooBoxIsOk(); + } + + public void testOperators() throws Exception { + checkFooBoxIsOk(); + } + + public void testIf() throws Exception { + checkFooBoxIsOk(); + } + + public void testSwitch() throws Exception { + checkFooBoxIsOk(); + } + + public void testDoWhile() throws Exception { + checkFooBoxIsOk(); + } + + public void testWhile() throws Exception { + checkFooBoxIsOk(); + } + + public void testFor() throws Exception { + checkFooBoxIsOk(); + } + + public void testForIn() throws Exception { + checkFooBoxIsOk(); + } + + public void testInvocation() throws Exception { + checkFooBoxIsOk(); + } + + public void testBreak() throws Exception { + checkFooBoxIsOk(); + } + + public void testContinue() throws Exception { + checkFooBoxIsOk(); + } + + public void testLabel() throws Exception { + checkFooBoxIsOk(); + } + + public void testLabelSiblingClash() throws Exception { + checkFooBoxIsOkWithDirectives(); + } + + public void testLabelNestedClash() throws Exception { + checkFooBoxIsOkWithDirectives(); + } + + public void testTryCatchFinally() throws Exception { + checkFooBoxIsOk(); + } + + public void testCatchScope() throws Exception { + checkFooBoxIsOk(); + } + + public void testObjectScopes() throws Exception { + checkFooBoxIsOk(); + } +} diff --git a/js/js.translator/testData/jsCode/cases/break.kt b/js/js.translator/testData/jsCode/cases/break.kt new file mode 100644 index 00000000000..82e8c7278e0 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/break.kt @@ -0,0 +1,19 @@ +package foo + +fun box(): String { + var c: Int = 0 + + js(""" + for (var i = 0; i < 10; i++) { + c = i; + + if (i === 3) { + break; + } + } + """) + + assertEquals(3, c) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/catchScope.kt b/js/js.translator/testData/jsCode/cases/catchScope.kt new file mode 100644 index 00000000000..fb44eb2c500 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/catchScope.kt @@ -0,0 +1,20 @@ +package foo + +fun test(action: ()->Unit): String = js(""" + var e = { message: "ok" }; + + try { + action(); + } catch (e) { + return e.message; + } + + return e.message; +""") + +fun box(): String { + assertEquals("ok", test {}) + assertEquals("not ok", test { throw Exception("not ok") }) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/continue.kt b/js/js.translator/testData/jsCode/cases/continue.kt new file mode 100644 index 00000000000..e58fa7008a3 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/continue.kt @@ -0,0 +1,19 @@ +package foo + +fun box(): String { + var c: Int = 0 + + js(""" + for (var i = 1; i < 6; i++) { + if (i % 2 === 0) { + continue; + } + + c++; + } + """) + + assertEquals(3, c) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/doWhile.kt b/js/js.translator/testData/jsCode/cases/doWhile.kt new file mode 100644 index 00000000000..21364498243 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/doWhile.kt @@ -0,0 +1,19 @@ +package foo + +fun factorial(n: Int): Int = js(""" + var result = 1; + var i = 0; + + do { + result *= ++i; + } while(i < n); + + return result; +""") + +fun box(): String { + assertEquals(24, factorial(4)) + assertEquals(120, factorial(5)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/for.kt b/js/js.translator/testData/jsCode/cases/for.kt new file mode 100644 index 00000000000..93862b75c88 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/for.kt @@ -0,0 +1,18 @@ +package foo + +fun factorial(n: Int): Int = js(""" + var result = 1; + + for (var i = 1; i <= n; i++) { + result *= i; + } + + return result; +""") + +fun box(): String { + assertEquals(24, factorial(4)) + assertEquals(120, factorial(5)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/forIn.kt b/js/js.translator/testData/jsCode/cases/forIn.kt new file mode 100644 index 00000000000..c1e210aa532 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/forIn.kt @@ -0,0 +1,18 @@ +package foo + +fun countKeys(a: Array): Int = js(""" + var result = 0; + + for (var key in a) { + result += 1; + } + + return result; +""") + +fun box(): String { + assertEquals(3, countKeys(array(1,2,3))) + assertEquals(4, countKeys(array(1,2,3,4))) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/function.kt b/js/js.translator/testData/jsCode/cases/function.kt new file mode 100644 index 00000000000..43655cb484a --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/function.kt @@ -0,0 +1,13 @@ +package foo + +fun callWithArgs(sumFunc: (Int, Int) -> Int, a: Int, b: Int): Int { + return sumFunc(a, b) +} + +fun box(): String { + val kotlinSum: (Int, Int) -> Int = {(a, b) -> a + b} + val jsSum: (Int, Int) -> Int = js("function (a, b) { return a + b; }") + assertEquals(callWithArgs(kotlinSum, 1, 2), callWithArgs(jsSum, 1, 2)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/if.kt b/js/js.translator/testData/jsCode/cases/if.kt new file mode 100644 index 00000000000..8d3a6fcdfcb --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/if.kt @@ -0,0 +1,18 @@ +package foo + +fun testIf(flag: Boolean): Int = js(""" + if (flag) + return 1; + else + return -1; + + return 0; +""") + + +fun box(): String { + assertEquals(1, testIf(true)) + assertEquals(-1, testIf(false)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/invocation.kt b/js/js.translator/testData/jsCode/cases/invocation.kt new file mode 100644 index 00000000000..6301f928cf3 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/invocation.kt @@ -0,0 +1,9 @@ +package foo + +fun run(a: A, b: B, func: (A, B) -> C): C = js("func(a, b)") + +fun box(): String { + assertEquals(3, run(1, 2) {(a, b) -> a + b}) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/label.kt b/js/js.translator/testData/jsCode/cases/label.kt new file mode 100644 index 00000000000..b70201a54f9 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/label.kt @@ -0,0 +1,59 @@ +package foo + +fun testLabelledBlock() { + var c: Int = 0 + + js(""" + block: { + c = 1; + break block; + c = 2; + } + """) + + assertEquals(1, c, "testLabelledBlock") +} + +fun testBreakInFor() { + var c: Int = 0 + + js(""" + outer: for (var i = 0; i < 10; i++) { + for (var j = 0; j < 10; j++) { + if (i === 1) { + break outer; + } + + c += 1; + } + } + """) + + assertEquals(10, c, "testBreakInFor") +} + +fun testContinueInFor() { + var c: Int = 0 + + js(""" + outer: for (var i = 0; i < 10; i++) { + for (var j = 0; j < 10; j++) { + if (i >= 1) { + continue outer; + } + + c += 1; + } + } + """) + + assertEquals(10, c, "testContinueInFor") +} + +fun box(): String { + testLabelledBlock() + testBreakInFor() + testContinueInFor() + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/labelNestedClash.kt b/js/js.translator/testData/jsCode/cases/labelNestedClash.kt new file mode 100644 index 00000000000..d0225e6c8d3 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/labelNestedClash.kt @@ -0,0 +1,30 @@ +package foo + +// CHECK_LABELS_COUNT: function=box name=block count=1 +// CHECK_LABELS_COUNT: function=box name=block_0 count=1 + +fun box(): String { + var i = 0 + var j = 0 + + js(""" + block: { + i++; + + block: { + j++; + break block; + j++; + } + + break block; + i++; + } + + + """) + + assertEquals(1, i, "i") + assertEquals(1, j, "j") + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/labelSiblingClash.kt b/js/js.translator/testData/jsCode/cases/labelSiblingClash.kt new file mode 100644 index 00000000000..6b2583bcc91 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/labelSiblingClash.kt @@ -0,0 +1,26 @@ +package foo + +// CHECK_LABELS_COUNT: function=box name=block count=2 + +fun box(): String { + var i = 0 + var j = 0 + + js(""" + block: { + i++; + break block; + i++; + } + + block: { + j++; + break block; + j++; + } + """) + + assertEquals(1, i, "i") + assertEquals(1, j, "j") + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/literal.kt b/js/js.translator/testData/jsCode/cases/literal.kt new file mode 100644 index 00000000000..dcf17185f96 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/literal.kt @@ -0,0 +1,38 @@ +package foo + +native trait HasName { + val name: String +} + +fun assertArrayEquals(expected: Array, actual: Array) { + val expectedSize = expected.size + val actualSize = actual.size + + if (expectedSize != actualSize) { + throw Exception("expected size -- $expectedSize, actual size -- $actualSize") + } + + for (i in 0..expectedSize) { + val expectedIth = expected[i] + val actualIth = actual[i] + + if (expected[i] != actual[i]) { + throw Exception("expected[$i] -- $expectedIth, actual[$i] -- $actualIth") + } + } +} + +fun box(): String { + assertEquals(10, js("10"), "Int") + assertEquals(10.5, js("10.5"), "Float") + assertEquals("10", js("'10'"), "String") + assertEquals(true, js("true"), "True") + assertEquals(false, js("false"), "False") + + val obj: HasName = js("({name: 'OBJ'})") + assertEquals("OBJ", obj.name, "Object") + + assertArrayEquals(array(1, 2, 3), js("[1, 2, 3]")) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/object.kt b/js/js.translator/testData/jsCode/cases/object.kt new file mode 100644 index 00000000000..1328c101b6f --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/object.kt @@ -0,0 +1,20 @@ +package foo + +native trait Summizer { + fun sum(a: Int, b: Int): Int +} + +fun getSummizer(): Summizer = js(""" + var summizer = { + sum: function(a, b) { return a + b;} + }; + + return summizer; +"""); + +fun box(): String { + val summizer = getSummizer() + assertEquals(3, summizer.sum(1, 2)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/objectScopes.kt b/js/js.translator/testData/jsCode/cases/objectScopes.kt new file mode 100644 index 00000000000..ec8ea9ffc90 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/objectScopes.kt @@ -0,0 +1,15 @@ +package foo + +native trait Summizer { + fun sum(a: Int, b: Int): Int +} + +fun box(): String { + val summizer1: Summizer = js("({ sum: function(a, b) { return a + b; }})") + assertEquals(3, summizer1.sum(1, 2), "summizer1") + + val summizer2: Summizer = js("({ sum: function(a, b) { return a + b; }})") + assertEquals(3, summizer2.sum(1, 2), "summizer2") + + return "OK"; +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/operators.kt b/js/js.translator/testData/jsCode/cases/operators.kt new file mode 100644 index 00000000000..9cdacf7b8fb --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/operators.kt @@ -0,0 +1,67 @@ +package foo + +data class A(val value: Int) + +fun box(): String { + assertEquals(-1, js("-1"), "- (unary)") + assertEquals(1, js("+'1'"), "+ (unary)") + + assertEquals(1, js("2 - 1"), "- (binary)") + assertEquals(3, js("2 + 1"), "+ (binary)") + assertEquals(10, js("2 * 5"), "*") + assertEquals(5, js("10 / 2"), "/") + assertEquals(1, js("11 % 2"), "%") + + assertEquals(36, js("9 << 2"), "<<") + assertEquals(2, js("9 >> 2"), ">>") + assertEquals(1073741821, js("-9 >>> 2"), ">>>") + assertEquals(0, js("0 & 1"), "&") + assertEquals(1, js("0 | 1"), "|") + assertEquals(1, js("0 ^ 1"), "^") + assertEquals(-2, js("~1"), "~") + + var i = 2 + assertEquals(1, js("--i"), "-- prefix") + assertEquals(1, js("i--"), "-- postfix (1)") + assertEquals(0, js("i"), "-- postfix (0)") + assertEquals(1, js("++i"), "++ prefix") + assertEquals(1, js("i++"), "++ postfix (1)") + assertEquals(2, js("i"), "++ postfix (0)") + + assertEquals(true , js("true || false"), "||") + assertEquals(false , js("true && false"), "&&") + assertEquals(false , js("!true"), "!") + + assertEquals(true , js("1 < 2"), "<") + assertEquals(false, js("1 > 2"), ">") + assertEquals(true, js("1 <= 2"), "<=") + assertEquals(false, js("1 >= 2"), ">=") + + assertEquals(false, js("2 === '2'"), "===") + assertEquals(true, js("2 !== '2'"), "!==") + assertEquals(true, js("2 == '2'"), "==") + assertEquals(false, js("2 != '2'"), "!=") + + assertEquals("odd", js("(1 % 2 === 0)?'even':'odd'"), "?:") + assertEquals("even", js("(4 % 2 === 0)?'even':'odd'"), "?:") + assertEquals(3, js("1,2,3"), ", (comma)") + + var j = 0 + assertEquals(1, js("j = 1"), "=") + assertEquals(3, js("j += 2"), "+=") + assertEquals(2, js("j -= 1"), "-=") + assertEquals(14, js("j *= 7"), "*=") + assertEquals(7, js("j /= 2"), "/=") + assertEquals(1, js("j %= 2"), "%=") + + assertEquals(Unit, js("(void 0)"), "void") + assertEquals(true, js("'key' in {'key': 10}"), "in") + assertEquals("string", js("typeof 'str'"), "typeof") + assertEquals(A(2), js("new _.foo.A(2)"), "new") + assertEquals(true, js("new String('str') instanceof String"), "instanceof") + + var s: Any = js("({key: 10})") + assertEquals(Unit, js("delete s.key, s.key"), "delete") + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/quotes.kt b/js/js.translator/testData/jsCode/cases/quotes.kt new file mode 100644 index 00000000000..1dd29c1cffa --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/quotes.kt @@ -0,0 +1,14 @@ +package foo + +fun singleQuoted(i: Int): Int = js("return i") +fun tripleQuoted(i: Int): Int = js("""return i""") +fun tripleQuotedInnerQuotes(i: Int): String = js("""return "i"""") + + +fun box(): String { + assertEquals(0, singleQuoted(0)) + assertEquals(0, tripleQuoted(0)) + assertEquals("i", tripleQuotedInnerQuotes(0)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/switch.kt b/js/js.translator/testData/jsCode/cases/switch.kt new file mode 100644 index 00000000000..7bc9cc4d0ef --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/switch.kt @@ -0,0 +1,27 @@ +package foo + +fun testSwitch(number: Int): String = js(""" + var result; + + switch(number) { + case 1: + result = "one"; + break; + case 2: + result = "two"; + break; + default: + result = "don't know"; + break; + } + + return result; +""") + +fun box(): String { + assertEquals("one", testSwitch(1)) + assertEquals("two", testSwitch(2)) + assertEquals("don't know", testSwitch(3)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/tryCatchFinally.kt b/js/js.translator/testData/jsCode/cases/tryCatchFinally.kt new file mode 100644 index 00000000000..6bb00f5ffa6 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/tryCatchFinally.kt @@ -0,0 +1,27 @@ +package foo + +class Counter { + var count: Int = 0 + + public fun inc() { + count++ + } +} + +fun test(c: Counter, ex: Exception): Unit = js(""" + try { + throw ex; + } catch (e) { + c.inc() + } finally { + c.inc() + } +""") + +fun box(): String { + val c = Counter() + test(c, NullPointerException()) + assertEquals(2, c.count) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/jsCode/cases/while.kt b/js/js.translator/testData/jsCode/cases/while.kt new file mode 100644 index 00000000000..a1ac13e38e9 --- /dev/null +++ b/js/js.translator/testData/jsCode/cases/while.kt @@ -0,0 +1,19 @@ +package foo + +fun factorial(n: Int): Int = js(""" + var result = 1; + var i = 1; + + while(i <= n) { + result *= i++; + } + + return result; +""") + +fun box(): String { + assertEquals(24, factorial(4)) + assertEquals(120, factorial(5)) + + return "OK" +} \ No newline at end of file