From c068c7c820f961d6fa0ef119445cda6902cd5d22 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Sat, 10 Feb 2024 09:43:46 +0000 Subject: [PATCH] Extract snapshots integration --- .../example/src/test/scala/ExampleTests.scala | 28 --------------- .../src/test/scala/SnapshotsIntegration.scala | 34 +++++++++++++++++++ 2 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 modules/example/src/test/scala/SnapshotsIntegration.scala diff --git a/modules/example/src/test/scala/ExampleTests.scala b/modules/example/src/test/scala/ExampleTests.scala index ed01c55..390e94d 100644 --- a/modules/example/src/test/scala/ExampleTests.scala +++ b/modules/example/src/test/scala/ExampleTests.scala @@ -7,34 +7,6 @@ import munit.Assertions import munit.FunSuite import example.Snapshots -// This is a sample integration for Munit -trait SnapshotsIntegration { - self: FunSuite => - def assertSnapshot(name: String, contents: String) = { - Snapshots.read(name) match { - case None => - Snapshots.recordChanges( - name, - contents, - Diffs.create(contents, "").createDiffOnlyReport() - ) - - Assertions.fail( - s"No snapshot was found for $name, please run checkSnapshots command and accept a snapshot for this test" - ) - - case Some(value) => - val diff = Diffs.create(contents, value) - if (!diff.isEmpty) { - val diffReport = diff.createDiffOnlyReport() - Snapshots.recordChanges(name, contents, diffReport) - Assertions.assertNoDiff(contents, value) - } else - Snapshots.clearChanges(name) - } - } -} - class ExampleTests extends FunSuite with SnapshotsIntegration { test("hello") { assertSnapshot("my.snapshot", "hello?") diff --git a/modules/example/src/test/scala/SnapshotsIntegration.scala b/modules/example/src/test/scala/SnapshotsIntegration.scala new file mode 100644 index 0000000..7d5cc07 --- /dev/null +++ b/modules/example/src/test/scala/SnapshotsIntegration.scala @@ -0,0 +1,34 @@ +import munit._ +import com.indoorvivants.snapshots._ +import munit.internal.difflib.Diffs +import munit.Assertions +import munit.FunSuite +import example.Snapshots + +// This is a sample integration for Munit +trait SnapshotsIntegration { + self: FunSuite => + def assertSnapshot(name: String, contents: String) = { + Snapshots.read(name) match { + case None => + Snapshots.recordChanges( + name, + contents, + Diffs.create(contents, "").createDiffOnlyReport() + ) + + Assertions.fail( + s"No snapshot was found for $name, please run checkSnapshots command and accept a snapshot for this test" + ) + + case Some(value) => + val diff = Diffs.create(contents, value) + if (!diff.isEmpty) { + val diffReport = diff.createDiffOnlyReport() + Snapshots.recordChanges(name, contents, diffReport) + Assertions.assertNoDiff(contents, value) + } else + Snapshots.clearChanges(name) + } + } +}