JS backend: tests for KT-5772
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,10 @@ public final class NumberTest extends SingleFileTranslationTest {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testNumberIsCheck() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testConversionsWithoutTruncation() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package foo
|
||||
|
||||
class A(val value: Int) : Comparable<A> {
|
||||
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<A>) < y, "meth: (x: Comparable<A>) < y")
|
||||
assertEquals(true, (x: Comparable<A>) > y, "meth: (x: Comparable<A>) > y")
|
||||
assertEquals(1, (x: Comparable<A>) compareTo y, "meth: (x: Comparable<A>) compareTo y")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
|
||||
testExtensionFunctionAsCompareTo()
|
||||
|
||||
testMethodAsCompareTo()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -10,6 +10,7 @@ fun id(s: String, value: Int): Int {
|
||||
fun box(): String {
|
||||
|
||||
assertEquals(-1, 1.compareTo(2))
|
||||
assertEquals(-1, (1: Comparable<Int>).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<Short>) 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<Byte>) 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<Double>) 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<Float>) compareTo 2.0f)
|
||||
|
||||
assertEquals(1, id("A", 10) compareTo id("B", 5))
|
||||
assertEquals("AB", global)
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
Reference in New Issue
Block a user