Prepare kotlin-power-assert files for git merge with Kotlin

This commit is contained in:
Brian Norman
2023-11-22 15:53:49 -06:00
parent 9d2ff1f2de
commit f9bcd697da
62 changed files with 1 additions and 5346 deletions
@@ -0,0 +1,162 @@
/*
* 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,
)
}
}
@@ -0,0 +1,73 @@
/*
* 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,
)
}
}
@@ -0,0 +1,92 @@
/*
* 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)
}
@@ -0,0 +1,105 @@
/*
* 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!!
}
}
@@ -0,0 +1,369 @@
/*
* 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 ?: ""
}
}
}
@@ -0,0 +1,39 @@
/*
* 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(),
)
}
}
@@ -0,0 +1,52 @@
/*
* 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,
)
}
}
@@ -0,0 +1,43 @@
/*
* 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(),
)
}
}