From 4b533b297e4f68baef429f5fa3d016573c2f96b0 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 25 Apr 2016 19:40:22 +0300 Subject: [PATCH] reified assertFailsWith available in kotlin-test for JS #KT-11346 --- js/js.libraries/src/stdlib/testCode.kt | 9 +++++++++ libraries/stdlib/test/collections/ArraysTest.kt | 16 ++++++++-------- .../stdlib/test/collections/CollectionTest.kt | 16 ++++++++-------- libraries/stdlib/test/collections/MapTest.kt | 2 +- .../stdlib/test/collections/SequenceTest.kt | 4 ++-- libraries/stdlib/test/language/RangeTest.kt | 4 ++-- .../properties/delegation/MapAccessorsTest.kt | 4 ++-- libraries/stdlib/test/text/StringTest.kt | 16 ++++++++-------- libraries/stdlib/test/utils/TODOTest.kt | 7 +++---- 9 files changed, 43 insertions(+), 35 deletions(-) diff --git a/js/js.libraries/src/stdlib/testCode.kt b/js/js.libraries/src/stdlib/testCode.kt index 29cb53c6c86..28dbef0291b 100644 --- a/js/js.libraries/src/stdlib/testCode.kt +++ b/js/js.libraries/src/stdlib/testCode.kt @@ -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 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/) diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index e684939b57d..74fe5960f5e 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -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 { 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 { 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 { 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 { intArrayOf().reduceRight { a, b -> a + b } - } is UnsupportedOperationException) + } } @test fun reverseInPlace() { diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 3bb00377b34..0648ce604ae 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -290,9 +290,9 @@ class CollectionTest { } } - assertTrue(assertFails { + assertFailsWith { arrayListOf().reduceIndexed { index, a, b -> a + b } - } is UnsupportedOperationException) + } } @test fun reduceRightIndexed() { @@ -310,9 +310,9 @@ class CollectionTest { } } - assertTrue(assertFails { + assertFailsWith { arrayListOf().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 { arrayListOf().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 { arrayListOf().reduceRight { a, b -> a + b } - } is UnsupportedOperationException) + } } @test fun groupBy() { diff --git a/libraries/stdlib/test/collections/MapTest.kt b/libraries/stdlib/test/collections/MapTest.kt index ae233531701..723f101631c 100644 --- a/libraries/stdlib/test/collections/MapTest.kt +++ b/libraries/stdlib/test/collections/MapTest.kt @@ -29,7 +29,7 @@ class MapTest { @Suppress("INVISIBLE_MEMBER") @test fun getOrImplicitDefault() { val data: MutableMap = hashMapOf("bar" to 1) - assertTrue(assertFails { data.getOrImplicitDefault("foo") } is NoSuchElementException) + assertFailsWith { data.getOrImplicitDefault("foo") } assertEquals(1, data.getOrImplicitDefault("bar")) val mutableWithDefault = data.withDefault { 42 } diff --git a/libraries/stdlib/test/collections/SequenceTest.kt b/libraries/stdlib/test/collections/SequenceTest.kt index fd5663a30ac..c9fee5e40c5 100644 --- a/libraries/stdlib/test/collections/SequenceTest.kt +++ b/libraries/stdlib/test/collections/SequenceTest.kt @@ -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 { 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 { fibonacci().take(-1) } } @test fun subSequence() { diff --git a/libraries/stdlib/test/language/RangeTest.kt b/libraries/stdlib/test/language/RangeTest.kt index eaaa81213ca..4f7a26a1fb9 100644 --- a/libraries/stdlib/test/language/RangeTest.kt +++ b/libraries/stdlib/test/language/RangeTest.kt @@ -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 { 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 { 'A' until '\u0000' } } @test fun doubleRange() { diff --git a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt index a6e08864858..cca6bc3c21f 100644 --- a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt @@ -28,7 +28,7 @@ class ValByMapExtensionsTest { assertEquals(null, f) assertEquals(1, i) assertEquals(1.0, x) - assertTrue(assertFails { d } is NoSuchElementException) + assertFailsWith { 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 { d } map["d"] = null assertEquals(null, d) } diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index e5fa02b5467..9429a260d83 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -903,9 +903,9 @@ class StringTest { } } - assertTrue(assertFails { + assertFailsWith { arg1("").reduceIndexed { index, a, b -> '\n' } - } is UnsupportedOperationException) + } } @test fun reduceRightIndexed() = withOneCharSequenceArg { arg1 -> @@ -921,27 +921,27 @@ class StringTest { } } - assertTrue(assertFails { + assertFailsWith { 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 { 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 { arg1("").reduceRight { a, b -> '\n' } - } is UnsupportedOperationException) + } } @test fun groupBy() = withOneCharSequenceArg("abAbaABcD") { data -> diff --git a/libraries/stdlib/test/utils/TODOTest.kt b/libraries/stdlib/test/utils/TODOTest.kt index 9ec7358221e..d099f461f7a 100644 --- a/libraries/stdlib/test/utils/TODOTest.kt +++ b/libraries/stdlib/test/utils/TODOTest.kt @@ -43,13 +43,12 @@ class TODOTest { } private fun assertNotImplemented(block: () -> Unit) { - assertTrue(assertFails(block) is NotImplementedError) + assertFailsWith(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(block = block) + assertTrue(message in e.message!!) }