Skip to content

Commit

Permalink
Basic test demonstrating serializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Sep 10, 2020
1 parent 82371f6 commit 87a4948
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue440Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ContentModel;
using ExtendedXmlSerializer.ContentModel.Format;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using System;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue440Tests
{
[Fact]
public void Verify()
{
var container = new ConfigurationContainer().Type<IInterface>()
.Register()
.Serializer()
.Using(Serializer.Default)
.Create()
.ForTesting();
var instance = new Container {Interface = new Implementation()};
container.Cycle(instance)
.Interface.Should()
.BeOfType<Implementation>()
.And.Subject.To<Implementation>()
.Created.Should()
.BeTrue();
}

sealed class Container
{
public IInterface Interface { get; set; }
}

sealed class Serializer : ISerializer<IInterface>
{
public static Serializer Default { get; } = new Serializer();

Serializer() {}

public IInterface Get(IFormatReader parameter)
{
var name = parameter.Content();
var type = Type.GetType(name) ?? throw new InvalidOperationException($"Could not parse '{name}'");
var result = (Implementation)Activator.CreateInstance(type);
result.Created = true;
return result;
}

public void Write(IFormatWriter writer, IInterface instance)
{
writer.Content(instance.GetType().AssemblyQualifiedName);
}
}

public interface IInterface {}

public sealed class Implementation : IInterface
{
public bool Created { get; set; }
}
}
}

0 comments on commit 87a4948

Please sign in to comment.