-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Roundtrip short and ushort serialization
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
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,55 @@ | ||
using Fauna.Mapping; | ||
using Fauna.Serialization; | ||
using Fauna.Types; | ||
using NUnit.Framework; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Fauna.Test.Serialization; | ||
|
||
[TestFixture] | ||
public class RoundTripTests | ||
{ | ||
private static readonly MappingContext ctx = new(); | ||
|
||
public static string Serialize(object? obj) | ||
{ | ||
using var stream = new MemoryStream(); | ||
using var writer = new Utf8FaunaWriter(stream); | ||
Serializer.Serialize(ctx, writer, obj); | ||
writer.Flush(); | ||
return Encoding.UTF8.GetString(stream.ToArray()); | ||
} | ||
|
||
public static T Deserialize<T>(string str) where T : notnull | ||
{ | ||
var reader = new Utf8FaunaReader(str); | ||
reader.Read(); | ||
var obj = Deserializer.Generate<T>(ctx).Deserialize(ctx, ref reader); | ||
if (reader.Read()) | ||
{ | ||
throw new SerializationException($"Token stream is not exhausted but should be: {reader.CurrentTokenType}"); | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
[Test] | ||
public void RoundTripShort() | ||
{ | ||
const short test = 40; | ||
var serialized = Serialize(test); | ||
var deserialized = Deserialize<short>(serialized); | ||
Assert.AreEqual(test, deserialized); | ||
} | ||
|
||
[Test] | ||
public void RoundTripUShort() | ||
{ | ||
const ushort test = 40; | ||
var serialized = Serialize(test); | ||
var deserialized = Deserialize<ushort>(serialized); | ||
Assert.AreEqual(test, deserialized); | ||
} | ||
|
||
} |
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
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