This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds com.soywiz.korio.test.assertEqualsJvmFileReference (#495)
It uses dynamic access to assertEquals, to not require including kotlin-test or create separate artifacts just for this testing tool
- Loading branch information
1 parent
6ebcc09
commit 57e0417
Showing
6 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 2 additions & 9 deletions
11
korge/src/jvmTest/kotlin/com/soywiz/korge/test/assertEqualsFileReference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,8 @@ | ||
package com.soywiz.korge.test | ||
|
||
import java.io.* | ||
import kotlin.test.* | ||
import com.soywiz.korio.test.* | ||
|
||
// Use ./gradlew jvmTestFix to update these files | ||
fun assertEqualsFileReference(path: String, content: String) { | ||
val file = File("src/jvmTest/resources/$path").absoluteFile | ||
if (System.getenv("UPDATE_TEST_REF") == "true") { | ||
file.parentFile.mkdirs() | ||
file.writeText(content) | ||
} | ||
//if (!file.exists()) assertTrue(false, "File '$file' doesn't exists") | ||
assertEquals(file.takeIf { it.exists() }?.readText(), content) | ||
assertEqualsJvmFileReference(path, content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
korio/src/jvmMain/kotlin/com/soywiz/korio/test/assertEqualsJvmFileReference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.soywiz.korio.test | ||
|
||
import com.soywiz.korio.dynamic.* | ||
import java.io.* | ||
|
||
/** | ||
* Checks that a string matches a file in `src/jvmTest/resources`. | ||
* When environment variable UPDATE_TEST_REF=true, instead of checking, | ||
* this functions updates the file with the expected content. | ||
* | ||
* This is similar to jest's snapshot testing: <https://jestjs.io/docs/snapshot-testing> | ||
* | ||
* --- | ||
* | ||
* To simplify its usage, it is recommended to define a `jvmTestFix` gradle task as follows: | ||
* | ||
* Use ./gradlew jvmTestFix to update these files | ||
* | ||
* jvmTestFix gradle task should add environment("UPDATE_TEST_REF", "true") | ||
* | ||
* ```kotlin | ||
* val jvmTestFix = tasks.create("jvmTestFix", Test::class) { | ||
* group = "verification" | ||
* environment("UPDATE_TEST_REF", "true") | ||
* testClassesDirs = jvmTest.testClassesDirs | ||
* classpath = jvmTest.classpath | ||
* bootstrapClasspath = jvmTest.bootstrapClasspath | ||
* systemProperty("java.awt.headless", "true") | ||
* } | ||
* ``` | ||
*/ | ||
fun assertEqualsJvmFileReference(path: String, content: String) { | ||
val file = File("src/jvmTest/resources/$path").absoluteFile | ||
if (System.getenv("UPDATE_TEST_REF") == "true") { | ||
file.parentFile.mkdirs() | ||
file.writeText(content) | ||
} | ||
|
||
val message: String? = null | ||
val expected: String = file.takeIf { it.exists() }?.readText() ?: "" | ||
val actual: String = content | ||
|
||
Dyn.global["kotlin.test.AssertionsKt"] | ||
.dynamicInvokeOrThrow("getAsserter") | ||
.dynamicInvokeOrThrow("assertEquals", message, expected, actual) | ||
} |