reified assertFailsWith available in kotlin-test for JS
#KT-11346
This commit is contained in:
@@ -9,6 +9,15 @@ public fun todo(block: () -> Any) {
|
||||
println("TODO at " + block)
|
||||
}
|
||||
|
||||
/** Asserts that a [block] fails with a specific exception of type [T] being thrown.
|
||||
* Since inline method doesn't allow to trace where it was invoked, it is required to pass a [message] to distinguish this method call from others.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noinline block: () -> Unit): T {
|
||||
val exception = assertFails(block) // TODO: message (in 1.1)
|
||||
assertTrue(exception is T, (message?.let { "$it. " } ?: "") + "An exception thrown is not of the expected type: $exception")
|
||||
return exception as T
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the JS implementation of asserter using [QUnit](http://QUnitjs.com/)
|
||||
|
||||
@@ -663,9 +663,9 @@ class ArraysTest {
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceIndexed { index, a, b -> if (index != 2) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceIndexed { index, a, b -> if (index != 2) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
intArrayOf().reduceIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduceRightIndexed() {
|
||||
@@ -679,9 +679,9 @@ class ArraysTest {
|
||||
expect(1.toByte()) { byteArrayOf(3, 2, 1).reduceRightIndexed { index, a, b -> if (index != 1) (a - b).toByte() else a.toByte() } }
|
||||
expect(1.toShort()) { shortArrayOf(3, 2, 1).reduceRightIndexed { index, a, b -> if (index != 1) (a - b).toShort() else a.toShort() } }
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
intArrayOf().reduceRightIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduce() {
|
||||
@@ -695,9 +695,9 @@ class ArraysTest {
|
||||
expect(0.toByte()) { byteArrayOf(3, 2, 1).reduce { a, b -> (a - b).toByte() } }
|
||||
expect(0.toShort()) { shortArrayOf(3, 2, 1).reduce { a, b -> (a - b).toShort() } }
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
intArrayOf().reduce { a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduceRight() {
|
||||
@@ -711,9 +711,9 @@ class ArraysTest {
|
||||
expect(2.toByte()) { byteArrayOf(1, 2, 3).reduceRight { a, b -> (a - b).toByte() } }
|
||||
expect(2.toShort()) { shortArrayOf(1, 2, 3).reduceRight { a, b -> (a - b).toShort() } }
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
intArrayOf().reduceRight { a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reverseInPlace() {
|
||||
|
||||
@@ -290,9 +290,9 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arrayListOf<Int>().reduceIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduceRightIndexed() {
|
||||
@@ -310,9 +310,9 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arrayListOf<Int>().reduceRightIndexed { index, a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduce() {
|
||||
@@ -321,9 +321,9 @@ class CollectionTest {
|
||||
list.reduce { a, b -> a + b }
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arrayListOf<Int>().reduce { a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduceRight() {
|
||||
@@ -332,9 +332,9 @@ class CollectionTest {
|
||||
list.reduceRight { a, b -> a + b }
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arrayListOf<Int>().reduceRight { a, b -> a + b }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun groupBy() {
|
||||
|
||||
@@ -29,7 +29,7 @@ class MapTest {
|
||||
@Suppress("INVISIBLE_MEMBER")
|
||||
@test fun getOrImplicitDefault() {
|
||||
val data: MutableMap<String, Int> = hashMapOf("bar" to 1)
|
||||
assertTrue(assertFails { data.getOrImplicitDefault("foo") } is NoSuchElementException)
|
||||
assertFailsWith<NoSuchElementException> { data.getOrImplicitDefault("foo") }
|
||||
assertEquals(1, data.getOrImplicitDefault("bar"))
|
||||
|
||||
val mutableWithDefault = data.withDefault { 42 }
|
||||
|
||||
@@ -115,7 +115,7 @@ public class SequenceTest {
|
||||
listOf(2, 3, 4, 5).let { assertEquals(it, it.asSequence().drop(0).toList()) }
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(7).joinToString(limit = 10))
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().drop(3).drop(4).joinToString(limit = 10))
|
||||
assertTrue(assertFails { fibonacci().drop(-1) } is IllegalArgumentException)
|
||||
assertFailsWith<IllegalArgumentException> { fibonacci().drop(-1) }
|
||||
}
|
||||
|
||||
@test fun take() {
|
||||
@@ -129,7 +129,7 @@ public class SequenceTest {
|
||||
assertEquals(emptyList(), fibonacci().take(1).drop(1).toList())
|
||||
assertEquals(emptyList(), fibonacci().take(1).drop(2).toList())
|
||||
|
||||
assertTrue(assertFails { fibonacci().take(-1) } is IllegalArgumentException)
|
||||
assertFailsWith<IllegalArgumentException> { fibonacci().take(-1) }
|
||||
}
|
||||
|
||||
@test fun subSequence() {
|
||||
|
||||
@@ -141,7 +141,7 @@ public class RangeTest {
|
||||
assertTrue(9L in openRange)
|
||||
assertFalse(10L in openRange)
|
||||
|
||||
assertTrue(assertFails { 0L until Long.MIN_VALUE } is IllegalArgumentException)
|
||||
assertFailsWith<IllegalArgumentException> { 0L until Long.MIN_VALUE }
|
||||
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ public class RangeTest {
|
||||
assertTrue('Y' in openRange)
|
||||
assertFalse('Z' in openRange)
|
||||
|
||||
assertTrue(assertFails { 'A' until '\u0000' } is IllegalArgumentException)
|
||||
assertFailsWith<IllegalArgumentException> { 'A' until '\u0000' }
|
||||
}
|
||||
|
||||
@test fun doubleRange() {
|
||||
|
||||
@@ -28,7 +28,7 @@ class ValByMapExtensionsTest {
|
||||
assertEquals(null, f)
|
||||
assertEquals(1, i)
|
||||
assertEquals(1.0, x)
|
||||
assertTrue(assertFails { d } is NoSuchElementException)
|
||||
assertFailsWith<NoSuchElementException> { d }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ class VarByMapExtensionsTest {
|
||||
map["a"] = null
|
||||
a // fails { a } // does not fail due to KT-8135
|
||||
|
||||
assertTrue(assertFails { d } is NoSuchElementException)
|
||||
assertFailsWith<NoSuchElementException> { d }
|
||||
map["d"] = null
|
||||
assertEquals(null, d)
|
||||
}
|
||||
|
||||
@@ -903,9 +903,9 @@ class StringTest {
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arg1("").reduceIndexed { index, a, b -> '\n' }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduceRightIndexed() = withOneCharSequenceArg { arg1 ->
|
||||
@@ -921,27 +921,27 @@ class StringTest {
|
||||
}
|
||||
}
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arg1("").reduceRightIndexed { index, a, b -> '\n' }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduce() = withOneCharSequenceArg { arg1 ->
|
||||
// get the smallest character(by char value)
|
||||
assertEquals('a', arg1("bacfd").reduce { v, c -> if (v > c) c else v })
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arg1("").reduce { a, b -> '\n' }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun reduceRight() = withOneCharSequenceArg { arg1 ->
|
||||
// get the smallest character(by char value)
|
||||
assertEquals('a', arg1("bacfd").reduceRight { c, v -> if (v > c) c else v })
|
||||
|
||||
assertTrue(assertFails {
|
||||
assertFailsWith<UnsupportedOperationException> {
|
||||
arg1("").reduceRight { a, b -> '\n' }
|
||||
} is UnsupportedOperationException)
|
||||
}
|
||||
}
|
||||
|
||||
@test fun groupBy() = withOneCharSequenceArg("abAbaABcD") { data ->
|
||||
|
||||
@@ -43,13 +43,12 @@ class TODOTest {
|
||||
}
|
||||
|
||||
private fun assertNotImplemented(block: () -> Unit) {
|
||||
assertTrue(assertFails(block) is NotImplementedError)
|
||||
assertFailsWith<NotImplementedError>(block = block)
|
||||
}
|
||||
|
||||
private fun assertNotImplementedWithMessage(message: String, block: () -> Unit) {
|
||||
val e = assertFails(block)
|
||||
assertTrue(e is NotImplementedError)
|
||||
assertTrue(message in e!!.message!!)
|
||||
val e = assertFailsWith<NotImplementedError>(block = block)
|
||||
assertTrue(message in e.message!!)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user