From f2716a973cef6a5e9ece1ae91e4e46cd72521cdb Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 14 Jul 2015 19:57:15 +0300 Subject: [PATCH] TODO as a function. Add an overload with a string reason. #KT-8153 Fixed --- libraries/stdlib/src/kotlin/util/Standard.kt | 9 ++++- libraries/stdlib/test/utils/TODOTest.kt | 41 +++++++++++++------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/libraries/stdlib/src/kotlin/util/Standard.kt b/libraries/stdlib/src/kotlin/util/Standard.kt index 7c86a52e43e..12bf682578e 100644 --- a/libraries/stdlib/src/kotlin/util/Standard.kt +++ b/libraries/stdlib/src/kotlin/util/Standard.kt @@ -4,7 +4,14 @@ package kotlin /** * Always throws an [UnsupportedOperationException] stating that operation is not implemented. */ -public val TODO: Nothing get() = throw UnsupportedOperationException("An operation is not implemented.") +public fun TODO(): Nothing = throw UnsupportedOperationException("An operation is not implemented.") + +/** + * Always throws an [UnsupportedOperationException] stating that operation is not implemented. + * + * @param reason a string explaining why the implementation is missing. + */ +public fun TODO(reason: String): Nothing = throw UnsupportedOperationException("An operation is not implemented: $reason") /** diff --git a/libraries/stdlib/test/utils/TODOTest.kt b/libraries/stdlib/test/utils/TODOTest.kt index 5ba03d729b7..30c351d6b29 100644 --- a/libraries/stdlib/test/utils/TODOTest.kt +++ b/libraries/stdlib/test/utils/TODOTest.kt @@ -5,32 +5,47 @@ import kotlin.test.* import org.junit.Test as test private class PartiallyImplementedClass { - public val prop: String get() = TODO - - internal fun method1() = TODO as String - public fun method2(): Int = TODO + public val prop: String get() = TODO() +// fun method1() = TODO() as String // <- doesn't compile in JS + public fun method2(): Int = TODO() public fun method3(switch: Boolean, value: String): String { if (!switch) - TODO + TODO("what if false") else { if (value.length() < 3) - throw TODO - else - return value + throw TODO("write message") } + + return value + } + + public fun method4() { + TODO() } } class TODOTest { + private fun assertNotImplemented(block: () -> Unit) { + assertTrue(fails(block) is UnsupportedOperationException) + } + + private fun assertNotImplementedWithMessage(message: String, block: () -> Unit) { + val e = fails(block) + assertTrue(e is UnsupportedOperationException) + assertTrue(message in e!!.getMessage()!!) + } + + test fun usage() { val inst = PartiallyImplementedClass() - assertTrue(fails { inst.prop } is UnsupportedOperationException) - assertTrue(fails { inst.method1() } is UnsupportedOperationException) - assertTrue(fails { inst.method2() } is UnsupportedOperationException) - assertTrue(fails { inst.method3(false, "test") } is UnsupportedOperationException) - assertTrue(fails { inst.method3(true, "t") } is UnsupportedOperationException) + assertNotImplemented { inst.prop } +// assertNotImplemented{ inst.method1() } + assertNotImplemented { inst.method2() } + assertNotImplemented { inst.method4() } + assertNotImplementedWithMessage("what if false") { inst.method3(false, "test") } + assertNotImplementedWithMessage("write message") { inst.method3(true, "t") } assertEquals("test", inst.method3(true, "test")) } }