KT-2752: add basic tests for JsName

This commit is contained in:
Alexey Andreev
2016-05-30 14:26:24 +03:00
parent 5e3aa33b13
commit 5ce158f297
44 changed files with 300 additions and 157 deletions
@@ -0,0 +1,9 @@
package foo
open class Super {
<!JS_NAME_CLASH!>fun foo() = 23<!>
}
class Sub : Super() {
<!JS_NAME_CLASH!>@JsName("foo") fun bar() = 42<!>
}
@@ -0,0 +1,21 @@
package
package foo {
public final class Sub : foo.Super {
public constructor Sub()
@kotlin.js.JsName(name = "foo") public final fun bar(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final override /*1*/ /*fake_override*/ fun foo(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public open class Super {
public constructor Super()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun foo(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,5 @@
package foo
@JsName("bar") fun foo(x: Int) = x
private fun bar() = 42
@@ -0,0 +1,6 @@
package
package foo {
private fun bar(): kotlin.Int
@kotlin.js.JsName(name = "bar") public fun foo(/*0*/ x: kotlin.Int): kotlin.Int
}
@@ -0,0 +1,5 @@
package foo
<!JS_NAME_CLASH!>@JsName("x") fun foo(x: Int) = x<!>
<!JS_NAME_CLASH!>@JsName("x") fun bar() = 42<!>
@@ -0,0 +1,6 @@
package
package foo {
@kotlin.js.JsName(name = "x") public fun bar(): kotlin.Int
@kotlin.js.JsName(name = "x") public fun foo(/*0*/ x: kotlin.Int): kotlin.Int
}
@@ -0,0 +1,5 @@
package foo
<!JS_NAME_CLASH!>@JsName("bar") fun foo(x: Int) = x<!>
<!JS_NAME_CLASH!>fun bar() = 42<!>
@@ -0,0 +1,6 @@
package
package foo {
public fun bar(): kotlin.Int
@kotlin.js.JsName(name = "bar") public fun foo(/*0*/ x: kotlin.Int): kotlin.Int
}
@@ -0,0 +1,5 @@
package foo
<!JS_NAME_CLASH!>@JsName("bar") private fun foo(x: Int) = x<!>
<!JS_NAME_CLASH!>fun bar() = 42<!>
@@ -0,0 +1,6 @@
package
package foo {
public fun bar(): kotlin.Int
@kotlin.js.JsName(name = "bar") private fun foo(/*0*/ x: kotlin.Int): kotlin.Int
}
@@ -0,0 +1,7 @@
package foo
class A(val x: String) {
<!JS_NAME_CLASH!>@JsName("aa") constructor(x: Int) : this("int $x")<!>
}
<!JS_NAME_CLASH!>fun aa() {}<!>
@@ -0,0 +1,14 @@
package
package foo {
public fun aa(): kotlin.Unit
public final class A {
@kotlin.js.JsName(name = "aa") public constructor A(/*0*/ x: kotlin.Int)
public constructor A(/*0*/ x: kotlin.String)
public final val x: kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -319,6 +319,30 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
doTest(fileName);
}
@TestMetadata("jsNameAndOverridden.kt")
public void testJsNameAndOverridden() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameAndOverridden.kt");
doTest(fileName);
}
@TestMetadata("jsNameAndPrivateDontClash.kt")
public void testJsNameAndPrivateDontClash() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameAndPrivateDontClash.kt");
doTest(fileName);
}
@TestMetadata("jsNameClash.kt")
public void testJsNameClash() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameClash.kt");
doTest(fileName);
}
@TestMetadata("jsNameClashWithDefault.kt")
public void testJsNameClashWithDefault() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/jsNameClashWithDefault.kt");
doTest(fileName);
}
@TestMetadata("methodAndMethod.kt")
public void testMethodAndMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt");
@@ -355,6 +379,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
doTest(fileName);
}
@TestMetadata("privateJsNameClash.kt")
public void testPrivateJsNameClash() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/privateJsNameClash.kt");
doTest(fileName);
}
@TestMetadata("propertyAndMethodInSubclass.kt")
public void testPropertyAndMethodInSubclass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/propertyAndMethodInSubclass.kt");
@@ -367,6 +397,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
doTest(fileName);
}
@TestMetadata("topLevelMethodAndJsNameConstructor.kt")
public void testTopLevelMethodAndJsNameConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/topLevelMethodAndJsNameConstructor.kt");
doTest(fileName);
}
@TestMetadata("topLevelMethodAndProperty.kt")
public void testTopLevelMethodAndProperty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/topLevelMethodAndProperty.kt");
@@ -135,7 +135,7 @@ class FQNGenerator {
}
}
val explicitName = getJsName(descriptor)
val explicitName = getJsName(resolvedDescriptor)
return when {
explicitName != null -> Pair(explicitName, true)
needsStableMangling(resolvedDescriptor) ->
@@ -26,11 +26,12 @@ import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DeclarationChecker
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.SimpleDeclarationChecker
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.source.getPsi
class JsNameChecker : DeclarationChecker {
class JsNameChecker : SimpleDeclarationChecker {
private val fqnGenerator = FQNGenerator()
private val scopes = mutableMapOf<DeclarationDescriptor, MutableMap<String, DeclarationDescriptor>>()
private val clashedDescriptors = mutableSetOf<DeclarationDescriptor>()
@@ -4775,6 +4775,51 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
}
}
@TestMetadata("js/js.translator/testData/box/jsName")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class JsName extends AbstractBoxJsTest {
public void testAllFilesPresentInJsName() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/box/jsName"), Pattern.compile("^([^_](.+))\\.kt$"), true);
}
@TestMetadata("avoidNameClash.kt")
public void testAvoidNameClash() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsName/avoidNameClash.kt");
doTest(fileName);
}
@TestMetadata("overriddenMethod.kt")
public void testOverriddenMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsName/overriddenMethod.kt");
doTest(fileName);
}
@TestMetadata("overridenFromInterface.kt")
public void testOverridenFromInterface() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsName/overridenFromInterface.kt");
doTest(fileName);
}
@TestMetadata("privateMethod.kt")
public void testPrivateMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsName/privateMethod.kt");
doTest(fileName);
}
@TestMetadata("secondaryConstructor.kt")
public void testSecondaryConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsName/secondaryConstructor.kt");
doTest(fileName);
}
@TestMetadata("simpleJsName.kt")
public void testSimpleJsName() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/jsName/simpleJsName.kt");
doTest(fileName);
}
}
@TestMetadata("js/js.translator/testData/box/labels")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -59,4 +59,4 @@ abstract class AbstractLocalClassesTest : BorrowedTest("localClasses/")
abstract class AbstractNonLocalReturnsTest : BorrowedInlineTest("nonLocalReturns/")
abstract class AbstractTypeAliasesTests : BorrowedTest("typealias/")
abstract class AbstractTypeAliasesTests : BorrowedTest("typealias/")
@@ -27,7 +27,6 @@ import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.hash.LinkedHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.ReflectionTypes;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.js.config.JsConfig;
import org.jetbrains.kotlin.js.naming.FQNGenerator;
@@ -82,9 +81,6 @@ public final class StaticContext {
@NotNull
private final StandardClasses standardClasses;
@NotNull
private final ReflectionTypes reflectionTypes;
@NotNull
private final JsScope rootScope;
@@ -142,7 +138,6 @@ public final class StaticContext {
this.rootScope = rootScope;
this.standardClasses = standardClasses;
this.config = config;
this.reflectionTypes = new ReflectionTypes(moduleDescriptor);
currentModule = moduleDescriptor;
}
@@ -171,11 +166,6 @@ public final class StaticContext {
return namer;
}
@NotNull
public ReflectionTypes getReflectionTypes() {
return reflectionTypes;
}
@NotNull
private JsScope getRootScope() {
return rootScope;
+1 -1
View File
@@ -8,7 +8,7 @@ package foo
import test.*
// CHECK_CONTAINS_NO_CALLS: testClassObject
// CHECK_CONTAINS_NO_CALLS: testClassObject_0
internal fun testFinalInline(): String {
return Z().finalInline({"final"})
@@ -17,7 +17,7 @@ public fun log(s: String): String {
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(s: String): String = log(s + ";")
@@ -12,7 +12,7 @@ public fun <T, R> apply(x: T, fn: (T)->R): R =
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun multiplyBy2(x: Int): Int = x * 2
@@ -9,7 +9,7 @@ public fun sum(x: Int, y: Int): Int =
// MODULE: main(lib)
// FILE: main.kt
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = utils.sum(x, y)
@@ -13,7 +13,7 @@ public fun <T, R> apply(x: T, fn: T.()->R): R =
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal class A(val n: Int)
+1 -1
View File
@@ -13,7 +13,7 @@ public fun <T, R> apply(x: T, fn: (T)->R): R =
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int): Int = apply(x) { it * 2 }
@@ -13,7 +13,7 @@ public fun <T, R> apply(x: T, fn: (T)->R): R =
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = apply(x) { it + y }
@@ -15,7 +15,7 @@ public fun <T, R> apply(x: T, fn: (T)->R): R {
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = apply(x) { it + 1 } * y
+1 -1
View File
@@ -14,7 +14,7 @@ public class A(public val x: Int) {
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(a: A, y: Int): Int = a.plus(y)
+1 -1
View File
@@ -13,7 +13,7 @@ public fun sum(x: Int, y: Int): Int =
import utils.*
// CHECK_CONTAINS_NO_CALLS: test
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = sum(x, y)
+18
View File
@@ -0,0 +1,18 @@
package foo
object A {
@JsName("js_method") fun f() = "method"
@JsName("js_property") val f: String get() = "property"
}
fun test() = js("""
var a = Kotlin.modules.JS_TESTS.foo.A;
return a.js_method() + ";" + a.js_property;
""")
fun box(): String {
val result = test()
assertEquals("method;property", result);
return "OK"
}
@@ -0,0 +1,20 @@
package foo
open class A {
@JsName("js_f") open fun f(x: Int) = "A.f($x)"
}
class B : A() {
override fun f(x: Int) = "B.f($x)"
}
fun test() = js("""
var module = Kotlin.modules.JS_TESTS.foo;
return new (module.A)().js_f(23) + ";" + new (module.B)().js_f(42);
""")
fun box(): String {
val result = test()
assertEquals("A.f(23);B.f(42)", result);
return "OK"
}
@@ -0,0 +1,20 @@
package foo
interface A {
@JsName("js_f") fun f(x: Int): String
}
class B : A {
override fun f(x: Int) = "B.f($x)"
}
fun test() = js("""
var module = Kotlin.modules.JS_TESTS.foo;
return new (module.B)().js_f(23);
""")
fun box(): String {
val result = test()
assertEquals("B.f(23)", result);
return "OK"
}
+15
View File
@@ -0,0 +1,15 @@
package foo
object A {
@JsName("js_f") private fun f(x: Int) = "f($x)"
}
fun test() = js("""
return Kotlin.modules.JS_TESTS.foo.A.js_f(23);
""")
fun box(): String {
val result = test()
assertEquals("f(23)", result);
return "OK"
}
@@ -0,0 +1,15 @@
package foo
class A(val x: String) {
@JsName("A_int") constructor(x: Int) : this("int $x")
}
fun test() = js("""
return Kotlin.modules.JS_TESTS.foo.A_int(23).x;
""")
fun box(): String {
val result = test()
assertEquals("int 23", result);
return "OK"
}
+22
View File
@@ -0,0 +1,22 @@
package foo
object A {
@JsName("js_f") fun f(x: Int) = "f($x)"
@JsName("js_g") fun g(x: Int) = "g($x)"
@JsName("js_p") val p = "p"
@JsName("js_q") val q: String get() = "q"
}
fun test() = js("""
var a = Kotlin.modules.JS_TESTS.foo.A;
return a.js_f(23) + ";" + a.js_g(42) + ";" + a.js_p + ";" + a.js_q;
""")
fun box(): String {
val result = test()
assertEquals("f(23);g(42);p;q", result);
return "OK"
}
@@ -1,30 +0,0 @@
/*
* Copy of JVM-backend test
* Found at: compiler/testData/codegen/boxInline/trait/trait.1.kt
*/
package foo
import test.*
// CHECK_CONTAINS_NO_CALLS: testClassObject_0
internal fun testFinalInline(): String {
return Z().finalInline({"final"})
}
internal fun testFinalInline2(instance: InlineTrait): String {
return instance.finalInline({"final2"})
}
internal fun testClassObject(): String {
return InlineTrait.finalInline({"classobject"})
}
fun box(): String {
if (testFinalInline() != "final") return "test1: ${testFinalInline()}"
if (testFinalInline2(Z()) != "final2") return "test2: ${testFinalInline2(Z())}"
if (testClassObject() != "classobject") return "test3: ${testClassObject()}"
return "OK"
}
@@ -1,12 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(s: String): String = log(s + ";")
fun box(): String {
assertEquals("a;", test("a"))
assertEquals("a;b;", test("b"))
return "OK"
}
@@ -1,13 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun multiplyBy2(x: Int): Int = x * 2
internal fun test(x: Int): Int = apply(x, ::multiplyBy2)
fun box(): String {
assertEquals(6, test(3))
return "OK"
}
@@ -1,10 +0,0 @@
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = utils.sum(x, y)
fun box(): String {
assertEquals(3, test(1, 2))
assertEquals(5, test(2, 3))
return "OK"
}
@@ -1,13 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal class A(val n: Int)
internal fun test(a: A, m: Int): Int = apply(a) { n * m }
fun box(): String {
assertEquals(6, test(A(2), 3))
return "OK"
}
@@ -1,11 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int): Int = apply(x) { it * 2 }
fun box(): String {
assertEquals(6, test(3))
return "OK"
}
@@ -1,11 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = apply(x) { it + y }
fun box(): String {
assertEquals(3, test(1, 2))
return "OK"
}
@@ -1,11 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = apply(x) { it + 1 } * y
fun box(): String {
assertEquals(6, test(1, 3))
return "OK"
}
@@ -1,11 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(a: A, y: Int): Int = a.plus(y)
fun box(): String {
assertEquals(5, test(A(2), 3))
return "OK"
}
@@ -1,12 +0,0 @@
import utils.*
// CHECK_CONTAINS_NO_CALLS: test_0
internal fun test(x: Int, y: Int): Int = sum(x, y)
fun box(): String {
assertEquals(3, test(1, 2))
assertEquals(5, test(2, 3))
return "OK"
}