Skip to content

Commit 8f9545a

Browse files
committed
Add ThatDeriveFrom() extension method
1 parent 46ab5a5 commit 8f9545a

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<ItemGroup>
6161
<Compile Include="KernelAssertionTests.cs" />
6262
<Compile Include="Properties\AssemblyInfo.cs" />
63+
<Compile Include="TypeExtensionTests.cs" />
6364
</ItemGroup>
6465
<ItemGroup>
6566
<None Include="app.config" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using NUnit.Framework;
2+
3+
namespace FluentAssertions.Ioc.Ninject.Tests
4+
{
5+
[TestFixture]
6+
public class TypeExtensionTests
7+
{
8+
[Test]
9+
public void ThatDerivesFrom_returns_expected_types()
10+
{
11+
// Arrange
12+
var expected = new [] { typeof(Bar) };
13+
14+
// Act
15+
var actual = FindAssembly.Containing<Foo>().GetTypes().ThatDeriveFrom<Foo>();
16+
17+
// Assert
18+
actual.ShouldBeEquivalentTo(expected);
19+
}
20+
}
21+
22+
public class Foo { }
23+
24+
public class Bar : Foo { }
25+
}

src/FluentAssertions.Ioc.Ninject/TypeExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@ public static IEnumerable<Type> EndingWith(this IEnumerable<Type> types, string
3535
{
3636
return types.Where(x => x.Name.EndsWith(endingWith));
3737
}
38+
39+
/// <summary>
40+
/// Filters to only include types which are derived from TBase.
41+
/// </summary>
42+
/// <typeparam name="TBase">The base class filtered types should derive from.</typeparam>
43+
/// <param name="types">The types to filer.</param>
44+
public static IEnumerable<Type> ThatDeriveFrom<TBase>(this IEnumerable<Type> types)
45+
{
46+
return types.Where(x => x.IsSubclassOf(typeof (TBase)));
47+
}
3848
}
3949
}

0 commit comments

Comments
 (0)