From 4fd1b46e91c067c7118dc2dd321ad885e1126f88 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 21 May 2015 20:51:26 +0300 Subject: [PATCH] Refactor JS tests: unified package name, test JS specific map implementations only in JS. Add required files to build StdLibTestToJSTest. Remove tests StdLibMapJsTest and StdLibSetJsTest because they are tested elsewhere. --- .../js/test/semantics/StdLibMapJsTest.java | 27 ---- .../js/test/semantics/StdLibSetJsTest.java | 27 ---- .../js/test/semantics/StdLibTestToJSTest.java | 3 + .../test/collections/CollectionBehaviors.kt | 115 +++++++++++++++ .../stdlib/test/collections/CollectionTest.kt | 133 +----------------- .../stdlib/test/collections/ComparisonDSL.kt | 28 ++++ libraries/stdlib/test/js/JsArrayScript.kt | 13 +- libraries/stdlib/test/js/JsArrayTest.kt | 10 +- libraries/stdlib/test/js/MapJsTest.kt | 19 ++- libraries/stdlib/test/js/SetJsTest.kt | 22 ++- .../stdlib/test/js/javautilCollectionsTest.kt | 2 +- 11 files changed, 195 insertions(+), 204 deletions(-) delete mode 100644 js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibMapJsTest.java delete mode 100644 js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibSetJsTest.java create mode 100644 libraries/stdlib/test/collections/CollectionBehaviors.kt create mode 100644 libraries/stdlib/test/collections/ComparisonDSL.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibMapJsTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibMapJsTest.java deleted file mode 100644 index 93d3c10bfe0..00000000000 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibMapJsTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.js.test.semantics; - -import junit.framework.Test; - -//NOTE: well, it has tests -@SuppressWarnings("JUnitTestCaseWithNoTests") -public final class StdLibMapJsTest extends JsUnitTestBase { - public static Test suite() throws Exception { - return createTestSuiteForFile("libraries/stdlib/test/js/MapJsTest.kt"); - } -} diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibSetJsTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibSetJsTest.java deleted file mode 100644 index 68015cb5602..00000000000 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibSetJsTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.js.test.semantics; - -import junit.framework.Test; - -@SuppressWarnings("JUnitTestCaseWithNoTests") -public final class StdLibSetJsTest extends JsUnitTestBase { - public static Test suite() throws Exception { - return createTestSuiteForFile("libraries/stdlib/test/js/SetJsTest.kt"); - } -} - diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibTestToJSTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibTestToJSTest.java index 5dd2568e703..692fb9d4eae 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibTestToJSTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/StdLibTestToJSTest.java @@ -30,6 +30,9 @@ public class StdLibTestToJSTest extends StdLibQUnitTestSupport { "GetOrElseTest.kt", "collections/ListSpecificTest.kt", "collections/IteratorsTest.kt", + "collections/CollectionBehaviors.kt", + "collections/ComparisonDSL.kt", + "../../../js/js.libraries/test/core/assertTypeEquals.kt", "text/StringTest.kt", // TODO review: somethings FAILED if run: "js/JsDomTest.kt", diff --git a/libraries/stdlib/test/collections/CollectionBehaviors.kt b/libraries/stdlib/test/collections/CollectionBehaviors.kt new file mode 100644 index 00000000000..7f83aaf8b4a --- /dev/null +++ b/libraries/stdlib/test/collections/CollectionBehaviors.kt @@ -0,0 +1,115 @@ +/* + * Copyright 2010-2015 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 test.collections.behaviors + +import test.collections.CompareContext + +public fun CompareContext>.listBehavior() { + equalityBehavior() + collectionBehavior() + compareProperty( { listIterator() }, { listIteratorBehavior() }) + compareProperty( { listIterator(0) }, { listIteratorBehavior() }) + + propertyFails { listIterator(-1) } + propertyFails { listIterator(size() + 1) } + + for (index in expected.indices) + propertyEquals { this[index] } + + propertyFails { this[size()] } + + propertyEquals { indexOf(elementAtOrNull(0)) } + propertyEquals { lastIndexOf(elementAtOrNull(0)) } + + propertyFails { subList(0, size() + 1)} + propertyFails { subList(-1, 0)} + propertyEquals { subList(0, size()) } +} + +public fun CompareContext>.listIteratorBehavior() { + listIteratorProperties() + + while (expected.hasNext()) { + propertyEquals { next() } + listIteratorProperties() + } + propertyFails { next() } + + while (expected.hasPrevious()) { + propertyEquals { previous() } + listIteratorProperties() + } + propertyFails { previous() } +} + +public fun CompareContext>.listIteratorProperties() { + propertyEquals { hasNext() } + propertyEquals { hasPrevious() } + propertyEquals { nextIndex() } + propertyEquals { previousIndex() } +} + +public fun CompareContext>.iteratorBehavior() { + propertyEquals { hasNext() } + + while (expected.hasNext()) { + propertyEquals { next() } + propertyEquals { hasNext() } + } + propertyFails { next() } +} + +public fun CompareContext>.setBehavior(objectName: String = "") { + equalityBehavior(objectName) + collectionBehavior(objectName) + compareProperty({ iterator() }, { iteratorBehavior() }) +} + +public fun CompareContext>.mapBehavior() { + equalityBehavior() + propertyEquals { size() } + propertyEquals { isEmpty() } + + (object {}).let { propertyEquals { containsKey(it)} } + + if (expected.isEmpty().not()) + propertyEquals { contains(keySet().first()) } + + compareProperty( { keySet() }, { setBehavior("keySet") } ) + compareProperty( { entrySet() }, { setBehavior("entrySet") } ) + compareProperty( { values() }, { collectionBehavior("values") }) +} + +public fun CompareContext.equalityBehavior(objectName: String = "") { + val prefix = objectName + if (objectName.isNotEmpty()) "." else "" + equals(objectName) + propertyEquals(prefix + "hashCode") { hashCode() } + propertyEquals(prefix + "toString") { toString() } +} + + +public fun CompareContext>.collectionBehavior(objectName: String = "") { + val prefix = objectName + if (objectName.isNotEmpty()) "." else "" + propertyEquals (prefix + "size") { size() } + propertyEquals (prefix + "isEmpty") { isEmpty() } + + (object {}).let { propertyEquals { contains(it)} } + propertyEquals { contains(firstOrNull()) } + propertyEquals { containsAll(this) } +} + + diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 77e40a42ac6..e81962a043c 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -3,6 +3,7 @@ package test.collections import java.util.* import kotlin.test.* import org.junit.Test as test +import test.collections.behaviors.* class CollectionTest { @@ -534,143 +535,11 @@ class CollectionTest { compare(listOf("value").toMutableSet(), setOf("value")) { setBehavior() } } - test fun specialJsSets() { - val specialJsStringSet = HashSet() - specialJsStringSet.add("kotlin") - compare(hashSetOf("kotlin"), specialJsStringSet) { setBehavior() } - - val specialJsNumberSet = HashSet() - specialJsNumberSet.add(3.14) - compare(hashSetOf(3.14), specialJsNumberSet) { setBehavior() } - } - test fun specialMaps() { compare(hashMapOf(), mapOf()) { mapBehavior() } compare(linkedMapOf(), emptyMap()) { mapBehavior() } compare(linkedMapOf(2 to 3), mapOf(2 to 3)) { mapBehavior() } } - test fun specialJsMaps() { - val specialJsStringMap = HashMap() - specialJsStringMap.put("k1", "v1") - compare(hashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior() } - - val specialJsNumberMap = HashMap(4) - specialJsNumberMap.put(5, "v5") - compare(hashMapOf(5 to "v5"), specialJsNumberMap) { mapBehavior() } - } - - private fun CompareContext>.listBehavior() { - equalityBehavior() - collectionBehavior() - compareProperty( { listIterator() }, { listIteratorBehavior() }) - compareProperty( { listIterator(0) }, { listIteratorBehavior() }) - - propertyFails { listIterator(-1) } - propertyFails { listIterator(size() + 1) } - - for (index in expected.indices) - propertyEquals { this[index] } - - propertyFails { this[size()] } - - propertyEquals { indexOf(elementAtOrNull(0)) } - propertyEquals { lastIndexOf(elementAtOrNull(0)) } - - propertyFails { subList(0, size() + 1)} - propertyFails { subList(-1, 0)} - propertyEquals { subList(0, size()) } - } - - private fun CompareContext>.listIteratorBehavior() { - listIteratorProperties() - - while (expected.hasNext()) { - propertyEquals { next() } - listIteratorProperties() - } - propertyFails { next() } - - while (expected.hasPrevious()) { - propertyEquals { previous() } - listIteratorProperties() - } - propertyFails { previous() } - } - - private fun CompareContext>.listIteratorProperties() { - propertyEquals { hasNext() } - propertyEquals { hasPrevious() } - propertyEquals { nextIndex() } - propertyEquals { previousIndex() } - } - - private fun CompareContext>.iteratorBehavior() { - propertyEquals { hasNext() } - - while (expected.hasNext()) { - propertyEquals { next() } - propertyEquals { hasNext() } - } - propertyFails { next() } - } - - private fun CompareContext>.setBehavior(objectName: String = "") { - equalityBehavior(objectName) - collectionBehavior(objectName) - compareProperty({ iterator() }, { iteratorBehavior() }) - } - - private fun CompareContext>.mapBehavior() { - equalityBehavior() - propertyEquals { size() } - propertyEquals { isEmpty() } - - (object {}).let { propertyEquals { containsKey(it)} } - - if (expected.isEmpty().not()) - propertyEquals { contains(keySet().first()) } - - compareProperty( { keySet() }, { setBehavior("keySet") } ) - compareProperty( { entrySet() }, { setBehavior("entrySet") } ) - compareProperty( { values() }, { collectionBehavior("values") }) - } - - private fun CompareContext.equalityBehavior(objectName: String = "") { - val prefix = objectName + if (objectName.isNotEmpty()) "." else "" - equals(objectName) - propertyEquals(prefix + "hashCode") { hashCode() } - propertyEquals(prefix + "toString") { toString() } - } - - - private fun CompareContext>.collectionBehavior(objectName: String = "") { - val prefix = objectName + if (objectName.isNotEmpty()) "." else "" - propertyEquals (prefix + "size") { size() } - propertyEquals (prefix + "isEmpty") { isEmpty() } - - (object {}).let { propertyEquals { contains(it)} } - propertyEquals { contains(firstOrNull()) } - propertyEquals { containsAll(this) } - } - } -private fun compare(expected: T, actual: T, block: CompareContext.() -> Unit) = CompareContext(expected, actual).block() - -private class CompareContext(public val expected: T, public val actual: T) { - - public fun equals(message: String = "") { assertEquals(expected, actual, message) } - public fun propertyEquals

(message: String = "", getter: T.() -> P) { assertEquals(expected.getter(), actual.getter(), message) } - public fun propertyFails(getter: T.() -> Unit) { assertFailEquals({expected.getter()}, {actual.getter()}) } - public fun compareProperty

(getter: T.() -> P, block: CompareContext

.() -> Unit) { - compare(expected.getter(), actual.getter(), block) - } - - private fun assertFailEquals(expected: () -> Unit, actual: () -> Unit) { - val expectedFail = fails(expected) - val actualFail = fails(actual) - //assertEquals(expectedFail != null, actualFail != null) - assertTypeEquals(expectedFail, actualFail) - } -} diff --git a/libraries/stdlib/test/collections/ComparisonDSL.kt b/libraries/stdlib/test/collections/ComparisonDSL.kt new file mode 100644 index 00000000000..eade428e95d --- /dev/null +++ b/libraries/stdlib/test/collections/ComparisonDSL.kt @@ -0,0 +1,28 @@ +package test.collections + +import kotlin.test.* + +public fun compare(expected: T, actual: T, block:CompareContext.() -> Unit) { + CompareContext(expected, actual).block() +} + +public class CompareContext(public val expected: T, public val actual: T) { + + public fun equals(message: String = "") { + assertEquals(expected, actual, message) + } + public fun propertyEquals

(message: String = "", getter: T.() -> P) { + assertEquals(expected.getter(), actual.getter(), message) + } + public fun propertyFails(getter: T.() -> Unit) { assertFailEquals({expected.getter()}, {actual.getter()}) } + public fun compareProperty

(getter: T.() -> P, block: CompareContext

.() -> Unit) { + compare(expected.getter(), actual.getter(), block) + } + + private fun assertFailEquals(expected: () -> Unit, actual: () -> Unit) { + val expectedFail = fails(expected) + val actualFail = fails(actual) + //assertEquals(expectedFail != null, actualFail != null) + assertTypeEquals(expectedFail, actualFail) + } +} \ No newline at end of file diff --git a/libraries/stdlib/test/js/JsArrayScript.kt b/libraries/stdlib/test/js/JsArrayScript.kt index 19c895bc139..4195cd07568 100644 --- a/libraries/stdlib/test/js/JsArrayScript.kt +++ b/libraries/stdlib/test/js/JsArrayScript.kt @@ -1,17 +1,18 @@ +// this package referenced from testArrayScriptTest package jstest fun testSize(): Int { - val a1 = array() - val a2 = array("foo") - val a3 = array("foo", "bar") + val a1 = arrayOf() + val a2 = arrayOf("foo") + val a3 = arrayOf("foo", "bar") return a1.size() + a2.size() + a3.size() } fun testToListToString(): String { - val a1 = array() - val a2 = array("foo") - val a3 = array("foo", "bar") + val a1 = arrayOf() + val a2 = arrayOf("foo") + val a3 = arrayOf("foo", "bar") return a1.toList().toString() + "-" + a2.toList().toString() + "-" + a3.toList().toString() } diff --git a/libraries/stdlib/test/js/JsArrayTest.kt b/libraries/stdlib/test/js/JsArrayTest.kt index c9aaf78ab97..9c69b8bef10 100644 --- a/libraries/stdlib/test/js/JsArrayTest.kt +++ b/libraries/stdlib/test/js/JsArrayTest.kt @@ -1,4 +1,4 @@ -package jstest +package test.collections import org.junit.Test as test import kotlin.test.* @@ -7,9 +7,9 @@ import java.util.ArrayList; class JsArrayTest { test fun arraySizeAndToList() { - val a1 = array() - val a2 = array("foo") - val a3 = array("foo", "bar") + val a1 = arrayOf() + val a2 = arrayOf("foo") + val a3 = arrayOf("foo", "bar") assertEquals(0, a1.size()) assertEquals(1, a2.size()) @@ -22,7 +22,7 @@ class JsArrayTest { } test fun arrayListFromCollection() { - var c: Collection = array("A", "B", "C").toList() + var c: Collection = arrayOf("A", "B", "C").toList() var a = ArrayList(c) assertEquals(3, a.size()) diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index 05198ed5689..01bd585feae 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt @@ -1,5 +1,6 @@ -package testPackage +package test.collections +import test.collections.behaviors.mapBehavior import kotlin.test.* import java.util.* @@ -22,8 +23,8 @@ class ComplexMapJsTest : MapJsTest() { override fun > Collection.toNormalizedList(): List = this.toSortedList() // hashMapOf returns ComlpexHashMap because it is Generic - override fun emptyMutableMap(): MutableMap = hashMapOf() - override fun emptyMutableMapWithNullableKeyValue(): MutableMap = hashMapOf() + override fun emptyMutableMap(): MutableMap = genericHashMapOf() + override fun emptyMutableMapWithNullableKeyValue(): MutableMap = genericHashMapOf() } class PrimitiveMapJsTest : MapJsTest() { @@ -41,6 +42,16 @@ class PrimitiveMapJsTest : MapJsTest() { override fun > Collection.toNormalizedList(): List = this.toSortedList() override fun emptyMutableMap(): MutableMap = HashMap() override fun emptyMutableMapWithNullableKeyValue(): MutableMap = HashMap() + + test fun compareBehavior() { + val specialJsStringMap = HashMap() + specialJsStringMap.put("k1", "v1") + compare(genericHashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior() } + + val specialJsNumberMap = HashMap(4) + specialJsNumberMap.put(5, "v5") + compare(genericHashMapOf(5 to "v5"), specialJsNumberMap) { mapBehavior() } + } } class LinkedHashMapTest : MapJsTest() { @@ -475,4 +486,6 @@ abstract class MapJsTest { } return map } + + fun genericHashMapOf(vararg values: Pair) = hashMapOf(*values) } diff --git a/libraries/stdlib/test/js/SetJsTest.kt b/libraries/stdlib/test/js/SetJsTest.kt index 03dfd44b121..6acb7c3886f 100644 --- a/libraries/stdlib/test/js/SetJsTest.kt +++ b/libraries/stdlib/test/js/SetJsTest.kt @@ -2,6 +2,7 @@ package test.collections import kotlin.test.* import org.junit.Test +import test.collections.behaviors.setBehavior import java.util.HashSet import java.util.LinkedHashSet @@ -22,8 +23,10 @@ class ComplexSetJsTest : SetJsTest() { } // hashSetOf returns ComlpexHashSet because it is Generic - override fun createEmptyMutableSet(): MutableSet = hashSetOf() - override fun createEmptyMutableSetWithNullableValues(): MutableSet = hashSetOf() + override fun createEmptyMutableSet(): MutableSet = genericHashSetOf() + override fun createEmptyMutableSetWithNullableValues(): MutableSet = genericHashSetOf() + + } class PrimitiveSetJsTest : SetJsTest() { @@ -38,6 +41,17 @@ class PrimitiveSetJsTest : SetJsTest() { assertEquals(data, set) } + + Test fun compareBehavior() { + val specialJsStringSet = HashSet() + specialJsStringSet.add("kotlin") + compare(genericHashSetOf("kotlin"), specialJsStringSet) { setBehavior() } + + val specialJsNumberSet = HashSet() + specialJsNumberSet.add(3.14) + compare(genericHashSetOf(3.14), specialJsNumberSet) { setBehavior() } + } + } class LinkedHashSetJsTest : SetJsTest() { @@ -58,7 +72,7 @@ abstract class SetJsTest { val data: Set = createTestMutableSet() val empty: Set = createEmptyMutableSet() - val SPECIAL_NAMES = array("__proto__", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable") + val SPECIAL_NAMES = arrayOf("__proto__", "constructor", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable") Test fun size() { assertEquals(2, data.size()) @@ -222,4 +236,6 @@ abstract class SetJsTest { set.add("foo") return set } + + fun genericHashSetOf(vararg values: T) = hashSetOf(*values) } diff --git a/libraries/stdlib/test/js/javautilCollectionsTest.kt b/libraries/stdlib/test/js/javautilCollectionsTest.kt index d5aae6146db..500f15e4ef6 100644 --- a/libraries/stdlib/test/js/javautilCollectionsTest.kt +++ b/libraries/stdlib/test/js/javautilCollectionsTest.kt @@ -1,4 +1,4 @@ -package testPackage +package test.collections import java.util.Collections import java.util.ArrayList