diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/CompareToTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/CompareToTest.java new file mode 100644 index 00000000000..c5bd194fa8e --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/CompareToTest.java @@ -0,0 +1,28 @@ +/* + * 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; + +public final class CompareToTest extends AbstractExpressionTest { + + public CompareToTest() { + super("compareTo/"); + } + + public void testCustomCompareToMethod() throws Exception { + checkFooBoxIsOk(); + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java index 3b707b628b8..b52a1fc3948 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/NumberTest.java @@ -70,6 +70,10 @@ public final class NumberTest extends SingleFileTranslationTest { checkFooBoxIsOk(); } + public void testNumberIsCheck() throws Exception { + checkFooBoxIsOk(); + } + public void testConversionsWithoutTruncation() throws Exception { checkFooBoxIsOk(); } diff --git a/js/js.translator/testData/expression/compareTo/cases/customCompareToMethod.kt b/js/js.translator/testData/expression/compareTo/cases/customCompareToMethod.kt new file mode 100644 index 00000000000..3184d92f26a --- /dev/null +++ b/js/js.translator/testData/expression/compareTo/cases/customCompareToMethod.kt @@ -0,0 +1,40 @@ +package foo + +class A(val value: Int) : Comparable { + override public fun compareTo(other: A): Int = other.value compareTo value +} + +class B(val value: Int) + +fun testExtensionFunctionAsCompareTo() { + val compareTo: B.( B ) -> Int = { (other) -> other.value compareTo this.value } + + val x: B = B(100) + val y: B = B(200) + + assertEquals(false, x < y, "ext fun: x < y") + assertEquals(true, x > y, "ext fun: x > y") + assertEquals(1, x compareTo y, "ext fun: x compareTo y") +} + +fun testMethodAsCompareTo() { + val x: A = A(100) + val y: A = A(200) + + assertEquals(false, x < y, "meth: x < y") + assertEquals(true, x > y, "meth: x > y") + assertEquals(1, x compareTo y, "meth: x compareTo y") + + assertEquals(false, (x: Comparable) < y, "meth: (x: Comparable) < y") + assertEquals(true, (x: Comparable) > y, "meth: (x: Comparable) > y") + assertEquals(1, (x: Comparable) compareTo y, "meth: (x: Comparable) compareTo y") +} + +fun box(): String { + + testExtensionFunctionAsCompareTo() + + testMethodAsCompareTo() + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/number/cases/numberCompareTo.kt b/js/js.translator/testData/number/cases/numberCompareTo.kt index 4b64ce4b1fa..762978e3520 100644 --- a/js/js.translator/testData/number/cases/numberCompareTo.kt +++ b/js/js.translator/testData/number/cases/numberCompareTo.kt @@ -10,6 +10,7 @@ fun id(s: String, value: Int): Int { fun box(): String { assertEquals(-1, 1.compareTo(2)) + assertEquals(-1, (1: Comparable).compareTo(2)) assertEquals(-1, 1 compareTo 2) assertEquals(-1, 1 compareTo (2:Short)) @@ -31,6 +32,7 @@ fun box(): String { assertEquals(-1, (1: Short) compareTo 2) assertEquals(-1, (1: Short) compareTo (2:Short)) + assertEquals(-1, ((1: Short): Comparable) compareTo (2:Short)) assertEquals(-1, (1: Short) compareTo (2:Byte)) assertEquals(-1, (1: Short) compareTo 2.0) assertEquals(-1, (1: Short) compareTo 2.0f) @@ -38,6 +40,7 @@ fun box(): String { assertEquals(1, (10: Byte) compareTo 2) assertEquals(1, (10: Byte) compareTo (2:Short)) assertEquals(1, (10: Byte) compareTo (2:Byte)) + assertEquals(1, ((10: Byte): Comparable) compareTo (2:Byte)) assertEquals(1, (10: Byte) compareTo 2.0) assertEquals(1, (10: Byte) compareTo 2.0f) @@ -45,6 +48,7 @@ fun box(): String { assertEquals(0, 2.0 compareTo (2:Short)) assertEquals(0, 2.0 compareTo (2:Byte)) assertEquals(0, 2.0 compareTo 2.0) + assertEquals(0, (2.0: Comparable) compareTo 2.0) assertEquals(0, 2.0 compareTo 2.0f) assertEquals(1, 3.0f compareTo 2) @@ -52,6 +56,7 @@ fun box(): String { assertEquals(1, 3.0f compareTo (2:Byte)) assertEquals(1, 3.0f compareTo 2.0) assertEquals(1, 3.0f compareTo 2.0f) + assertEquals(1, (3.0f: Comparable) compareTo 2.0f) assertEquals(1, id("A", 10) compareTo id("B", 5)) assertEquals("AB", global) diff --git a/js/js.translator/testData/number/cases/numberIsCheck.kt b/js/js.translator/testData/number/cases/numberIsCheck.kt new file mode 100644 index 00000000000..963f2eb2438 --- /dev/null +++ b/js/js.translator/testData/number/cases/numberIsCheck.kt @@ -0,0 +1,49 @@ +package foo + +// For now, check is Byte(is Short, is Int is Float, is Double) translates to typeof ... == "number" + +fun testNum(numX: Number) { + assertEquals(true, numX is Number, "numX is Number") + assertEquals(true, numX is Int, "numX is Int") + + assertEquals(true, numX is Short, "numX is Short") + assertEquals(true, numX is Byte, "numX is Byte") + + assertEquals(true, numX is Double, "numX is Double") + assertEquals(true, numX is Float, "numX is Float") +} + +fun testAny(anyX: Any) { + assertEquals(true, anyX is Number, "anyX is Number") + assertEquals(true, anyX is Int, "anyX is Int") + + assertEquals(true, anyX is Short, "anyX is Short") + assertEquals(true, anyX is Byte, "anyX is Byte") + + assertEquals(true, anyX is Double, "anyX is Double") + assertEquals(true, anyX is Float, "anyX is Float") +} + +fun box(): String { + val intX = 100 + + assertEquals(true, intX is Number, "intX is Number") + assertEquals(true, intX is Int, "intX is Int") + + var anyX: Any = "A" + assertEquals(false, anyX is Number, "anyX is Number") + + testNum(100) + testNum(100: Short) + testNum(100: Byte) + testNum(100.0) + testNum(100.0f) + + testAny(100) + testAny(100: Short) + testAny(100: Byte) + testAny(100.0) + testAny(100.0f) + + return "OK" +} \ No newline at end of file