|
| 1 | +package au.com.origin.snapshots; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 4 | + |
| 5 | +import au.com.origin.snapshots.config.BaseSnapshotConfig; |
| 6 | +import au.com.origin.snapshots.config.SnapshotConfig; |
| 7 | +import au.com.origin.snapshots.exceptions.SnapshotMatchException; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.Optional; |
| 15 | +import org.assertj.core.api.Assertions; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.TestInfo; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | + |
| 22 | +@ExtendWith(MockitoExtension.class) |
| 23 | +public class UpdateSnapshotTest { |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + public void beforeEach() throws Exception { |
| 27 | + File file = |
| 28 | + new File("src/test/java/au/com/origin/snapshots/__snapshots__/UpdateSnapshotTest.snap"); |
| 29 | + String content = |
| 30 | + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateAllSnapshots=[\n" |
| 31 | + + "OLD\n" |
| 32 | + + "]\n" |
| 33 | + + "\n" |
| 34 | + + "\n" |
| 35 | + + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateClassNameSnapshots=[\n" |
| 36 | + + "OLD\n" |
| 37 | + + "]\n" |
| 38 | + + "\n" |
| 39 | + + "\n" |
| 40 | + + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateNoSnapshots=[\n" |
| 41 | + + "OLD\n" |
| 42 | + + "]"; |
| 43 | + Path parentDir = file.getParentFile().toPath(); |
| 44 | + if (!Files.exists(parentDir)) { |
| 45 | + Files.createDirectories(parentDir); |
| 46 | + } |
| 47 | + Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void canUpdateAllSnapshots(TestInfo testInfo) throws IOException { |
| 52 | + SnapshotConfig config = |
| 53 | + new BaseSnapshotConfig() { |
| 54 | + @Override |
| 55 | + public Optional<String> updateSnapshot() { |
| 56 | + return Optional.of(""); |
| 57 | + } |
| 58 | + }; |
| 59 | + SnapshotVerifier snapshotVerifier = |
| 60 | + new SnapshotVerifier(config, testInfo.getTestClass().get(), false); |
| 61 | + Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get()); |
| 62 | + expect.toMatchSnapshot("NEW"); |
| 63 | + snapshotVerifier.validateSnapshots(); |
| 64 | + |
| 65 | + String content = |
| 66 | + new String( |
| 67 | + Files.readAllBytes( |
| 68 | + Paths.get( |
| 69 | + "src/test/java/au/com/origin/snapshots/__snapshots__/UpdateSnapshotTest.snap")), |
| 70 | + StandardCharsets.UTF_8); |
| 71 | + Assertions.assertThat(content) |
| 72 | + .isEqualTo( |
| 73 | + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateAllSnapshots=[\n" |
| 74 | + + "NEW\n" |
| 75 | + + "]\n" |
| 76 | + + "\n" |
| 77 | + + "\n" |
| 78 | + + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateClassNameSnapshots=[\n" |
| 79 | + + "OLD\n" |
| 80 | + + "]\n" |
| 81 | + + "\n" |
| 82 | + + "\n" |
| 83 | + + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateNoSnapshots=[\n" |
| 84 | + + "OLD\n" |
| 85 | + + "]"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void canUpdateNoSnapshots(TestInfo testInfo) { |
| 90 | + SnapshotConfig config = |
| 91 | + new BaseSnapshotConfig() { |
| 92 | + @Override |
| 93 | + public Optional<String> updateSnapshot() { |
| 94 | + return Optional.empty(); |
| 95 | + } |
| 96 | + }; |
| 97 | + SnapshotVerifier snapshotVerifier = |
| 98 | + new SnapshotVerifier(config, testInfo.getTestClass().get(), false); |
| 99 | + Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get()); |
| 100 | + assertThrows(SnapshotMatchException.class, () -> expect.toMatchSnapshot("FOOBAR")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void canUpdateNewSnapshots() { |
| 105 | + SnapshotConfig config = |
| 106 | + new BaseSnapshotConfig() { |
| 107 | + @Override |
| 108 | + public Optional<String> updateSnapshot() { |
| 109 | + return Optional.of("new"); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + // TODO Pending Implementation |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void canUpdateClassNameSnapshots(TestInfo testInfo) throws IOException { |
| 118 | + SnapshotConfig config = |
| 119 | + new BaseSnapshotConfig() { |
| 120 | + @Override |
| 121 | + public Optional<String> updateSnapshot() { |
| 122 | + return Optional.of("UpdateSnapshotTest"); |
| 123 | + } |
| 124 | + }; |
| 125 | + SnapshotVerifier snapshotVerifier = |
| 126 | + new SnapshotVerifier(config, testInfo.getTestClass().get(), false); |
| 127 | + Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get()); |
| 128 | + expect.toMatchSnapshot("NEW"); |
| 129 | + snapshotVerifier.validateSnapshots(); |
| 130 | + |
| 131 | + String content = |
| 132 | + new String( |
| 133 | + Files.readAllBytes( |
| 134 | + Paths.get( |
| 135 | + "src/test/java/au/com/origin/snapshots/__snapshots__/UpdateSnapshotTest.snap")), |
| 136 | + StandardCharsets.UTF_8); |
| 137 | + Assertions.assertThat(content) |
| 138 | + .isEqualTo( |
| 139 | + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateAllSnapshots=[\n" |
| 140 | + + "OLD\n" |
| 141 | + + "]\n" |
| 142 | + + "\n" |
| 143 | + + "\n" |
| 144 | + + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateClassNameSnapshots=[\n" |
| 145 | + + "NEW\n" |
| 146 | + + "]\n" |
| 147 | + + "\n" |
| 148 | + + "\n" |
| 149 | + + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateNoSnapshots=[\n" |
| 150 | + + "OLD\n" |
| 151 | + + "]"); |
| 152 | + } |
| 153 | +} |
0 commit comments