Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selfie #22

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

// com.diffplug.selfie:selfie

testImplementation("com.diffplug.selfie:selfie-runner-junit5:1.1.0")

// io.github.origin-energy:java-snapshot-testing

testImplementation("io.github.origin-energy:java-snapshot-testing-junit5:$javaSnapshotTestingVersion")
testImplementation("io.github.origin-energy:java-snapshot-testing-plugin-jackson:$javaSnapshotTestingVersion")
testImplementation("org.slf4j:slf4j-simple:2.0.12")
Expand All @@ -26,6 +32,8 @@ dependencies {
testImplementation("com.fasterxml.jackson.core:jackson-databind")
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

// common

testImplementation(platform("org.junit:junit-bom:5.10.2"))
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
Expand Down
25 changes: 16 additions & 9 deletions src/test/kotlin/org/rogervinas/MyImplTest.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.rogervinas

import au.com.origin.snapshots.Expect
import au.com.origin.snapshots.junit5.SnapshotExtension
import com.diffplug.selfie.Camera
import com.diffplug.selfie.Selfie
import com.diffplug.selfie.Snapshot
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
import kotlin.random.Random

@ExtendWith(SnapshotExtension::class)
internal class MyImplTest {

private lateinit var expect: Expect

internal class MyImplTest {
private val myImpl = MyImpl(
Random(seed=1234),
Clock.fixed(Instant.parse("2022-10-01T10:30:00.000Z"), ZoneId.of("UTC"))
Expand All @@ -25,12 +23,21 @@ internal class MyImplTest {
@ValueSource(ints = [1, 2, 3, 4, 5, 6, 7, 8, 9])
fun `should do something`(input: Int) {
val myResult = myImpl.doSomething(input)
expect.serializer("json").scenario("$input").toMatchSnapshot(myResult)
expectSelfie(myResult).toMatchDisk("$input")
}

@Test
fun `should do something more`() {
val myResult = myImpl.doSomethingMore()
expect.serializer("json").toMatchSnapshot(myResult)
expectSelfie(myResult).toMatchDisk()
}

private fun expectSelfie(actual: MyResult): Selfie.DiskSelfie {
return Selfie.expectSelfie(actual, JSON_CAMERA)
}
private val JSON_CAMERA = Camera<Any> { actual ->
val mapper = ObjectMapper()
mapper.findAndRegisterModules()
Snapshot.of(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(actual))
}
}
71 changes: 71 additions & 0 deletions src/test/kotlin/org/rogervinas/MyImplTest.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
╔═ should do something/1 ═╗
{
"oneInteger" : 1,
"oneDouble" : 3.7,
"oneString" : "a",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/2 ═╗
{
"oneInteger" : 2,
"oneDouble" : 7.4,
"oneString" : "aa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/3 ═╗
{
"oneInteger" : 3,
"oneDouble" : 11.100000000000001,
"oneString" : "aaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/4 ═╗
{
"oneInteger" : 4,
"oneDouble" : 14.8,
"oneString" : "aaaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/5 ═╗
{
"oneInteger" : 5,
"oneDouble" : 18.5,
"oneString" : "aaaaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/6 ═╗
{
"oneInteger" : 6,
"oneDouble" : 22.200000000000003,
"oneString" : "aaaaaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/7 ═╗
{
"oneInteger" : 7,
"oneDouble" : 25.900000000000002,
"oneString" : "aaaaaaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/8 ═╗
{
"oneInteger" : 8,
"oneDouble" : 29.6,
"oneString" : "aaaaaaaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something/9 ═╗
{
"oneInteger" : 9,
"oneDouble" : 33.300000000000004,
"oneString" : "aaaaaaaaa",
"oneDateTime" : [ 2022, 5, 3, 13, 46, 18 ]
}
╔═ should do something more ═╗
{
"oneInteger" : 345130239,
"oneDouble" : 0.6887620080485805,
"oneString" : "aaaaaaaaa",
"oneDateTime" : [ 2022, 10, 1, 10, 30 ]
}
╔═ [end of file] ═╗
98 changes: 0 additions & 98 deletions src/test/snapshots/org/rogervinas/MyImplTest.snap

This file was deleted.