|
1 | 1 | using System.Buffers;
|
| 2 | +using System.Runtime.CompilerServices; |
2 | 3 | using System.Text;
|
3 | 4 | using CookieCrumble.Formatters;
|
4 | 5 | using CookieCrumble.Xunit;
|
| 6 | +using Xunit.Sdk; |
5 | 7 |
|
6 | 8 | namespace CookieCrumble;
|
7 | 9 |
|
8 | 10 | public class SnapshotTests
|
9 | 11 | {
|
| 12 | + private const string _strictModeExceptionMessage = |
| 13 | + "Strict mode is enabled and no snapshot has been found " + |
| 14 | + "for the current test. Create a new snapshot locally and " + |
| 15 | + "rerun your tests."; |
| 16 | + |
10 | 17 | static SnapshotTests()
|
11 | 18 | {
|
12 | 19 | Snapshot.RegisterTestFramework(new XunitFramework());
|
@@ -109,6 +116,83 @@ public void SnapshotBuilder_Segment_Custom_Global_Serializer()
|
109 | 116 | snapshot.Match();
|
110 | 117 | }
|
111 | 118 |
|
| 119 | + [Theory] |
| 120 | + [InlineData("on")] |
| 121 | + [InlineData("true")] |
| 122 | + public async Task Match_StrictMode_On(string strictMode) |
| 123 | + { |
| 124 | + Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", strictMode); |
| 125 | + |
| 126 | + var snapshot = new Snapshot(); |
| 127 | + snapshot.Add(new MyClass { Foo = "123", }); |
| 128 | + |
| 129 | + async Task Act1() => await snapshot.MatchAsync(); |
| 130 | + void Act2() => snapshot.Match(); |
| 131 | + async Task Act3() => await snapshot.MatchMarkdownAsync(); |
| 132 | + void Act4() => snapshot.MatchMarkdown(); |
| 133 | + |
| 134 | + try |
| 135 | + { |
| 136 | + Assert.Equal( |
| 137 | + _strictModeExceptionMessage, |
| 138 | + (await Assert.ThrowsAsync<XunitException>(Act1)).Message); |
| 139 | + |
| 140 | + Assert.Equal(_strictModeExceptionMessage, Assert.Throws<XunitException>(Act2).Message); |
| 141 | + |
| 142 | + Assert.Equal( |
| 143 | + _strictModeExceptionMessage, |
| 144 | + (await Assert.ThrowsAsync<XunitException>(Act3)).Message); |
| 145 | + |
| 146 | + Assert.Equal(_strictModeExceptionMessage, Assert.Throws<XunitException>(Act4).Message); |
| 147 | + } |
| 148 | + finally |
| 149 | + { |
| 150 | + Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", null); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + [Theory] |
| 155 | + [InlineData(1, "off")] |
| 156 | + [InlineData(2, "false")] |
| 157 | + [InlineData(3, null)] |
| 158 | + public async Task Match_StrictMode_Off(int number, string? strictMode) |
| 159 | + { |
| 160 | + Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", strictMode); |
| 161 | + |
| 162 | + var snapshot = new Snapshot(); |
| 163 | + snapshot.Add(new MyClass { Foo = "123", }); |
| 164 | + |
| 165 | + async Task Act1() => await snapshot.SetPostFix($"MA_{number}").MatchAsync(); |
| 166 | + void Act2() => snapshot.SetPostFix($"M_{number}").Match(); |
| 167 | + async Task Act3() => await snapshot.SetPostFix($"MMA_{number}").MatchMarkdownAsync(); |
| 168 | + void Act4() => snapshot.SetPostFix($"MM_{number}").MatchMarkdown(); |
| 169 | + |
| 170 | + try |
| 171 | + { |
| 172 | + var result1 = await Record.ExceptionAsync(Act1); |
| 173 | + var result2 = Record.Exception(Act2); |
| 174 | + var result3 = await Record.ExceptionAsync(Act3); |
| 175 | + var result4 = Record.Exception(Act4); |
| 176 | + |
| 177 | + static string GetCallerFilePath([CallerFilePath] string filePath = "") => filePath; |
| 178 | + var directory = Path.GetDirectoryName(GetCallerFilePath()) + "/__snapshots__"; |
| 179 | + |
| 180 | + File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_MA_{number}.snap"); |
| 181 | + File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_M_{number}.snap"); |
| 182 | + File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_MMA_{number}.md"); |
| 183 | + File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_MM_{number}.md"); |
| 184 | + |
| 185 | + Assert.Null(result1); |
| 186 | + Assert.Null(result2); |
| 187 | + Assert.Null(result3); |
| 188 | + Assert.Null(result4); |
| 189 | + } |
| 190 | + finally |
| 191 | + { |
| 192 | + Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", null); |
| 193 | + } |
| 194 | + } |
| 195 | + |
112 | 196 | public class MyClass
|
113 | 197 | {
|
114 | 198 | public string Foo { get; set; } = "Bar";
|
|
0 commit comments