From 8ce05eb0878c3e1a3bad28bf4c1b50ad1b21a451 Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Tue, 6 Jan 2015 21:02:22 +0100 Subject: [PATCH 1/4] Added new overloads to Types --- .../ConventionData/TypeExtensions.cs | 6 + .../ConventionData/Types.cs | 198 ++++++++++++++++-- 2 files changed, 185 insertions(+), 19 deletions(-) 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..d5e8b9e 100644 --- a/TestStack.ConventionTests/ConventionData/Types.cs +++ b/TestStack.ConventionTests/ConventionData/Types.cs @@ -2,8 +2,9 @@ { using System; 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. @@ -19,31 +20,190 @@ public Types(string descriptionOfTypes) 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(descriptionOfTypes) { TypesToVerify = types.ToArray() }; + } + + private static string GetAssemblyName(Assembly assembly) + { + return assembly.GetName().Name; } } } \ No newline at end of file From 5191246156019a3a1b78ceee0ff812bfa01b77a0 Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Tue, 6 Jan 2015 21:17:21 +0100 Subject: [PATCH 2/4] Removed usages of obsolete method --- Samples/SampleApp.Tests/DomainTests.cs | 3 +-- TestStack.ConventionTests.Tests/TypeBasedConventions.cs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) 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/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] From 6c6cb1bf4d0278ae33a9a07f0958ec41012ca0c7 Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Tue, 6 Jan 2015 21:04:47 +0100 Subject: [PATCH 3/4] Made Types immutable --- TestStack.ConventionTests.Tests/CsvReportTests.cs | 12 ++++++------ TestStack.ConventionTests/ConventionData/Types.cs | 11 ++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) 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/ConventionData/Types.cs b/TestStack.ConventionTests/ConventionData/Types.cs index d5e8b9e..f3ae693 100644 --- a/TestStack.ConventionTests/ConventionData/Types.cs +++ b/TestStack.ConventionTests/ConventionData/Types.cs @@ -11,12 +11,17 @@ /// public class Types : IConventionData { - 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 Type[] TypesToVerify { get; private set; } public string Description { get; private set; } @@ -198,7 +203,7 @@ public static Types InAssemblies(IEnumerable assemblies, string descri /// A description of the matched types. public static Types InCollection(IEnumerable types, string descriptionOfTypes) { - return new Types(descriptionOfTypes) { TypesToVerify = types.ToArray() }; + return new Types(types, descriptionOfTypes); } private static string GetAssemblyName(Assembly assembly) From e6406a19c7d7caaafa11b257d12d1f28f44cf268 Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Thu, 8 Jan 2015 12:32:44 +0100 Subject: [PATCH 4/4] Made TypesToCheck IEnumerable --- TestStack.ConventionTests/ConventionData/Types.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/TestStack.ConventionTests/ConventionData/Types.cs b/TestStack.ConventionTests/ConventionData/Types.cs index f3ae693..70f69d8 100644 --- a/TestStack.ConventionTests/ConventionData/Types.cs +++ b/TestStack.ConventionTests/ConventionData/Types.cs @@ -1,6 +1,7 @@ namespace TestStack.ConventionTests.ConventionData { using System; + using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -9,7 +10,7 @@ /// /// This is where we set what our convention is all about. /// - public class Types : IConventionData + public class Types : IConventionData, IEnumerable { public Types(string descriptionOfTypes) : this(Enumerable.Empty(), descriptionOfTypes) { @@ -21,7 +22,7 @@ public Types(IEnumerable types, string descriptionOfTypes) Description = descriptionOfTypes; } - public Type[] TypesToVerify { get; private set; } + public IEnumerable TypesToVerify { get; private set; } public string Description { get; private set; } @@ -210,5 +211,15 @@ 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