Skip to content

Commit

Permalink
Snapshot testing (#208)
Browse files Browse the repository at this point in the history
* snapshot testing with verify

* formatting

* add back old serialization tests

* pass verify

* use json correctly

* formatting

* Don't use Quibble and order ourselves because ordering doesn't matter

* whitespace on snapshot

* Better json diffing?  Quibble is back

* add common project

* add object unit tests to see how verify would work

* format

* move random exes to new solution folder

* update lock files
  • Loading branch information
adamhathcock authored Jan 16, 2025
1 parent f79ae7e commit bafd130
Show file tree
Hide file tree
Showing 55 changed files with 1,776 additions and 451 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,13 @@ dotnet_diagnostic.NUnit2039.severity = warning # Consider using Assert.That(actu
indent_style = space
indent_size = 2
tab_width = 2

# Verify
[*.{received,verified}.{json}]
charset = utf-8-bom
end_of_line = lf
indent_size = unset
indent_style = unset
insert_final_newline = false
tab_width = unset
trim_trailing_whitespace = false
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
* text=auto

# need original files to be windows
*.txt text eol=crlf
*.txt text eol=crlf

# Verify
*.verified.json text eol=lf working-tree-encoding=UTF-8
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ tools

.DS_Store
*.snupkg
coverage.xml
coverage.xml

*.received.*
6 changes: 5 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
<PackageVersion Include="Speckle.DoubleNumerics" Version="4.0.1" />
<PackageVersion Include="SimpleExec" Version="12.0.0" />
<PackageVersion Include="System.Threading.Channels" Version="9.0.1" />
<PackageVersion Include="Verify" Version="28.9.0" />
<PackageVersion Include="Verify.Quibble" Version="2.1.1" />
<PackageVersion Include="Verify.Xunit" Version="28.9.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.assert" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
<GlobalPackageReference Include="PolySharp" Version="1.15.0" />
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<GlobalPackageReference Include="GitVersion.MsBuild" Version="5.12.0" />
<GlobalPackageReference Include="Speckle.InterfaceGenerator" Version="0.9.6" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Speckle.Sdk.Serialisation.V2.Send;
using Speckle.Sdk.Transports;

namespace Speckle.Sdk.Serialization.Tests;
namespace Speckle.Sdk.Testing.Framework;

public class DummyReceiveServerObjectManager(Dictionary<string, string> objects) : IServerObjectManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Speckle.Sdk.Serialisation.V2.Send;
using Speckle.Sdk.Transports;

namespace Speckle.Sdk.Serialization.Tests;
namespace Speckle.Sdk.Testing.Framework;

public class DummySendServerObjectManager(ConcurrentDictionary<string, string> savedObjects) : IServerObjectManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Speckle.Sdk.SQLite;

namespace Speckle.Sdk.Serialization.Tests;
namespace Speckle.Sdk.Testing.Framework;

public class DummySqLiteReceiveManager(Dictionary<string, string> savedObjects) : ISqLiteJsonCacheManager
public sealed class DummySqLiteReceiveManager(Dictionary<string, string> savedObjects) : ISqLiteJsonCacheManager
{
public void Dispose() { }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Speckle.Sdk.SQLite;

namespace Speckle.Sdk.Serialization.Tests;
namespace Speckle.Sdk.Testing.Framework;

public class DummySqLiteSendManager : ISqLiteJsonCacheManager
public sealed class DummySqLiteSendManager : ISqLiteJsonCacheManager
{
public string? GetObject(string id) => throw new NotImplementedException();

Expand Down
24 changes: 24 additions & 0 deletions Speckle.Sdk.Testing/Framework/IdStringSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Diagnostics.CodeAnalysis;
using Argon;
using Speckle.Sdk.Common;
using Speckle.Sdk.Serialisation;

namespace Speckle.Sdk.Testing.Framework;

[SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
public class IdStringSerializer : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var id = (Id)value;
writer.WriteValue(id.Value);
}

public override object? ReadJson(JsonReader reader, Type type, object? existingValue, JsonSerializer serializer)
{
var json = reader.ReadAsString();
return new Id(json.NotNull());
}

public override bool CanConvert(Type type) => typeof(Id) == type;
}
24 changes: 24 additions & 0 deletions Speckle.Sdk.Testing/Framework/JsonStringSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Diagnostics.CodeAnalysis;
using Argon;
using Speckle.Sdk.Common;
using Speckle.Sdk.Serialisation;

namespace Speckle.Sdk.Testing.Framework;

[SuppressMessage("Design", "CA1062:Validate arguments of public methods")]
public class JsonStringSerializer : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var json = (Json)value;
writer.WriteRawValue(JObject.Parse(json.Value).ToString(Formatting.Indented));
}

public override object? ReadJson(JsonReader reader, Type type, object? existingValue, JsonSerializer serializer)
{
var json = reader.ReadAsString();
return new Json(json.NotNull());
}

public override bool CanConvert(Type type) => typeof(Json) == type;
}
1 change: 1 addition & 0 deletions Speckle.Sdk.Testing/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: CollectionBehavior(DisableTestParallelization = true)]
19 changes: 19 additions & 0 deletions Speckle.Sdk.Testing/Speckle.Sdk.Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Verify.Quibble" />
<PackageReference Include="Verify.Xunit" />
<PackageReference Include="xunit.runner.visualstudio"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Speckle.Sdk\Speckle.Sdk.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions Speckle.Sdk.Testing/SpeckleVerify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Runtime.CompilerServices;
using Argon;
using Speckle.Sdk.Serialisation;
using Speckle.Sdk.Testing.Framework;

namespace Speckle.Sdk.Testing;

public static class SpeckleVerify
{
[ModuleInitializer]
public static void Initialize()
{
VerifierSettings.DontScrubGuids();
VerifierSettings.DontScrubDateTimes();

VerifierSettings.UseStrictJson();
VerifierSettings.DontIgnoreEmptyCollections();
VerifierSettings.SortPropertiesAlphabetically();
VerifierSettings.SortJsonObjects();
if (!VerifyQuibble.Initialized)
{
VerifyQuibble.Initialize();
}
}

private static readonly JsonSerializer _jsonSerializer = new()
{
NullValueHandling = NullValueHandling.Include,
Formatting = Formatting.Indented,
Converters = { new JsonStringSerializer() },
};

private static async Task VerifyJsonObjects(IDictionary<string, Json> objects, string sourceFile) =>
await VerifyJson(JObject.FromObject(objects, _jsonSerializer).ToString(), sourceFile: sourceFile);

public static async Task VerifyJsonDictionary(
IDictionary<string, string> objects,
[CallerFilePath] string sourceFile = ""
) => await VerifyJsonObjects(objects.ToDictionary(x => x.Key, x => new Json(x.Value)), sourceFile);

public static async Task VerifyJsonDictionary(
IDictionary<Id, Json> objects,
[CallerFilePath] string sourceFile = ""
) => await VerifyJsonObjects(objects.ToDictionary(x => x.Key.Value, x => x.Value), sourceFile);
}
7 changes: 7 additions & 0 deletions Speckle.Sdk.Testing/VerifyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Speckle.Sdk.Testing;

public class VerifyTests
{
[Fact]
public Task TestVerify() => VerifyChecks.Run();
}
Loading

0 comments on commit bafd130

Please sign in to comment.