-
Notifications
You must be signed in to change notification settings - Fork 6k
KT-81448: Support wildcards for power-assert function selection #5519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
51f33e4
74b666a
82f96f6
96847db
66906ca
88790b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.compiler.plugin.CliOption | |
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor | ||
import org.jetbrains.kotlin.config.CompilerConfiguration | ||
import org.jetbrains.kotlin.powerassert.PowerAssertPluginNames.PLUGIN_ID | ||
import java.util.regex.Pattern | ||
|
||
class PowerAssertCommandLineProcessor : CommandLineProcessor { | ||
override val pluginId: String get() = PLUGIN_ID | ||
|
@@ -36,6 +37,13 @@ class PowerAssertCommandLineProcessor : CommandLineProcessor { | |
required = false, // TODO required for Kotlin/JS | ||
allowMultipleOccurrences = true, | ||
), | ||
CliOption( | ||
optionName = "functionRegex", | ||
valueDescription = "regex matched against a function full-qualified name. Format is '\$flagsInt:\$pattern'.", | ||
description = "regex matched against the fully qualified path of function to intercept", | ||
required = false, // TODO required for Kotlin/JS | ||
allowMultipleOccurrences = true, | ||
), | ||
) | ||
|
||
override fun processOption( | ||
|
@@ -45,6 +53,11 @@ class PowerAssertCommandLineProcessor : CommandLineProcessor { | |
) { | ||
return when (option.optionName) { | ||
"function" -> configuration.add(KEY_FUNCTIONS, value) | ||
"functionRegex" -> { | ||
val flags = value.substringBefore(':', "").toIntOrNull() ?: 0 | ||
val pattern = value.substringAfter(':') | ||
configuration.add(KEY_FUNCTION_REGEXES, Pattern.compile(pattern, flags).toRegex()) | ||
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was mostly because we're using pattern on both ends anyways, and this is necessary to get an equal pattern on the other end. But looking over the flags, there aren't any that I think would be useful here, so I'm not opposed to dropping it. |
||
} | ||
else -> error("Unexpected config option ${option.optionName}") | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
assertTrue: --- | ||
assertTrue(booleanValue) | ||
| | ||
false | ||
==> expected: <true> but was: <false>--- | ||
assertEquals: --- | ||
assertEquals(a, b) | ||
| | | ||
| 5 | ||
3 | ||
==> expected: <3> but was: <5>--- | ||
assertFalse: --- | ||
assertFalse(booleanValue) | ||
| | ||
true | ||
==> expected: <false> but was: <true>--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// FUNCTION_REGEX: org\.junit\.jupiter\.api\.Assertions\.assert.* | ||
// WITH_JUNIT5 | ||
|
||
import org.junit.jupiter.api.Assertions | ||
|
||
fun box(): String = runAll( | ||
"assertTrue" to { test1() }, | ||
"assertEquals" to { test2() }, | ||
"assertFalse" to { test3() }, | ||
) | ||
|
||
fun test1() { | ||
val booleanValue = false | ||
Assertions.assertTrue(booleanValue) | ||
} | ||
|
||
|
||
fun test2() { | ||
val a = 3 | ||
val b = 5 | ||
Assertions.assertEquals(a, b) | ||
} | ||
|
||
fun test3() { | ||
val booleanValue = true | ||
Assertions.assertFalse(booleanValue) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
assertTrue: --- | ||
assertTrue(booleanValue) | ||
| | ||
false | ||
--- | ||
assertEquals: --- | ||
assertEquals(a, b) | ||
| | | ||
| 5 | ||
3 | ||
. Expected <3>, actual <5>.--- | ||
assertFalse: --- | ||
assertFalse(booleanValue) | ||
| | ||
true | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// FUNCTION_REGEX: kotlin\.test\.assert.* | ||
|
||
import kotlin.test.* | ||
|
||
fun box(): String = runAll( | ||
"assertTrue" to { test1() }, | ||
"assertEquals" to { test2() }, | ||
"assertFalse" to { test3() }, | ||
) | ||
|
||
fun test1() { | ||
val booleanValue = false | ||
assertTrue(booleanValue) | ||
} | ||
|
||
|
||
fun test2() { | ||
val a = 3 | ||
val b = 5 | ||
assertEquals(a, b) | ||
} | ||
|
||
fun test3() { | ||
val booleanValue = true | ||
assertFalse(booleanValue) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I think it might just be better to make this a Set of Strings. Pattern might logically be correct, but also seems a little too specific. Maybe @Tapchicoma has more specific thoughts here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see why you might want to use Stirng. There's two primary reasons I went with pattern:
Pattern.compile("...")
(IntelliJ's language annotation can't be applied to types, and applying it to the property doesn't work). This is particularly useful when escaping.
s in package names.