mirror of https://github.com/SixLabors/ImageSharp
5 changed files with 233 additions and 88 deletions
@ -0,0 +1,87 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.ComponentModel; |
||||
|
using System.Globalization; |
||||
|
using System.IO; |
||||
|
using Xunit.Abstractions; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.TestUtilities |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// <see cref="IXunitSerializationInfo"/>-compatible serialization for cross-process use-cases.
|
||||
|
/// </summary>
|
||||
|
internal class BasicSerializer : IXunitSerializationInfo |
||||
|
{ |
||||
|
private readonly Dictionary<string, string> map = new Dictionary<string, string>(); |
||||
|
|
||||
|
public const char Separator = ':'; |
||||
|
|
||||
|
private string DumpToString() |
||||
|
{ |
||||
|
using var ms = new MemoryStream(); |
||||
|
using var writer = new StreamWriter(ms); |
||||
|
foreach (KeyValuePair<string, string> kv in this.map) |
||||
|
{ |
||||
|
writer.WriteLine($"{kv.Key}{Separator}{kv.Value}"); |
||||
|
} |
||||
|
writer.Flush(); |
||||
|
byte[] data = ms.ToArray(); |
||||
|
return System.Convert.ToBase64String(data); |
||||
|
} |
||||
|
|
||||
|
private void LoadDump(string dump) |
||||
|
{ |
||||
|
byte[] data = System.Convert.FromBase64String(dump); |
||||
|
|
||||
|
using var ms = new MemoryStream(data); |
||||
|
using var reader = new StreamReader(ms); |
||||
|
for (string s = reader.ReadLine(); s != null ; s = reader.ReadLine()) |
||||
|
{ |
||||
|
string[] kv = s.Split(Separator); |
||||
|
this.map[kv[0]] = kv[1]; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static string Serialize(IXunitSerializable serializable) |
||||
|
{ |
||||
|
var serializer = new BasicSerializer(); |
||||
|
serializable.Serialize(serializer); |
||||
|
return serializer.DumpToString(); |
||||
|
} |
||||
|
|
||||
|
public static T Deserialize<T>(string dump) where T : IXunitSerializable |
||||
|
{ |
||||
|
T result = Activator.CreateInstance<T>(); |
||||
|
var serializer = new BasicSerializer(); |
||||
|
serializer.LoadDump(dump); |
||||
|
result.Deserialize(serializer); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public void AddValue(string key, object value, Type type = null) |
||||
|
{ |
||||
|
Guard.NotNull(key, nameof(key)); |
||||
|
if (value == null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
type ??= value.GetType(); |
||||
|
|
||||
|
this.map[key] = TypeDescriptor.GetConverter(type).ConvertToInvariantString(value); |
||||
|
} |
||||
|
|
||||
|
public object GetValue(string key, Type type) |
||||
|
{ |
||||
|
Guard.NotNull(key, nameof(key)); |
||||
|
|
||||
|
if (!this.map.TryGetValue(key, out string str)) |
||||
|
{ |
||||
|
return type.IsValueType ? Activator.CreateInstance(type) : null; |
||||
|
} |
||||
|
|
||||
|
return TypeDescriptor.GetConverter(type).ConvertFromInvariantString(str); |
||||
|
} |
||||
|
|
||||
|
public T GetValue<T>(string key) => (T)this.GetValue(key, typeof(T)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
using SixLabors.ImageSharp.Tests.TestUtilities; |
||||
|
using Xunit; |
||||
|
using Xunit.Abstractions; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests |
||||
|
{ |
||||
|
public class BasicSerializerTests |
||||
|
{ |
||||
|
class TestObj : IXunitSerializable |
||||
|
{ |
||||
|
public double Length { get; set; } |
||||
|
public string Name { get; set; } |
||||
|
public int Lives { get; set; } |
||||
|
|
||||
|
public void Deserialize(IXunitSerializationInfo info) |
||||
|
{ |
||||
|
info.AddValue(nameof(Length), Length); |
||||
|
info.AddValue(nameof(Name), Name); |
||||
|
info.AddValue(nameof(this.Lives), Lives); |
||||
|
} |
||||
|
|
||||
|
public void Serialize(IXunitSerializationInfo info) |
||||
|
{ |
||||
|
this.Length = info.GetValue<double>(nameof(Length)); |
||||
|
this.Name = info.GetValue<string>(nameof(Name)); |
||||
|
this.Lives = info.GetValue<int>(nameof(Lives)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void SerializeDeserialize_ShouldPreserveValues() |
||||
|
{ |
||||
|
var obj = new TestObj() {Length = 123, Name = "Lol123!", Lives = 7}; |
||||
|
|
||||
|
string str = BasicSerializer.Serialize(obj); |
||||
|
TestObj mirror = BasicSerializer.Deserialize<TestObj>(str); |
||||
|
|
||||
|
Assert.Equal(obj.Length, mirror.Length); |
||||
|
Assert.Equal(obj.Name, mirror.Name); |
||||
|
Assert.Equal(obj.Lives, mirror.Lives); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
<configuration> |
||||
|
<packageSources> |
||||
|
<clear /> |
||||
|
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> |
||||
|
<!-- For RemoteExecutor: --> |
||||
|
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" /> |
||||
|
</packageSources> |
||||
|
</configuration> |
||||
Loading…
Reference in new issue