-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor, added constructor selection ability #13
- Loading branch information
Peter Csajtai
committed
May 9, 2017
1 parent
a0be113
commit a0ec1ee
Showing
18 changed files
with
334 additions
and
143 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Stashbox.Exceptions; | ||
|
||
namespace Stashbox.Tests | ||
{ | ||
[TestClass] | ||
public class ConstructorSelectionTests | ||
{ | ||
[TestMethod] | ||
public void ConstructorSelectionTests_ArgTypes() | ||
{ | ||
using (var container = new StashboxContainer(config => config.WithUnknownTypeResolution())) | ||
{ | ||
container.RegisterType<Test>(context => context.WithConstructorByArgumentTypes(typeof(Dep), typeof(Dep2))); | ||
container.Resolve<Test>(); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void ConstructorSelectionTests_Args() | ||
{ | ||
using (var container = new StashboxContainer(config => config.WithUnknownTypeResolution())) | ||
{ | ||
var dep = new Dep(); | ||
var dep2 = new Dep2(); | ||
|
||
container.RegisterType<Test>(context => context.WithConstructorByArguments(dep, dep2)); | ||
var test = container.Resolve<Test>(); | ||
|
||
Assert.AreSame(dep, test.Dep); | ||
Assert.AreSame(dep2, test.Dep2); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ResolutionFailedException))] | ||
public void ConstructorSelectionTests_ArgTypes_Throws_ResolutionFailed() | ||
{ | ||
using (var container = new StashboxContainer()) | ||
{ | ||
container.RegisterType<Test>(context => context.WithConstructorByArgumentTypes(typeof(Dep), typeof(Dep2))); | ||
container.Resolve<Test>(); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ConstructorNotFoundException))] | ||
public void ConstructorSelectionTests_ArgTypes_Throws_MissingConstructor() | ||
{ | ||
using (var container = new StashboxContainer()) | ||
{ | ||
container.RegisterType<Test>(context => context.WithConstructorByArgumentTypes()); | ||
container.Resolve<Test>(); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ConstructorNotFoundException))] | ||
public void ConstructorSelectionTests_Args_Throws_MissingConstructor() | ||
{ | ||
using (var container = new StashboxContainer()) | ||
{ | ||
container.RegisterType<Test>(context => context.WithConstructorByArguments()); | ||
container.Resolve<Test>(); | ||
} | ||
} | ||
|
||
public class Dep | ||
{ } | ||
|
||
public class Dep2 | ||
{ } | ||
|
||
public class Dep3 | ||
{ } | ||
|
||
public class Test | ||
{ | ||
public Dep Dep { get; } | ||
public Dep2 Dep2 { get; } | ||
|
||
public Test(Dep dep) | ||
{ | ||
Assert.Fail("wrong constructor"); | ||
} | ||
|
||
public Test(Dep dep, Dep2 dep2) | ||
{ | ||
this.Dep = dep; | ||
this.Dep2 = dep2; | ||
} | ||
|
||
public Test(Dep dep, Dep2 dep2, Dep3 dep3) | ||
{ | ||
Assert.Fail("wrong constructor"); | ||
} | ||
} | ||
} | ||
} |
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
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
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 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Stashbox.Exceptions | ||
{ | ||
/// <summary> | ||
/// Represents a constructor not found exception. | ||
/// </summary> | ||
public class ConstructorNotFoundException : Exception | ||
{ | ||
/// <summary> | ||
/// Constructs a <see cref="ConstructorNotFoundException"/>. | ||
/// </summary> | ||
/// <param name="type">The type on the constructor was not found.</param> | ||
/// <param name="argumentsLength">The length of the arguments.</param> | ||
/// <param name="innerException">The inner exception</param> | ||
public ConstructorNotFoundException(Type type, int argumentsLength, Exception innerException = null) : | ||
base($"Constructor not found for {type.FullName} with {argumentsLength} arguments.", innerException) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.