From 8074ab27ea9be08d47de8fadf149d9a720df2e5e Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Thu, 18 Dec 2014 15:52:13 +0300 Subject: [PATCH] JS backend: fix KT-6112 jsonFromTuples is broken #KT-6112 Fixed --- js/js.libraries/src/core/json.kt | 12 +++++---- js/js.libraries/test/core/JsonTest.kt | 14 ++++++++++ .../k2js/test/semantics/JsonTest.java | 26 +++++++++++++++++++ js/js.translator/testData/kotlin_lib.js | 10 ------- 4 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 js/js.libraries/test/core/JsonTest.kt create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/semantics/JsonTest.java diff --git a/js/js.libraries/src/core/json.kt b/js/js.libraries/src/core/json.kt index 4a71dfca098..79b9102e17c 100644 --- a/js/js.libraries/src/core/json.kt +++ b/js/js.libraries/src/core/json.kt @@ -5,11 +5,13 @@ native public class Json() { public fun set(propertyName: String, value: Any?): Unit = noImpl } -library("jsonFromTuples") -public fun json(vararg pairs: Pair): Json = noImpl - -library("jsonFromTuples") -public fun json2(pairs: Array>): Json = noImpl +public fun json(vararg pairs: Pair): Json { + var res: dynamic = js("({})") + for((name, value) in pairs) { + res[name] = value + } + return res +} library("jsonAddProperties") public fun Json.add(other: Json): Json = noImpl diff --git a/js/js.libraries/test/core/JsonTest.kt b/js/js.libraries/test/core/JsonTest.kt new file mode 100644 index 00000000000..2af4ddf8baf --- /dev/null +++ b/js/js.libraries/test/core/JsonTest.kt @@ -0,0 +1,14 @@ +package test.js + +import kotlin.test.* +import org.junit.Test as test + +class JsonTest { + + test fun createJsonFromPairs() { + var obj = json(Pair("firstName", "John"), Pair("lastName", "Doe"), Pair("age", 30)) + assertEquals("John", obj["firstName"], "firstName") + assertEquals("Doe", obj["lastName"], "lastName") + assertEquals(30, obj["age"], "age") + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsonTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsonTest.java new file mode 100644 index 00000000000..a3dfaf96459 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/JsonTest.java @@ -0,0 +1,26 @@ +/* + * 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; + +import junit.framework.Test; + +@SuppressWarnings("JUnitTestCaseWithNoTests") +public final class JsonTest extends JsUnitTestBase { + public static Test suite() throws Exception { + return createTestSuiteForFile("js/js.libraries/test/core/JsonTest.kt"); + } +} diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index 4fb5bcc2d51..0d73f23f374 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -955,16 +955,6 @@ return new Kotlin.ArrayIterator(array); }; - Kotlin.jsonFromTuples = function (pairArr) { - var i = pairArr.length; - var res = {}; - while (i > 0) { - --i; - res[pairArr[i][0]] = pairArr[i][1]; - } - return res; - }; - Kotlin.jsonAddProperties = function (obj1, obj2) { for (var p in obj2) { if (obj2.hasOwnProperty(p)) {