JS: added reified is-check test cases

This commit is contained in:
Alexey Tsvetkov
2015-03-17 17:25:49 +03:00
parent 0a3ed7f12e
commit 8cdff2439b
18 changed files with 444 additions and 0 deletions
@@ -846,6 +846,10 @@ fun main(args: Array<String>) {
testClass(javaClass<AbstractInlineSizeReductionTest>()) {
model("inlineSizeReduction/cases")
}
testClass(javaClass<AbstractReifiedTest>()) {
model("reified/cases")
}
}
testGroup("js/js.tests/test", "compiler/testData") {
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2015 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.kotlin.js.test.semantics
import org.jetbrains.kotlin.js.test.SingleFileTranslationTest
public abstract class AbstractReifiedTest : SingleFileTranslationTest("reified/")
@@ -0,0 +1,121 @@
/*
* Copyright 2010-2015 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.kotlin.js.test.semantics;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("js/js.translator/testData/reified/cases")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class ReifiedTestGenerated extends AbstractReifiedTest {
public void testAllFilesPresentInCases() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/reified/cases"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("callChain.kt")
public void testCallChain() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/callChain.kt");
doTest(fileName);
}
@TestMetadata("extensionMethod.kt")
public void testExtensionMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/extensionMethod.kt");
doTest(fileName);
}
@TestMetadata("innerObject.kt")
public void testInnerObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/innerObject.kt");
doTest(fileName);
}
@TestMetadata("isBool.kt")
public void testIsBool() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/isBool.kt");
doTest(fileName);
}
@TestMetadata("isChar.kt")
public void testIsChar() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/isChar.kt");
doTest(fileName);
}
@TestMetadata("isClass.kt")
public void testIsClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/isClass.kt");
doTest(fileName);
}
@TestMetadata("isNumber.kt")
public void testIsNumber() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/isNumber.kt");
doTest(fileName);
}
@TestMetadata("isString.kt")
public void testIsString() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/isString.kt");
doTest(fileName);
}
@TestMetadata("lambdaNameClash.kt")
public void testLambdaNameClash() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/lambdaNameClash.kt");
doTest(fileName);
}
@TestMetadata("method.kt")
public void testMethod() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/method.kt");
doTest(fileName);
}
@TestMetadata("multipleTypeParameters.kt")
public void testMultipleTypeParameters() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/multipleTypeParameters.kt");
doTest(fileName);
}
@TestMetadata("noValueParameters.kt")
public void testNoValueParameters() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/noValueParameters.kt");
doTest(fileName);
}
@TestMetadata("parameterSwap.kt")
public void testParameterSwap() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/parameterSwap.kt");
doTest(fileName);
}
@TestMetadata("vararg.kt")
public void testVararg() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/reified/cases/vararg.kt");
doTest(fileName);
}
}
@@ -0,0 +1,4 @@
package foo
inline fun isInstance<reified T>(x: Any): Boolean =
x is T
@@ -0,0 +1,18 @@
package foo
// CHECK_NOT_CALLED: test
// CHECK_NOT_CALLED: test1
class A
class B
inline fun test<reified T>(x: Any): Boolean = test1<T>(x)
inline fun test1<reified R>(x: Any): Boolean = x is R
fun box(): String {
assertEquals(true, test<A>(A()), "test<A>(A())")
assertEquals(false, test<A>(B()), "test<A>(B())")
return "OK"
}
@@ -0,0 +1,29 @@
package foo
// CHECK_NOT_CALLED: test
// CHECK_NOT_CALLED: fn
class A(val x: Any? = null) {
inline
fun test<reified T, reified R>(b: B) = b.fn<T, R>()
inline
fun B.fn<reified T, reified R>() = x is T && y is R
}
class B(val y: Any? = null)
class X
class Y
fun box(): String {
val x = X()
val y = Y()
assertEquals(true, A(x).test<X, Y>(B(y)), "A(x).test<X, Y>(B(y))")
assertEquals(false, A(y).test<X, Y>(B(y)), "A(y).test<X, Y>(B(y))")
assertEquals(false, A(y).test<X, Y>(B(x)), "A(y).test<X, Y>(B(x))")
assertEquals(false, A(x).test<X, Y>(B(x)), "A(x).test<X, Y>(B(x))")
return "OK"
}
@@ -0,0 +1,31 @@
package foo
// CHECK_NOT_CALLED: typePredicate
open class A
class B
class C : A()
trait TypePredicate {
fun invoke(x: Any): Boolean
}
inline fun typePredicate<reified T>(): TypePredicate =
object : TypePredicate {
override fun invoke(x: Any): Boolean = x is T
}
fun box(): String {
val isA = typePredicate<A>()
val a: Any = A()
val b: Any = B()
val c: Any = C()
assertEquals(true, isA(a), "isA(a)")
assertEquals(false, isA(b), "isA(b)")
assertEquals(true, isA(c), "isA(c)")
return "OK"
}
@@ -0,0 +1,11 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Boolean>(true))
assertEquals(true, isInstance<Boolean>(false))
assertEquals(false, isInstance<Boolean>("true"))
return "OK"
}
@@ -0,0 +1,11 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Char>('c'))
assertEquals(false, isInstance<Char>(""))
assertEquals(false, isInstance<Char>("cc"))
return "OK"
}
@@ -0,0 +1,21 @@
package foo
// CHECK_NOT_CALLED: isInstance
trait A
trait B
open class C : A, B
class D : C()
fun box(): String {
assertEquals(false, isInstance<A>(0))
assertEquals(false, isInstance<A>(""))
assertEquals(true, isInstance<A>(C()))
assertEquals(true, isInstance<A>(D()))
assertEquals(true, isInstance<D>(D()))
assertEquals(true, isInstance<C>(D()))
return "OK"
}
@@ -0,0 +1,19 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<Short>(0.toShort()))
assertEquals(true, isInstance<Byte>(0.toByte()))
assertEquals(true, isInstance<Int>(0))
assertEquals(true, isInstance<Long>(0.toLong()))
assertEquals(true, isInstance<Double>(0.toDouble()))
assertEquals(true, isInstance<Float>(0.toFloat()))
assertEquals(true, isInstance<Number>(0))
assertEquals(true, isInstance<Number>(0.toLong()))
assertEquals(true, isInstance<Number>(0.0))
assertEquals(false, isInstance<Number>("0"))
return "OK"
}
@@ -0,0 +1,10 @@
package foo
// CHECK_NOT_CALLED: isInstance
fun box(): String {
assertEquals(true, isInstance<String>(""))
assertEquals(true, isInstance<String>("a"))
return "OK"
}
@@ -0,0 +1,33 @@
package foo
// CHECK_CALLED: doRun
// CHECK_NOT_CALLED: test
class X
class Y
noinline
fun doRun<R>(fn: ()->R): R = fn()
inline fun test<reified A, reified B>(x: Any, y: Any): Boolean =
doRun {
val isA = null
x is A
}
&& doRun {
val result = y is B
val isB = null
result
}
fun box(): String {
val x = X()
val y = Y()
assertEquals(true, test<X, Y>(x, y), "test<X, Y>(x, y)")
assertEquals(false, test<X, Y>(x, x), "test<X, Y>(x, x)")
assertEquals(false, test<X, Y>(y, x), "test<X, Y>(y, x)")
assertEquals(false, test<X, Y>(y, y), "test<X, Y>(y, y)")
return "OK"
}
@@ -0,0 +1,17 @@
package foo
// CHECK_NOT_CALLED: test
class A(val x: Any? = null) {
inline
fun test<reified T>() = x is T
}
class B
fun box(): String {
assertEquals(true, A(A()).test<A>(), "A(A()).test<A>()")
assertEquals(false, A(B()).test<A>(), "A(B()).test<A>()")
return "OK"
}
@@ -0,0 +1,20 @@
package foo
class X
class Y
class Z
inline fun test<reified A, B, reified C>(x: Any): String =
when (x) {
is A -> "A"
is C -> "C"
else -> "Unknown"
}
fun box(): String {
assertEquals("A", test<X, Y, Z>(X()))
assertEquals("Unknown", test<X, Y, Z>(Y()))
assertEquals("C", test<X, Y, Z>(Z()))
return "OK"
}
@@ -0,0 +1,18 @@
package foo
// CHECK_NOT_CALLED: test
class A
inline fun test<reified T>(): String {
val a: Any = A()
return if (a is T) "A" else "Unknown"
}
fun box(): String {
assertEquals("A", test<A>())
assertEquals("Unknown", test<String>())
return "OK"
}
@@ -0,0 +1,22 @@
package foo
class A
class B
class C
inline fun test<reified T, reified R>(x: Any): String = test1<R, T>(x)
inline fun test1<reified R, reified T>(x: Any): String =
when (x) {
is R -> "R"
is T -> "T"
else -> "Unknown"
}
fun box(): String {
assertEquals("T", test<A, B>(A()), "test<T, R>(T())")
assertEquals("R", test<A, B>(B()), "test<T, R>(R())")
assertEquals("Unknown", test<A, B>(C()), "test<T, R>(L())")
return "OK"
}
@@ -0,0 +1,34 @@
package foo
// CHECK_NOT_CALLED: test
class A(val x: Int)
class B(val x: Int)
inline
fun test<reified T>(vararg xs: Any): List<T> {
val ts = arrayListOf<T>()
for (x in xs) {
if (x is T) {
ts.add(x)
}
}
return ts
}
fun box(): String {
val a1 = A(1)
val b2 = B(2)
val a3 = A(3)
val b4 = B(4)
assertEquals(listOf(a1), test<A>(a1, b2), "test(a1, b2)")
assertEquals(listOf(b2, b4), test<B>(a1, b2, a3, b4), "test<B>(a1, b2, a3, b4)")
val objects = arrayOf(a1, b2)
assertEquals(listOf(b2, b4), test<B>(a1, a3, *objects, a3, b4), "test<B>(a1, a3, *objects, a3, b4)")
return "OK"
}