Skip to content

Commit

Permalink
Add source generator tests for GuidIds
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Nov 11, 2023
1 parent 12f7728 commit 43dff93
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,58 @@ public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider()
}
#endif

[Fact]
public void CanSerializeToGuid_WithSystemTextJsonProvider_WithSourceGenerator()
{
var foo = GuidId1.New();

var serializedFoo = SystemTextJsonSerializer.Serialize(foo, SystemTextJsonSerializerContext.Custom.GuidId1);
var serializedGuid = SystemTextJsonSerializer.Serialize(foo.Value);

Assert.Equal(serializedFoo, serializedGuid);
}

[Fact]
public void CanDeserializeFromGuid_WithSystemTextJsonProvider_WithSourceGenerator()
{
var value = Guid.NewGuid();
var foo = new GuidId1(value);
var serializedGuid = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Custom.GuidId1);

var deserializedFoo = SystemTextJsonSerializer.Deserialize<GuidId1>(serializedGuid, SystemTextJsonSerializerContext.Custom.GuidId1);

Assert.Equal(foo, deserializedFoo);
}

#if NET6_0_OR_GREATER
[Fact]
public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider_WithSourceGenerator()
{
var value = new TypeWithDictionaryKeys()
{
Values = new()
};
var guid = new GuidId1(Guid.Parse("78104553-f1cd-41ec-bcb6-d3a8ff8d994d"));
value.Values.Add(guid, "My Value");
var serialized = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys);

var expected = $$"""
{
"values": {
"78104553-f1cd-41ec-bcb6-d3a8ff8d994d": "My Value"
}
}
""";
Assert.Equal(serialized, expected);

var deserialized = SystemTextJsonSerializer.Deserialize<TypeWithDictionaryKeys>(serialized, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys);

Assert.NotNull(deserialized.Values);
Assert.True(deserialized.Values.ContainsKey(guid));
Assert.Equal("My Value", deserialized.Values[guid]);
}
#endif

[Fact]
public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if NET6_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Serialization;
using StronglyTypedIds.IntegrationTests.Types;

namespace StronglyTypedIds.IntegrationTests;

[JsonSerializable(typeof(GuidId1))]
[JsonSerializable(typeof(ConvertersGuidId))]
[JsonSerializable(typeof(ConvertersGuidId2))]
[JsonSerializable(typeof(GuidIdTests.TypeWithDictionaryKeys))]
internal partial class SystemTextJsonSerializerContext : JsonSerializerContext
{
internal static SystemTextJsonSerializerContext Custom
=> new(new JsonSerializerOptions
{
Converters =
{
new GuidId1.GuidId1SystemTextJsonConverter(),
new ConvertersGuidId.ConvertersGuidIdSystemTextJsonConverter(),
new ConvertersGuidId2.ConvertersGuidId2SystemTextJsonConverter(),
}
});

internal static SystemTextJsonSerializerContext Web
=> new(new JsonSerializerOptions()
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new GuidId1.GuidId1SystemTextJsonConverter(),
new ConvertersGuidId.ConvertersGuidIdSystemTextJsonConverter(),
new ConvertersGuidId2.ConvertersGuidId2SystemTextJsonConverter(),
}
});
}
#endif

0 comments on commit 43dff93

Please sign in to comment.