From edc5a5a94fbc43648a30a4067ec3f6bbc3485712 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 8 Sep 2024 16:03:57 +0200 Subject: [PATCH] improve data driven unit tests to use kotest withData() --- .../io_kotest_framework_datatest.xml | 23 ++++++ codeGenCpu6502/build.gradle | 1 + codeGenCpu6502/codeGenCpu6502.iml | 1 + codeGenIntermediate/build.gradle | 1 + codeGenIntermediate/codeGenIntermediate.iml | 1 + compiler/test/ModuleImporterTests.kt | 2 +- compiler/test/ProjectConfig.kt | 2 +- compiler/test/TestBuiltinFunctions.kt | 2 +- compiler/test/TestCallgraph.kt | 2 +- compiler/test/TestCompilerOnCharLit.kt | 2 +- compiler/test/TestCompilerOnExamples.kt | 79 ++++++++++--------- .../test/TestCompilerOnImportsAndIncludes.kt | 2 +- compiler/test/TestCompilerOnRanges.kt | 43 +++++----- compiler/test/TestCompilerOptionLibdirs.kt | 2 +- compiler/test/TestGoldenRam.kt | 2 +- .../TestImportedModulesOrderAndOptions.kt | 2 +- compiler/test/TestLaunchEmu.kt | 2 +- compiler/test/TestMemory.kt | 2 +- compiler/test/TestNumbers.kt | 2 +- compiler/test/TestNumericLiteral.kt | 2 +- compiler/test/TestOptimization.kt | 2 +- compiler/test/TestPtNumber.kt | 2 +- compiler/test/TestScoping.kt | 2 +- compiler/test/TestStringEncodings.kt | 2 +- compiler/test/TestSubroutines.kt | 2 +- compiler/test/TestSymbolTable.kt | 2 +- compiler/test/TestTypecasts.kt | 2 +- compiler/test/TestZeropage.kt | 2 +- compiler/test/helpers/mapCombinations.kt | 35 -------- compiler/test/helpers_pathsTests.kt | 2 +- docs/source/todo.rst | 4 - intermediate/build.gradle | 1 + intermediate/intermediate.iml | 1 + virtualmachine/build.gradle | 1 + virtualmachine/virtualmachine.iml | 3 +- 35 files changed, 115 insertions(+), 123 deletions(-) create mode 100644 .idea/libraries/io_kotest_framework_datatest.xml diff --git a/.idea/libraries/io_kotest_framework_datatest.xml b/.idea/libraries/io_kotest_framework_datatest.xml new file mode 100644 index 000000000..d00292090 --- /dev/null +++ b/.idea/libraries/io_kotest_framework_datatest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/codeGenCpu6502/build.gradle b/codeGenCpu6502/build.gradle index f978df784..e50bd0dc2 100644 --- a/codeGenCpu6502/build.gradle +++ b/codeGenCpu6502/build.gradle @@ -28,6 +28,7 @@ dependencies { implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.0" testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' + testImplementation 'io.kotest:kotest-framework-datatest:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/codeGenCpu6502/codeGenCpu6502.iml b/codeGenCpu6502/codeGenCpu6502.iml index 7061784ab..ec4851d05 100644 --- a/codeGenCpu6502/codeGenCpu6502.iml +++ b/codeGenCpu6502/codeGenCpu6502.iml @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/codeGenIntermediate/build.gradle b/codeGenIntermediate/build.gradle index 4d3c3dca4..35425a75f 100644 --- a/codeGenIntermediate/build.gradle +++ b/codeGenIntermediate/build.gradle @@ -31,6 +31,7 @@ dependencies { testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' + testImplementation 'io.kotest:kotest-framework-datatest:5.9.1' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/codeGenIntermediate/codeGenIntermediate.iml b/codeGenIntermediate/codeGenIntermediate.iml index 9626d27b3..be7cc4cb2 100644 --- a/codeGenIntermediate/codeGenIntermediate.iml +++ b/codeGenIntermediate/codeGenIntermediate.iml @@ -14,6 +14,7 @@ + \ No newline at end of file diff --git a/compiler/test/ModuleImporterTests.kt b/compiler/test/ModuleImporterTests.kt index 8e008b7e6..9dc63d68a 100644 --- a/compiler/test/ModuleImporterTests.kt +++ b/compiler/test/ModuleImporterTests.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import com.github.michaelbull.result.getErrorOrElse import com.github.michaelbull.result.getOrElse diff --git a/compiler/test/ProjectConfig.kt b/compiler/test/ProjectConfig.kt index 7ebaf4402..7577b62cc 100644 --- a/compiler/test/ProjectConfig.kt +++ b/compiler/test/ProjectConfig.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.core.config.AbstractProjectConfig import io.kotest.core.spec.SpecExecutionOrder diff --git a/compiler/test/TestBuiltinFunctions.kt b/compiler/test/TestBuiltinFunctions.kt index a2d6e4116..699607628 100644 --- a/compiler/test/TestBuiltinFunctions.kt +++ b/compiler/test/TestBuiltinFunctions.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe diff --git a/compiler/test/TestCallgraph.kt b/compiler/test/TestCallgraph.kt index b15abff02..3443ab8c6 100644 --- a/compiler/test/TestCallgraph.kt +++ b/compiler/test/TestCallgraph.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestCompilerOnCharLit.kt b/compiler/test/TestCompilerOnCharLit.kt index d345fbda9..3e6bac15d 100644 --- a/compiler/test/TestCompilerOnCharLit.kt +++ b/compiler/test/TestCompilerOnCharLit.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.fail import io.kotest.assertions.withClue diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index 6745d03b9..bc79f657e 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -1,6 +1,7 @@ -package prog8tests +package prog8tests.compiler import io.kotest.core.spec.style.FunSpec +import io.kotest.datatest.withData import io.kotest.matchers.shouldNotBe import prog8.code.core.ICompilationTarget import prog8.code.target.* @@ -11,7 +12,6 @@ import prog8tests.helpers.* import java.nio.file.Path import kotlin.io.path.absolute import kotlin.io.path.exists -import kotlin.io.path.readText /** @@ -89,13 +89,14 @@ class TestCompilerOnExamplesC64: FunSpec({ listOf(false, true) ) - onlyC64.forEach { - val (source, optimize) = it - val target = C64Target() - val (displayName, filepath) = prepareTestFiles(source, optimize, target) - test(displayName) { - compileTheThing(filepath, optimize, target) shouldNotBe null - } + val target = C64Target() + withData( + nameFn = { it.second.first }, + onlyC64.map { it to prepareTestFiles(it.first, it.second, target) } + ) { (params, prep) -> + val filepath = prep.second + val optimize = params.second + compileTheThing(filepath, optimize, target) shouldNotBe null } }) @@ -145,13 +146,14 @@ class TestCompilerOnExamplesCx16: FunSpec({ listOf(false, true) ) - onlyCx16.forEach { - val (source, optimize) = it - val target = Cx16Target() - val (displayName, filepath) = prepareTestFiles(source, optimize, target) - test(displayName) { - compileTheThing(filepath, optimize, target) shouldNotBe null - } + val target = Cx16Target() + withData( + nameFn = { it.second.first }, + onlyCx16.map { it to prepareTestFiles(it.first, it.second, target) } + ) { (params, prep) -> + val filepath = prep.second + val optimize = params.second + compileTheThing(filepath, optimize, target) shouldNotBe null } }) @@ -181,40 +183,39 @@ class TestCompilerOnExamplesBothC64andCx16: FunSpec({ "tehtriz", "textelite", ), - listOf(false, true) + listOf(false, true), + listOf(C64Target(), Cx16Target()) ) - bothCx16AndC64.forEach { - val (source, optimize) = it - val c64target = C64Target() - val cx16target = Cx16Target() - val (displayNameC64, filepathC64) = prepareTestFiles(source, optimize, c64target) - val (displayNameCx16, filepathCx16) = prepareTestFiles(source, optimize, cx16target) - test(displayNameC64) { - compileTheThing(filepathC64, optimize, c64target) shouldNotBe null - } - test(displayNameCx16) { - compileTheThing(filepathCx16, optimize, cx16target) shouldNotBe null - } + withData( + nameFn = { it.third.first }, + bothCx16AndC64.map { Triple(it.second, it.third, prepareTestFiles(it.first, it.second, it.third)) } + ) { params -> + val filepath = params.third.second + val optimize = params.first + compileTheThing(filepath, optimize, params.second) shouldNotBe null } }) class TestCompilerOnExamplesVirtual: FunSpec({ - val onlyVirtual = listOf( + val onlyVirtual = cartesianProduct( + listOf( "bouncegfx", "bsieve", "pixelshader", "sincos" - ) + ), + listOf(false, true) + ) - onlyVirtual.forEach { - val target = VMTarget() - val (displayName, filepath) = prepareTestFiles(it, false, target) - test(displayName) { - val src = filepath.readText() - compileText(target, false, src, writeAssembly = true) shouldNotBe null - compileText(target, true, src, writeAssembly = true) shouldNotBe null - } + val target = VMTarget() + withData( + nameFn = { it.second.first }, + onlyVirtual.map { it to prepareTestFiles(it.first, it.second, target) } + ) { (params, prep) -> + val filepath = prep.second + val optimize = params.second + compileTheThing(filepath, optimize, target) shouldNotBe null } }) diff --git a/compiler/test/TestCompilerOnImportsAndIncludes.kt b/compiler/test/TestCompilerOnImportsAndIncludes.kt index 57601db20..0e5e65931 100644 --- a/compiler/test/TestCompilerOnImportsAndIncludes.kt +++ b/compiler/test/TestCompilerOnImportsAndIncludes.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestCompilerOnRanges.kt b/compiler/test/TestCompilerOnRanges.kt index a984b1c27..6d7d5925a 100644 --- a/compiler/test/TestCompilerOnRanges.kt +++ b/compiler/test/TestCompilerOnRanges.kt @@ -1,7 +1,8 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec +import io.kotest.datatest.withData import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import io.kotest.matchers.string.shouldContain @@ -93,26 +94,26 @@ class TestCompilerOnRanges: FunSpec({ } context("floatArrayInitializerWithRange") { - val combos = cartesianProduct( - listOf("", "42", "41"), // sizeInDecl - listOf("%import floats", ""), // optEnableFloats - listOf(Cx16Target(), C64Target()), // platform - listOf(false, true) // optimize - ) - - combos.forEach { - val (sizeInDecl, optEnableFloats, platform, optimize) = it - val displayName = - when (sizeInDecl) { + withData( + nameFn = { + when (it.first) { "" -> "no" "42" -> "correct" else -> "wrong" } + " array size given" + - ", " + (if (optEnableFloats == "") "without" else "with") + " %option enable_floats" + - ", ${platform.name}, optimize: $optimize" + ", " + (if (it.second == "") "without" else "with") + " %option enable_floats" + + ", ${it.third.name}, optimize: ${it.fourth}" + }, + cartesianProduct( + listOf("", "42", "41"), // sizeInDecl + listOf("%import floats", ""), // optEnableFloats + listOf(Cx16Target(), C64Target()), // platform + listOf(false, true) // optimize + ) + ) { seq -> + val (sizeInDecl, optEnableFloats, platform, optimize) = seq - test(displayName) { - val result = compileText(platform, optimize, """ + val result = compileText(platform, optimize, """ $optEnableFloats main { sub start() { @@ -121,12 +122,10 @@ class TestCompilerOnRanges: FunSpec({ } } """) - if (optEnableFloats != "" && (sizeInDecl=="" || sizeInDecl=="42")) - result shouldNotBe null - else - result shouldBe null - - } + if (optEnableFloats != "" && (sizeInDecl=="" || sizeInDecl=="42")) + result shouldNotBe null + else + result shouldBe null } } diff --git a/compiler/test/TestCompilerOptionLibdirs.kt b/compiler/test/TestCompilerOptionLibdirs.kt index 717d6108a..f0961cf7c 100644 --- a/compiler/test/TestCompilerOptionLibdirs.kt +++ b/compiler/test/TestCompilerOptionLibdirs.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldNotBe diff --git a/compiler/test/TestGoldenRam.kt b/compiler/test/TestGoldenRam.kt index 6fd17f899..aa8be1832 100644 --- a/compiler/test/TestGoldenRam.kt +++ b/compiler/test/TestGoldenRam.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import com.github.michaelbull.result.expectError import com.github.michaelbull.result.getOrThrow diff --git a/compiler/test/TestImportedModulesOrderAndOptions.kt b/compiler/test/TestImportedModulesOrderAndOptions.kt index 04089e8fa..9bdd37d62 100644 --- a/compiler/test/TestImportedModulesOrderAndOptions.kt +++ b/compiler/test/TestImportedModulesOrderAndOptions.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestLaunchEmu.kt b/compiler/test/TestLaunchEmu.kt index 564d6fd04..ddd2df089 100644 --- a/compiler/test/TestLaunchEmu.kt +++ b/compiler/test/TestLaunchEmu.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.core.spec.style.FunSpec import prog8.code.target.VMTarget diff --git a/compiler/test/TestMemory.kt b/compiler/test/TestMemory.kt index 9c1280994..b8790fd48 100644 --- a/compiler/test/TestMemory.kt +++ b/compiler/test/TestMemory.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestNumbers.kt b/compiler/test/TestNumbers.kt index fa445d919..f033f6074 100644 --- a/compiler/test/TestNumbers.kt +++ b/compiler/test/TestNumbers.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestNumericLiteral.kt b/compiler/test/TestNumericLiteral.kt index 68007fd60..b714cfe21 100644 --- a/compiler/test/TestNumericLiteral.kt +++ b/compiler/test/TestNumericLiteral.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestOptimization.kt b/compiler/test/TestOptimization.kt index 7f760f0fb..d08285bc5 100644 --- a/compiler/test/TestOptimization.kt +++ b/compiler/test/TestOptimization.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestPtNumber.kt b/compiler/test/TestPtNumber.kt index fd95cad6b..58d692fd3 100644 --- a/compiler/test/TestPtNumber.kt +++ b/compiler/test/TestPtNumber.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestScoping.kt b/compiler/test/TestScoping.kt index 239d15040..d83b36529 100644 --- a/compiler/test/TestScoping.kt +++ b/compiler/test/TestScoping.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestStringEncodings.kt b/compiler/test/TestStringEncodings.kt index c988dbbc1..9540ec057 100644 --- a/compiler/test/TestStringEncodings.kt +++ b/compiler/test/TestStringEncodings.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import com.github.michaelbull.result.Ok import com.github.michaelbull.result.expectError diff --git a/compiler/test/TestSubroutines.kt b/compiler/test/TestSubroutines.kt index 682fbf177..b5bcff5db 100644 --- a/compiler/test/TestSubroutines.kt +++ b/compiler/test/TestSubroutines.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestSymbolTable.kt b/compiler/test/TestSymbolTable.kt index b3d671214..bf6b1c051 100644 --- a/compiler/test/TestSymbolTable.kt +++ b/compiler/test/TestSymbolTable.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.fail import io.kotest.core.spec.style.FunSpec diff --git a/compiler/test/TestTypecasts.kt b/compiler/test/TestTypecasts.kt index 4a2636026..236e28790 100644 --- a/compiler/test/TestTypecasts.kt +++ b/compiler/test/TestTypecasts.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.ints.shouldBeGreaterThan diff --git a/compiler/test/TestZeropage.kt b/compiler/test/TestZeropage.kt index d4ce81c86..cd7c2a2b3 100644 --- a/compiler/test/TestZeropage.kt +++ b/compiler/test/TestZeropage.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import com.github.michaelbull.result.expectError import com.github.michaelbull.result.getOrElse diff --git a/compiler/test/helpers/mapCombinations.kt b/compiler/test/helpers/mapCombinations.kt index 3342a9905..64c9ecda7 100644 --- a/compiler/test/helpers/mapCombinations.kt +++ b/compiler/test/helpers/mapCombinations.kt @@ -29,38 +29,3 @@ fun cartesianProduct( yield(Product(a, b, c, d)) } } - -fun mapCombinations(dim1: Iterable, dim2: Iterable, combine2: (A, B) -> R) = - sequence { - for (a in dim1) - for (b in dim2) - yield(combine2(a, b)) - }.toList() - -fun mapCombinations( - dim1: Iterable, - dim2: Iterable, - dim3: Iterable, - combine3: (A, B, C) -> R -) = - sequence { - for (a in dim1) - for (b in dim2) - for (c in dim3) - yield(combine3(a, b, c)) - }.toList() - -fun mapCombinations( - dim1: Iterable, - dim2: Iterable, - dim3: Iterable, - dim4: Iterable, - combine4: (A, B, C, D) -> R -) = - sequence { - for (a in dim1) - for (b in dim2) - for (c in dim3) - for (d in dim4) - yield(combine4(a, b, c, d)) - }.toList() diff --git a/compiler/test/helpers_pathsTests.kt b/compiler/test/helpers_pathsTests.kt index e8f814ebc..aa6358836 100644 --- a/compiler/test/helpers_pathsTests.kt +++ b/compiler/test/helpers_pathsTests.kt @@ -1,4 +1,4 @@ -package prog8tests +package prog8tests.compiler import io.kotest.assertions.throwables.shouldThrow import io.kotest.assertions.withClue diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 8a253d06b..181a4fc9c 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,10 +1,6 @@ TODO ==== -convert example tests that loop over arrays, to data driven withData() -move those tests to a new separated module in the project so they're not always ran as part of compiler:test - - Improve register load order in subroutine call args assignments: in certain situations, the "wrong" order of evaluation of function call arguments is done which results in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!) diff --git a/intermediate/build.gradle b/intermediate/build.gradle index 24e42048f..0ef1d482e 100644 --- a/intermediate/build.gradle +++ b/intermediate/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation project(':codeCore') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' + testImplementation 'io.kotest:kotest-framework-datatest:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/intermediate/intermediate.iml b/intermediate/intermediate.iml index 296a24a8e..207c859b1 100644 --- a/intermediate/intermediate.iml +++ b/intermediate/intermediate.iml @@ -13,5 +13,6 @@ + \ No newline at end of file diff --git a/virtualmachine/build.gradle b/virtualmachine/build.gradle index 41c1528fc..33a952d7c 100644 --- a/virtualmachine/build.gradle +++ b/virtualmachine/build.gradle @@ -28,6 +28,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:2.0.0" testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.9.1' + testImplementation 'io.kotest:kotest-framework-datatest:5.9.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/virtualmachine/virtualmachine.iml b/virtualmachine/virtualmachine.iml index 6cf1ccbbb..43d5b4bbd 100644 --- a/virtualmachine/virtualmachine.iml +++ b/virtualmachine/virtualmachine.iml @@ -12,7 +12,8 @@ - + + \ No newline at end of file