forked from autofac/Autofac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to DNX beta8 and added additional unit tests from ASP.NET dep…
…endency injection repository (will be replaced with test package when available).
- Loading branch information
Showing
43 changed files
with
993 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"sdk": { "version": "1.0.0-beta8-15736" }, | ||
"sdk": { "version": "1.0.0-beta8" }, | ||
"sources": ["src", "test", "samples"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
test/Autofac.Extensions.DependencyInjection.Test/Specification/AllContainerTestsBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Framework.DependencyInjection.Tests.Fakes; | ||
using Xunit; | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests | ||
{ | ||
public abstract class AllContainerTestsBase | ||
{ | ||
protected abstract IServiceProvider CreateContainer(); | ||
|
||
[Fact] | ||
public void SingleServiceCanBeResolved() | ||
{ | ||
var container = CreateContainer(); | ||
|
||
var service = container.GetService<IFakeService>(); | ||
|
||
Assert.NotNull(service); | ||
Assert.Equal("FakeServiceSimpleMethod", service.SimpleMethod()); | ||
} | ||
|
||
[Fact] | ||
public void ServiceInstanceCanBeResolved() | ||
{ | ||
var container = CreateContainer(); | ||
|
||
var service = container.GetService<IFakeServiceInstance>(); | ||
|
||
Assert.NotNull(service); | ||
Assert.Equal("Instance", service.SimpleMethod()); | ||
} | ||
|
||
[Fact] | ||
public void TransientServiceCanBeResolved() | ||
{ | ||
var container = CreateContainer(); | ||
|
||
var service1 = container.GetService<IFakeService>(); | ||
var service2 = container.GetService<IFakeService>(); | ||
|
||
Assert.NotNull(service1); | ||
Assert.NotEqual(service1, service2); | ||
} | ||
|
||
[Fact] | ||
public void SingleServiceCanBeIEnumerableResolved() | ||
{ | ||
var container = CreateContainer(); | ||
|
||
var services = container.GetService<IEnumerable<IFakeService>>(); | ||
|
||
Assert.NotNull(services); | ||
Assert.Equal(1, services.Count()); | ||
Assert.Equal("FakeServiceSimpleMethod", services.Single().SimpleMethod()); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServiceCanBeIEnumerableResolved() | ||
{ | ||
var container = CreateContainer(); | ||
|
||
var services = container.GetService<IEnumerable<IFakeMultipleService>>(); | ||
|
||
var results = services.Select(x => x.SimpleMethod()).ToArray(); | ||
|
||
Assert.NotNull(results); | ||
Assert.Equal(2, results.Count()); | ||
Assert.Contains("FakeOneMultipleServiceAnotherMethod", results); | ||
Assert.Contains("FakeTwoMultipleServiceAnotherMethod", results); | ||
} | ||
|
||
[Fact] | ||
public void OuterServiceCanHaveOtherServicesInjected() | ||
{ | ||
var container = CreateContainer(); | ||
|
||
var service = container.GetService<IFakeOuterService>(); | ||
|
||
string singleValue; | ||
string[] multipleValues; | ||
service.Interrogate(out singleValue, out multipleValues); | ||
|
||
Assert.NotNull(service); | ||
Assert.Equal(2, multipleValues.Count()); | ||
Assert.Contains("FakeServiceSimpleMethod", singleValue); | ||
Assert.Contains("FakeOneMultipleServiceAnotherMethod", multipleValues); | ||
Assert.Contains("FakeTwoMultipleServiceAnotherMethod", multipleValues); | ||
} | ||
|
||
[Fact] | ||
public void FactoryServicesCanBeCreatedByGetService() | ||
{ | ||
// Arrange | ||
var container = CreateContainer(); | ||
|
||
// Act | ||
var service = container.GetService<IFactoryService>(); | ||
|
||
// Assert | ||
Assert.Equal(42, service.Value); | ||
Assert.NotNull(service.FakeService); | ||
} | ||
|
||
[Fact] | ||
public void FactoryServicesAreCreatedAsPartOfCreatingObjectGraph() | ||
{ | ||
// Arrange | ||
var container = CreateContainer(); | ||
|
||
// Act | ||
var service1 = container.GetService<ServiceAcceptingFactoryService>(); | ||
var service2 = container.GetService<ServiceAcceptingFactoryService>(); | ||
|
||
// Assert | ||
Assert.Equal(42, service1.TransientService.Value); | ||
Assert.NotNull(service1.TransientService.FakeService); | ||
|
||
Assert.Equal(42, service2.TransientService.Value); | ||
Assert.NotNull(service2.TransientService.FakeService); | ||
|
||
Assert.NotNull(service1.ScopedService.FakeService); | ||
|
||
// Verify scoping works | ||
Assert.NotSame(service1.TransientService, service2.TransientService); | ||
Assert.Same(service1.ScopedService, service2.ScopedService); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
test/Autofac.Extensions.DependencyInjection.Test/Specification/AutofacContainerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Autofac; | ||
using Autofac.Extensions.DependencyInjection; | ||
using Microsoft.Framework.DependencyInjection.Tests.Fakes; | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests | ||
{ | ||
public class AutofacContainerTests : ScopingContainerTestBase | ||
{ | ||
protected override IServiceProvider CreateContainer() | ||
{ | ||
var builder = new ContainerBuilder(); | ||
|
||
builder.Populate(TestServices.DefaultServices()); | ||
|
||
IContainer container = builder.Build(); | ||
return container.Resolve<IServiceProvider>(); | ||
} | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
test/Autofac.Extensions.DependencyInjection.Test/Specification/Fakes/AbstractClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public abstract class AbstractClass | ||
{ | ||
public AbstractClass() | ||
{ | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/Autofac.Extensions.DependencyInjection.Test/Specification/Fakes/AnotherClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class AnotherClass | ||
{ | ||
private readonly IFakeService _fakeService; | ||
|
||
public AnotherClass(IFakeService fakeService) | ||
{ | ||
_fakeService = fakeService; | ||
} | ||
|
||
public string LessSimpleMethod() | ||
{ | ||
return "[" + _fakeService.SimpleMethod() + "]"; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ofac.Extensions.DependencyInjection.Test/Specification/Fakes/AnotherClassAcceptingData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class AnotherClassAcceptingData | ||
{ | ||
private readonly IFakeService _fakeService; | ||
private readonly string _one; | ||
private readonly string _two; | ||
|
||
public AnotherClassAcceptingData(IFakeService fakeService, string one, string two) | ||
{ | ||
_fakeService = fakeService; | ||
_one = one; | ||
_two = two; | ||
} | ||
|
||
public string LessSimpleMethod() | ||
{ | ||
return string.Format("[{0}] {1} {2}", _fakeService.SimpleMethod(), _one, _two); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...utofac.Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithAmbiguousCtors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class ClassWithAmbiguousCtors | ||
{ | ||
public ClassWithAmbiguousCtors(string data) | ||
{ | ||
} | ||
|
||
public ClassWithAmbiguousCtors(IFakeService service, string data) | ||
{ | ||
} | ||
|
||
public ClassWithAmbiguousCtors(IFakeService service, int data) | ||
{ | ||
} | ||
|
||
public ClassWithAmbiguousCtors(IFakeService service, string data1, int data2) | ||
{ | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...c.Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithInternalConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection | ||
{ | ||
public class ClassWithInternalConstructor | ||
{ | ||
internal ClassWithInternalConstructor() | ||
{ | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...sions.DependencyInjection.Test/Specification/Fakes/ClassWithNestedReferencesToProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class ClassWithNestedReferencesToProvider : IDisposable | ||
{ | ||
private IServiceProvider _serviceProvider; | ||
private ClassWithNestedReferencesToProvider _nested; | ||
|
||
public ClassWithNestedReferencesToProvider(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_nested = new ClassWithNestedReferencesToProvider(_serviceProvider, 0); | ||
} | ||
|
||
private ClassWithNestedReferencesToProvider(IServiceProvider serviceProvider, int level) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
if (level > 1) | ||
{ | ||
_nested = new ClassWithNestedReferencesToProvider(_serviceProvider, level + 1); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_nested?.Dispose(); | ||
(_serviceProvider as IDisposable)?.Dispose(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ofac.Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithOptionalArgsCtor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class ClassWithOptionalArgsCtor | ||
{ | ||
public ClassWithOptionalArgsCtor(string whatever = "BLARGH") | ||
{ | ||
Whatever = whatever; | ||
} | ||
|
||
public string Whatever { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Autofac.Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithPrivateCtor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class ClassWithPrivateCtor | ||
{ | ||
private ClassWithPrivateCtor() | ||
{ | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
....Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithProtectedConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection | ||
{ | ||
public class ClassWithProtectedConstructor | ||
{ | ||
internal ClassWithProtectedConstructor() | ||
{ | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
test/Autofac.Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithStaticCtor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class ClassWithStaticCtor | ||
{ | ||
static ClassWithStaticCtor() | ||
{ | ||
|
||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../Autofac.Extensions.DependencyInjection.Test/Specification/Fakes/ClassWithThrowingCtor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes | ||
{ | ||
public class ClassWithThrowingCtor | ||
{ | ||
public ClassWithThrowingCtor(IFakeService service) | ||
{ | ||
throw new Exception(nameof(ClassWithThrowingCtor)); | ||
} | ||
} | ||
} |
Oops, something went wrong.