[PowerAssert] Update base package

This commit is contained in:
Brian Norman
2023-11-17 10:23:40 -06:00
committed by Space Team
parent 6570d3fd55
commit d8167fe82b
24 changed files with 45 additions and 979 deletions
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert.gradle
open class PowerAssertGradleExtension {
var functions: List<String> = listOf("kotlin.assert")
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert.gradle
import org.gradle.api.Project
import org.gradle.api.provider.Provider
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
@@ -14,19 +14,19 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert
import com.bnorm.power.delegate.FunctionDelegate
import com.bnorm.power.delegate.LambdaFunctionDelegate
import com.bnorm.power.delegate.SamConversionLambdaFunctionDelegate
import com.bnorm.power.delegate.SimpleFunctionDelegate
import com.bnorm.power.diagram.IrTemporaryVariable
import com.bnorm.power.diagram.Node
import com.bnorm.power.diagram.SourceFile
import com.bnorm.power.diagram.buildDiagramNesting
import com.bnorm.power.diagram.buildDiagramNestingNullable
import com.bnorm.power.diagram.buildTree
import com.bnorm.power.diagram.irDiagramString
import org.jetbrains.kotlin.powerassert.delegate.FunctionDelegate
import org.jetbrains.kotlin.powerassert.delegate.LambdaFunctionDelegate
import org.jetbrains.kotlin.powerassert.delegate.SamConversionLambdaFunctionDelegate
import org.jetbrains.kotlin.powerassert.delegate.SimpleFunctionDelegate
import org.jetbrains.kotlin.powerassert.diagram.IrTemporaryVariable
import org.jetbrains.kotlin.powerassert.diagram.Node
import org.jetbrains.kotlin.powerassert.diagram.SourceFile
import org.jetbrains.kotlin.powerassert.diagram.buildDiagramNesting
import org.jetbrains.kotlin.powerassert.diagram.buildDiagramNestingNullable
import org.jetbrains.kotlin.powerassert.diagram.buildTree
import org.jetbrains.kotlin.powerassert.diagram.irDiagramString
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert
import com.bnorm.power.diagram.SourceFile
import org.jetbrains.kotlin.powerassert.diagram.SourceFile
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power.delegate
package org.jetbrains.kotlin.powerassert.delegate
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irCall
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package com.bnorm.power.delegate
package org.jetbrains.kotlin.powerassert.delegate
import com.bnorm.power.irLambda
import org.jetbrains.kotlin.powerassert.irLambda
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irReturn
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package com.bnorm.power.delegate
package org.jetbrains.kotlin.powerassert.delegate
import com.bnorm.power.irLambda
import org.jetbrains.kotlin.powerassert.irLambda
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irReturn
import org.jetbrains.kotlin.ir.builders.irSamConversion
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power.delegate
package org.jetbrains.kotlin.powerassert.delegate
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power.diagram
package org.jetbrains.kotlin.powerassert.diagram
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irBlock
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power.diagram
package org.jetbrains.kotlin.powerassert.diagram
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.expressions.IrCall
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package com.bnorm.power.diagram
package org.jetbrains.kotlin.powerassert.diagram
import com.bnorm.power.irString
import org.jetbrains.kotlin.powerassert.irString
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.SourceRangeInfo
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
@@ -35,10 +35,10 @@ import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
import org.jetbrains.kotlin.ir.expressions.addArgument
fun IrBuilderWithScope.irDiagramString(
sourceFile: SourceFile,
prefix: IrExpression? = null,
call: IrCall,
variables: List<IrTemporaryVariable>,
sourceFile: SourceFile,
prefix: IrExpression? = null,
call: IrCall,
variables: List<IrTemporaryVariable>,
): IrExpression {
val callInfo = sourceFile.getSourceRangeInfo(call)
val callIndent = callInfo.startColumnNumber
@@ -99,9 +99,9 @@ private data class ValueDisplay(
)
private fun IrTemporaryVariable.toValueDisplay(
fileSource: SourceFile,
callIndent: Int,
originalInfo: SourceRangeInfo,
fileSource: SourceFile,
callIndent: Int,
originalInfo: SourceRangeInfo,
): ValueDisplay {
val info = fileSource.getSourceRangeInfo(original)
var indent = info.startColumnNumber - callIndent
@@ -156,9 +156,9 @@ private fun IrTemporaryVariable.toValueDisplay(
* ```
*/
private fun findDisplayOffset(
sourceFile: SourceFile,
expression: IrExpression,
source: String,
sourceFile: SourceFile,
expression: IrExpression,
source: String,
): Int {
return when (expression) {
is IrMemberAccessExpression<*> -> memberAccessOffset(sourceFile, expression, source)
@@ -168,9 +168,9 @@ private fun findDisplayOffset(
}
private fun memberAccessOffset(
sourceFile: SourceFile,
expression: IrMemberAccessExpression<*>,
source: String,
sourceFile: SourceFile,
expression: IrMemberAccessExpression<*>,
source: String,
): Int {
when (expression.origin) {
// special case to handle `value != null`
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power.diagram
package org.jetbrains.kotlin.powerassert.diagram
import org.jetbrains.kotlin.ir.builders.IrStatementsBuilder
import org.jetbrains.kotlin.ir.builders.irGet
@@ -1,4 +1,4 @@
package com.bnorm.power.diagram
package org.jetbrains.kotlin.powerassert.diagram
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.ir.IrElement
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert
import com.google.auto.service.AutoService
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.bnorm.power
package org.jetbrains.kotlin.powerassert
import com.google.auto.service.AutoService
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.powerassert.PowerAssertIrGenerationExtension
val KEY_FUNCTIONS = CompilerConfigurationKey<List<String>>("fully-qualified function names")
@@ -1,162 +0,0 @@
/*
* Copyright (C) 2020 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import kotlin.test.Test
import kotlin.test.assertEquals
class ArithmeticExpressionTest {
@Test
fun `assertion with inline addition`() {
val actual = executeMainAssertion("assert(1 + 1 == 4)")
assertEquals(
"""
Assertion failed
assert(1 + 1 == 4)
| |
| false
2
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline subtraction`() {
val actual = executeMainAssertion("assert(3 - 1 == 4)")
assertEquals(
"""
Assertion failed
assert(3 - 1 == 4)
| |
| false
2
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline multiplication`() {
val actual = executeMainAssertion("assert(1 * 2 == 4)")
assertEquals(
"""
Assertion failed
assert(1 * 2 == 4)
| |
| false
2
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline division`() {
val actual = executeMainAssertion("assert(2 / 1 == 4)")
assertEquals(
"""
Assertion failed
assert(2 / 1 == 4)
| |
| false
2
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline prefix increment`() {
val actual = executeMainAssertion(
"""
var i = 1
assert(++i == 4)
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
assert(++i == 4)
| |
| false
2
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline postfix increment`() {
val actual = executeMainAssertion(
"""
var i = 1
assert(i++ == 4)
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
assert(i++ == 4)
| |
| false
1
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline prefix decrement`() {
val actual = executeMainAssertion(
"""
var i = 3
assert(--i == 4)
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
assert(--i == 4)
| |
| false
2
""".trimIndent(),
actual,
)
}
@Test
fun `assertion with inline postfix decrement`() {
val actual = executeMainAssertion(
"""
var i = 3
assert(i-- == 4)
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
assert(i-- == 4)
| |
| false
3
""".trimIndent(),
actual,
)
}
}
@@ -1,73 +0,0 @@
/*
* Copyright (C) 2022 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import kotlin.test.Test
import kotlin.test.assertEquals
class CastExpressionTest {
@Test
fun `instance check is correctly aligned`() {
val actual = executeMainAssertion("""assert(null is String)""")
assertEquals(
"""
Assertion failed
assert(null is String)
|
false
""".trimIndent(),
actual,
)
}
@Test
fun `negative instance check is correctly aligned`() {
val actual = executeMainAssertion("""assert("Hello, world!" !is String)""")
assertEquals(
"""
Assertion failed
assert("Hello, world!" !is String)
|
false
""".trimIndent(),
actual,
)
}
@Test
fun `smart casts do not duplicate output`() {
val actual = executeMainAssertion(
"""
val greeting: Any = "hello"
assert(greeting is String && greeting.length == 2)
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
assert(greeting is String && greeting.length == 2)
| | | | |
| | | | false
| | | 5
| | hello
| true
hello
""".trimIndent(),
actual,
)
}
}
@@ -1,92 +0,0 @@
/*
* Copyright (C) 2020 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.name.FqName
import java.io.OutputStream
import java.lang.reflect.InvocationTargetException
import kotlin.test.assertEquals
import kotlin.test.fail
private val DEFAULT_COMPILER_PLUGIN_REGISTRARS = arrayOf(
PowerAssertCompilerPluginRegistrar(setOf(FqName("kotlin.assert"))),
)
fun compile(
list: List<SourceFile>,
vararg compilerPluginRegistrars: CompilerPluginRegistrar = DEFAULT_COMPILER_PLUGIN_REGISTRARS,
): KotlinCompilation.Result {
return KotlinCompilation().apply {
sources = list
messageOutputStream = object : OutputStream() {
override fun write(b: Int) {
// black hole all writes
}
override fun write(b: ByteArray, off: Int, len: Int) {
// black hole all writes
}
}
this.compilerPluginRegistrars = compilerPluginRegistrars.toList()
inheritClassPath = true
}.compile()
}
fun executeAssertion(
@Language("kotlin") source: String,
vararg compilerPluginRegistrars: CompilerPluginRegistrar = DEFAULT_COMPILER_PLUGIN_REGISTRARS,
): String {
val result = compile(
listOf(SourceFile.kotlin("main.kt", source, trimIndent = false)),
*compilerPluginRegistrars,
)
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, result.messages)
val kClazz = result.classLoader.loadClass("MainKt")
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
try {
try {
main.invoke(null)
} catch (t: InvocationTargetException) {
throw t.cause!!
}
fail("should have thrown assertion")
} catch (t: Throwable) {
return t.message ?: ""
}
}
fun executeMainAssertion(mainBody: String) = executeAssertion(
"""
fun main() {
$mainBody
}
""",
)
fun assertMessage(
@Language("kotlin") source: String,
message: String,
vararg compilerPluginRegistrars: CompilerPluginRegistrar = DEFAULT_COMPILER_PLUGIN_REGISTRARS,
) {
val actual = executeAssertion(source, *compilerPluginRegistrars)
assertEquals(message, actual)
}
@@ -1,105 +0,0 @@
/*
* Copyright (C) 2021 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.name.FqName
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.fail
class DebugFunctionTest {
@Test
fun `debug function transformation`() {
val actual = executeMainDebug(
"""
dbg(1 + 2 + 3)
""".trimIndent(),
)
assertEquals(
"""
dbg(1 + 2 + 3)
| |
| 6
3
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `debug function transformation with message`() {
val actual = executeMainDebug(
"""
dbg(1 + 2 + 3, "Message:")
""".trimIndent(),
)
assertEquals(
"""
Message:
dbg(1 + 2 + 3, "Message:")
| |
| 6
3
""".trimIndent(),
actual.trim(),
)
}
}
private fun executeMainDebug(mainBody: String): String {
val file = SourceFile.kotlin(
name = "main.kt",
contents = """
fun <T> dbg(value: T): T = value
fun <T> dbg(value: T, msg: String): T {
throw RuntimeException("result:"+msg)
return value
}
fun main() {
$mainBody
}
""",
trimIndent = false,
)
val result = compile(listOf(file), PowerAssertCompilerPluginRegistrar(setOf(FqName("dbg"))))
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)
val kClazz = result.classLoader.loadClass("MainKt")
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
return getMainResult(main)
}
fun getMainResult(main: Method): String {
try {
main.invoke(null)
fail("main did not throw expected exception")
} catch (t: InvocationTargetException) {
with(t.cause) {
if (this is RuntimeException && message != null && message!!.startsWith("result:")) {
return message!!.substringAfter("result:")
}
}
throw t.cause!!
}
}
@@ -1,369 +0,0 @@
/*
* Copyright (C) 2022 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.name.FqName
import java.lang.reflect.InvocationTargetException
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.fail
class InfixFunctionTest {
@Test
fun `extension infix function call includes receiver`() {
val actual = runExtensionInfix(
"""
(1 + 1) mustEqual (2 + 4)
""".trimIndent(),
)
assertEquals(
"""
(1 + 1) mustEqual (2 + 4)
| |
| 6
2
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension infix function call with constant receiver`() {
val actual = runExtensionInfix(
"""
1 mustEqual (2 + 4)
""".trimIndent(),
)
assertEquals(
"""
1 mustEqual (2 + 4)
|
6
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension infix function call with constant parameter`() {
val actual = runExtensionInfix(
"""
(1 + 1) mustEqual 6
""".trimIndent(),
)
assertEquals(
"""
(1 + 1) mustEqual 6
|
2
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension infix function call with only constants`() {
val actual = runExtensionInfix(
"""
2 mustEqual 6
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension non-infix function call includes receiver`() {
val actual = runExtensionInfix(
"""
(1 + 1).mustEqual(2 + 4)
""".trimIndent(),
)
assertEquals(
"""
(1 + 1).mustEqual(2 + 4)
| |
| 6
2
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension non-infix function call with constant receiver`() {
val actual = runExtensionInfix(
"""
1.mustEqual(2 + 4)
""".trimIndent(),
)
assertEquals(
"""
1.mustEqual(2 + 4)
|
6
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension non-infix function call with constant parameter`() {
val actual = runExtensionInfix(
"""
(1 + 1).mustEqual(6)
""".trimIndent(),
)
assertEquals(
"""
(1 + 1).mustEqual(6)
|
2
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `extension non-infix function call with only constants`() {
val actual = runExtensionInfix(
"""
2.mustEqual(6)
""".trimIndent(),
)
assertEquals(
"""
Assertion failed
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch infix function call includes receiver`() {
val actual = runDispatchInfix(
"""
Wrapper(1 + 1) mustEqual (2 + 4)
""".trimIndent(),
)
assertEquals(
"""
Wrapper(1 + 1) mustEqual (2 + 4)
| | |
| | 6
| 2
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch infix function call with constant receiver`() {
val actual = runDispatchInfix(
"""
Wrapper(1) mustEqual (2 + 4)
""".trimIndent(),
)
assertEquals(
"""
Wrapper(1) mustEqual (2 + 4)
| |
| 6
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch infix function call with constant parameter`() {
val actual = runDispatchInfix(
"""
Wrapper(1 + 1) mustEqual 6
""".trimIndent(),
)
assertEquals(
"""
Wrapper(1 + 1) mustEqual 6
| |
| 2
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch infix function call with only constants`() {
val actual = runDispatchInfix(
"""
Wrapper(2) mustEqual 6
""".trimIndent(),
)
assertEquals(
"""
Wrapper(2) mustEqual 6
|
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch non-infix function call includes receiver`() {
val actual = runDispatchInfix(
"""
Wrapper(1 + 1).mustEqual(2 + 4)
""".trimIndent(),
)
assertEquals(
"""
Wrapper(1 + 1).mustEqual(2 + 4)
| | |
| | 6
| 2
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch non-infix function call with constant receiver`() {
val actual = runDispatchInfix(
"""
Wrapper(1).mustEqual(2 + 4)
""".trimIndent(),
)
assertEquals(
"""
Wrapper(1).mustEqual(2 + 4)
| |
| 6
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch non-infix function call with constant parameter`() {
val actual = runDispatchInfix(
"""
Wrapper(1 + 1).mustEqual(6)
""".trimIndent(),
)
assertEquals(
"""
Wrapper(1 + 1).mustEqual(6)
| |
| 2
Wrapper
""".trimIndent(),
actual.trim(),
)
}
@Test
fun `dispatch non-infix function call with only constants`() {
val actual = runDispatchInfix(
"""
Wrapper(2).mustEqual(6)
""".trimIndent(),
)
assertEquals(
"""
Wrapper(2).mustEqual(6)
|
Wrapper
""".trimIndent(),
actual.trim(),
)
}
private fun runExtensionInfix(mainBody: String): String {
return run(
SourceFile.kotlin(
name = "main.kt",
contents = """
infix fun <V> V.mustEqual(expected: V): Unit = assert(this == expected)
fun <V> V.mustEqual(expected: V, message: () -> String): Unit =
assert(this == expected, message)
fun main() {
$mainBody
}
""".trimIndent(),
trimIndent = false,
),
setOf(FqName("mustEqual")),
)
}
private fun runDispatchInfix(mainBody: String): String {
return run(
SourceFile.kotlin(
name = "main.kt",
contents = """
class Wrapper<V>(
private val value: V
) {
infix fun mustEqual(expected: V): Unit = assert(value == expected)
fun mustEqual(expected: V, message: () -> String): Unit =
assert(value == expected, message)
override fun toString() = "Wrapper"
}
fun main() {
$mainBody
}
""".trimIndent(),
trimIndent = false,
),
setOf(FqName("Wrapper.mustEqual")),
)
}
private fun run(file: SourceFile, fqNames: Set<FqName>, main: String = "MainKt"): String {
val result = compile(listOf(file), PowerAssertCompilerPluginRegistrar(fqNames))
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, "Failed with messages: " + result.messages)
val kClazz = result.classLoader.loadClass(main)
val mainMethod = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
try {
try {
mainMethod.invoke(null)
} catch (t: InvocationTargetException) {
throw t.cause!!
}
fail("should have thrown assertion")
} catch (t: Throwable) {
return t.message ?: ""
}
}
}
@@ -1,39 +0,0 @@
/*
* Copyright (C) 2023 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import kotlin.test.Test
class ObjectLiteralTest {
@Test
fun `internals of object literal should not be separated`() {
assertMessage(
"""
fun main() {
assert(object { override fun toString() = "ANONYMOUS" }.toString() == "toString()")
}""",
"""
Assertion failed
assert(object { override fun toString() = "ANONYMOUS" }.toString() == "toString()")
| | |
| | false
| ANONYMOUS
ANONYMOUS
""".trimIndent(),
)
}
}
@@ -1,52 +0,0 @@
/*
* Copyright (C) 2021 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import kotlin.test.Test
import kotlin.test.assertEquals
class RegexMatchTest {
@Test
fun `regex matches`() {
val actual = executeMainAssertion("""assert("Hello, World".matches("[A-Za-z]+".toRegex()))""")
assertEquals(
"""
Assertion failed
assert("Hello, World".matches("[A-Za-z]+".toRegex()))
| |
| [A-Za-z]+
false
""".trimIndent(),
actual,
)
}
@Test
fun `infix regex matches`() {
val actual = executeMainAssertion("""assert("Hello, World" matches "[A-Za-z]+".toRegex())""")
assertEquals(
"""
Assertion failed
assert("Hello, World" matches "[A-Za-z]+".toRegex())
| |
| [A-Za-z]+
false
""".trimIndent(),
actual,
)
}
}
@@ -1,43 +0,0 @@
/*
* Copyright (C) 2023 Brian Norman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bnorm.power
import kotlin.test.Test
class VarargTest {
@Test
fun `implicit array of vararg parameters is excluded from diagram`() {
assertMessage(
"""
fun main() {
var i = 0
assert(listOf("a", "b", "c") == listOf(i++, i++, i++))
}""",
"""
Assertion failed
assert(listOf("a", "b", "c") == listOf(i++, i++, i++))
| | | | | |
| | | | | 2
| | | | 1
| | | 0
| | [0, 1, 2]
| false
[a, b, c]
""".trimIndent(),
)
}
}