Skip to content

Commit

Permalink
Update the cross framework spec to include complex POCO object, Type …
Browse files Browse the repository at this point in the history
…serialization, and support for netcoreapp3.1 and net5.0
  • Loading branch information
Arkatufus committed Mar 24, 2021
1 parent ada4198 commit 4605dd3
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/Hyperion.Tests/CrossFrameworkSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
using Hyperion.Tests.Generator;
using Xunit;
using Xunit.Abstractions;

namespace Hyperion.Tests
{
public class CrossFrameworkSerializationTests
{
private readonly ITestOutputHelper _log;
private readonly Serializer _serializer;
private readonly CrossFrameworkClass _originalObject;
private readonly CrossFrameworkMixedClass _originalMixedObject;

public CrossFrameworkSerializationTests()
public CrossFrameworkSerializationTests(ITestOutputHelper log)
{
_log = log;
_serializer = new Serializer();
_originalObject = CrossFrameworkInitializer.Init();
_originalMixedObject = CrossFrameworkInitializer.InitMixed();
}

public static IEnumerable<object[]> SerializationFiles()
{
const string defaultOutputPath = CrossFrameworkInitializer.DefaultOutputPath;
var testFiles = Directory.GetFiles(defaultOutputPath, "*.tf");
var testFiles = Directory.GetFiles(defaultOutputPath, "test_file_.*.tf");
return testFiles.Select(x => new object[] { x });
}

public static IEnumerable<object[]> MixedSerializationFiles()
{
const string defaultOutputPath = CrossFrameworkInitializer.DefaultOutputPath;
var testFiles = Directory.GetFiles(defaultOutputPath, "mixed_test_file_.*.tf");
return testFiles.Select(x => new object[] { x });
}

Expand All @@ -32,8 +45,20 @@ public void CanSerializeCrossFramework(string fileName)
using (var fileStream = new FileStream(fileName, FileMode.Open))
{
var crossFrameworkClass = _serializer.Deserialize<CrossFrameworkClass>(fileStream);
_originalObject.Should()
.Be(crossFrameworkClass, $"[CrossFrameworkClass] {fileName} deserialization should work.");
}
}

Assert.Equal(_originalObject, crossFrameworkClass);
[Theory]
[MemberData(nameof(MixedSerializationFiles))]
public void CanSerializeComplexObjectCrossFramework(string fileName)
{
using (var fileStream = new FileStream(fileName, FileMode.Open))
{
var deserialized = _serializer.Deserialize<CrossFrameworkMixedClass>(fileStream);
_originalMixedObject.Should()
.Be(deserialized, $"[CrossFrameworkMixedClass] {fileName} deserialization should work.");
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/Hyperion.Tests/Generator/CrossFrameworkClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,47 @@ private bool Equals(CrossFrameworkClass other)
&& Struct.Equals(other.Struct);
}
}

public interface ICrossFrameworkA
{
string Name { get; set; }
}

public interface ICrossFrameworkB : ICrossFrameworkA
{
string Sound { get; set; }
}

public class CrossFrameworkBase : ICrossFrameworkB
{
public string Name { get; set; }
public string Sound { get; set; }
}

public class CrossFrameworkMixedClass : CrossFrameworkBase
{
public Type FriendType { get; set; }
public CrossFrameworkClass Data { get; set; }

public override bool Equals(object obj)
{
if (!(obj is CrossFrameworkMixedClass other))
return false;
return other.Equals(this);
}

protected bool Equals(CrossFrameworkMixedClass other)
{
if (other.Sound != Sound || other.Name != Name)
return false;
return other.Data.Equals(Data);
}

public override int GetHashCode()
{
int hash = Name.GetHashCode();
hash = (hash * 397) ^ Sound.GetHashCode();
return (Data != null ? (hash * 397) ^ Data.GetHashCode() : hash);
}
}
}
11 changes: 11 additions & 0 deletions src/Hyperion.Tests/Generator/CrossFrameworkInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ public static class CrossFrameworkInitializer
{
public const string DefaultOutputPath = "../../../testfiles";

public static CrossFrameworkMixedClass InitMixed()
{
return new CrossFrameworkMixedClass
{
Name = "Cookie",
Sound = "Bark",
FriendType = typeof(CrossFrameworkClass),
Data = Init()
};
}

public static CrossFrameworkClass Init()
{
return new CrossFrameworkClass()
Expand Down
9 changes: 8 additions & 1 deletion src/Hyperion.Tests/Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ private static void Main(string[] args)
}

CrossFrameworkClass crossFrameworkClass = CrossFrameworkInitializer.Init();
var crossFrameworkMixedClass = CrossFrameworkInitializer.InitMixed();

string fileName = $"test_file_{FrameworkName.ToLowerInvariant()}.tf";
string fullPath = Path.Combine(outputPath, fileName);

using (var fileStream = new FileStream(fullPath, FileMode.Create))
{
Serializer.Serialize(crossFrameworkClass, fileStream);
}

fileName = $"mixed_test_file_{FrameworkName.ToLowerInvariant()}.tf";
fullPath = Path.Combine(outputPath, fileName);
using (var fileStream = new FileStream(fullPath, FileMode.Create))
{
Serializer.Serialize(crossFrameworkMixedClass, fileStream);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Hyperion.Tests/Hyperion.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net461;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>net461;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
<LangVersion>latest</LangVersion>
<StartupObject>Hyperion.Tests.Generator.Program</StartupObject>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 4605dd3

Please sign in to comment.