-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and fix serializers for FunctionReference and DynamicReference.
The serializers for FunctionReference and DynamicReference have been refactored and fixed. This includes updating conditions for properties and handling potential null values. Removed redundant converter in TestUtil following restructuring of serialization tests.
- Loading branch information
Showing
10 changed files
with
108 additions
and
271 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
71 changes: 0 additions & 71 deletions
71
Linguini.Serialization.Test/CallArgumentsSerializerTest.cs
This file was deleted.
Oops, something went wrong.
84 changes: 0 additions & 84 deletions
84
Linguini.Serialization.Test/DynamicReferenceSerializerTest.cs
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
Linguini.Serialization.Test/SerializeAndDeserializeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Diagnostics; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Linguini.Serialization.Converters; | ||
using Linguini.Syntax.Ast; | ||
using NUnit.Framework; | ||
using Attribute = Linguini.Syntax.Ast.Attribute; | ||
|
||
|
||
namespace Linguini.Serialization.Test; | ||
|
||
[TestFixture] | ||
public class SerializeAndDeserializeTest | ||
{ | ||
[Test] | ||
[TestCaseSource(nameof(AstExamples))] | ||
[Parallelizable] | ||
public void SerializeDeserializeTest(object x) | ||
{ | ||
SerializeAndDeserializeTest.SerializeDeserializeTest(x); | ||
} | ||
|
||
public static IEnumerable<object> AstExamples() | ||
{ | ||
yield return new CallArgumentsBuilder() | ||
.AddPositionalArg(InlineExpressionBuilder.CreateMessageReference("x")) | ||
.AddNamedArg("y", 3) | ||
.Build(); | ||
yield return new Attribute("desc", new PatternBuilder("description")); | ||
yield return new DynamicReference("dyn", "attr", new CallArgumentsBuilder() | ||
.AddPositionalArg(InlineExpressionBuilder.CreateMessageReference("x")) | ||
.AddNamedArg("y", 3)); | ||
yield return new FunctionReference("foo", new CallArgumentsBuilder() | ||
.AddPositionalArg(3) | ||
.AddNamedArg("test", InlineExpressionBuilder.CreateTermReference("x", "y")) | ||
.Build() | ||
); | ||
yield return new Identifier("test"); | ||
} | ||
|
||
private static void SerializeDeserializeTest<T>(T expected) | ||
{ | ||
// Serialize the object to JSON string. | ||
var jsonString = JsonSerializer.Serialize(expected, Options); | ||
|
||
// Deserialize the JSON string back into an object. | ||
Debug.Assert(expected != null, nameof(expected) + " != null"); | ||
var deserializedObject = JsonSerializer.Deserialize(jsonString, expected.GetType(), Options); | ||
|
||
// Now you have a 'deserializedObject' which should be equivalent to the original 'expected' object. | ||
Assert.That(deserializedObject, Is.Not.Null); | ||
Assert.That(deserializedObject, Is.EqualTo(expected)); | ||
} | ||
|
||
private static readonly JsonSerializerOptions Options = new() | ||
{ | ||
IgnoreReadOnlyFields = false, | ||
WriteIndented = true, | ||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
Converters = | ||
{ | ||
new AttributeSerializer(), | ||
new CallArgumentsSerializer(), | ||
new CommentSerializer(), | ||
new FunctionReferenceSerializer(), | ||
new IdentifierSerializer(), | ||
new JunkSerializer(), | ||
new MessageReferenceSerializer(), | ||
new MessageSerializer(), | ||
new DynamicReferenceSerializer(), | ||
new NamedArgumentSerializer(), | ||
new ParseErrorSerializer(), | ||
new PatternSerializer(), | ||
new PlaceableSerializer(), | ||
new ResourceSerializer(), | ||
new PlaceableSerializer(), | ||
new SelectExpressionSerializer(), | ||
new TermReferenceSerializer(), | ||
new TermSerializer(), | ||
new VariantSerializer(), | ||
new VariableReferenceSerializer(), | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.