From 2fd8119a105159393c2e6dc62ee12eed9883a028 Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Mon, 29 Nov 2021 20:11:24 +0300 Subject: [PATCH] [JS IR] Add a test for converting arrays to String --- .../kotlin/js/test/BoxJsTestGenerated.java | 6 ++ .../js/test/ir/IrBoxJsTestGenerated.java | 6 ++ .../testData/box/builtins/arrayToString.kt | 81 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 js/js.translator/testData/box/builtins/arrayToString.kt diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/BoxJsTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/BoxJsTestGenerated.java index ef1e503d1f4..254dbd3f635 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/BoxJsTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/BoxJsTestGenerated.java @@ -50,6 +50,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/builtins"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS, true); } + @Test + @TestMetadata("arrayToString.kt") + public void testArrayToString() throws Exception { + runTest("js/js.translator/testData/box/builtins/arrayToString.kt"); + } + @Test @TestMetadata("hashCode.kt") public void testHashCode() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java index 61adc5fc301..b022e8adb55 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrBoxJsTestGenerated.java @@ -50,6 +50,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("js/js.translator/testData/box/builtins"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.JS_IR, true); } + @Test + @TestMetadata("arrayToString.kt") + public void testArrayToString() throws Exception { + runTest("js/js.translator/testData/box/builtins/arrayToString.kt"); + } + @Test @TestMetadata("hashCode.kt") public void testHashCode() throws Exception { diff --git a/js/js.translator/testData/box/builtins/arrayToString.kt b/js/js.translator/testData/box/builtins/arrayToString.kt new file mode 100644 index 00000000000..9fec3815a69 --- /dev/null +++ b/js/js.translator/testData/box/builtins/arrayToString.kt @@ -0,0 +1,81 @@ +// EXPECTED_REACHABLE_NODES: 1277 + +// This test's purpose is not preventing regressions, but rather making sure the array-to-string conversion is tested at all. +// The `expected` values in asserts just reflect the current state of things. It doesn't mean things _must_ be this way. +// So feel free to update them if you're sure that the array-to-string conversion should behave differently. +// See also: KT-14013 + +var LOG = "" + +fun log(message: Any?) { + LOG += message + LOG += ";" +} + +fun pullLog(): String { + val string = LOG + LOG = "" + return string +} + +fun testKt14013() { + val a: Any? = arrayOf(null, 1) + log(a) + log(a.toString()) + log(a!!.toString()) + + if (testUtils.isLegacyBackend()) { + assertEquals(",1;[...];,1;", pullLog(), "testKt14013") + } else { + assertEquals(",1;[...];[...];", pullLog(), "testKt14013") + } +} + +fun concreteArrayToString(a: Array) { + log(a.toString()) + log("$a") + log("" + a) +} + +fun genericValueToString(a: T) { + log(a.toString()) + log("$a") + log("" + a) +} + +fun anyValueToString(a: Any) { + log(a.toString()) + log("$a") + log("" + a) +} + +fun box(): String { + testKt14013() + + val a = arrayOf(1, 2, 3) + concreteArrayToString(a) + + if (testUtils.isLegacyBackend()) { + assertEquals("1,2,3;1,2,3;[...];", pullLog(), "concreteArrayToString") + } else { + assertEquals("[...];1,2,3;1,2,3;", pullLog(), "concreteArrayToString") + } + + genericValueToString(a) + + if (testUtils.isLegacyBackend()) { + assertEquals("[...];1,2,3;[...];", pullLog(), "genericValueToString") + } else { + assertEquals("[...];1,2,3;1,2,3;", pullLog(), "genericValueToString") + } + + anyValueToString(a) + + if (testUtils.isLegacyBackend()) { + assertEquals("1,2,3;1,2,3;[...];", pullLog(), "anyValueToString") + } else { + assertEquals("[...];1,2,3;1,2,3;", pullLog(), "anyValueToString") + } + + return "OK" +}