# Support for Groovy style "Power Assertion" in Kotlin?

**URL:** <https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427>\
**Category:** Language Design\
**Created:** [October 21, 2014, 7:00pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427 "2014-10-21T19:00:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![twadzins](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@twadzins](https://discuss.kotlinlang.org/u/twadzins)\
**Post date:** [October 21, 2014, 7:00pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/1 "2014-10-21T19:00:10Z")

</div>

I wonder if Kotlin might now or in the future have the language support to be able to do something like the handy "power assertion" feature as seen in Groovy.

[Groovy’s “Power Assert”](http://dontmindthelanguage.wordpress.com/2009/12/11/groovy-1-7-power-assert/)

Example:

```kotlin

a = 10

b = 9

 

assert 91 == a * b

//produces…

Exception thrown
 
Assertion failed:
 
assert 91 == a * b

          |  | | |

          |  10| 9

          |   90

          false

```

[](http://dontmindthelanguage.wordpress.com/2009/12/11/groovy-1-7-power-assert/)

---

<div class="post-metadata">

**Author:** ![Dittert](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dittert/32/1736_2.png) [@Dittert](https://discuss.kotlinlang.org/u/Dittert)\
**Post date:** [October 21, 2014, 9:29pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/2 "2014-10-21T21:29:39Z")

</div>

This really is a nice feature of Groovy! But I suppose being that concise would only be possible with access to the syntax tree. I don't see any way for a custom assert statement to get a hold of the original expression 91 == a \* b. An alternative would be a assert keyword with those capabilites that is part of the Kotlin language.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [October 22, 2014, 10:05am UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/3 "2014-10-22T10:05:52Z")

</div>

Or we could do some hard-core magic, but anyways it's hard to provide anything like that as a user-defined library.

---

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [December 30, 2015, 12:39am UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/4 "2015-12-30T00:39:29Z")

</div>

@abreslav Would you, in principle, accept a contribution of this feature, assuming it is well done? No promises yet, but I miss this feature every day, and have some experience with designing/implementing it. (I’m the original author of power asserts for Spock, Groovy, and Scala ([GitHub - pniederw/expecty: Power assertions (as known from Groovy and Spock) for the Scala language.](https://github.com/pniederw/expecty)).)

---

<div class="post-metadata">

**Author:** ![alexd457](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@alexd457](https://discuss.kotlinlang.org/u/alexd457)\
**Post date:** [December 30, 2015, 4:46pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/5 "2015-12-30T16:46:59Z")

</div>

My dream is for `assert` to not be a mere debugging mechanism that doesn’t work in production, but to be an elegant way to check conditionals.

```
assert param > 0 : IllegalArgumentException("My message")

```

or even (just like the [`guard` statement in Swift](http://ericcerney.com/swift-guard-statement/))

```
assert (param is String) else {
      ... do stuff ...
}

// param has been smart cast to String
print (param.length)

```

Of course, this is still compatible with “power assertions” to give a friendlier message in an AssertionError when no `else` is specified.

---

<div class="post-metadata">

**Author:** ![pniederw1](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pniederw1](https://discuss.kotlinlang.org/u/pniederw1)\
**Post date:** [December 30, 2015, 7:21pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/6 "2015-12-30T19:21:50Z")

</div>

> [@alexd457](#):
>
> My dream is for assert to not be a mere debugging mechanism that doesn’t work in production

Which assert doesn’t work in production? Are you saying you want an assert that’s always enabled, and doesn’t require runtime assertions to be turned on for the JVM? (Currently, the assertion functions in `kotlin.test` fit that bill, and I can’t think of a reason not to use them in production code.)

---

<div class="post-metadata">

**Author:** ![alexd457](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@alexd457](https://discuss.kotlinlang.org/u/alexd457)\
**Post date:** [January 3, 2016, 12:04am UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/7 "2016-01-03T00:04:05Z")

</div>

It’s two things. The first, yes, is to be always-enabled. The second, is to throw meaningful exceptions.

---

<div class="post-metadata">

**Author:** ![Jire](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jire/32/1697_2.png) [@Jire](https://discuss.kotlinlang.org/u/Jire)\
**Post date:** [January 3, 2016, 1:48pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/8 "2016-01-03T13:48:29Z")

</div>

If all he wants is something similar, doesn’t this fit the bill without too much magic?

```
infix fun <T> T.assert(test: T): T = if (this != test) throw AssertionError() else this

fun Boolean.assert() = this assert true

```

Example usage:

```
val a = 10
val b = 9

91 assert a * b
a * b assert 91

(91 == a * b).assert()

```

Can also be used as an expression since `<T> assert(T)` returns itself.

```
val fifty = 10 * 5 assert 50

```

Enjoy and hope it helps 😀

---

<div class="post-metadata">

**Author:** ![Jire](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jire/32/1697_2.png) [@Jire](https://discuss.kotlinlang.org/u/Jire)\
**Post date:** [January 4, 2016, 12:44pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/9 "2016-01-04T12:44:39Z")

</div>

Nice suggestion. I will try to make something that gives you a similar feature to Swift’s guards.

EDIT: Would this suite your needs?

```
inline fun assert(predicate: () -> Boolean) = assert(predicate())

infix fun assert(result: Boolean) = Guard(result)

class Guard(val result: Boolean) {

	infix fun or(exception: Exception) {
		if (result)
			throw exception
	}

	infix fun or(body: () -> Any) {
		if (result)
			body()
	}

}

```

Example usage:

```
assert (param > 0) or IllegalArgumentException("My message")

```

And:

```
assert (param is String) or {
    ... do stuff ...
}

// unfortunately Kotlin doesn't do smart cast here (because it would slow compilation)

```

---

<div class="post-metadata">

**Author:** ![pabl0rg](https://avatars.discourse-cdn.com/v4/letter/p/e495f1/32.png) [@pabl0rg](https://discuss.kotlinlang.org/u/pabl0rg)\
**Post date:** [January 5, 2016, 2:03pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/10 "2016-01-05T14:03:20Z")

</div>

you may find this interesting [GitHub - npryce/hamkrest: Hamcrest for Kotlin](https://github.com/npryce/hamkrest)

---

<div class="post-metadata">

**Author:** ![alexd457](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@alexd457](https://discuss.kotlinlang.org/u/alexd457)\
**Post date:** [January 5, 2016, 8:54pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/11 "2016-01-05T20:54:02Z")

</div>

That’s pretty good. Would it be possible to throw AssertionError when no `or` is specified? Or at least a compile-time error?

---

<div class="post-metadata">

**Author:** ![liufsd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/liufsd/32/1731_2.png) [@liufsd](https://discuss.kotlinlang.org/u/liufsd)\
**Post date:** [September 29, 2016, 11:41am UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/12 "2016-09-29T11:41:40Z")

</div>

sometimes we have define the option type such as “var a: ClassA? = null” ,and then i need check a :  
val realA = a?:return  
if(realA.somePr == true){  
//do …  
}  
//…  
.but i want to use it like swift :  
guard let realA = a else {  
return  
}  
if(realA.somePr == true){  
//do …  
}  
//…  
how to impl it?

---

<div class="post-metadata">

**Author:** ![Jire](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jire/32/1697_2.png) [@Jire](https://discuss.kotlinlang.org/u/Jire)\
**Post date:** [October 25, 2016, 9:51pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/13 "2016-10-25T21:51:52Z")

</div>

Does this seem like it could work for you?

```
inline infix fun <T> T.or(orBlock: () -> T) = if (this == null) orBlock() else this

```

Here’s an example:

```
val a: String? = null
val realA = a or {
    "b" // imagine we actually did work instead
}
println(realA) // prints out "b"

```

---

<div class="post-metadata">

**Author:** ![liufsd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/liufsd/32/1731_2.png) [@liufsd](https://discuss.kotlinlang.org/u/liufsd)\
**Post date:** [November 18, 2016, 6:18am UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/14 "2016-11-18T06:18:59Z")

</div>

HI~ Thanks~

---

<div class="post-metadata">

**Author:** ![themightyjon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/themightyjon/32/5640_2.png) [@themightyjon](https://discuss.kotlinlang.org/u/themightyjon)\
**Post date:** [October 21, 2018, 4:55pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/15 "2018-10-21T16:55:28Z")

</div>

@pniederw1 have you seen the kotlin compiler plugin architecture? Saw a talk about this at KotlinConf 2018 and the first thing that came to mind was “OMG this could make Spock stuff possible” (big fan :D)

This was the talk:

[![](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/2/2f4c8fc4a5e6b362d5d36bb0e46f25da2f8515e6.jpeg "KotlinConf 2018 - Writing Your First Kotlin Compiler Plugin by Kevin Most") ](https://www.youtube.com/watch?v=w-GMlaziIyo)

> **[Writing Your First Kotlin Compiler Plugin](https://speakerdeck.com/kevinmost/writing-your-first-kotlin-compiler-plugin)**
>
> The Kotlin compiler plugin API gives us powerful features like Parcelize and the synthetic view accessor methods in kotlinx.android. These features could not be built using similar, but more limited, mechanisms, such as annotation processing.
> 
> The...

I was just starting to look into the feasibility of it, poking around thinking of power assertions and parameterised tests, since I miss those greatly. I see this was a while back, so wondering if you are still considering something towards this?

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [April 26, 2021, 12:25pm UTC](https://discuss.kotlinlang.org/t/support-for-groovy-style-power-assertion-in-kotlin/427/16 "2021-04-26T12:25:40Z")

</div>

The best thing I saw until now was this plugin:

> **[GitHub - bnorm/kotlin-power-assert: Kotlin compiler plugin to enable...](https://github.com/bnorm/kotlin-power-assert)**
>
> Kotlin compiler plugin to enable diagrammed function calls in the Kotlin programming language - GitHub - bnorm/kotlin-power-assert: Kotlin compiler plugin to enable diagrammed function calls in the...

The only limitation is that it only works with expressions evaluated to booleans. Fluent assertions don’t seem to work yet.
