Skip to content

Commit 002269e

Browse files
committed
Create ResultTextEncodingTests.cs
1 parent d089a58 commit 002269e

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Lives here, rather than in Verify.Tests, since UseEncoding is a static setting, and this
2+
// project runs serially with BaseTest resetting it between tests.
3+
public class ResultTextEncodingTests :
4+
BaseTest
5+
{
6+
// Latin1 writes no preamble, so nothing on disk tells a reader the file is not UTF8. Reading
7+
// it as UTF8 turns the 0xE9 byte into U+FFFD, which is what makes this the case that catches
8+
// VerifyResult.Text ignoring the configured encoding.
9+
const string value = "café";
10+
11+
[Fact]
12+
public async Task ReadsExistingVerifiedWithConfiguredEncoding()
13+
{
14+
VerifierSettings.UseEncoding(Encoding.Latin1);
15+
16+
using var temp = new TempDirectory();
17+
await File.WriteAllTextAsync(VerifiedPath(temp), value, Encoding.Latin1);
18+
19+
var result = await Verify(value, Settings(temp));
20+
21+
Assert.Equal(value, result.Text);
22+
}
23+
24+
[Fact]
25+
public async Task RoundTripsWhatVerifyWrote()
26+
{
27+
VerifierSettings.UseEncoding(Encoding.Latin1);
28+
29+
using var temp = new TempDirectory();
30+
var settings = Settings(temp);
31+
settings.AutoVerify();
32+
33+
var result = await Verify(value, settings);
34+
35+
var file = result.Files.Single();
36+
Assert.Equal(Encoding.Latin1.GetBytes(value), await File.ReadAllBytesAsync(file));
37+
Assert.Equal(value, result.Text);
38+
}
39+
40+
[Fact]
41+
public async Task DefaultEncodingIsUnaffected()
42+
{
43+
using var temp = new TempDirectory();
44+
await File.WriteAllTextAsync(VerifiedPath(temp), value, new UTF8Encoding(true));
45+
46+
var result = await Verify(value, Settings(temp));
47+
48+
Assert.Equal(value, result.Text);
49+
}
50+
51+
static VerifySettings Settings(TempDirectory temp)
52+
{
53+
var settings = new VerifySettings();
54+
settings.UseDirectory(temp);
55+
settings.DisableDiff();
56+
return settings;
57+
}
58+
59+
static string VerifiedPath(TempDirectory temp, [CallerMemberName] string name = "") =>
60+
temp.BuildPath($"{nameof(ResultTextEncodingTests)}.{name}.verified.txt");
61+
}

0 commit comments

Comments
 (0)