Minor in JS backend: added regression tests.

#{KT-2219, KT-2470, KT-2507, KT-2222, KT-2995, KT-2221} Obsolete
This commit is contained in:
Zalim Bashorov
2014-03-13 21:40:29 +04:00
parent 604e062f91
commit 7b503bbe6f
15 changed files with 182 additions and 36 deletions
@@ -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();
}
}
@@ -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();
}
@@ -29,7 +29,4 @@ public final class IdentifierClashTest extends AbstractExpressionTest {
public void testOverloadedFun() throws Exception {
fooBoxTest();
}
public void testDummyFunctionToMakeTestWork() {
}
}
@@ -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();
@@ -85,4 +85,8 @@ public final class PatternMatchingTest extends SingleFileTranslationTest {
public void testWhenWithOnlyElse() throws Exception {
fooBoxTest();
}
public void testIfInWhen() throws Exception {
checkFooBoxIsOk();
}
}
@@ -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();
}
}
}
@@ -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<String>("mama"))() != "mama") return "test 8 failed";
if ( (fooT2<String>("mama"))("papa") != "mamapapa") return "test 9 failed";
return "OK"
@@ -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("<default-name>")
fun assertEquals(expected: Any, actual: Any) {
if (expected != actual) throw Exception("expected = $expected, actual = $actual")
}
fun box(): String {
assertEquals("<default-name>", Foo().name)
assertEquals("BarBaz", Foo("BarBaz").name)
return "OK"
}
@@ -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"
}
@@ -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"
}
@@ -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()
}
@@ -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"
}
@@ -0,0 +1,8 @@
package foo
class A(val ok: String) {
fun initialize() = ok
}
fun box(): String = A("OK").initialize()
@@ -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"
}
@@ -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 <T : Throwable> failsWith(block: () -> Any): T {
try {
block()
}
catch (e: T) {
return e
}
throw Exception("Should have failed")
}
fun box(): String {
val a = failsWith<Exception> {
throw Exception("OK")
}
return a.message
}