From 49e51220d790465c4a5b16688a97c8fb0a8ca1f3 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Tue, 31 Jul 2012 13:15:54 +0400 Subject: [PATCH] Correct equals for tuples. --- .../k2js/test/semantics/TupleTest.java | 4 +++ js/js.translator/testFiles/kotlin_lib.js | 12 ++++++- .../testFiles/tuple/cases/tuplesEquals.kt | 31 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 js/js.translator/testFiles/tuple/cases/tuplesEquals.kt diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/TupleTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/TupleTest.java index b1506a2cbd3..b938748fe17 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/TupleTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/TupleTest.java @@ -36,6 +36,10 @@ public final class TupleTest extends SingleFileTranslationTest { fooBoxTest(); } + public void testTuplesEquals() throws Exception { + fooBoxTest(); + } + } diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index 540267ac256..610b73b613a 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -23,7 +23,17 @@ var kotlin = {set:function (receiver, key, value) { "use strict"; Kotlin.equals = function (obj1, obj2) { - if ((obj1 === null)|| (obj1 === undefined)) return obj2 === null; + if ((obj1 === null) || (obj1 === undefined)) return obj2 === null; + if (obj1 instanceof Array) { + if (!(obj2 instanceof Array)) return false; + if (obj1.length != obj2.length) return false; + for (var i = 0; i < obj1.length; i++) { + if (!Kotlin.equals(obj1[i], obj2[i])) { + return false; + } + } + return true; + } if (typeof obj1 == "object") { if (obj1.equals !== undefined) { return obj1.equals(obj2); diff --git a/js/js.translator/testFiles/tuple/cases/tuplesEquals.kt b/js/js.translator/testFiles/tuple/cases/tuplesEquals.kt new file mode 100644 index 00000000000..8768ee0e43c --- /dev/null +++ b/js/js.translator/testFiles/tuple/cases/tuplesEquals.kt @@ -0,0 +1,31 @@ +package foo + +class A(val i: Int = 2) { + fun equals(other: Any?): Boolean { + if (other !is A) { + return false + } + return i % 2 == other.i % 2 + } +} + +fun box(): Boolean { + val c = #(3, 1) + if (c != #(3, 1)) { + return false + } + if (c == #(1, 3)) { + return false + } + val b = #(A(2), A(1)) + if (b != #(A(0), A(3))) { + return false + } + if (b == #(A(2), A(4))) { + return false + } + if (#("aaa") != #("aaa")) { + return false + } + return true +}