Skip to content

Commit a1227ef

Browse files
committed
Clean up api surface
1 parent f3a50db commit a1227ef

File tree

7 files changed

+209
-106
lines changed

7 files changed

+209
-106
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using FluentAssertions.Execution;
6+
using FluentAssertions.Ioc.Ninject.Internal;
7+
using Ninject;
8+
9+
namespace FluentAssertions.Ioc.Ninject
10+
{
11+
public class CollectionOfTypesKernelAssertions
12+
{
13+
private readonly IKernel kernel;
14+
private readonly IEnumerable<Type> types;
15+
16+
protected internal CollectionOfTypesKernelAssertions(IKernel kernel, IEnumerable<Type> types)
17+
{
18+
this.kernel = kernel;
19+
this.types = types;
20+
}
21+
22+
/// <summary>
23+
/// Asserts that the selected types can be resolved with a single instance.
24+
/// </summary>
25+
public void WithSingleInstance()
26+
{
27+
var failed = new List<ActivationError>();
28+
29+
// Try to activate each type in the list
30+
foreach (var type in types)
31+
{
32+
var activationError = kernel.GetSingleInstanceActivationError(type);
33+
34+
if (activationError != null)
35+
{
36+
failed.Add(activationError);
37+
}
38+
}
39+
40+
// Assert
41+
Execute.Assertion.ForCondition(!failed.Any())
42+
.FailWith(BuildFailureMessage(failed));
43+
}
44+
45+
/// <summary>
46+
/// Asserts that the selected interfaces can be resolved with at least one instance.
47+
/// </summary>
48+
public void WithAtLeastOneInstance()
49+
{
50+
var failed = new List<ActivationError>();
51+
52+
// Try to activate each interface in the list
53+
foreach (var type in types)
54+
{
55+
var error = kernel.GetAllInstancesActivationError(type);
56+
57+
if (error != null)
58+
{
59+
failed.Add(error);
60+
}
61+
62+
}
63+
64+
// Assert
65+
Execute.Assertion.ForCondition(!failed.Any())
66+
.FailWith(BuildFailureMessage(failed));
67+
}
68+
69+
private string BuildFailureMessage(List<ActivationError> failed)
70+
{
71+
// Build the failure message
72+
var builder = new StringBuilder();
73+
builder.AppendLine("The following types could not be resolved:").AppendLine();
74+
75+
foreach (var error in failed)
76+
builder.AppendFormat(" - {0}", error.Type.FullName).AppendLine();
77+
78+
return builder.ToString();
79+
}
80+
}
81+
}

src/FluentAssertions.Ioc.Ninject/FluentAssertions.Ioc.Ninject.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="Internal\ActivationError.cs" />
5556
<Compile Include="AssemblyExtensions.cs" />
57+
<Compile Include="CollectionOfTypesKernelAssertions.cs" />
5658
<Compile Include="FindAssembly.cs" />
59+
<Compile Include="Internal\KernelExtensions.cs" />
5760
<Compile Include="KernelAssertions.cs" />
5861
<Compile Include="KernelExtensions.cs" />
5962
<Compile Include="Properties\AssemblyInfo.cs" />
63+
<Compile Include="SingleTypeKernelAssertions.cs" />
6064
</ItemGroup>
6165
<ItemGroup>
6266
<None Include="FluentAssertions.Ioc.Ninject.nuspec" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using Ninject;
3+
4+
namespace FluentAssertions.Ioc.Ninject.Internal
5+
{
6+
class ActivationError
7+
{
8+
public Type Type { get; set; }
9+
public ActivationException Exception { get; set; }
10+
}
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Linq;
3+
using Ninject;
4+
5+
namespace FluentAssertions.Ioc.Ninject.Internal
6+
{
7+
internal static class KernelExtensions
8+
{
9+
public static ActivationError GetSingleInstanceActivationError(this IKernel kernel, Type type)
10+
{
11+
ActivationError error = null;
12+
13+
try
14+
{
15+
kernel.Get(type);
16+
}
17+
catch (ActivationException ex)
18+
{
19+
error = new ActivationError { Type = type, Exception = ex };
20+
}
21+
22+
return error;
23+
}
24+
25+
public static ActivationError GetAllInstancesActivationError(this IKernel kernel, Type type)
26+
{
27+
ActivationError error = null;
28+
29+
try
30+
{
31+
var instances = kernel.GetAll(type);
32+
if (!instances.Any())
33+
{
34+
error = new ActivationError { Type = type };
35+
}
36+
}
37+
catch (ActivationException ex)
38+
{
39+
error = new ActivationError { Type = type, Exception = ex };
40+
}
41+
42+
return error;
43+
}
44+
}
45+
}
Lines changed: 5 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using FluentAssertions.Execution;
63
using Ninject;
74

85
namespace FluentAssertions.Ioc.Ninject
@@ -21,121 +18,24 @@ protected internal KernelAssertions(IKernel kernel)
2118
/// Gets the kernel which is being asserted.
2219
/// </summary>
2320
public IKernel Subject { get; private set; }
24-
25-
/// <summary>
26-
/// Gets the types to be resolved.
27-
/// </summary>
28-
private IEnumerable<Type> Types { get; set; }
29-
21+
3022
/// <summary>
3123
/// Specifies the types to try resolving from the kernel.
3224
/// </summary>
3325
/// <param name="types">The types to resolve.</param>
34-
public KernelAssertions Resolve(IEnumerable<Type> types)
26+
public CollectionOfTypesKernelAssertions Resolve(IEnumerable<Type> types)
3527
{
36-
Types = types;
37-
return this;
28+
return new CollectionOfTypesKernelAssertions(Subject, types);
3829
}
3930

4031
/// <summary>
4132
/// Specifies the type to try resolving from the kernel.
4233
/// </summary>
4334
/// <typeparam name="T">The type to resolve.</typeparam>
4435
/// <returns></returns>
45-
public KernelAssertions Resolve<T>()
46-
{
47-
Types = new List<Type>() { typeof(T) };
48-
return this;
49-
}
50-
51-
/// <summary>
52-
/// Asserts that the selected type(s) can be resolved with a single instance.
53-
/// </summary>
54-
public void WithSingleInstance()
55-
{
56-
var failed = new List<ActivationError>();
57-
58-
// Try to activate each type in the list
59-
foreach (var type in Types)
60-
{
61-
try
62-
{
63-
Subject.Get(type);
64-
65-
}
66-
catch (ActivationException ex)
67-
{
68-
failed.Add(new ActivationError() { Type = type, Exception = ex });
69-
}
70-
}
71-
72-
// Assert
73-
Execute.Assertion.ForCondition(failed.Count() == 0)
74-
.FailWith(BuildFailureMessage(failed));
75-
}
76-
77-
/// <summary>
78-
/// Asserts that the selected interface(s) can be resolved with at least one instance.
79-
/// </summary>
80-
public void WithAtLeastOneInstance()
81-
{
82-
var failed = new List<ActivationError>();
83-
84-
// Try to activate each interface in the list
85-
foreach (var type in Types)
86-
{
87-
try
88-
{
89-
var instances = Subject.GetAll(type);
90-
if (!instances.Any())
91-
{
92-
failed.Add(new ActivationError() { Type = type });
93-
}
94-
}
95-
catch (ActivationException ex)
96-
{
97-
failed.Add(new ActivationError() { Type = type, Exception = ex });
98-
}
99-
}
100-
101-
// Assert
102-
Execute.Assertion.ForCondition(failed.Count() == 0)
103-
.FailWith(BuildFailureMessage(failed));
104-
}
105-
106-
/// <summary>
107-
/// Asserts that the specified interface resolves to a concrete type.
108-
/// </summary>
109-
/// <typeparam name="T">The expected concrete type.</typeparam>
110-
public void To<T>()
111-
{
112-
foreach (var type in Types)
113-
{
114-
var actual = Subject.Get(type).GetType();
115-
var expected = typeof (T);
116-
117-
Execute.Assertion.ForCondition(actual == expected)
118-
.FailWith("Expected interface {0} to resolve to type {1}, but found {2} instead", type.Name, expected.Name, actual.Name);
119-
120-
}
121-
}
122-
123-
private string BuildFailureMessage(List<ActivationError> failed)
124-
{
125-
// Build the failure message
126-
var builder = new StringBuilder();
127-
builder.AppendLine("The following types could not be resolved:").AppendLine();
128-
129-
foreach (var error in failed)
130-
builder.AppendFormat(" - {0}", error.Type.FullName).AppendLine();
131-
132-
return builder.ToString();
133-
}
134-
135-
class ActivationError
36+
public SingleTypeKernelAssertions Resolve<T>()
13637
{
137-
public Type Type { get; set; }
138-
public ActivationException Exception { get; set; }
38+
return new SingleTypeKernelAssertions(Subject, typeof (T));
13939
}
14040
}
14141
}

src/FluentAssertions.Ioc.Ninject/KernelExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Ninject;
1+
using System;
2+
using Ninject;
23

34
namespace FluentAssertions.Ioc.Ninject
45
{
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using FluentAssertions.Execution;
3+
using FluentAssertions.Ioc.Ninject.Internal;
4+
using Ninject;
5+
6+
namespace FluentAssertions.Ioc.Ninject
7+
{
8+
public class SingleTypeKernelAssertions
9+
{
10+
private readonly IKernel kernel;
11+
private readonly Type type;
12+
13+
protected internal SingleTypeKernelAssertions(IKernel kernel, Type type)
14+
{
15+
this.kernel = kernel;
16+
this.type = type;
17+
}
18+
19+
/// <summary>
20+
/// Asserts that the selected type can be resolved with a single instance.
21+
/// </summary>
22+
public void WithSingleInstance()
23+
{
24+
var error = kernel.GetSingleInstanceActivationError(type);
25+
26+
// Assert
27+
Execute.Assertion.ForCondition(error == null)
28+
.FailWith(BuildFailureMessage(error));
29+
}
30+
31+
/// <summary>
32+
/// Asserts that the selected interface can be resolved with at least one instance.
33+
/// </summary>
34+
public void WithAtLeastOneInstance()
35+
{
36+
var error = kernel.GetAllInstancesActivationError(type);
37+
38+
// Assert
39+
Execute.Assertion.ForCondition(error == null)
40+
.FailWith(BuildFailureMessage(error));
41+
}
42+
43+
/// <summary>
44+
/// Asserts that the specified interface resolves to a concrete type.
45+
/// </summary>
46+
/// <typeparam name="T">The expected concrete type.</typeparam>
47+
public void To<T>()
48+
{
49+
var actual = kernel.Get(type).GetType();
50+
var expected = typeof (T);
51+
52+
Execute.Assertion.ForCondition(actual == expected)
53+
.FailWith("Expected interface {0} to resolve to type {1}, but found {2} instead", type.Name, expected.Name, actual.Name);
54+
}
55+
56+
private string BuildFailureMessage(ActivationError error)
57+
{
58+
return String.Format("Unable to resolve type {0}", error.Type.FullName);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)