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.
This commit is contained in:
@@ -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 <T> CompareContext<List<T>>.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 <T> CompareContext<ListIterator<T>>.listIteratorBehavior() {
|
||||
listIteratorProperties()
|
||||
|
||||
while (expected.hasNext()) {
|
||||
propertyEquals { next() }
|
||||
listIteratorProperties()
|
||||
}
|
||||
propertyFails { next() }
|
||||
|
||||
while (expected.hasPrevious()) {
|
||||
propertyEquals { previous() }
|
||||
listIteratorProperties()
|
||||
}
|
||||
propertyFails { previous() }
|
||||
}
|
||||
|
||||
public fun CompareContext<ListIterator<*>>.listIteratorProperties() {
|
||||
propertyEquals { hasNext() }
|
||||
propertyEquals { hasPrevious() }
|
||||
propertyEquals { nextIndex() }
|
||||
propertyEquals { previousIndex() }
|
||||
}
|
||||
|
||||
public fun <T> CompareContext<Iterator<T>>.iteratorBehavior() {
|
||||
propertyEquals { hasNext() }
|
||||
|
||||
while (expected.hasNext()) {
|
||||
propertyEquals { next() }
|
||||
propertyEquals { hasNext() }
|
||||
}
|
||||
propertyFails { next() }
|
||||
}
|
||||
|
||||
public fun <T> CompareContext<Set<T>>.setBehavior(objectName: String = "") {
|
||||
equalityBehavior(objectName)
|
||||
collectionBehavior(objectName)
|
||||
compareProperty({ iterator() }, { iteratorBehavior() })
|
||||
}
|
||||
|
||||
public fun <K, V> CompareContext<Map<K, V>>.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 <T> CompareContext<T>.equalityBehavior(objectName: String = "") {
|
||||
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
|
||||
equals(objectName)
|
||||
propertyEquals(prefix + "hashCode") { hashCode() }
|
||||
propertyEquals(prefix + "toString") { toString() }
|
||||
}
|
||||
|
||||
|
||||
public fun <T> CompareContext<Collection<T>>.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) }
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String>()
|
||||
specialJsStringSet.add("kotlin")
|
||||
compare(hashSetOf("kotlin"), specialJsStringSet) { setBehavior() }
|
||||
|
||||
val specialJsNumberSet = HashSet<Double>()
|
||||
specialJsNumberSet.add(3.14)
|
||||
compare(hashSetOf(3.14), specialJsNumberSet) { setBehavior() }
|
||||
}
|
||||
|
||||
test fun specialMaps() {
|
||||
compare(hashMapOf<String, Int>(), mapOf<String, Int>()) { mapBehavior() }
|
||||
compare(linkedMapOf<Int, String>(), emptyMap<Int, String>()) { mapBehavior() }
|
||||
compare(linkedMapOf(2 to 3), mapOf(2 to 3)) { mapBehavior() }
|
||||
}
|
||||
|
||||
test fun specialJsMaps() {
|
||||
val specialJsStringMap = HashMap<String, Any>()
|
||||
specialJsStringMap.put("k1", "v1")
|
||||
compare(hashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior() }
|
||||
|
||||
val specialJsNumberMap = HashMap<Int, Any>(4)
|
||||
specialJsNumberMap.put(5, "v5")
|
||||
compare(hashMapOf(5 to "v5"), specialJsNumberMap) { mapBehavior() }
|
||||
}
|
||||
|
||||
private fun <T> CompareContext<List<T>>.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 <T> CompareContext<ListIterator<T>>.listIteratorBehavior() {
|
||||
listIteratorProperties()
|
||||
|
||||
while (expected.hasNext()) {
|
||||
propertyEquals { next() }
|
||||
listIteratorProperties()
|
||||
}
|
||||
propertyFails { next() }
|
||||
|
||||
while (expected.hasPrevious()) {
|
||||
propertyEquals { previous() }
|
||||
listIteratorProperties()
|
||||
}
|
||||
propertyFails { previous() }
|
||||
}
|
||||
|
||||
private fun CompareContext<ListIterator<*>>.listIteratorProperties() {
|
||||
propertyEquals { hasNext() }
|
||||
propertyEquals { hasPrevious() }
|
||||
propertyEquals { nextIndex() }
|
||||
propertyEquals { previousIndex() }
|
||||
}
|
||||
|
||||
private fun <T> CompareContext<Iterator<T>>.iteratorBehavior() {
|
||||
propertyEquals { hasNext() }
|
||||
|
||||
while (expected.hasNext()) {
|
||||
propertyEquals { next() }
|
||||
propertyEquals { hasNext() }
|
||||
}
|
||||
propertyFails { next() }
|
||||
}
|
||||
|
||||
private fun <T> CompareContext<Set<T>>.setBehavior(objectName: String = "") {
|
||||
equalityBehavior(objectName)
|
||||
collectionBehavior(objectName)
|
||||
compareProperty({ iterator() }, { iteratorBehavior() })
|
||||
}
|
||||
|
||||
private fun <K, V> CompareContext<Map<K, V>>.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 <T> CompareContext<T>.equalityBehavior(objectName: String = "") {
|
||||
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
|
||||
equals(objectName)
|
||||
propertyEquals(prefix + "hashCode") { hashCode() }
|
||||
propertyEquals(prefix + "toString") { toString() }
|
||||
}
|
||||
|
||||
|
||||
private fun <T> CompareContext<Collection<T>>.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<T>(expected: T, actual: T, block: CompareContext<T>.() -> Unit) = CompareContext(expected, actual).block()
|
||||
|
||||
private class CompareContext<out T>(public val expected: T, public val actual: T) {
|
||||
|
||||
public fun equals(message: String = "") { assertEquals(expected, actual, message) }
|
||||
public fun propertyEquals<P>(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<P>(getter: T.() -> P, block: CompareContext<P>.() -> 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
public fun compare<T>(expected: T, actual: T, block:CompareContext<T>.() -> Unit) {
|
||||
CompareContext(expected, actual).block()
|
||||
}
|
||||
|
||||
public class CompareContext<out T>(public val expected: T, public val actual: T) {
|
||||
|
||||
public fun equals(message: String = "") {
|
||||
assertEquals(expected, actual, message)
|
||||
}
|
||||
public fun propertyEquals<P>(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<P>(getter: T.() -> P, block: CompareContext<P>.() -> 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)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
// this package referenced from testArrayScriptTest
|
||||
package jstest
|
||||
|
||||
fun testSize(): Int {
|
||||
val a1 = array<String>()
|
||||
val a2 = array("foo")
|
||||
val a3 = array("foo", "bar")
|
||||
val a1 = arrayOf<String>()
|
||||
val a2 = arrayOf("foo")
|
||||
val a3 = arrayOf("foo", "bar")
|
||||
|
||||
return a1.size() + a2.size() + a3.size()
|
||||
}
|
||||
|
||||
fun testToListToString(): String {
|
||||
val a1 = array<String>()
|
||||
val a2 = array("foo")
|
||||
val a3 = array("foo", "bar")
|
||||
val a1 = arrayOf<String>()
|
||||
val a2 = arrayOf("foo")
|
||||
val a3 = arrayOf("foo", "bar")
|
||||
|
||||
return a1.toList().toString() + "-" + a2.toList().toString() + "-" + a3.toList().toString()
|
||||
}
|
||||
|
||||
@@ -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<String>()
|
||||
val a2 = array("foo")
|
||||
val a3 = array("foo", "bar")
|
||||
val a1 = arrayOf<String>()
|
||||
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<String> = array("A", "B", "C").toList()
|
||||
var c: Collection<String> = arrayOf("A", "B", "C").toList()
|
||||
var a = ArrayList(c)
|
||||
|
||||
assertEquals(3, a.size())
|
||||
|
||||
@@ -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 <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toSortedList()
|
||||
// hashMapOf returns ComlpexHashMap because it is Generic
|
||||
override fun emptyMutableMap(): MutableMap<String, Int> = hashMapOf()
|
||||
override fun emptyMutableMapWithNullableKeyValue(): MutableMap<String?, Int?> = hashMapOf()
|
||||
override fun emptyMutableMap(): MutableMap<String, Int> = genericHashMapOf()
|
||||
override fun emptyMutableMapWithNullableKeyValue(): MutableMap<String?, Int?> = genericHashMapOf()
|
||||
}
|
||||
|
||||
class PrimitiveMapJsTest : MapJsTest() {
|
||||
@@ -41,6 +42,16 @@ class PrimitiveMapJsTest : MapJsTest() {
|
||||
override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toSortedList()
|
||||
override fun emptyMutableMap(): MutableMap<String, Int> = HashMap()
|
||||
override fun emptyMutableMapWithNullableKeyValue(): MutableMap<String?, Int?> = HashMap()
|
||||
|
||||
test fun compareBehavior() {
|
||||
val specialJsStringMap = HashMap<String, Any>()
|
||||
specialJsStringMap.put("k1", "v1")
|
||||
compare(genericHashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior() }
|
||||
|
||||
val specialJsNumberMap = HashMap<Int, Any>(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<K, V>(vararg values: Pair<K, V>) = hashMapOf(*values)
|
||||
}
|
||||
|
||||
@@ -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<String> = hashSetOf()
|
||||
override fun createEmptyMutableSetWithNullableValues(): MutableSet<String?> = hashSetOf()
|
||||
override fun createEmptyMutableSet(): MutableSet<String> = genericHashSetOf()
|
||||
override fun createEmptyMutableSetWithNullableValues(): MutableSet<String?> = genericHashSetOf()
|
||||
|
||||
|
||||
}
|
||||
|
||||
class PrimitiveSetJsTest : SetJsTest() {
|
||||
@@ -38,6 +41,17 @@ class PrimitiveSetJsTest : SetJsTest() {
|
||||
|
||||
assertEquals(data, set)
|
||||
}
|
||||
|
||||
Test fun compareBehavior() {
|
||||
val specialJsStringSet = HashSet<String>()
|
||||
specialJsStringSet.add("kotlin")
|
||||
compare(genericHashSetOf("kotlin"), specialJsStringSet) { setBehavior() }
|
||||
|
||||
val specialJsNumberSet = HashSet<Double>()
|
||||
specialJsNumberSet.add(3.14)
|
||||
compare(genericHashSetOf(3.14), specialJsNumberSet) { setBehavior() }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class LinkedHashSetJsTest : SetJsTest() {
|
||||
@@ -58,7 +72,7 @@ abstract class SetJsTest {
|
||||
val data: Set<String> = createTestMutableSet()
|
||||
val empty: Set<String> = 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<T>(vararg values: T) = hashSetOf(*values)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package testPackage
|
||||
package test.collections
|
||||
|
||||
import java.util.Collections
|
||||
import java.util.ArrayList
|
||||
|
||||
Reference in New Issue
Block a user