@@ -68,54 +68,120 @@ assert(
68
68
)
69
69
```
70
70
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
+
71
93
## Gradle Plugin
72
94
73
95
Builds of the Gradle plugin are available through the
74
96
[ Gradle Plugin Portal] [ kotlin-power-assert-gradle ] .
75
97
76
98
``` kotlin
77
99
plugins {
78
- kotlin(" multiplatform" ) version " 1.4.30 "
100
+ kotlin(" multiplatform" ) version " 1.5.0 "
79
101
id(" com.bnorm.power.kotlin-power-assert" ) version " 0.7.0"
80
102
}
81
103
```
82
104
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.
87
107
88
108
``` kotlin
89
109
configure< com.bnorm.power.PowerAssertGradleExtension > {
90
110
functions = listOf (" kotlin.assert" , " kotlin.test.assertTrue" )
91
111
}
92
- ```
112
+ ```
93
113
94
114
## Kotlin IR
95
115
96
116
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.
99
120
100
- ##### Kotlin/JVM
101
121
``` kotlin
102
- tasks.withType<KotlinCompile >().configureEach {
103
- kotlinOptions {
104
- useIR = true
122
+ target {
123
+ js(IR ) {
105
124
}
106
125
}
107
126
```
108
127
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
+
110
135
``` 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" )
114
179
}
115
180
```
116
181
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.
119
184
120
185
[ groovy-power-assert ] : https://groovy-lang.org/testing.html#_power_assertions
121
186
[ 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
0 commit comments