Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for #200 #201

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 92 additions & 4 deletions src/Hyperion.Tests/SurrogateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// -----------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -15,7 +16,7 @@
namespace Hyperion.Tests
{
#region test support classes

public interface IOriginal
{
ISurrogate ToSurrogate();
Expand All @@ -30,13 +31,30 @@ public class Foo : IOriginal
{
public string Bar { get; set; }

public Foo() { }

public Foo(string bar)
{
Bar = bar;
}

public ISurrogate ToSurrogate() => new FooSurrogate
{
Bar = Bar
};
}

public class FooSurrogate : ISurrogate
public class ChildFoo : Foo
{
public ChildFoo() { }

public ChildFoo(string bar)
{
Bar = bar;
}
}

public class FooSurrogate : ISurrogate, IEquatable<FooSurrogate>, IEquatable<Foo>
{
public string Bar { get; set; }

Expand All @@ -60,6 +78,33 @@ public Foo Restore()
Bar = Bar
};
}

public bool Equals(FooSurrogate other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(Bar, other.Bar);
}

public bool Equals(Foo other)
{
if (other == null) return false;
return Equals(other.ToSurrogate());
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
var foo = obj as Foo;
if (foo != null) return Equals(foo);
return Equals(obj as FooSurrogate);
}

public override int GetHashCode()
{
return Bar.GetHashCode();
}
}

public class SurrogatedKey : IOriginal
Expand All @@ -72,7 +117,7 @@ public SurrogatedKey(string key)
}

public ISurrogate ToSurrogate() => new KeySurrogate(Key);
public override bool Equals(object obj) => obj is SurrogatedKey && Key == ((SurrogatedKey) obj).Key;
public override bool Equals(object obj) => obj is SurrogatedKey && Key == ((SurrogatedKey)obj).Key;
public override int GetHashCode() => Key?.GetHashCode() ?? 0;
}

Expand Down Expand Up @@ -172,11 +217,54 @@ public void CanSerializeWithSurrogateInCollection()
serializer.Serialize(dictionary, stream);
stream.Position = 0;

var actual = serializer.Deserialize<Dictionary<SurrogatedKey, Foo>> (stream);
var actual = serializer.Deserialize<Dictionary<SurrogatedKey, Foo>>(stream);

Assert.Equal(key, actual.Keys.First());
Assert.Equal(foo.Bar, actual[key].Bar);
Assert.Equal(2, invoked.Count);
}

[Fact]
public void CanDeserializeSurrogateWithIEquatableInsideArrays()
{
var surrogating = 0;
var desurrogate = new List<ISurrogate>();
var serializer = new Serializer(new SerializerOptions(
preserveObjectReferences: true,
surrogates: new[]
{
Surrogate.Create<IOriginal, ISurrogate>(
from =>
{
surrogating++;
return from.ToSurrogate();
},
to =>
{
desurrogate.Add(to);
return to.FromSurrogate();
}
),
}));

var stream = new MemoryStream();
var expected = new Foo[]
{
new ChildFoo("one"),
new ChildFoo("two"),
new ChildFoo("one"),
};

serializer.Serialize(expected, stream);
stream.Position = 0;

var actual = (Foo[])serializer.Deserialize<object>(stream);

Assert.Equal(expected.Length, actual.Length);
Assert.Equal(expected[0].Bar, actual[0].Bar);
Assert.Equal(3, desurrogate.Count);
Assert.Equal(3, surrogating);
}

}
}