diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClassInheritanceTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClassInheritanceTest.java index 6d554c7b7fa..f168272c8bf 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClassInheritanceTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/ClassInheritanceTest.java @@ -32,6 +32,10 @@ public final class ClassInheritanceTest extends SingleFileTranslationTest { checkFooBoxIsOk(); } + public void testOverrideAnyMethods() throws Exception { + checkFooBoxIsOk(); + } + public void testBaseCall() throws Exception { checkFooBoxIsOk(); } @@ -75,9 +79,14 @@ public final class ClassInheritanceTest extends SingleFileTranslationTest { public void testKt3499() throws Exception { fooBoxTest(); } + public void testFromFakeClasses() throws Exception { checkFooBoxIsOk(); } + + public void testWithInitializeMethod() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/FunctionTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/FunctionTest.java index ba67cdc4c9a..915556400c2 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/FunctionTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/FunctionTest.java @@ -78,6 +78,10 @@ public class FunctionTest extends AbstractExpressionTest { fooBoxTest(); } + public void testFunctionExpression() throws Exception { + checkFooBoxIsOk(); + } + public void testExpressionAsFunction() throws Exception { fooBoxTest(); } @@ -123,6 +127,14 @@ public class FunctionTest extends AbstractExpressionTest { checkFooBoxIsOk(); } + public void testOverloadOverridenFun() throws Exception { + checkFooBoxIsOk(); + } + + public void testOverloadClassConstructorByFactoryMethod() throws Exception { + checkFooBoxIsOk(); + } + public void testManglingAnyMethods() throws Exception { checkFooBoxIsOk(); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/IdentifierClashTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/IdentifierClashTest.java index 430c59bb6bc..7dd723c44e9 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/IdentifierClashTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/IdentifierClashTest.java @@ -29,7 +29,4 @@ public final class IdentifierClashTest extends AbstractExpressionTest { public void testOverloadedFun() throws Exception { fooBoxTest(); } - - public void testDummyFunctionToMakeTestWork() { - } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java index 70b7d989a36..ecc090ab29a 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java @@ -174,12 +174,6 @@ public final class MiscTest extends AbstractExpressionTest { fooBoxIsValue("OK"); } - //TODO:see http://youtrack.jetbrains.com/issue/KT-2565 - @SuppressWarnings("UnusedDeclaration") - public void TODO_testFunctionExpression() throws Exception { - fooBoxIsValue("OK"); - } - public void testExclExclThrows() throws Exception { try { fooBoxTest(); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/PatternMatchingTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/PatternMatchingTest.java index a2e07c93a2a..bc76c3d1bf0 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/PatternMatchingTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/PatternMatchingTest.java @@ -85,4 +85,8 @@ public final class PatternMatchingTest extends SingleFileTranslationTest { public void testWhenWithOnlyElse() throws Exception { fooBoxTest(); } + + public void testIfInWhen() throws Exception { + checkFooBoxIsOk(); + } } \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ToStringTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/RegressionTest.java similarity index 66% rename from js/js.tests/test/org/jetbrains/k2js/test/semantics/ToStringTest.java rename to js/js.tests/test/org/jetbrains/k2js/test/semantics/RegressionTest.java index 85d4ba9f156..95e05b4ed56 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/ToStringTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/RegressionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * 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. @@ -16,15 +16,15 @@ package org.jetbrains.k2js.test.semantics; -public final class ToStringTest extends AbstractExpressionTest { +import org.jetbrains.k2js.test.SingleFileTranslationTest; - public ToStringTest() { - super("toString/"); +public final class RegressionTest extends SingleFileTranslationTest { + + public RegressionTest() { + super("regression/"); } - public void testToString() throws Exception { - // TODO uncomment this method to make the test case fail - //fooBoxIsValue("Foo(James)"); + public void testKt2470() throws Exception { + checkFooBoxIsOk(); } - -} \ No newline at end of file +} diff --git a/js/js.translator/testData/expression/misc/cases/functionExpression.kt b/js/js.translator/testData/expression/function/cases/functionExpression.kt similarity index 92% rename from js/js.translator/testData/expression/misc/cases/functionExpression.kt rename to js/js.translator/testData/expression/function/cases/functionExpression.kt index 36fb93aadcf..0cef74c369f 100644 --- a/js/js.translator/testData/expression/misc/cases/functionExpression.kt +++ b/js/js.translator/testData/expression/function/cases/functionExpression.kt @@ -21,7 +21,7 @@ fun box(): Any? { if ( {(x: Int) -> x }(1) != 1) return "test 5 failed"; if ( 1.{ Int.(x: Int) -> x + this }(1) != 2) return "test 6 failed"; val tmp = 1.({ Int.() -> this })() - if (tmp != 1) return "test 7 failed, res: $tmp ${tmp is Int}"; + if (+tmp != 1) return "test 7 failed, res: $tmp ${tmp is Int}"; if ( (fooT1("mama"))() != "mama") return "test 8 failed"; if ( (fooT2("mama"))("papa") != "mamapapa") return "test 9 failed"; return "OK" diff --git a/js/js.translator/testData/expression/function/cases/overloadClassConstructorByFactoryMethod.kt b/js/js.translator/testData/expression/function/cases/overloadClassConstructorByFactoryMethod.kt new file mode 100644 index 00000000000..efca5e4bea1 --- /dev/null +++ b/js/js.translator/testData/expression/function/cases/overloadClassConstructorByFactoryMethod.kt @@ -0,0 +1,18 @@ +// KT-2995 creating factory methods to simulate overloaded constructors don't work in JavaScript + +package foo + +class Foo(val name: String) + +fun Foo() = Foo("") + +fun assertEquals(expected: Any, actual: Any) { + if (expected != actual) throw Exception("expected = $expected, actual = $actual") +} + +fun box(): String { + assertEquals("", Foo().name) + assertEquals("BarBaz", Foo("BarBaz").name) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/expression/function/cases/overloadOverridenFun.kt b/js/js.translator/testData/expression/function/cases/overloadOverridenFun.kt new file mode 100644 index 00000000000..a2cc1cd9167 --- /dev/null +++ b/js/js.translator/testData/expression/function/cases/overloadOverridenFun.kt @@ -0,0 +1,26 @@ +// KT-2219 if function overload overridden function its name doesn't translated correctly + +package foo + +fun assertEquals(expected: Any, actual: Any) { + if (expected != actual) throw Exception("expected = $expected, actual = $actual") +} + +trait I { + fun test(): String +} + +class P : I { + override fun test(): String = "foo" + test("bar") + + private fun test(p: String) = p + + fun test(s: String, i: Int) = "$i $s" +} + +fun box(): String { + assertEquals("foobar", P().test()) + assertEquals("35 baz", P().test("baz", 35)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/expression/function/cases/overloadingWithInheritance.kt b/js/js.translator/testData/expression/function/cases/overloadingWithInheritance.kt index dc740bdd075..110e767a07b 100644 --- a/js/js.translator/testData/expression/function/cases/overloadingWithInheritance.kt +++ b/js/js.translator/testData/expression/function/cases/overloadingWithInheritance.kt @@ -13,12 +13,12 @@ class C : A, B { } fun assertEquals(expected: Any, actual: Any) { - if (expected != actual) throw Exception("expected = $expected\nactual = $actual") + if (expected != actual) throw Exception("expected = $expected, actual = $actual") } fun box(): String { - assertEquals(C().foo(1), "A") - assertEquals(C().foo(""), "B") - assertEquals(C().foo(), "C") + assertEquals("A", C().foo(1)) + assertEquals("B", C().foo("")) + assertEquals("C", C().foo()) return "OK" } diff --git a/js/js.translator/testData/expression/toString/cases/toString.kt b/js/js.translator/testData/expression/toString/cases/toString.kt deleted file mode 100644 index fa829164eee..00000000000 --- a/js/js.translator/testData/expression/toString/cases/toString.kt +++ /dev/null @@ -1,13 +0,0 @@ -package foo - -class Foo(val name: String) { - - public override fun toString(): String { - return "Foo($name)" - } -} - -fun box(): String { - val a = Foo("James") - return a.toString() -} \ No newline at end of file diff --git a/js/js.translator/testData/inheritance/cases/overrideAnyMethods.kt b/js/js.translator/testData/inheritance/cases/overrideAnyMethods.kt new file mode 100644 index 00000000000..7720b2de551 --- /dev/null +++ b/js/js.translator/testData/inheritance/cases/overrideAnyMethods.kt @@ -0,0 +1,43 @@ +package foo + +native +fun String.charCodeAt(i: Int): Int = noImpl + +// Because String in JS doesn't have hashCode method +fun String.myHashCode(): Int { + var hash = 0 + + for (i in 0..size - 1) { + hash = 31 * hash + charCodeAt(i) + } + + return hash +} + +class Foo(val name: String) { + override fun equals(other: Any?): Boolean { + if (other is Foo) return name == other.name + return this identityEquals other + } + override fun hashCode(): Int = name.myHashCode() + override fun toString(): String = "Foo($name)" +} + +fun assertEquals(expected: Any, actual: Any) { + if (expected != actual) throw Exception("expected = $expected, actual = $actual") +} + +fun box(): String { + val james = Foo("James") + val anotherJames = Foo("James") + val max = Foo("Max") + + assertEquals(true, james == anotherJames) + assertEquals(false, james == max) + assertEquals("James".myHashCode(), james.hashCode()) + assertEquals("Max".myHashCode(), max.hashCode()) + assertEquals("Foo(James)", james.toString()) + assertEquals("Foo(Max)", max.toString()) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/inheritance/cases/withInitializeMethod.kt b/js/js.translator/testData/inheritance/cases/withInitializeMethod.kt new file mode 100644 index 00000000000..83c80576263 --- /dev/null +++ b/js/js.translator/testData/inheritance/cases/withInitializeMethod.kt @@ -0,0 +1,8 @@ +package foo + +class A(val ok: String) { + fun initialize() = ok +} + +fun box(): String = A("OK").initialize() + diff --git a/js/js.translator/testData/patternMatching/cases/ifInWhen.kt b/js/js.translator/testData/patternMatching/cases/ifInWhen.kt new file mode 100644 index 00000000000..7e45d517a15 --- /dev/null +++ b/js/js.translator/testData/patternMatching/cases/ifInWhen.kt @@ -0,0 +1,24 @@ +// KT-2221 if in when + +package foo + +fun test(caseId: Int, value: Int, expected: Int) { + val actual: Int + when (caseId) { + 0 -> if (value < 0) actual = -value else actual = value + 1 -> actual = if (value < 0) -value else value + else -> throw Exception("Unexpected case: $caseId") + } + + if (expected != actual) throw Exception("expected = $expected, actual = $actual") +} + +fun box(): String { + test(0, 3, 3) + test(0, -13, 13) + test(1, 23, 23) + test(1, -3, 3) + + return "OK" +} + diff --git a/js/js.translator/testData/regression/cases/kt2470.kt b/js/js.translator/testData/regression/cases/kt2470.kt new file mode 100644 index 00000000000..57749c35537 --- /dev/null +++ b/js/js.translator/testData/regression/cases/kt2470.kt @@ -0,0 +1,24 @@ +// KT-2470 another name mangling bug: kotlin.test.failsWith() gets generated to invalid JS + +package foo + +native val Exception.message: String = noImpl + +public fun failsWith(block: () -> Any): T { + try { + block() + } + catch (e: T) { + return e + } + + throw Exception("Should have failed") +} + +fun box(): String { + val a = failsWith { + throw Exception("OK") + } + + return a.message +}