Skip to content

Commit 70c1762

Browse files
committed
Add Encoding support to TextSnapshotStrategy and SnapshotTestBuilder.
1 parent f18fef1 commit 70c1762

4 files changed

Lines changed: 64 additions & 6 deletions

File tree

src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/ISnapshotTestBuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
using System.IO;
1010
using System.Runtime.CompilerServices;
11+
using System.Text;
1112

1213
namespace SoloX.CodeQuality.Test.Helpers.Snapshot
1314
{
@@ -57,6 +58,18 @@ public interface ISnapshotTestBuilder
5758
/// <returns>An updated snapshot test builder configured to use PNG image comparison.</returns>
5859
ISnapshotTestBuilder<Stream> WithPngStrategy(double differencesThreshold = 0.0);
5960

61+
/// <summary>
62+
/// Configures the snapshot test builder to use a UTF-8 encoded text-based comparison strategy.
63+
/// </summary>
64+
/// <remarks>Use this method when the expected and actual values should be compared as plain text using UTF-8 encoding,
65+
/// such as for verifying file contents or string outputs.</remarks>
66+
/// <param name="ignoreWhitespace">Indicates whether to ignore differences in whitespace when comparing text. If true, all whitespace characters
67+
/// are treated as equivalent, and differences in whitespace will not cause the test to fail. Default is true.</param>
68+
/// <param name="ignoreCase">Indicates whether to ignore case differences when comparing text. If true, uppercase and lowercase characters
69+
/// are treated as equivalent, and differences in case will not cause the test to fail. Default is false.</param>
70+
/// <returns>An instance of the snapshot test builder configured to compare string content using a UTF-8 text strategy.</returns>
71+
ISnapshotTestBuilder<string> WithUtf8TextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false);
72+
6073
/// <summary>
6174
/// Configures the snapshot test builder to use a text-based comparison strategy.
6275
/// </summary>
@@ -66,8 +79,9 @@ public interface ISnapshotTestBuilder
6679
/// are treated as equivalent, and differences in whitespace will not cause the test to fail. Default is true.</param>
6780
/// <param name="ignoreCase">Indicates whether to ignore case differences when comparing text. If true, uppercase and lowercase characters
6881
/// are treated as equivalent, and differences in case will not cause the test to fail. Default is false.</param>
82+
/// <param name="encoding">The text encoding to use when reading and writing text files. If null, the default encoding will be used.</param>
6983
/// <returns>An instance of the snapshot test builder configured to compare string content using a text strategy.</returns>
70-
ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false);
84+
ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null);
7185
}
7286

7387
/// <summary>

src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/Impl/TextSnapshotStrategy.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// ----------------------------------------------------------------------
88

99
using System.IO;
10+
using System.Text;
1011
using System.Threading.Tasks;
1112
using DiffPlex.Renderer;
1213

@@ -23,14 +24,16 @@ public class TextSnapshotStrategy : ISnapshotStrategy<string>
2324
{
2425
private readonly bool ignoreWhitespace;
2526
private readonly bool ignoreCase;
27+
private readonly Encoding encoding;
2628

2729
/// <inheritdoc/>
2830
public string FileExtension => "txt";
2931

30-
public TextSnapshotStrategy(bool ignoreWhitespace = true, bool ignoreCase = false)
32+
public TextSnapshotStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null)
3133
{
3234
this.ignoreWhitespace = ignoreWhitespace;
3335
this.ignoreCase = ignoreCase;
36+
this.encoding = encoding ?? Encoding.Default;
3437
}
3538

3639
/// <inheritdoc/>
@@ -41,13 +44,13 @@ public Task SaveAsync(string snapshotFile, string snapshotData)
4144
File.Delete(snapshotFile);
4245
}
4346

44-
return File.WriteAllTextAsync(snapshotFile, snapshotData);
47+
return File.WriteAllTextAsync(snapshotFile, snapshotData, this.encoding);
4548
}
4649

4750
/// <inheritdoc/>
4851
public async Task<CompareSnapshotResult<string>> CompareAsync(string snapshotReferenceFile, string snapshotData)
4952
{
50-
var referenceText = await File.ReadAllTextAsync(snapshotReferenceFile).ConfigureAwait(false);
53+
var referenceText = await File.ReadAllTextAsync(snapshotReferenceFile, this.encoding).ConfigureAwait(false);
5154
var snapshotDiffs = UnidiffRenderer.GenerateUnidiff(
5255
referenceText,
5356
snapshotData,

src/libs/SoloX.CodeQuality.Test.Helpers/Snapshot/SnapshotTestBuilder.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.IO;
1111
using System.Runtime.CompilerServices;
12+
using System.Text;
1213
using SoloX.CodeQuality.Test.Helpers.Snapshot.Impl;
1314

1415
namespace SoloX.CodeQuality.Test.Helpers.Snapshot
@@ -76,9 +77,14 @@ public ISnapshotTestBuilder<Stream> WithPngStrategy(double differencesThreshold
7677
return builder;
7778
}
7879

79-
public ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false)
80+
public ISnapshotTestBuilder<string> WithUtf8TextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false)
8081
{
81-
var builder = new SnapshotTestBuilderInternal<string>(this, new TextSnapshotStrategy(ignoreWhitespace, ignoreCase));
82+
return WithTextStrategy(ignoreWhitespace, ignoreCase, Encoding.UTF8);
83+
}
84+
85+
public ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null)
86+
{
87+
var builder = new SnapshotTestBuilderInternal<string>(this, new TextSnapshotStrategy(ignoreWhitespace, ignoreCase, encoding));
8288

8389
return builder;
8490
}

src/tests/SoloX.CodeQuality.Test.Helpers.UTest/Snapshot/SnapshotTestBuilderTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// </copyright>
77
// ----------------------------------------------------------------------
88

9+
using System.Text;
910
using Shouldly;
1011
using SoloX.CodeQuality.Test.Helpers.Snapshot;
1112
using Xunit;
@@ -48,6 +49,40 @@ public async Task ItShouldGenerateTextSnapshotAsync()
4849
}
4950
}
5051

52+
[Fact]
53+
public async Task ItShouldGenerateUtf8TextSnapshotAsync()
54+
{
55+
var sh = SnapshotTestBuilder
56+
.Create()
57+
.WithLocation(".")
58+
.WithUtf8TextStrategy()
59+
.Build();
60+
61+
var expectedFile = @$"./Snapshots/{nameof(ItShouldGenerateUtf8TextSnapshotAsync)}.snapshot.ref.txt";
62+
63+
try
64+
{
65+
var someGeneratedText = "some generated utf8 text";
66+
67+
await sh.CompareSnapshotAsync(nameof(ItShouldGenerateUtf8TextSnapshotAsync), someGeneratedText);
68+
69+
// Check that the snapshot reference file exists and has been generated with the same content as the generated text
70+
File.Exists(expectedFile)
71+
.ShouldBeTrue();
72+
73+
var generatedText = await File.ReadAllTextAsync(expectedFile, Encoding.UTF8);
74+
75+
generatedText.ShouldBe(someGeneratedText);
76+
}
77+
finally
78+
{
79+
if (File.Exists(expectedFile))
80+
{
81+
File.Delete(expectedFile);
82+
}
83+
}
84+
}
85+
5186
[Fact]
5287
public async Task ItShouldReplaceTextSnapshotAsync()
5388
{

0 commit comments

Comments
 (0)