diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.cs b/TestStack.ConventionTests.Tests/TypeBasedConventions.cs index 27d0aa5..c975a3d 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.cs +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.cs @@ -5,7 +5,6 @@ using ApprovalTests.Reporters; using NUnit.Framework; using TestAssembly; - using TestAssembly.Dtos; using TestStack.ConventionTests.ConventionData; using TestStack.ConventionTests.Conventions; @@ -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("nHibernate Entitites", + types => types.Where(t => t.IsConcreteClass() && t.Namespace == typeof (SampleDomainClass).Namespace)); } [Test] @@ -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(); var convention = new ClassTypeHasSpecificNamespace(t => t.Name.EndsWith("Dto"), "TestAssembly.Dtos", "Dto"); var ex = Assert.Throws(() =>Convention.Is(convention, types)); @@ -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(); var convention = new ClassTypeHasSpecificNamespace(t => t.Name.EndsWith("Dto"), "TestAssembly.Dtos", "Dto"); Convention.IsWithApprovedExeptions(convention, types); diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt index fc2936a..d0deea5 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace.approved.txt @@ -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 diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt index 6281d35..547d592 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions.approved.txt @@ -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 diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt index 5a005e3..faaec50 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.dtos_exists_in_dto_namespace_wth_approved_exceptions1.approved.txt @@ -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 diff --git a/TestStack.ConventionTests/ConventionData/TypeExtensions.cs b/TestStack.ConventionTests/ConventionData/TypeExtensions.cs new file mode 100644 index 0000000..93a8ea8 --- /dev/null +++ b/TestStack.ConventionTests/ConventionData/TypeExtensions.cs @@ -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(this Type type) + { + return typeof(TAssignableTo).IsAssignableFrom(type); + } + + public static IEnumerable 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 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(); + } + } +} \ No newline at end of file diff --git a/TestStack.ConventionTests/ConventionData/Types.cs b/TestStack.ConventionTests/ConventionData/Types.cs index e491811..9452403 100644 --- a/TestStack.ConventionTests/ConventionData/Types.cs +++ b/TestStack.ConventionTests/ConventionData/Types.cs @@ -1,6 +1,8 @@ namespace TestStack.ConventionTests.ConventionData { using System; + using System.Collections; + using System.Collections.Generic; using System.Linq; /// @@ -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() + { + var assembly = typeof(T).Assembly; + return new Types(assembly.GetName().Name) + { + TypesToVerify = assembly.GetTypes() + }; + } + + public static Types InAssemblyOf(string descriptionOfTypes, Func, IEnumerable> types) + { + var assembly = typeof (T).Assembly; + return new Types(descriptionOfTypes) + { + TypesToVerify = types(assembly.GetTypes()).ToArray() + }; + } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/Conventions/AllClassesHaveDefaultConstructor.cs b/TestStack.ConventionTests/Conventions/AllClassesHaveDefaultConstructor.cs index 4de15e4..28edb3c 100644 --- a/TestStack.ConventionTests/Conventions/AllClassesHaveDefaultConstructor.cs +++ b/TestStack.ConventionTests/Conventions/AllClassesHaveDefaultConstructor.cs @@ -2,7 +2,6 @@ { using System.Linq; using TestStack.ConventionTests.ConventionData; - using TestStack.ConventionTests.Internal; public class AllClassesHaveDefaultConstructor : IConvention { diff --git a/TestStack.ConventionTests/Conventions/AllMethodsAreVirtual.cs b/TestStack.ConventionTests/Conventions/AllMethodsAreVirtual.cs index 70d7fd9..4dbd3c2 100644 --- a/TestStack.ConventionTests/Conventions/AllMethodsAreVirtual.cs +++ b/TestStack.ConventionTests/Conventions/AllMethodsAreVirtual.cs @@ -2,7 +2,6 @@ { using System.Linq; using TestStack.ConventionTests.ConventionData; - using TestStack.ConventionTests.Internal; public class AllMethodsAreVirtual : IConvention { diff --git a/TestStack.ConventionTests/Internal/ReflectionExtensions.cs b/TestStack.ConventionTests/Internal/ReflectionExtensions.cs index f078c18..cc700f5 100644 --- a/TestStack.ConventionTests/Internal/ReflectionExtensions.cs +++ b/TestStack.ConventionTests/Internal/ReflectionExtensions.cs @@ -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(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(this Type type) - { - return typeof (TAssignableTo).IsAssignableFrom(type); - } - - public static IEnumerable ConcreteTypes(this IEnumerable types) - { - return types.Where(t => t.IsClass && !t.IsAbstract); - } - - public static IEnumerable 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); - } } } \ No newline at end of file diff --git a/TestStack.ConventionTests/TestStack.ConventionTests.csproj b/TestStack.ConventionTests/TestStack.ConventionTests.csproj index 24ea926..8872291 100644 --- a/TestStack.ConventionTests/TestStack.ConventionTests.csproj +++ b/TestStack.ConventionTests/TestStack.ConventionTests.csproj @@ -58,6 +58,7 @@ + @@ -121,6 +122,6 @@ --> - + \ No newline at end of file