Merge remote-tracking branch 'origin/master'

Conflicts:
	js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java
	js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java
This commit is contained in:
Pavel V. Talanov
2012-07-04 18:32:24 +04:00
158 changed files with 4376 additions and 1097 deletions
@@ -184,27 +184,21 @@ public inline fun <T> Array<T>.reduceRight(operation: (T, T) -> T): T = reverse(
*/
public inline fun <T, K> Array<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, List<T>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <T, K> Array<T>.groupByTo(result: Map<K, List<T>>, toKey: (T) -> K) : Map<K, List<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun <T> Array<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Bool
*/
public inline fun <K> BooleanArray.groupBy(toKey: (Boolean) -> K) : Map<K, List<Boolean>> = groupByTo<K>(HashMap<K, List<Boolean>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> BooleanArray.groupByTo(result: Map<K, List<Boolean>>, toKey: (Boolean) -> K) : Map<K, List<Boolean>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Boolean>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun BooleanArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte =
*/
public inline fun <K> ByteArray.groupBy(toKey: (Byte) -> K) : Map<K, List<Byte>> = groupByTo<K>(HashMap<K, List<Byte>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> ByteArray.groupByTo(result: Map<K, List<Byte>>, toKey: (Byte) -> K) : Map<K, List<Byte>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Byte>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun ByteArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char =
*/
public inline fun <K> CharArray.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> = groupByTo<K>(HashMap<K, List<Char>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> CharArray.groupByTo(result: Map<K, List<Char>>, toKey: (Char) -> K) : Map<K, List<Char>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Char>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun CharArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double)
*/
public inline fun <K> DoubleArray.groupBy(toKey: (Double) -> K) : Map<K, List<Double>> = groupByTo<K>(HashMap<K, List<Double>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> DoubleArray.groupByTo(result: Map<K, List<Double>>, toKey: (Double) -> K) : Map<K, List<Double>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Double>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun DoubleArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Fl
*/
public inline fun <K> FloatArray.groupBy(toKey: (Float) -> K) : Map<K, List<Float>> = groupByTo<K>(HashMap<K, List<Float>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> FloatArray.groupByTo(result: Map<K, List<Float>>, toKey: (Float) -> K) : Map<K, List<Float>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Float>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun FloatArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int = reve
*/
public inline fun <K> IntArray.groupBy(toKey: (Int) -> K) : Map<K, List<Int>> = groupByTo<K>(HashMap<K, List<Int>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> IntArray.groupByTo(result: Map<K, List<Int>>, toKey: (Int) -> K) : Map<K, List<Int>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Int>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun IntArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -26,29 +26,3 @@ public inline fun <T, R, C: Collection<in R>> java.lang.Iterable<T>.mapTo(result
result.add(transform(item))
return result
}
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <T, R> java.lang.Iterable<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}
@@ -0,0 +1,27 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <T, R> java.lang.Iterable<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}
@@ -182,27 +182,21 @@ public inline fun <T> java.util.Iterator<T>.reduceRight(operation: (T, T) -> T):
*/
public inline fun <T, K> java.util.Iterator<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, List<T>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <T, K> java.util.Iterator<T>.groupByTo(result: Map<K, List<T>>, toKey: (T) -> K) : Map<K, List<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun <T> java.util.Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -267,17 +261,3 @@ public inline fun <in T> java.util.Iterator<T>.toSortedList(transform: fun(T) :
return answer
}
*/
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/JLangIterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
@@ -0,0 +1,15 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/JLangIterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
@@ -184,27 +184,21 @@ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long =
*/
public inline fun <K> LongArray.groupBy(toKey: (Long) -> K) : Map<K, List<Long>> = groupByTo<K>(HashMap<K, List<Long>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> LongArray.groupByTo(result: Map<K, List<Long>>, toKey: (Long) -> K) : Map<K, List<Long>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Long>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun LongArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Sh
*/
public inline fun <K> ShortArray.groupBy(toKey: (Short) -> K) : Map<K, List<Short>> = groupByTo<K>(HashMap<K, List<Short>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> ShortArray.groupByTo(result: Map<K, List<Short>>, toKey: (Short) -> K) : Map<K, List<Short>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<Short>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun ShortArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -184,27 +184,21 @@ public inline fun <T> Iterable<T>.reduceRight(operation: (T, T) -> T): T = rever
*/
public inline fun <T, K> Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, List<T>>(), toKey)
/**
* Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <T, K> Iterable<T>.groupByTo(result: Map<K, List<T>>, toKey: (T) -> K) : Map<K, List<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
val some = key
val more = some
val onceMore = more
val again = println(more)
}
return result
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
*/
public inline fun <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
@@ -1,7 +1,7 @@
package kotlin.browser
import org.w3c.dom.Document
import js.native
import org.w3c.dom.Document
private var _document: Document? = null
@@ -9,7 +9,13 @@ private var _document: Document? = null
* Provides access to the current active browsers DOM for the currently visible page.
*/
native public var document: Document
get() = _document!!
set(value) {
_document = value
}
get() {
// Note this code is only executed on the JVM
val answer = _document
return if (answer == null) {
kotlin.dom.createDocument()
} else answer
}
set(value) {
_document = value
}
+20 -1
View File
@@ -311,6 +311,25 @@ val NodeList?.last : Node?
get() = this.tail
/** Converts the node list to an XML String */
fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String {
return if (this == null)
"" else {
nodesToXmlString(this.toList(), xmlDeclaration)
}
}
/** Converts the collection of nodes to an XML String */
public fun nodesToXmlString(nodes: java.lang.Iterable<Node>, xmlDeclaration: Boolean = false): String {
// TODO this should work...
// return this.map<Node,String>{it.toXmlString()}.makeString("")
val builder = StringBuilder()
for (n in nodes) {
builder.append(n.toXmlString(xmlDeclaration))
}
return builder.toString().sure()
}
// Syntax sugar
inline fun Node.plus(child: Node?): Node {
@@ -381,7 +400,7 @@ Adds a newly created text node to an element which either already has an owner D
*/
fun Element.addText(text: String?, doc: Document? = null): Element {
if (text != null) {
val child = ownerDocument(doc).createTextNode(text)
val child = this.ownerDocument(doc).createTextNode(text)
this.appendChild(child)
}
return this
+4 -19
View File
@@ -153,13 +153,6 @@ fun Element.removeClass(cssClass: String): Boolean {
}
/** Converts the node list to an XML String */
fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String {
return if (this == null)
"" else {
nodesToXmlString(this.toList(), xmlDeclaration)
}
}
/** Creates a new document with the given document builder*/
public fun createDocument(builder: DocumentBuilder): Document {
@@ -225,7 +218,10 @@ public fun createTransformer(source: Source? = null, factory: TransformerFactory
}
/** Converts the node to an XML String */
public fun Node.toXmlString(xmlDeclaration: Boolean = false): String {
public fun Node.toXmlString(): String = toXmlString(false)
/** Converts the node to an XML String */
public fun Node.toXmlString(xmlDeclaration: Boolean): String {
val writer = StringWriter()
writeXmlString(writer, xmlDeclaration)
return writer.toString().sure()
@@ -237,14 +233,3 @@ public fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, if (xmlDeclaration) "no" else "yes")
transformer.transform(DOMSource(this), StreamResult(writer))
}
/** Converts the collection of nodes to an XML String */
public fun nodesToXmlString(nodes: java.lang.Iterable<Node>, xmlDeclaration: Boolean = false): String {
// TODO this should work...
// return this.map<Node,String>{it.toXmlString()}.makeString("")
val builder = StringBuilder()
for (n in nodes) {
builder.append(n.toXmlString(xmlDeclaration))
}
return builder.toString().sure()
}
@@ -21,10 +21,17 @@ class ClasspathBuilder(val parent: ModuleBuilder) {
}
}
class AnnotationsPathBuilder(val parent: ModuleBuilder) {
public fun plusAssign(name: String) {
parent.addAnnotationsPathEntry(name)
}
}
open class ModuleBuilder(val name: String): Module {
// http://youtrack.jetbrains.net/issue/KT-904
private val sourceFiles0: ArrayList<String?> = ArrayList<String?>()
private val classpathRoots0: ArrayList<String?> = ArrayList<String?>()
private val annotationsRoots0: ArrayList<String?> = ArrayList<String?>()
val sources: SourcesBuilder
get() = SourcesBuilder(this)
@@ -32,6 +39,9 @@ open class ModuleBuilder(val name: String): Module {
val classpath: ClasspathBuilder
get() = ClasspathBuilder(this)
val annotationsPath: AnnotationsPathBuilder
get() = AnnotationsPathBuilder(this)
public fun addSourceFiles(pattern: String) {
sourceFiles0.add(pattern)
}
@@ -40,8 +50,13 @@ open class ModuleBuilder(val name: String): Module {
classpathRoots0.add(name)
}
public fun addAnnotationsPathEntry(name: String) {
annotationsRoots0.add(name)
}
public override fun getSourceFiles(): List<String?>? = sourceFiles0
public override fun getClasspathRoots(): List<String?>? = classpathRoots0
public override fun getAnnotationsRoots(): List<String?>? = annotationsRoots0
public override fun getModuleName(): String? = name
}
@@ -5,7 +5,7 @@ import java.util.NoSuchElementException
// TODO should not need this - its here for the JS stuff
import java.lang.UnsupportedOperationException
enum class State {
public enum class State {
Ready
NotReady
Done
+5 -5
View File
@@ -2,14 +2,14 @@ package test.standard
import kotlin.*
import kotlin.test.*
import junit.framework.TestCase
import org.junit.Test as test
class GetOrElseTest() : TestCase() {
class GetOrElseTest {
val v1: String? = "hello"
val v2: String? = null
var counter = 0
fun testDefaultValue() {
test fun defaultValue() {
assertEquals("hello", v1?: "bar")
expect("hello") {
@@ -17,7 +17,7 @@ class GetOrElseTest() : TestCase() {
}
}
fun testDefaultValueOnNull() {
test fun defaultValueOnNull() {
assertEquals("bar", v2?: "bar")
expect("bar") {
@@ -30,7 +30,7 @@ class GetOrElseTest() : TestCase() {
return "bar"
}
fun testLazyDefaultValue() {
test fun lazyDefaultValue() {
counter = 0
assertEquals("hello", v1?: calculateBar())
+10 -3
View File
@@ -6,6 +6,11 @@ import org.junit.Test as test
class ListTest {
test fun toString() {
val data = arrayList("foo", "bar")
assertEquals("[foo, bar]", data.toString())
}
test fun head() {
val data = arrayList("foo", "bar")
assertEquals("foo", data.head)
@@ -13,7 +18,9 @@ class ListTest {
test fun tail() {
val data = arrayList("foo", "bar", "whatnot")
assertEquals(arrayList("bar", "whatnot"), data.tail)
val actual = data.tail
val expected = arrayList("bar", "whatnot")
assertEquals(expected, actual)
}
test fun first() {
@@ -28,9 +35,9 @@ class ListTest {
test fun withIndices() {
val data = arrayList("foo", "bar")
val withIndices = data.withIndices()
val wis = data.withIndices()
var index = 0
for (withIndex in withIndices) {
for (withIndex in wis) {
assertEquals(withIndex._1, index)
assertEquals(withIndex._2, data[index])
index++
+4 -2
View File
@@ -34,7 +34,8 @@ class DomTest {
val e = doc.createElement("foo")!!
e + "hello"
println("element after text ${e.toXmlString()}")
val xml = e.toXmlString()
println("element after text ${xml}")
assertEquals("hello", e.text)
@@ -44,7 +45,8 @@ class DomTest {
fun assertCssClass(e: Element, value: String?): Unit {
val cl = e.classes
val cl2 = e.getAttribute("class")
println("element ${e.toXmlString()} has cssClass `${cl}` class attr `${cl2}`")
val xml = e.toXmlString()
println("element ${xml} has cssClass `${cl}` class attr `${cl2}`")
assertEquals(value, cl, "value of element.cssClass")
assertEquals(value, cl2, "value of element.getAttribute(\"class\")")
@@ -2,7 +2,6 @@ package iterators
import kotlin.*
import kotlin.test.*
import kotlin.util.*
import org.junit.Test
@@ -0,0 +1,28 @@
package iterators
import kotlin.test.assertEquals
import org.junit.Test as test
import kotlin.test.failsWith
class IteratorsJVMTest {
test fun flatMapAndTakeExtractTheTransformedElements() {
fun intToBinaryDigits() = { (i: Int) ->
val binary = Integer.toBinaryString(i).sure()
var index = 0
iterate<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayList(
'0', // fibonacci(0) = 0
'1', // fibonacci(1) = 1
'1', // fibonacci(2) = 1
'1', '0', // fibonacci(3) = 2
'1', '1', // fibonacci(4) = 3
'1', '0', '1' // fibonacci(5) = 5
)
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
}
}
@@ -4,13 +4,13 @@ import kotlin.test.assertEquals
import org.junit.Test as test
import kotlin.test.failsWith
class IteratorsTest {
fun fibonacci(): java.util.Iterator<Int> {
// fibonacci terms
var index = 0; var a = 0; var b = 1
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
}
private fun fibonacci(): java.util.Iterator<Int> {
// fibonacci terms
var index = 0; var a = 0; var b = 1
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
}
class IteratorsTest {
test fun filterAndTakeWhileExtractTheElementsWithinRange() {
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
@@ -29,25 +29,6 @@ class IteratorsTest {
assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList())
}
test fun flatMapAndTakeExtractTheTransformedElements() {
fun intToBinaryDigits() = { (i: Int) ->
val binary = Integer.toBinaryString(i).sure()
var index = 0
iterate<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayList(
'0', // fibonacci(0) = 0
'1', // fibonacci(1) = 1
'1', // fibonacci(2) = 1
'1', '0', // fibonacci(3) = 2
'1', '1', // fibonacci(4) = 3
'1', '0', '1' // fibonacci(5) = 5
)
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
}
test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
}
+52
View File
@@ -0,0 +1,52 @@
package test.js
import kotlin.*
import kotlin.browser.*
import kotlin.dom.*
import kotlin.test.*
import org.w3c.dom.*
import org.junit.Test as test
class JsDomTest {
test fun testCreateDocument() {
var doc = document
assertNotNull(doc, "Should have created a document")
val e = doc.createElement("foo")!!
assertCssClass(e, "")
// now lets update the cssClass property
e.classes = "foo"
assertCssClass(e, "foo")
// now using the attribute directly
e.setAttribute("class", "bar")
assertCssClass(e, "bar")
}
test fun addText() {
var doc = document
assertNotNull(doc, "Should have created a document")
val e = doc.createElement("foo")!!
e + "hello"
val xml = e.toXmlString()
println("element after text ${xml}")
assertEquals("hello", e.text)
}
fun assertCssClass(e: Element, value: String?): Unit {
val cl = e.classes
val cl2 = e.getAttribute("class") ?: ""
val xml = e.toXmlString()
println("element ${xml} has cssClass `${cl}` class attr `${cl2}`")
assertEquals(value, cl, "value of element.cssClass")
assertEquals(value, cl2, "value of element.getAttribute(\"class\")")
}
}
@@ -124,7 +124,7 @@ import js.noImpl
if (fields != null) {
if (fields.notEmpty()) {
println("")
println(" class object {")
println(" public class object {")
for (field in fields) {
if (field != null) {
val modifiers = field.getModifiers()
@@ -106,7 +106,10 @@ fun main(args: Array<String>) {
it.replaceAll("java.lang.Iterable<T", "Iterable<T")
}
generateFile(File(outDir, "JUtilIteratorsFromJLangIterables.kt"), "package kotlin", File(srcDir, "JLangIterables.kt"), File(srcDir, "JLangIterablesJVM.kt")) {
generateFile(File(outDir, "JUtilIteratorsFromJLangIterables.kt"), "package kotlin", File(srcDir, "JLangIterables.kt")) {
it.replaceAll("java.lang.Iterable<T", "java.util.Iterator<T")
}
generateFile(File(outDir, "JUtilIteratorsFromJLangIterablesJVM.kt"), "package kotlin", File(srcDir, "JLangIterablesJVM.kt")) {
it.replaceAll("java.lang.Iterable<T", "java.util.Iterator<T")
}
@@ -122,7 +125,10 @@ fun main(args: Array<String>) {
}
}
generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt"), File(srcDir, "JUtilCollectionsJVM.kt")) {
generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
}
generateFile(File(outDir, "JUtilIterablesFromJUtilCollectionsJVM.kt"), "package kotlin", File(srcDir, "JUtilCollectionsJVM.kt")) {
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
}