diff --git a/Samples/SampleApp.Tests/DomainTests.cs b/Samples/SampleApp.Tests/DomainTests.cs index c53b6c7..22b61a1 100644 --- a/Samples/SampleApp.Tests/DomainTests.cs +++ b/Samples/SampleApp.Tests/DomainTests.cs @@ -1,6 +1,5 @@ namespace SampleApp.Tests { - using System.Linq; using NUnit.Framework; using SampleApp.Domain; using TestStack.ConventionTests; @@ -15,7 +14,7 @@ public class DomainTests public DomainTests() { domainEntities = Types.InAssemblyOf("Domain Entities", - types => types.Where(t=>t.Namespace.StartsWith("SampleApp.Domain"))); + type => type.Namespace.StartsWith("SampleApp.Domain")); } [Test] diff --git a/TestStack.ConventionTests.Tests/CsvReportTests.cs b/TestStack.ConventionTests.Tests/CsvReportTests.cs index d5767e9..e9649c3 100644 --- a/TestStack.ConventionTests.Tests/CsvReportTests.cs +++ b/TestStack.ConventionTests.Tests/CsvReportTests.cs @@ -14,12 +14,12 @@ public class CsvReportTests [Test] public void Can_run_convention_with_simple_reporter() { - Convention.IsWithApprovedExeptions(new CollectionsRelationsConvention(), new Types("Entities") - { - TypesToVerify = - typeof (Leaf).Assembly.GetExportedTypes() - .Where(t => t.Namespace == typeof (Leaf).Namespace).ToArray() - }, new CsvReporter()); + var typesToVerify = typeof (Leaf).Assembly.GetExportedTypes() + .Where(t => t.Namespace == typeof (Leaf).Namespace); + + Convention.IsWithApprovedExeptions(new CollectionsRelationsConvention(), + new Types(typesToVerify, "Entities"), + new CsvReporter()); } } } \ No newline at end of file diff --git a/TestStack.ConventionTests.Tests/TypeBasedConventions.cs b/TestStack.ConventionTests.Tests/TypeBasedConventions.cs index 2e8a12a..f78eda9 100644 --- a/TestStack.ConventionTests.Tests/TypeBasedConventions.cs +++ b/TestStack.ConventionTests.Tests/TypeBasedConventions.cs @@ -1,6 +1,5 @@ namespace TestStack.ConventionTests.Tests { - using System.Linq; using ApprovalTests; using ApprovalTests.Reporters; using NUnit.Framework; @@ -17,7 +16,7 @@ public class TypeBasedConventions public TypeBasedConventions() { nhibernateEntities = Types.InAssemblyOf("nHibernate Entitites", - types => types.Where(t => t.IsConcreteClass() && t.Namespace == typeof (SampleDomainClass).Namespace)); + type => type.IsConcreteClass() && type.Namespace == typeof (SampleDomainClass).Namespace); } [Test] diff --git a/TestStack.ConventionTests/ConventionData/TypeExtensions.cs b/TestStack.ConventionTests/ConventionData/TypeExtensions.cs index 013ea3d..e92bef7 100644 --- a/TestStack.ConventionTests/ConventionData/TypeExtensions.cs +++ b/TestStack.ConventionTests/ConventionData/TypeExtensions.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; + using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; @@ -24,6 +25,11 @@ public static bool IsStatic(this Type type) return type.IsClass && !(type.IsSealed && type.IsAbstract); } + public static bool IsCompilerGenerated(this Type type) + { + return type.IsDefined(typeof(CompilerGeneratedAttribute), true); + } + public static bool HasDefaultConstructor(this Type type) { return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | diff --git a/TestStack.ConventionTests/ConventionData/Types.cs b/TestStack.ConventionTests/ConventionData/Types.cs index a9e1305..70f69d8 100644 --- a/TestStack.ConventionTests/ConventionData/Types.cs +++ b/TestStack.ConventionTests/ConventionData/Types.cs @@ -1,49 +1,225 @@ namespace TestStack.ConventionTests.ConventionData { using System; + using System.Collections; using System.Collections.Generic; + using System.ComponentModel; using System.Linq; - using System.Runtime.CompilerServices; + using System.Reflection; /// /// This is where we set what our convention is all about. /// - public class Types : IConventionData + public class Types : IConventionData, IEnumerable { - public Types(string descriptionOfTypes) + public Types(string descriptionOfTypes) : this(Enumerable.Empty(), descriptionOfTypes) { + } + + public Types(IEnumerable types, string descriptionOfTypes) + { + TypesToVerify = types.ToArray(); Description = descriptionOfTypes; } - public Type[] TypesToVerify { get; set; } + public IEnumerable TypesToVerify { get; private set; } public string Description { get; private set; } - public bool HasData {get { return TypesToVerify.Any(); }} + public bool HasData + { + get { return TypesToVerify.Any(); } + } + + /// + /// Gets a filtered list of types from the assembly of the specified + /// type, , using the specified . + /// + /// A type residing in the assembly to get types from. + /// A function to test each type for a condition. + public static Types InAssemblyOf(Func predicate) + { + return InAssemblyOf(typeof(T), predicate); + } + + /// + /// Gets a filtered list of types from the assembly of the specified + /// type, , using the specified . + /// + /// A type residing in the assembly to get types from. + /// A function to test each type for a condition. + public static Types InAssemblyOf(Type type, Func predicate) + { + return InAssembly(type.Assembly, predicate); + } + + /// + /// Gets a filtered list of types from the specified using the specified . + /// + /// The assembly to get types from. + /// A function to test each type for a condition. + public static Types InAssembly(Assembly assembly, Func predicate) + { + return InAssembly(assembly, GetAssemblyName(assembly), predicate); + } + + /// + /// Gets a filtered list of types from the assembly of the specified + /// type, , using the specified . + /// + /// A type residing in the assembly to get types from. + /// A description of the matched types. + /// A function to test each type for a condition. + public static Types InAssemblyOf(string descriptionOfTypes, Func predicate) + { + return InAssemblyOf(typeof(T), descriptionOfTypes, predicate); + } + + /// + /// Gets a filtered list of types from the assembly of the specified + /// type, , using the specified . + /// + /// A type residing in the assembly to get types from. + /// A description of the matched types. + /// A function to test each type for a condition. + public static Types InAssemblyOf(Type type, string descriptionOfTypes, Func predicate) + { + return InAssembly(type.Assembly, descriptionOfTypes, predicate); + } + + /// + /// Gets a filtered list of types from the specified using the specified . + /// + /// The assembly to get types from. + /// A description of the matched types. + /// A function to test each type for a condition. + public static Types InAssembly(Assembly assembly, string descriptionOfTypes, Func predicate) + { + return InAssemblies(new[] { assembly }, descriptionOfTypes, predicate); + } + /// + /// Gets an optionally filtered list of types from the specified using the specified . + /// + /// A list of assemblies to get types from. + /// A description of the matched types. + /// A function to test each type for a condition. + public static Types InAssemblies(IEnumerable assemblies, string descriptionOfTypes, Func predicate) + { + return InCollection(assemblies.SelectMany(x => x.GetTypes()).Where(predicate), descriptionOfTypes); + } + + /// + /// Gets an optionally filtered list of types from the assembly of the specified type, . + /// + /// A type residing in the assembly to get types from. + /// Compiler generated types will be excluded if set to true. public static Types InAssemblyOf(bool excludeCompilerGeneratedTypes = true) { - var assembly = typeof(T).Assembly; - var typesToVerify = assembly.GetTypes(); - if (excludeCompilerGeneratedTypes) - { - typesToVerify = typesToVerify - .Where(t => !t.GetCustomAttributes(typeof (CompilerGeneratedAttribute), true).Any()) - .ToArray(); - } - return new Types(assembly.GetName().Name) - { - TypesToVerify = typesToVerify - }; + return InAssemblyOf(typeof(T), excludeCompilerGeneratedTypes); + } + + /// + /// Gets an optionally filtered list of types from the assembly of the specified type, . + /// + /// A type residing in the assembly to get types from. + /// Compiler generated types will be excluded if set to true. + public static Types InAssemblyOf(Type type, bool excludeCompilerGeneratedTypes = true) + { + return InAssembly(type.Assembly, excludeCompilerGeneratedTypes); + } + + /// + /// Gets an optionally filtered list of types from the specified . + /// + /// The assembly to get types from. + /// Compiler generated types will be excluded if set to true. + public static Types InAssembly(Assembly assembly, bool excludeCompilerGeneratedTypes = true) + { + return InAssembly(assembly, GetAssemblyName(assembly), excludeCompilerGeneratedTypes); } + /// + /// Gets an optionally filtered list of types from the assembly of the specified + /// type, , using the specified filter. + /// + /// A type residing in the assembly to get types from. + /// A description of the matched types. + /// A function to filter or add matched types. + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is obsolete and should not be used. Use the overload with a predicate instead.")] public static Types InAssemblyOf(string descriptionOfTypes, Func, IEnumerable> types) { - var assembly = typeof (T).Assembly; - return new Types(descriptionOfTypes) - { - TypesToVerify = types(assembly.GetTypes()).ToArray() - }; + return InCollection(types(typeof(T).Assembly.GetTypes()), descriptionOfTypes); + } + + /// + /// Gets an optionally filtered list of types from the assembly of the specified type, . + /// + /// A type residing in the assembly to get types from. + /// A description of the matched types. + /// Compiler generated types will be excluded if set to true. + public static Types InAssemblyOf(string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true) + { + return InAssemblyOf(typeof(T), descriptionOfTypes, excludeCompilerGeneratedTypes); + } + + /// + /// Gets an optionally filtered list of types from the assembly of the specified type, . + /// + /// A type residing in the assembly to get types from. + /// A description of the matched types. + /// Compiler generated types will be excluded if set to true. + public static Types InAssemblyOf(Type type, string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true) + { + return InAssembly(type.Assembly, descriptionOfTypes, excludeCompilerGeneratedTypes); + } + + /// + /// Gets an optionally filtered list of types from the specified . + /// + /// The assembly to get types from. + /// A description of the matched types. + /// Compiler generated types will be excluded if set to true. + public static Types InAssembly(Assembly assembly, string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true) + { + return InAssemblies(new[] { assembly }, descriptionOfTypes, excludeCompilerGeneratedTypes); + } + + /// + /// Gets an optionally filtered list of types from the specified . + /// + /// A list of assemblies to get types from. + /// A description of the matched types. + /// Compiler generated types will be excluded if set to true. + public static Types InAssemblies(IEnumerable assemblies, string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true) + { + return InAssemblies(assemblies, descriptionOfTypes, type => !(excludeCompilerGeneratedTypes && type.IsCompilerGenerated())); + } + + /// + /// Gets a list of types from the specified collection. + /// + /// The types. + /// A description of the matched types. + public static Types InCollection(IEnumerable types, string descriptionOfTypes) + { + return new Types(types, descriptionOfTypes); + } + + private static string GetAssemblyName(Assembly assembly) + { + return assembly.GetName().Name; + } + + public IEnumerator GetEnumerator() + { + return TypesToVerify.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); } } } \ No newline at end of file