Skip to content

Commit

Permalink
Improved test with service location.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo committed Sep 19, 2020
1 parent e8cc4cf commit 4ac9d77
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue451Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public sealed class Issue451Tests
[Fact]
public void Verify()
{
var provider = new ServiceLocator();
var serializer = new ConfigurationContainer().Type<Subject>()
.WithInterceptor(Interceptor.Default)
.WithInterceptor(new Interceptor(provider))
.Create()
.ForTesting();

Expand All @@ -31,19 +32,26 @@ public void Verify()
cycled.Count.Should().Be(2);
}

sealed class ServiceLocator : IServiceProvider
{
public object GetService(Type serviceType) => typeof(Subject).IsAssignableFrom(serviceType)
? new ActivatedSubject()
: throw new InvalidOperationException();
}

sealed class Interceptor : ISerializationInterceptor<Subject>
{
public static Interceptor Default { get; } = new Interceptor();
readonly IServiceProvider _provider;

Interceptor() {}
public Interceptor(IServiceProvider provider) => _provider = provider;

public Subject Serializing(IFormatWriter writer, Subject instance)
{
instance.Count++;
return instance;
}

public Subject Activating(Type instanceType) => new ActivatedSubject();
public Subject Activating(Type instanceType) => (Subject)_provider.GetService(instanceType);

public Subject Deserialized(IFormatReader reader, Subject instance)
{
Expand Down

0 comments on commit 4ac9d77

Please sign in to comment.