Skip to content

Commit

Permalink
More spec test separation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Mar 8, 2019
1 parent 0af5802 commit 1a7b3a4
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 47 deletions.
27 changes: 27 additions & 0 deletions test/Autofac.Specification.Test/Lifetime/ExternallyOwned.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Autofac.Specification.Test.Util;
using Xunit;

namespace Autofac.Specification.Test.Lifetime
{
public class ExternallyOwned
{
[Fact]
public void RootInstancesNotDisposedOnContainerDisposal()
{
var cb = new ContainerBuilder();
cb.RegisterType<A>().ExternallyOwned();
var c = cb.Build();
var a1 = c.Resolve<A>();
var a2 = c.Resolve<A>();
c.Dispose();

Assert.False(a1.IsDisposed);
Assert.False(a2.IsDisposed);
}

private class A : DisposeTracker
{
}
}
}
38 changes: 38 additions & 0 deletions test/Autofac.Specification.Test/Resolution/ComplexGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Autofac.Specification.Test.Resolution.Graph1;
using Xunit;

namespace Autofac.Specification.Test.Resolution
{
public class ComplexGraph
{
[Fact]
public void CanCorrectlyBuildGraph1()
{
var builder = new ContainerBuilder();

builder.RegisterType<A1>().SingleInstance();
builder.RegisterType<CD1>().As<IC1, ID1>().SingleInstance();
builder.RegisterType<E1>().SingleInstance();
builder.Register(ctr => new B1(ctr.Resolve<A1>()));

var target = builder.Build();

var e = target.Resolve<E1>();
var a = target.Resolve<A1>();
var b = target.Resolve<B1>();
var c = target.Resolve<IC1>();
var d = target.Resolve<ID1>();

Assert.IsType<CD1>(c);
var cd = (CD1)c;

Assert.Same(a, b.A);
Assert.Same(a, cd.A);
Assert.NotSame(b, cd.B);
Assert.Same(c, e.C);
Assert.NotSame(b, e.B);
Assert.NotSame(e.B, cd.B);
}
}
}
12 changes: 12 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/A1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Linq;
using Autofac.Specification.Test.Util;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public class A1 : DisposeTracker
{
}
}
18 changes: 18 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/B1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Linq;
using Autofac.Specification.Test.Util;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public class B1 : DisposeTracker
{
public B1(A1 a)
{
this.A = a;
}

public A1 A { get; private set; }
}
}
18 changes: 18 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/C1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Linq;
using Autofac.Specification.Test.Util;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public class C1 : DisposeTracker
{
public C1(B1 b)
{
this.B = b;
}

public B1 B { get; private set; }
}
}
21 changes: 21 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/CD1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq;
using Autofac.Specification.Test.Util;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public class CD1 : DisposeTracker, IC1, ID1
{
public CD1(A1 a, B1 b)
{
this.A = a;
this.B = b;
}

public A1 A { get; private set; }

public B1 B { get; private set; }
}
}
21 changes: 21 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/E1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq;
using Autofac.Specification.Test.Util;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public class E1 : DisposeTracker
{
public E1(B1 b, IC1 c)
{
this.B = b;
this.C = c;
}

public B1 B { get; private set; }

public IC1 C { get; private set; }
}
}
18 changes: 18 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/F1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public class F1
{
public F1(IList<A1> aList)
{
this.AList = aList;
}

public IList<A1> AList { get; private set; }
}
}
11 changes: 11 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/IC1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Linq;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public interface IC1
{
}
}
11 changes: 11 additions & 0 deletions test/Autofac.Specification.Test/Resolution/Graph1/ID1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Linq;

namespace Autofac.Specification.Test.Resolution.Graph1
{
// In the below scenario, B1 depends on A1, CD depends on A1 and B1,
// and E depends on IC1 and B1.
public interface ID1
{
}
}
17 changes: 17 additions & 0 deletions test/Autofac.Specification.Test/Util/DisposeTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Autofac.Specification.Test.Util
{
public class DisposeTracker : IDisposable
{
public event EventHandler<EventArgs> Disposing;

public bool IsDisposed { get; set; }

public void Dispose()
{
this.IsDisposed = true;
this.Disposing?.Invoke(this, EventArgs.Empty);
}
}
}
18 changes: 0 additions & 18 deletions test/Autofac.Test/ContainerBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,6 @@ public class Abc : DisposeTracker, IA, IB, IC
{
}

[Fact]
public void WithExternalFactory()
{
var cb = new ContainerBuilder();
cb.RegisterType<Abc>()
.As<IA>()
.ExternallyOwned();
var c = cb.Build();
var a1 = c.Resolve<IA>();
var a2 = c.Resolve<IA>();
c.Dispose();

Assert.NotNull(a1);
Assert.NotSame(a1, a2);
Assert.False(((Abc)a1).IsDisposed);
Assert.False(((Abc)a2).IsDisposed);
}

[Fact]
public void WithInternalSingleton()
{
Expand Down
29 changes: 0 additions & 29 deletions test/Autofac.Test/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,6 @@ namespace Autofac.Test
{
public class IntegrationTests
{
[Fact]
public void CanCorrectlyBuildGraph1()
{
var builder = new ContainerBuilder();

builder.RegisterType<A1>().SingleInstance();
builder.RegisterType<CD1>().As<IC1, ID1>().SingleInstance();
builder.RegisterType<E1>().SingleInstance();
builder.Register(ctr => new B1(ctr.Resolve<A1>()));

var target = builder.Build();

var e = target.Resolve<E1>();
var a = target.Resolve<A1>();
var b = target.Resolve<B1>();
var c = target.Resolve<IC1>();
var d = target.Resolve<ID1>();

Assert.IsType<CD1>(c);
var cd = (CD1)c;

Assert.Same(a, b.A);
Assert.Same(a, cd.A);
Assert.NotSame(b, cd.B);
Assert.Same(c, e.C);
Assert.NotSame(b, e.B);
Assert.NotSame(e.B, cd.B);
}

[Fact]
public void DetectsAndIdentifiesCircularDependencies()
{
Expand Down

0 comments on commit 1a7b3a4

Please sign in to comment.