Skip to content

Commit 892f619

Browse files
authored
Merge pull request #40 from bnorm/kotlin-1.5.0
Kotlin 1.5.0
2 parents 2a7693e + 7e4e2f9 commit 892f619

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

README.md

+84-18
Original file line numberDiff line numberDiff line change
@@ -68,54 +68,120 @@ assert(
6868
)
6969
```
7070

71+
## Beyond Assert
72+
73+
The plugin by default will transform `assert` function calls but can also
74+
transform other functions like `require`, `check`, `assertTrue`, and many, many
75+
more.
76+
77+
Functions which can be transformed have specific requirements. A function must
78+
have a form which allows taking a `String` or `() -> String` value as the last
79+
parameter. This can either be as an overload or the original function.
80+
81+
For example, the `assert` function has 2 definitions:
82+
* `fun assert(value: Boolean)`
83+
* `fun assert(value: Boolean, lazyMessage: () -> Any)`
84+
85+
If the first function definition is called, it will be transformed into calling
86+
the second definition with the diagram message supplied as the last parameter.
87+
If the second definition is called, it will be transformed into calling the same
88+
function but with the diagram message appended to the last parameter.
89+
90+
This transformed function call doesn't need to throw an exception either. See
91+
[Advanced Usage][#advanced-usage] for some examples.
92+
7193
## Gradle Plugin
7294

7395
Builds of the Gradle plugin are available through the
7496
[Gradle Plugin Portal][kotlin-power-assert-gradle].
7597

7698
```kotlin
7799
plugins {
78-
kotlin("multiplatform") version "1.4.30"
100+
kotlin("multiplatform") version "1.5.0"
79101
id("com.bnorm.power.kotlin-power-assert") version "0.7.0"
80102
}
81103
```
82104

83-
The plugin by default will transform `assert` function call but can also
84-
transform other functions like `require`, `check`, and/or `assertTrue`. The
85-
function needs to validate the Boolean expression evaluates to `true` and has a
86-
form which also takes a String or String producing lambda.
105+
The Gradle plugin allows configuring the functions which should be transformed
106+
with a list of fully-qualified function names.
87107

88108
```kotlin
89109
configure<com.bnorm.power.PowerAssertGradleExtension> {
90110
functions = listOf("kotlin.assert", "kotlin.test.assertTrue")
91111
}
92-
```
112+
```
93113

94114
## Kotlin IR
95115

96116
Using this compiler plugin only works if the code is compiled using Kotlin
97-
1.4.30 and IR is enabled. This plugin supports all IR based compiler backends:
98-
JVM, JS, and Native!
117+
1.5.0. This plugin supports all IR based compiler backends: JVM, JS, and Native!
118+
Only Kotlin/JS still uses the legacy compiler backend by default, use the
119+
following to make sure IR is enabled.
99120

100-
##### Kotlin/JVM
101121
```kotlin
102-
tasks.withType<KotlinCompile>().configureEach {
103-
kotlinOptions {
104-
useIR = true
122+
target {
123+
js(IR) {
105124
}
106125
}
107126
```
108127

109-
##### Kotlin/JS
128+
## Advanced Usage
129+
130+
### Function Call Tracing
131+
132+
Similar to Rust's `dbg!` macro, functions which take arbitrary parameters can
133+
be transformed. For example:
134+
110135
```kotlin
111-
target {
112-
js(IR) {
113-
}
136+
fun <T> dbg(value: T): T = value
137+
138+
fun <T> dbg(value: T, msg: String): T {
139+
println(msg)
140+
return value
141+
}
142+
143+
fun main() {
144+
println(dbg(1 + 2 + 3))
145+
}
146+
```
147+
148+
Prints the following:
149+
150+
```text
151+
dbg(1 + 2 + 3)
152+
| |
153+
| 6
154+
3
155+
6
156+
```
157+
158+
### Soft Assertion
159+
160+
To achieve soft assertion, the following template can be implemented:
161+
162+
```kotlin
163+
typealias LazyMessage = () -> Any
164+
165+
interface AssertScope {
166+
fun assert(assertion: Boolean, lazyMessage: LazyMessage? = null)
167+
}
168+
169+
fun <R> assertSoftly(block: AssertScope.() -> R): R = TODO("implement")
170+
```
171+
172+
You can then use the template as follows:
173+
174+
```kotlin
175+
val jane: Person = TODO()
176+
assertSoftly {
177+
assert(jane.firstName == "Jane")
178+
assert(jane.lastName == "Doe")
114179
}
115180
```
116181

117-
##### Kotlin/Native
118-
IR already enabled by default!
182+
A working example is [available][soft-assert-example] in this repository in the
183+
sample directory.
119184

120185
[groovy-power-assert]: https://groovy-lang.org/testing.html#_power_assertions
121186
[kotlin-power-assert-gradle]: https://plugins.gradle.org/plugin/com.bnorm.power.kotlin-power-assert
187+
[soft-assert-example]: https://github.com/bnorm/kotlin-power-assert/blob/master/sample/src/commonMain/kotlin/com/bnorm/power/AssertScope.kt

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
kotlin("jvm") version "1.4.30" apply false
2+
kotlin("jvm") version "1.5.0" apply false
33
id("org.jetbrains.dokka") version "0.10.0" apply false
44
id("com.gradle.plugin-publish") version "0.11.0" apply false
55
id("com.github.gmazzo.buildconfig") version "2.0.2" apply false

sample/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
kotlin("multiplatform") version "1.4.30"
2+
kotlin("multiplatform") version "1.5.0"
33
id("com.bnorm.power.kotlin-power-assert") version "0.7.0"
44
}
55

0 commit comments

Comments
 (0)