Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ You can checkout this Github repository or you can use the NuGet package:

**Install using the command line from the Package Manager:**
```bash
Install-Package SoloX.CodeQuality.Prod -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Prod -version 3.0.1-preview.2
or
Install-Package SoloX.CodeQuality.Test -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Test -version 3.0.1-preview.2
```

**Install using the .Net CLI:**
```bash
dotnet add package SoloX.CodeQuality.Prod --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Prod --version 3.0.1-preview.2
or
dotnet add package SoloX.CodeQuality.Test --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Test --version 3.0.1-preview.2
```

**Install editing your project file (csproj):**
```xml
<PackageReference Include="SoloX.CodeQuality.Prod" Version="3.0.1-preview.1">
<PackageReference Include="SoloX.CodeQuality.Prod" Version="3.0.1-preview.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
or
<PackageReference Include="SoloX.CodeQuality.Test" Version="3.0.1-preview.1">
<PackageReference Include="SoloX.CodeQuality.Test" Version="3.0.1-preview.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down Expand Up @@ -223,17 +223,17 @@ You can checkout this Github repository or use the NuGet package:

**Install using the command line from the Package Manager:**
```bash
Install-Package SoloX.CodeQuality.Playwright -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Playwright -version 3.0.1-preview.2
```

**Install using the .Net CLI:**
```bash
dotnet add package SoloX.CodeQuality.Playwright --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Playwright --version 3.0.1-preview.2
```

**Install editing your project file (csproj):**
```xml
<PackageReference Include="SoloX.CodeQuality.Playwright" Version="3.0.1-preview.1" />
<PackageReference Include="SoloX.CodeQuality.Playwright" Version="3.0.1-preview.2" />
```

* * *
Expand Down Expand Up @@ -429,35 +429,35 @@ You can checkout this Github repository or you can use the NuGet package:

**Install using the command line from the Package Manager:**
```bash
Install-Package SoloX.CodeQuality.Test.Helpers -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Test.Helpers -version 3.0.1-preview.2

Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 3.0.1-preview.2

Install-Package SoloX.CodeQuality.Test.Helpers.XUnit.V3 -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Test.Helpers.XUnit.V3 -version 3.0.1-preview.2

Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 3.0.1-preview.1
Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 3.0.1-preview.2
```

**Install using the .Net CLI:**
```bash
dotnet add package SoloX.CodeQuality.Test.Helpers --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Test.Helpers --version 3.0.1-preview.2

dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 3.0.1-preview.2

dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit.V3 --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit.V3 --version 3.0.1-preview.2

dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 3.0.1-preview.1
dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 3.0.1-preview.2
```

**Install editing your project file (csproj):**
```xml
<PackageReference Include="SoloX.CodeQuality.Test.Helpers" Version="3.0.1-preview.1" />
<PackageReference Include="SoloX.CodeQuality.Test.Helpers" Version="3.0.1-preview.2" />

<PackageReference Include="SoloX.CodeQuality.Test.Helpers.XUnit" Version="3.0.1-preview.1" />
<PackageReference Include="SoloX.CodeQuality.Test.Helpers.XUnit" Version="3.0.1-preview.2" />

<PackageReference Include="SoloX.CodeQuality.Test.Helpers.XUnit.V3" Version="3.0.1-preview.1" />
<PackageReference Include="SoloX.CodeQuality.Test.Helpers.XUnit.V3" Version="3.0.1-preview.2" />

<PackageReference Include="SoloX.CodeQuality.Test.Helpers.NUnit" Version="3.0.1-preview.1" />
<PackageReference Include="SoloX.CodeQuality.Test.Helpers.NUnit" Version="3.0.1-preview.2" />
```

* * *
Expand Down
2 changes: 1 addition & 1 deletion src/SharedProperties.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>3.0.1-preview.1</Version>
<Version>3.0.1-preview.2</Version>
<Authors>Xavier Solau</Authors>
<Copyright>Copyright © 2021-2026 Xavier Solau</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

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

/// <summary>
/// Configures the snapshot test builder to use a UTF-8 encoded text-based comparison strategy.
/// </summary>
/// <remarks>Use this method when the expected and actual values should be compared as plain text using UTF-8 encoding,
/// such as for verifying file contents or string outputs.</remarks>
/// <param name="ignoreWhitespace">Indicates whether to ignore differences in whitespace when comparing text. If true, all whitespace characters
/// are treated as equivalent, and differences in whitespace will not cause the test to fail. Default is true.</param>
/// <param name="ignoreCase">Indicates whether to ignore case differences when comparing text. If true, uppercase and lowercase characters
/// are treated as equivalent, and differences in case will not cause the test to fail. Default is false.</param>
/// <returns>An instance of the snapshot test builder configured to compare string content using a UTF-8 text strategy.</returns>
ISnapshotTestBuilder<string> WithUtf8TextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false);

/// <summary>
/// Configures the snapshot test builder to use a text-based comparison strategy.
/// </summary>
Expand All @@ -66,8 +79,9 @@ public interface ISnapshotTestBuilder
/// are treated as equivalent, and differences in whitespace will not cause the test to fail. Default is true.</param>
/// <param name="ignoreCase">Indicates whether to ignore case differences when comparing text. If true, uppercase and lowercase characters
/// are treated as equivalent, and differences in case will not cause the test to fail. Default is false.</param>
/// <param name="encoding">The text encoding to use when reading and writing text files. If null, the default encoding will be used.</param>
/// <returns>An instance of the snapshot test builder configured to compare string content using a text strategy.</returns>
ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false);
ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// ----------------------------------------------------------------------

using System.IO;
using System.Text;
using System.Threading.Tasks;
using DiffPlex.Renderer;

Expand All @@ -23,14 +24,16 @@ public class TextSnapshotStrategy : ISnapshotStrategy<string>
{
private readonly bool ignoreWhitespace;
private readonly bool ignoreCase;
private readonly Encoding encoding;

/// <inheritdoc/>
public string FileExtension => "txt";

public TextSnapshotStrategy(bool ignoreWhitespace = true, bool ignoreCase = false)
public TextSnapshotStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null)
{
this.ignoreWhitespace = ignoreWhitespace;
this.ignoreCase = ignoreCase;
this.encoding = encoding ?? Encoding.Default;
}

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

return File.WriteAllTextAsync(snapshotFile, snapshotData);
return File.WriteAllTextAsync(snapshotFile, snapshotData, this.encoding);
}

/// <inheritdoc/>
public async Task<CompareSnapshotResult<string>> CompareAsync(string snapshotReferenceFile, string snapshotData)
{
var referenceText = await File.ReadAllTextAsync(snapshotReferenceFile).ConfigureAwait(false);
var referenceText = await File.ReadAllTextAsync(snapshotReferenceFile, this.encoding).ConfigureAwait(false);
var snapshotDiffs = UnidiffRenderer.GenerateUnidiff(
referenceText,
snapshotData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using SoloX.CodeQuality.Test.Helpers.Snapshot.Impl;

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

public ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false)
public ISnapshotTestBuilder<string> WithUtf8TextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false)
{
var builder = new SnapshotTestBuilderInternal<string>(this, new TextSnapshotStrategy(ignoreWhitespace, ignoreCase));
return WithTextStrategy(ignoreWhitespace, ignoreCase, Encoding.UTF8);
}

public ISnapshotTestBuilder<string> WithTextStrategy(bool ignoreWhitespace = true, bool ignoreCase = false, Encoding? encoding = null)
{
var builder = new SnapshotTestBuilderInternal<string>(this, new TextSnapshotStrategy(ignoreWhitespace, ignoreCase, encoding));

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// </copyright>
// ----------------------------------------------------------------------

using System.Text;
using Shouldly;
using SoloX.CodeQuality.Test.Helpers.Snapshot;
using Xunit;
Expand Down Expand Up @@ -48,6 +49,40 @@ public async Task ItShouldGenerateTextSnapshotAsync()
}
}

[Fact]
public async Task ItShouldGenerateUtf8TextSnapshotAsync()
{
var sh = SnapshotTestBuilder
.Create()
.WithLocation(".")
.WithUtf8TextStrategy()
.Build();

var expectedFile = @$"./Snapshots/{nameof(ItShouldGenerateUtf8TextSnapshotAsync)}.snapshot.ref.txt";

try
{
var someGeneratedText = "some generated utf8 text";

await sh.CompareSnapshotAsync(nameof(ItShouldGenerateUtf8TextSnapshotAsync), someGeneratedText);

// Check that the snapshot reference file exists and has been generated with the same content as the generated text
File.Exists(expectedFile)
.ShouldBeTrue();

var generatedText = await File.ReadAllTextAsync(expectedFile, Encoding.UTF8);

generatedText.ShouldBe(someGeneratedText);
}
finally
{
if (File.Exists(expectedFile))
{
File.Delete(expectedFile);
}
}
}

[Fact]
public async Task ItShouldReplaceTextSnapshotAsync()
{
Expand Down
Loading