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
3 changes: 1 addition & 2 deletions Samples/SampleApp.Tests/DomainTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace SampleApp.Tests
{
using System.Linq;
using NUnit.Framework;
using SampleApp.Domain;
using TestStack.ConventionTests;
Expand All @@ -15,7 +14,7 @@ public class DomainTests
public DomainTests()
{
domainEntities = Types.InAssemblyOf<DomainClass>("Domain Entities",
types => types.Where(t=>t.Namespace.StartsWith("SampleApp.Domain")));
type => type.Namespace.StartsWith("SampleApp.Domain"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is an issue, but this would stop us doing a select many. Can you think of a reason you would need to project from a type into multiple types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. Maybe you could return all implemented interfaces or base classes? Anyway, the method is still there, it's just obsolete. I could remove the ObsoleteAttribute and just let it sit there with the rest 😄

}

[Test]
Expand Down
12 changes: 6 additions & 6 deletions TestStack.ConventionTests.Tests/CsvReportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
3 changes: 1 addition & 2 deletions TestStack.ConventionTests.Tests/TypeBasedConventions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace TestStack.ConventionTests.Tests
{
using System.Linq;
using ApprovalTests;
using ApprovalTests.Reporters;
using NUnit.Framework;
Expand All @@ -17,7 +16,7 @@ public class TypeBasedConventions
public TypeBasedConventions()
{
nhibernateEntities = Types.InAssemblyOf<SampleDomainClass>("nHibernate Entitites",
types => types.Where(t => t.IsConcreteClass() && t.Namespace == typeof (SampleDomainClass).Namespace));
type => type.IsConcreteClass() && type.Namespace == typeof (SampleDomainClass).Namespace);
}

[Test]
Expand Down
6 changes: 6 additions & 0 deletions TestStack.ConventionTests/ConventionData/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 |
Expand Down
220 changes: 198 additions & 22 deletions TestStack.ConventionTests/ConventionData/Types.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// This is where we set what our convention is all about.
/// </summary>
public class Types : IConventionData
public class Types : IConventionData, IEnumerable<Type>
{
public Types(string descriptionOfTypes)
public Types(string descriptionOfTypes) : this(Enumerable.Empty<Type>(), descriptionOfTypes)
{
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should at least .ToArray() internally. The reason I use array is so make sure that it is not a lazy enumerable which can be executed multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 😄

public Types(IEnumerable<Type> types, string descriptionOfTypes)
{
TypesToVerify = types.ToArray();
Description = descriptionOfTypes;
}

public Type[] TypesToVerify { get; set; }
public IEnumerable<Type> TypesToVerify { get; private set; }

public string Description { get; private set; }

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

/// <summary>
/// Gets a filtered list of types from the assembly of the specified
/// type, <typeparam name="T" />, using the specified <param name="predicate" />.
/// </summary>
/// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssemblyOf<T>(Func<Type, bool> predicate)
{
return InAssemblyOf(typeof(T), predicate);
}

/// <summary>
/// Gets a filtered list of types from the assembly of the specified
/// type, <param name="type" />, using the specified <param name="predicate" />.
/// </summary>
/// <param name="type">A type residing in the assembly to get types from.</param>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssemblyOf(Type type, Func<Type, bool> predicate)
{
return InAssembly(type.Assembly, predicate);
}

/// <summary>
/// Gets a filtered list of types from the specified <param name="assembly" /> using the specified <param name="predicate" />.
/// </summary>
/// <param name="assembly">The assembly to get types from.</param>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssembly(Assembly assembly, Func<Type, bool> predicate)
{
return InAssembly(assembly, GetAssemblyName(assembly), predicate);
}

/// <summary>
/// Gets a filtered list of types from the assembly of the specified
/// type, <typeparam name="T" />, using the specified <param name="predicate" />.
/// </summary>
/// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssemblyOf<T>(string descriptionOfTypes, Func<Type, bool> predicate)
{
return InAssemblyOf(typeof(T), descriptionOfTypes, predicate);
}

/// <summary>
/// Gets a filtered list of types from the assembly of the specified
/// type, <param name="type" />, using the specified <param name="predicate" />.
/// </summary>
/// <param name="type">A type residing in the assembly to get types from.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssemblyOf(Type type, string descriptionOfTypes, Func<Type, bool> predicate)
{
return InAssembly(type.Assembly, descriptionOfTypes, predicate);
}

/// <summary>
/// Gets a filtered list of types from the specified <param name="assembly" /> using the specified <param name="predicate" />.
/// </summary>
/// <param name="assembly">The assembly to get types from.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssembly(Assembly assembly, string descriptionOfTypes, Func<Type, bool> predicate)
{
return InAssemblies(new[] { assembly }, descriptionOfTypes, predicate);
}

/// <summary>
/// Gets an optionally filtered list of types from the specified <param name="assemblies" /> using the specified <param name="predicate" />.
/// </summary>
/// <param name="assemblies">A list of assemblies to get types from.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="predicate">A function to test each type for a condition.</param>
public static Types InAssemblies(IEnumerable<Assembly> assemblies, string descriptionOfTypes, Func<Type, bool> predicate)
{
return InCollection(assemblies.SelectMany(x => x.GetTypes()).Where(predicate), descriptionOfTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the assembly of the specified type, <typeparam name="T" />.
/// </summary>
/// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssemblyOf<T>(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);
}

/// <summary>
/// Gets an optionally filtered list of types from the assembly of the specified type, <param name="type" />.
/// </summary>
/// <param name="type">A type residing in the assembly to get types from.</param>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssemblyOf(Type type, bool excludeCompilerGeneratedTypes = true)
{
return InAssembly(type.Assembly, excludeCompilerGeneratedTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the specified <param name="assembly" />.
/// </summary>
/// <param name="assembly">The assembly to get types from.</param>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssembly(Assembly assembly, bool excludeCompilerGeneratedTypes = true)
{
return InAssembly(assembly, GetAssemblyName(assembly), excludeCompilerGeneratedTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the assembly of the specified
/// type, <typeparam name="T" />, using the specified <param name="types" /> filter.
/// </summary>
/// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="types">A function to filter or add matched types.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is obsolete and should not be used. Use the overload with a predicate instead.")]
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()
};
return InCollection(types(typeof(T).Assembly.GetTypes()), descriptionOfTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the assembly of the specified type, <typeparam name="T" />.
/// </summary>
/// <typeparam name="T">A type residing in the assembly to get types from.</typeparam>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssemblyOf<T>(string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true)
{
return InAssemblyOf(typeof(T), descriptionOfTypes, excludeCompilerGeneratedTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the assembly of the specified type, <param name="type" />.
/// </summary>
/// <param name="type">A type residing in the assembly to get types from.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssemblyOf(Type type, string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true)
{
return InAssembly(type.Assembly, descriptionOfTypes, excludeCompilerGeneratedTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the specified <param name="assembly" />.
/// </summary>
/// <param name="assembly">The assembly to get types from.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssembly(Assembly assembly, string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true)
{
return InAssemblies(new[] { assembly }, descriptionOfTypes, excludeCompilerGeneratedTypes);
}

/// <summary>
/// Gets an optionally filtered list of types from the specified <param name="assemblies" />.
/// </summary>
/// <param name="assemblies">A list of assemblies to get types from.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
/// <param name="excludeCompilerGeneratedTypes">Compiler generated types will be excluded if set to <c>true</c>.</param>
public static Types InAssemblies(IEnumerable<Assembly> assemblies, string descriptionOfTypes, bool excludeCompilerGeneratedTypes = true)
{
return InAssemblies(assemblies, descriptionOfTypes, type => !(excludeCompilerGeneratedTypes && type.IsCompilerGenerated()));
}

/// <summary>
/// Gets a list of types from the specified <param name="types" /> collection.
/// </summary>
/// <param name="types">The types.</param>
/// <param name="descriptionOfTypes">A description of the matched types.</param>
public static Types InCollection(IEnumerable<Type> types, string descriptionOfTypes)
{
return new Types(types, descriptionOfTypes);
}

private static string GetAssemblyName(Assembly assembly)
{
return assembly.GetName().Name;
}

public IEnumerator<Type> GetEnumerator()
{
return TypesToVerify.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}