Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions TestStack.ConventionTests.Tests/TypeBasedConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ApprovalTests.Reporters;
using NUnit.Framework;
using TestAssembly;
using TestAssembly.Dtos;
using TestStack.ConventionTests.ConventionData;
using TestStack.ConventionTests.Conventions;

Expand All @@ -17,13 +16,8 @@ public class TypeBasedConventions

public TypeBasedConventions()
{
var itemsToVerify = typeof (SampleDomainClass).Assembly.GetTypes()
.Where(t => t.IsClass && t.Namespace == typeof (SampleDomainClass).Namespace)
.ToArray();
nhibernateEntities = new Types("nHibernate Entitites")
{
TypesToVerify = itemsToVerify
};
nhibernateEntities = Types.InAssemblyOf<SampleDomainClass>("nHibernate Entitites",
types => types.Where(t => t.IsConcreteClass() && t.Namespace == typeof (SampleDomainClass).Namespace));
}

[Test]
Expand Down Expand Up @@ -57,10 +51,7 @@ public void all_methods_are_virtual_wth_approved_exceptions()
[Test]
public void dtos_exists_in_dto_namespace()
{
var types = new Types("TestAssembly types")
{
TypesToVerify = new[] { typeof(SomeDto), typeof(BlahDto), typeof(AnotherClass)}
};
var types = Types.InAssemblyOf<SomeDto>();
var convention = new ClassTypeHasSpecificNamespace(t => t.Name.EndsWith("Dto"), "TestAssembly.Dtos", "Dto");

var ex = Assert.Throws<ConventionFailedException>(() =>Convention.Is(convention, types));
Expand All @@ -70,10 +61,7 @@ public void dtos_exists_in_dto_namespace()
[Test]
public void dtos_exists_in_dto_namespace_wth_approved_exceptions()
{
var types = new Types("TestAssembly types")
{
TypesToVerify = new[] { typeof(SomeDto), typeof(BlahDto), typeof(AnotherClass) }
};
var types = Types.InAssemblyOf<SomeDto>();
var convention = new ClassTypeHasSpecificNamespace(t => t.Name.EndsWith("Dto"), "TestAssembly.Dtos", "Dto");

Convention.IsWithApprovedExeptions(convention, types);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
-------------------------------------------------------------------------------
'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly'
-------------------------------------------------------------------------

TestAssembly.SomeDto

'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
---------------------------------------------------------------------------------------
'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly'
---------------------------------------------------------------------------------

TestAssembly.Dtos.AnotherClass
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
-------------------------------------------------------------------------------
'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly'
-------------------------------------------------------------------------

TestAssembly.SomeDto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
---------------------------------------------------------------------------------------
'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly'
---------------------------------------------------------------------------------

TestAssembly.Dtos.AnotherClass
70 changes: 70 additions & 0 deletions TestStack.ConventionTests/ConventionData/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace TestStack.ConventionTests.ConventionData
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public static class TypeExtensions
{
public static bool IsConcreteClass(this Type t)
{
return t.IsClass && !t.IsAbstract;
}

public static bool IsEnum(this Type type)
{
return typeof(Enum).IsAssignableFrom(type);
}

public static bool IsStatic(this Type type)
{
return type.IsClass && !(type.IsSealed && type.IsAbstract);
}

public static bool HasDefaultConstructor(this Type type)
{
return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.DeclaredOnly)
.Any(constructorInfo => constructorInfo.GetParameters().Length == 0 && !constructorInfo.IsPrivate);
}

public static bool HasPublicDefaultConstructor(this Type type)
{
return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Any(constructorInfo => constructorInfo.GetParameters().Length == 0);
}

public static bool AssignableTo<TAssignableTo>(this Type type)
{
return typeof(TAssignableTo).IsAssignableFrom(type);
}

public static IEnumerable<MethodInfo> NonVirtualMethods(this Type type)
{
var methodInfos =
type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.DeclaredOnly);
return methodInfos.Where(methodInfo =>
!methodInfo.IsPrivate &&
methodInfo.DeclaringType == type &&
!methodInfo.Name.StartsWith("<"))
.Where(methodInfo => methodInfo.Name != "Equals")
.Where(methodInfo => !methodInfo.IsVirtual || methodInfo.IsFinal);
}

public static IEnumerable<Type> GetClosedInterfacesOf(this Type type, Type openGeneric)
{
return from i in type.GetInterfaces()
where i.IsGenericType
let defn = i.GetGenericTypeDefinition()
where defn == openGeneric
select i;
}

public static bool ClosesInterface(this Type t, Type openGeneric)
{
return t.GetClosedInterfacesOf(openGeneric).Any();
}
}
}
20 changes: 20 additions & 0 deletions TestStack.ConventionTests/ConventionData/Types.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace TestStack.ConventionTests.ConventionData
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

/// <summary>
Expand All @@ -18,5 +20,23 @@ public Types(string descriptionOfTypes)
public string Description { get; private set; }

public bool HasData {get { return TypesToVerify.Any(); }}

public static Types InAssemblyOf<T>()
{
var assembly = typeof(T).Assembly;
return new Types(assembly.GetName().Name)
{
TypesToVerify = assembly.GetTypes()
};
}

public static Types InAssemblyOf<T>(string descriptionOfTypes, Func<IEnumerable<Type>, IEnumerable<Type>> types)
{
var assembly = typeof (T).Assembly;
return new Types(descriptionOfTypes)
{
TypesToVerify = types(assembly.GetTypes()).ToArray()
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System.Linq;
using TestStack.ConventionTests.ConventionData;
using TestStack.ConventionTests.Internal;

public class AllClassesHaveDefaultConstructor : IConvention<Types>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
using System.Linq;
using TestStack.ConventionTests.ConventionData;
using TestStack.ConventionTests.Internal;

public class AllMethodsAreVirtual : IConvention<Types>
{
Expand Down
51 changes: 0 additions & 51 deletions TestStack.ConventionTests/Internal/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,56 +43,5 @@ public static Type[] SafeGetTypes(this Assembly assembly)
return Array.FindAll(ex.Types, x => x != null);
}
}

public static bool IsEnum(this Type type)
{
return typeof (Enum).IsAssignableFrom(type);
}

public static bool HasAttribute<TAttribute>(this Type type, bool inherit = true) where TAttribute : Attribute
{
return type.GetCustomAttributes(typeof (TAttribute), inherit).Length > 0;
}

public static bool IsStatic(this Type type)
{
return type.IsClass && !(type.IsSealed && type.IsAbstract);
}

public static bool HasDefaultConstructor(this Type type)
{
return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.DeclaredOnly)
.Any(constructorInfo => constructorInfo.GetParameters().Length == 0 && !constructorInfo.IsPrivate);
}

public static bool HasPublicDefaultConstructor(this Type type)
{
return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Any(constructorInfo => constructorInfo.GetParameters().Length == 0);
}

public static bool AssignableTo<TAssignableTo>(this Type type)
{
return typeof (TAssignableTo).IsAssignableFrom(type);
}

public static IEnumerable<Type> ConcreteTypes(this IEnumerable<Type> types)
{
return types.Where(t => t.IsClass && !t.IsAbstract);
}

public static IEnumerable<MethodInfo> NonVirtualMethods(this Type type)
{
var methodInfos =
type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.DeclaredOnly);
return methodInfos .Where(methodInfo =>
!methodInfo.IsPrivate &&
methodInfo.DeclaringType == type &&
!methodInfo.Name.StartsWith("<"))
.Where(methodInfo => methodInfo.Name != "Equals")
.Where(methodInfo => !methodInfo.IsVirtual || methodInfo.IsFinal);
}
}
}
3 changes: 2 additions & 1 deletion TestStack.ConventionTests/TestStack.ConventionTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConventionData\TypeExtensions.cs" />
<Compile Include="ConventionFailedException.cs" />
<Compile Include="IConventionResultContext.cs" />
<Compile Include="Internal\ConventionReportFailure.cs" />
Expand Down Expand Up @@ -121,6 +122,6 @@
</Target>
-->
<Target Name="AfterBuild">
<Copy SourceFiles="..\Readme.md" DestinationFiles="$(OutputPath)\Readme.txt" />
<Copy SourceFiles="..\Readme.md" DestinationFiles="$(OutputPath)\Readme.txt" Condition="'$(NCrunch)' != '1'" />
</Target>
</Project>