Skip to content

Commit

Permalink
Add a test to make sure the prism train is running.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Dec 20, 2023
1 parent c8d6fd7 commit e92df4c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.opentest4j.AssertionFailedError
import org.w3c.dom.NodeList
import org.xml.sax.InputSource

open class Harness(subproject: String) {
open class Harness(subproject: String, val onlyRunThisTest: Boolean = false) {
val subprojectFolder: Path
var settings = ""

Expand Down Expand Up @@ -196,22 +196,37 @@ open class Harness(subproject: String) {
.connect()
.use { connection ->
try {
val buildLauncher =
connection
.newBuild()
.setStandardError(System.err)
.setStandardOutput(System.out)
.forTasks(":${subprojectFolder.name}:$task")
.withArguments(
buildList<String> {
addAll(args)
add("--configuration-cache") // enabled vs disabled is 11s vs 24s
add("--stacktrace")
})
buildLauncher.run()
return null
if (onlyRunThisTest) {
connection
.newTestLauncher()
.setStandardError(System.err)
.setStandardOutput(System.out)
.withTaskAndTestClasses(
":${subprojectFolder.name}:$task", listOf("UT_${javaClass.simpleName}"))
.withArguments(
buildList<String> {
addAll(args)
add("--configuration-cache") // enabled vs disabled is 11s vs 24s
add("--stacktrace")
})
.run()
} else {
connection
.newBuild()
.setStandardError(System.err)
.setStandardOutput(System.out)
.forTasks(":${subprojectFolder.name}:$task")
.withArguments(
buildList<String> {
addAll(args)
add("--configuration-cache") // enabled vs disabled is 11s vs 24s
add("--stacktrace")
})
.run()
}
null
} catch (e: BuildException) {
return parseBuildException(task, e)
parseBuildException(task, e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2023 DiffPlug
*
* 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
*
* https://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.diffplug.selfie.junit5

import kotlin.test.Test
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.TestMethodOrder
import org.junitpioneer.jupiter.DisableIfTestFails

/** Simplest test for verifying read/write of a snapshot. */
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
@DisableIfTestFails
class PrismTrainTest : Harness("undertest-junit5", onlyRunThisTest = true) {
@Test @Order(1)
fun noSelfie() {
ut_snapshot().deleteIfExists()
ut_snapshot().assertDoesNotExist()
}

@Test @Order(2)
fun noTrain() {
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie ═╗
apple
╔═ [end of file] ═╗
"""
.trimIndent())
gradleReadSS()
}

@Test @Order(3)
fun withTrain() {
settings = "undertest.junit5.SettingsLensCount"
gradleReadSSFail()
// now let's write it
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie ═╗
apple
╔═ selfie[count] ═╗
5
╔═ [end of file] ═╗
"""
.trimIndent())
gradleReadSS()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package undertest.junit5

import com.diffplug.selfie.CompoundPrism
import com.diffplug.selfie.Snapshot
import com.diffplug.selfie.SnapshotLens
import com.diffplug.selfie.SnapshotValue
import com.diffplug.selfie.junit5.StandardSelfieSettings

class SettingsLensCount : StandardSelfieSettings() {
override fun setupPrismTrain(prismTrain: CompoundPrism) {
prismTrain
.forEverySnapshot()
.addLens(
object : SnapshotLens {
override val defaultLensName = "count"

override fun transform(testClass: String, key: String, snapshot: Snapshot) =
SnapshotValue.of(snapshot.value.valueString().count().toString())
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package undertest.junit5

import com.diffplug.selfie.Selfie.expectSelfie
import kotlin.test.Test

class UT_PrismTrainTest {
@Test fun selfie() {
expectSelfie("apple").toMatchDisk()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
╔═ selfie ═╗
apple
╔═ [end of file] ═╗

0 comments on commit e92df4c

Please sign in to comment.