Deprecated with ERROR preconditions with eager message.

This commit is contained in:
Ilya Gorbunov
2015-11-07 16:25:36 +03:00
parent 07c570f35d
commit 3639afafce
11 changed files with 40 additions and 62 deletions
@@ -10,35 +10,35 @@ fun box(): String {
val s = J::s val s = J::s
// Check that correct reflection objects are created // Check that correct reflection objects are created
assert(i !is KMutableProperty<*>, "Fail i class: ${i.javaClass}") assert(i !is KMutableProperty<*>) { "Fail i class: ${i.javaClass}" }
assert(s is KMutableProperty<*>, "Fail s class: ${s.javaClass}") assert(s is KMutableProperty<*>) { "Fail s class: ${s.javaClass}" }
// Check that no Method objects are created for such properties // Check that no Method objects are created for such properties
assert(i.javaGetter == null, "Fail i getter") assert(i.javaGetter == null) { "Fail i getter" }
assert(s.javaGetter == null, "Fail s getter") assert(s.javaGetter == null) { "Fail s getter" }
assert(s.javaSetter == null, "Fail s setter") assert(s.javaSetter == null) { "Fail s setter" }
// Check that correct Field objects are created // Check that correct Field objects are created
val ji = i.javaField!! val ji = i.javaField!!
val js = s.javaField!! val js = s.javaField!!
assert(Modifier.isFinal(ji.getModifiers()), "Fail i final") assert(Modifier.isFinal(ji.getModifiers())) { "Fail i final" }
assert(!Modifier.isFinal(js.getModifiers()), "Fail s final") assert(!Modifier.isFinal(js.getModifiers())) { "Fail s final" }
// Check that those Field objects work as expected // Check that those Field objects work as expected
val a = J(42, "abc") val a = J(42, "abc")
assert(ji.get(a) == 42, "Fail ji get") assert(ji.get(a) == 42) { "Fail ji get" }
assert(js.get(a) == "abc", "Fail js get") assert(js.get(a) == "abc") { "Fail js get" }
js.set(a, "def") js.set(a, "def")
assert(js.get(a) == "def", "Fail js set") assert(js.get(a) == "def") { "Fail js set" }
assert(a.s == "def", "Fail js access") assert(a.s == "def") { "Fail js access" }
// Check that valid Kotlin reflection objects are created by those Field objects // Check that valid Kotlin reflection objects are created by those Field objects
val ki = ji.kotlin as KProperty1<J, Int> val ki = ji.kotlin as KProperty1<J, Int>
val ks = js.kotlin as KMutableProperty1<J, String> val ks = js.kotlin as KMutableProperty1<J, String>
assert(ki.get(a) == 42, "Fail ki get") assert(ki.get(a) == 42) { "Fail ki get" }
assert(ks.get(a) == "def", "Fail ks get") assert(ks.get(a) == "def") { "Fail ks get" }
ks.set(a, "ghi") ks.set(a, "ghi")
assert(ks.get(a) == "ghi", "Fail ks set") assert(ks.get(a) == "ghi") { "Fail ks set" }
return "OK" return "OK"
} }
@@ -11,14 +11,14 @@ class Secondary {
} }
fun check(f: KFunction<Any>) { fun check(f: KFunction<Any>) {
assert(f.javaMethod == null, "Fail f method") assert(f.javaMethod == null) { "Fail f method" }
assert(f.javaConstructor != null, "Fail f constructor") assert(f.javaConstructor != null) { "Fail f constructor" }
val c = f.javaConstructor!! val c = f.javaConstructor!!
assert(c.kotlinFunction != null, "Fail m function") assert(c.kotlinFunction != null) { "Fail m function" }
val ff = c.kotlinFunction!! val ff = c.kotlinFunction!!
assert(f == ff, "Fail f != ff") assert(f == ff) { "Fail f != ff" }
} }
fun box(): String { fun box(): String {
@@ -19,9 +19,9 @@ fun box(): String {
assertEquals(setter, Class.forName("ExtensionPropertyKt").getMethod("setExt", javaClass<K>(), javaClass<Double>())) assertEquals(setter, Class.forName("ExtensionPropertyKt").getMethod("setExt", javaClass<K>(), javaClass<Double>()))
val k = K(42L) val k = K(42L)
assert(getter.invoke(null, k) == 42.0, "Fail k getter") assert(getter.invoke(null, k) == 42.0) { "Fail k getter" }
setter.invoke(null, k, -239.0) setter.invoke(null, k, -239.0)
assert(getter.invoke(null, k) == -239.0, "Fail k setter") assert(getter.invoke(null, k) == -239.0) { "Fail k setter" }
return "OK" return "OK"
} }
@@ -8,14 +8,14 @@ fun bar(s: String): Int = s.length()
fun String.baz(): Int = this.length() fun String.baz(): Int = this.length()
fun check(f: KFunction<Int>) { fun check(f: KFunction<Int>) {
assert(f.javaConstructor == null, "Fail f constructor") assert(f.javaConstructor == null) { "Fail f constructor" }
assert(f.javaMethod != null, "Fail f method") assert(f.javaMethod != null) { "Fail f method" }
val m = f.javaMethod!! val m = f.javaMethod!!
assert(m.kotlinFunction != null, "Fail m function") assert(m.kotlinFunction != null) { "Fail m function" }
val ff = m.kotlinFunction!! val ff = m.kotlinFunction!!
assert(f == ff, "Fail f != ff") assert(f == ff) { "Fail f != ff" }
} }
fun box(): String { fun box(): String {
@@ -6,7 +6,7 @@ class K(var value: Long)
fun box(): String { fun box(): String {
val p = K::value val p = K::value
assert(p.javaField != null, "Fail p field") assert(p.javaField != null) { "Fail p field" }
val getter = p.javaGetter!! val getter = p.javaGetter!!
val setter = p.javaSetter!! val setter = p.javaSetter!!
@@ -15,9 +15,9 @@ fun box(): String {
assertEquals(setter, javaClass<K>().getMethod("setValue", javaClass<Long>())) assertEquals(setter, javaClass<K>().getMethod("setValue", javaClass<Long>()))
val k = K(42L) val k = K(42L)
assert(getter.invoke(k) == 42L, "Fail k getter") assert(getter.invoke(k) == 42L) { "Fail k getter" }
setter.invoke(k, -239L) setter.invoke(k, -239L)
assert(getter.invoke(k) == -239L, "Fail k setter") assert(getter.invoke(k) == -239L) { "Fail k setter" }
return "OK" return "OK"
} }
@@ -6,7 +6,7 @@ var topLevel = "123"
fun box(): String { fun box(): String {
val p = ::topLevel val p = ::topLevel
assert(p.javaField != null, "Fail p field") assert(p.javaField != null) { "Fail p field" }
val field = p.javaField!! val field = p.javaField!!
val className = field.getDeclaringClass().getName() val className = field.getDeclaringClass().getName()
assertEquals("TopLevelPropertyKt", className) assertEquals("TopLevelPropertyKt", className)
@@ -17,9 +17,9 @@ fun box(): String {
assertEquals(getter, Class.forName("TopLevelPropertyKt").getMethod("getTopLevel")) assertEquals(getter, Class.forName("TopLevelPropertyKt").getMethod("getTopLevel"))
assertEquals(setter, Class.forName("TopLevelPropertyKt").getMethod("setTopLevel", javaClass<String>())) assertEquals(setter, Class.forName("TopLevelPropertyKt").getMethod("setTopLevel", javaClass<String>()))
assert(getter.invoke(null) == "123", "Fail k getter") assert(getter.invoke(null) == "123") { "Fail k getter" }
setter.invoke(null, "456") setter.invoke(null, "456")
assert(getter.invoke(null) == "456", "Fail k setter") assert(getter.invoke(null) == "456") { "Fail k setter" }
return "OK" return "OK"
} }
@@ -8,5 +8,5 @@ fun main(args: Array<String>) {
val annotationClass = javaClass<AnnotationClass>() val annotationClass = javaClass<AnnotationClass>()
val annotation = klass.getAnnotation(annotationClass)!! val annotation = klass.getAnnotation(annotationClass)!!
val value = annotation.value val value = annotation.value
require(value == "100 20000 2000000 2000000000000 3.14 3.14 true \u03c0 :)", "Annotation value: $value") require(value == "100 20000 2000000 2000000000000 3.14 3.14 true \u03c0 :)", { "Annotation value: $value" })
} }
@@ -20,7 +20,7 @@ public fun assert(value: Boolean) {
* Throws an [AssertionError] with an optional [message] if the [value] is false * Throws an [AssertionError] with an optional [message] if the [value] is false
* and runtime assertions have been enabled on the JVM using the *-ea* JVM option. * and runtime assertions have been enabled on the JVM using the *-ea* JVM option.
*/ */
@Deprecated("Use assert with lazy message instead.", ReplaceWith("assert(value) { message }")) @Deprecated("Use assert with lazy message instead.", ReplaceWith("assert(value) { message }"), DeprecationLevel.ERROR)
public fun assert(value: Boolean, message: Any = "Assertion failed") { public fun assert(value: Boolean, message: Any = "Assertion failed") {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
if (ASSERTIONS_ENABLED) { if (ASSERTIONS_ENABLED) {
@@ -16,7 +16,7 @@ public fun require(value: Boolean): Unit = require(value) { "Failed requirement"
* *
* @sample test.collections.PreconditionsTest.failingRequireWithMessage * @sample test.collections.PreconditionsTest.failingRequireWithMessage
*/ */
@Deprecated("Use require with lazy message instead.", ReplaceWith("require(value) { message }")) @Deprecated("Use require with lazy message instead.", ReplaceWith("require(value) { message }"), DeprecationLevel.ERROR)
public fun require(value: Boolean, message: Any = "Failed requirement"): Unit { public fun require(value: Boolean, message: Any = "Failed requirement"): Unit {
if (!value) { if (!value) {
throw IllegalArgumentException(message.toString()) throw IllegalArgumentException(message.toString())
@@ -46,7 +46,7 @@ public fun <T:Any> requireNotNull(value: T?): T = requireNotNull(value) { "Requi
* *
* @sample test.collections.PreconditionsTest.requireNotNull * @sample test.collections.PreconditionsTest.requireNotNull
*/ */
@Deprecated("Use requireNotNull with lazy message instead.", ReplaceWith("requireNotNull(value) { message }")) @Deprecated("Use requireNotNull with lazy message instead.", ReplaceWith("requireNotNull(value) { message }"), DeprecationLevel.ERROR)
public fun <T:Any> requireNotNull(value: T?, message: Any = "Required value was null"): T { public fun <T:Any> requireNotNull(value: T?, message: Any = "Required value was null"): T {
if (value == null) { if (value == null) {
throw IllegalArgumentException(message.toString()) throw IllegalArgumentException(message.toString())
@@ -80,7 +80,7 @@ public fun check(value: Boolean): Unit = check(value) { "Check failed" }
* *
* @sample test.collections.PreconditionsTest.failingCheckWithMessage * @sample test.collections.PreconditionsTest.failingCheckWithMessage
*/ */
@Deprecated("Use check with lazy message instead.", ReplaceWith("check(value) { message }")) @Deprecated("Use check with lazy message instead.", ReplaceWith("check(value) { message }"), DeprecationLevel.ERROR)
public fun check(value: Boolean, message: Any = "Check failed"): Unit { public fun check(value: Boolean, message: Any = "Check failed"): Unit {
if (!value) { if (!value) {
throw IllegalStateException(message.toString()) throw IllegalStateException(message.toString())
@@ -111,7 +111,7 @@ public fun <T:Any> checkNotNull(value: T?): T = checkNotNull(value) { "Required
* *
* @sample test.collections.PreconditionsTest.checkNotNull * @sample test.collections.PreconditionsTest.checkNotNull
*/ */
@Deprecated("Use checkNotNull with lazy message instead.", ReplaceWith("checkNotNull(value) { message }")) @Deprecated("Use checkNotNull with lazy message instead.", ReplaceWith("checkNotNull(value) { message }"), DeprecationLevel.ERROR)
public fun <T:Any> checkNotNull(value: T?, message: Any = "Required value was null"): T { public fun <T:Any> checkNotNull(value: T?, message: Any = "Required value was null"): T {
if (value == null) { if (value == null) {
throw IllegalStateException(message.toString()) throw IllegalStateException(message.toString())
+2 -24
View File
@@ -20,17 +20,6 @@ class PreconditionsTest() {
assertNotNull(error.getMessage()) assertNotNull(error.getMessage())
} }
@test fun passingRequireWithMessage() {
require(true, "Hello")
}
@test fun failingRequireWithMessage() {
val error = assertFailsWith(IllegalArgumentException::class) {
require(false, "Hello")
}
assertEquals("Hello", error.getMessage())
}
@test fun failingRequireWithLazyMessage() { @test fun failingRequireWithLazyMessage() {
val error = assertFailsWith(IllegalArgumentException::class) { val error = assertFailsWith(IllegalArgumentException::class) {
require(false) { "Hello" } require(false) { "Hello" }
@@ -53,17 +42,6 @@ class PreconditionsTest() {
assertNotNull(error.getMessage()) assertNotNull(error.getMessage())
} }
@test fun passingCheckWithMessage() {
check(true, "Hello")
}
@test fun failingCheckWithMessage() {
val error = assertFailsWith(IllegalStateException::class) {
check(false, "Hello")
}
assertEquals("Hello", error.getMessage())
}
@test fun failingCheckWithLazyMessage() { @test fun failingCheckWithLazyMessage() {
val error = assertFailsWith(IllegalStateException::class) { val error = assertFailsWith(IllegalStateException::class) {
check(false) { "Hello" } check(false) { "Hello" }
@@ -144,12 +122,12 @@ class PreconditionsTest() {
} }
@test fun passingAssertWithMessage() { @test fun passingAssertWithMessage() {
assert(true, "Hello") assert(true) { "Hello" }
} }
@test fun failingAssertWithMessage() { @test fun failingAssertWithMessage() {
val error = assertFails { val error = assertFails {
assert(false, "Hello") assert(false) { "Hello" }
} }
if (error is AssertionError) { if (error is AssertionError) {
assertEquals("Hello", error.getMessage()) assertEquals("Hello", error.getMessage())
@@ -65,9 +65,9 @@ class KotlinGradleIT: BaseGradleIT() {
project.build(userVariantArg, "build", options = BaseGradleIT.BuildOptions(withDaemon = true)) { project.build(userVariantArg, "build", options = BaseGradleIT.BuildOptions(withDaemon = true)) {
assertSuccessful() assertSuccessful()
val matches = "\\[PERF\\] Used memory after build: (\\d+) kb \\(([+-]?\\d+) kb\\)".toRegex().find(output) val matches = "\\[PERF\\] Used memory after build: (\\d+) kb \\(([+-]?\\d+) kb\\)".toRegex().find(output)
assert(matches != null && matches.groups.size() == 3, "Used memory after build is not reported by plugin") assert(matches != null && matches.groups.size() == 3) { "Used memory after build is not reported by plugin" }
val reportedGrowth = matches!!.groups.get(2)!!.value.removePrefix("+").toInt() val reportedGrowth = matches!!.groups.get(2)!!.value.removePrefix("+").toInt()
assert(reportedGrowth <= 700, "Used memory growth $reportedGrowth > 700") assert(reportedGrowth <= 700) { "Used memory growth $reportedGrowth > 700" }
} }
} }