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: 20 additions & 0 deletions TestAssembly/Collections/Branch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace TestAssembly.Collections
{
using System.Collections;
using System.Collections.Generic;

public class Branch : IEnumerable<Leaf>
{
readonly List<Leaf> items = new List<Leaf>();

public IEnumerator<Leaf> GetEnumerator()
{
return items.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
30 changes: 30 additions & 0 deletions TestAssembly/Collections/Forest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace TestAssembly.Collections
{
using System.Collections;
using System.Collections.Generic;

public class Forest : ICanAdd<Tree>, ICanRemove<Tree>
{
readonly List<Tree> items = new List<Tree>();

public void Add(Tree item)
{
items.Add(item);
}

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

public IEnumerator<Tree> GetEnumerator()
{
return items.GetEnumerator();
}

public bool Remove(Tree item)
{
return items.Remove(item);
}
}
}
9 changes: 9 additions & 0 deletions TestAssembly/Collections/ICanAdd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TestAssembly.Collections
{
using System.Collections.Generic;

public interface ICanAdd<T> : IEnumerable<T>
{
void Add(T item);
}
}
9 changes: 9 additions & 0 deletions TestAssembly/Collections/ICanRemove.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TestAssembly.Collections
{
using System.Collections.Generic;

public interface ICanRemove<T> : IEnumerable<T>
{
bool Remove(T item);
}
}
6 changes: 6 additions & 0 deletions TestAssembly/Collections/Leaf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TestAssembly.Collections
{
public class Leaf
{
}
}
31 changes: 31 additions & 0 deletions TestAssembly/Collections/Tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace TestAssembly.Collections
{
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Tree : ICanAdd<Branch>, IEnumerable<Leaf>
{
readonly List<Branch> items = new List<Branch>();

public void Add(Branch item)
{
items.Add(item);
}

public IEnumerator<Branch> GetEnumerator()
{
return items.GetEnumerator();
}

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

IEnumerator<Leaf> IEnumerable<Leaf>.GetEnumerator()
{
return items.SelectMany(i => i).GetEnumerator();
}
}
}
6 changes: 6 additions & 0 deletions TestAssembly/TestAssembly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<Compile Include="ClassWithPrivateDefaultCtor.cs" />
<Compile Include="ClassWithProtectedDefaultCtor.cs" />
<Compile Include="ClassWithPublicDefaultCtor.cs" />
<Compile Include="Collections\Branch.cs" />
<Compile Include="Collections\Forest.cs" />
<Compile Include="Collections\ICanAdd.cs" />
<Compile Include="Collections\ICanRemove.cs" />
<Compile Include="Collections\Leaf.cs" />
<Compile Include="Collections\Tree.cs" />
<Compile Include="Dtos\AnotherClass.cs" />
<Compile Include="Dtos\BlahDto.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ConventionReportFailure Format(string failingData)

public class FailingConvention : IConvention<FakeData>
{
public void Execute(FakeData data, IConventionResult result)
public void Execute(FakeData data, IConventionResultContext result)
{
result.Is("Header", new[] {"Different"});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
collection,item,can add,can remove
TestAssembly.Collections.Branch,TestAssembly.Collections.Leaf,False,False
TestAssembly.Collections.Forest,TestAssembly.Collections.Tree,True,True
TestAssembly.Collections.Tree,TestAssembly.Collections.Branch,True,False
TestAssembly.Collections.Tree,TestAssembly.Collections.Leaf,False,False
24 changes: 24 additions & 0 deletions TestStack.ConventionTests.Tests/CsvReportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace TestStack.ConventionTests.Tests
{
using System.Linq;
using ApprovalTests.Reporters;
using NUnit.Framework;
using TestAssembly.Collections;
using TestStack.ConventionTests.ConventionData;
using TestStack.ConventionTests.Tests.TestConventions;

[UseReporter(typeof(DiffReporter))]
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()
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace TestStack.ConventionTests.Tests.TestConventions
{
using System;
using System.Collections.Generic;
using System.Linq;
using TestAssembly.Collections;
using TestStack.ConventionTests.ConventionData;

public class CollectionsRelationsConvention : IConvention<Types>
{
public string ConventionTitle { get; private set; }

public void Execute(Types data, IConventionResultContext result)
{
ConventionTitle = "well, does the header apply here all across the board? How would that work for CSV?";
var types = data.TypesToVerify;
var collectionToItemLookup = from collection in types
where collection.IsClass
orderby collection.FullName
from item in GetItemTypes(collection)
select new
{
collection,
item,
can_add = typeof (ICanAdd<>).MakeGenericType(item).IsAssignableFrom(collection),
can_remove = typeof (ICanRemove<>).MakeGenericType(item).IsAssignableFrom(collection)
};

result.Is("Some title", collectionToItemLookup);
}

IEnumerable<Type> GetItemTypes(Type type)
{
return from @interface in type.GetInterfaces()
where @interface.IsGenericType
where @interface.GetGenericTypeDefinition() == typeof (IEnumerable<>)
let item = @interface.GetGenericArguments().Single()
orderby item.FullName
select item;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ConventionAssertionClassTests.cs" />
<Compile Include="CsvReportTests.cs" />
<Compile Include="ProjectBasedConventions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="TestConventions\CollectionsRelationsConvention.cs" />
<Compile Include="TypeBasedConventions.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions TestStack.ConventionTests.Tests/TypeBasedConventions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace TestStack.ConventionTests.Tests
{
using System.Linq;
using ApprovalTests;
using ApprovalTests.Reporters;
using NUnit.Framework;
Expand All @@ -16,7 +17,9 @@ public class TypeBasedConventions

public TypeBasedConventions()
{
var itemsToVerify = typeof (SampleDomainClass).Assembly.GetTypes();
var itemsToVerify = typeof (SampleDomainClass).Assembly.GetTypes()
.Where(t => t.IsClass && t.Namespace == typeof (SampleDomainClass).Namespace)
.ToArray();
nhibernateEntities = new Types("nHibernate Entitites")
{
TypesToVerify = itemsToVerify
Expand Down Expand Up @@ -76,4 +79,4 @@ public void dtos_exists_in_dto_namespace_wth_approved_exceptions()
Convention.IsWithApprovedExeptions(convention, types);
}
}
}
}
10 changes: 6 additions & 4 deletions TestStack.ConventionTests/Convention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public static class Convention
{
static readonly HtmlReportRenderer HtmlRenderer = new HtmlReportRenderer(AssemblyDirectory);
static readonly List<ResultInfo> Reports = new List<ResultInfo>();
static readonly List<ConventionResult> Reports = new List<ConventionResult>();

static Convention()
{
Expand All @@ -26,7 +26,7 @@ static Convention()
};
}

public static IEnumerable<ResultInfo> ConventionReports { get { return Reports; } }
public static IEnumerable<ConventionResult> ConventionReports { get { return Reports; } }
public static IList<IReportDataFormatter> Formatters { get; set; }

public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
Expand All @@ -40,7 +40,8 @@ public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSou
{
try
{
var conventionResult = Executor.GetConventionResults(convention, data);
var context = new ConventionContext(data.Description, Formatters);
var conventionResult = context.GetConventionResults(convention, data);
Reports.AddRange(conventionResult);

new ConventionReportTraceRenderer().Render(conventionResult);
Expand All @@ -55,7 +56,8 @@ public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSou
public static void IsWithApprovedExeptions<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
where TDataSource : IConventionData
{
var conventionResult = Executor.GetConventionResultsWithApprovedExeptions(convention, data);
var context = new ConventionContext(data.Description, Formatters);
var conventionResult = context.GetConventionResultsWithApprovedExeptions(convention, data);
Reports.AddRange(conventionResult);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class AllClassesHaveDefaultConstructor : IConvention<Types>
{
public void Execute(Types data, IConventionResult result)
public void Execute(Types data, IConventionResultContext result)
{
result.Is("Types must have a default constructor",
data.TypesToVerify.Where(t => t.HasDefaultConstructor() == false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class AllMethodsAreVirtual : IConvention<Types>
{
public void Execute(Types data, IConventionResult result)
public void Execute(Types data, IConventionResultContext result)
{
result.Is("Methods must be virtual", data.TypesToVerify.SelectMany(t => t.NonVirtualMethods()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ClassTypeHasSpecificNamespace(Func<Type, bool> classIsApplicable, string
this.classType = classType;
}

public void Execute(Types data, IConventionResult result)
public void Execute(Types data, IConventionResultContext result)
{
result.IsSymmetric(
string.Format("{0}s must be under the '{1}' namespace", classType, namespaceToCheck),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public FilesAreEmbeddedResources(string fileExtension)

public string FileExtension { get; private set; }

public void Execute(ProjectFiles data, IConventionResult result)
public void Execute(ProjectFiles data, IConventionResultContext result)
{
result.Is(
string.Format("{0} Files must be embedded resources", FileExtension),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ProjectDoesNotReferenceDllsFromBinOrObjDirectories : IConvention<Pr
{
const string AssemblyReferencingObjRegex = @"^(?<assembly>.*?(obj|bin).*?)$";

public void Execute(ProjectReferences data, IConventionResult result)
public void Execute(ProjectReferences data, IConventionResultContext result)
{
result.Is("Project must not reference dlls from bin or obj directories",
data.References.Where(IsBinOrObjReference));
Expand Down
2 changes: 1 addition & 1 deletion TestStack.ConventionTests/IConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IConvention<in T> where T : IConventionData
{
void Execute(T data, IConventionResult result);
void Execute(T data, IConventionResultContext result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using System.Collections.Generic;

public interface IConventionResult
public interface IConventionResultContext
{
void Is<T>(string resultTitle, IEnumerable<T> failingData);

Expand All @@ -14,13 +14,13 @@ public interface IConventionResult
/// and if a non-dto is in Project.Dto the test will also fail
/// </summary>
/// <typeparam name="TResult">The data type the convention is applied to</typeparam>
/// <param name="conventionResultTitle">Title of the convention, i.e Dto's must live in Project.Dto namespace</param>
/// <param name="conventionFailingData">Data failing to conform to the convention</param>
/// <param name="inverseResultTitle">The inverse scenario title, i.e Non-dtos must not live inside Project.Dto namespace</param>
/// <param name="inverseFailingData">Data failing to conform to the inverse of the convention</param>
/// <param name="firstSetFailureTitle">Title of the convention, i.e Dto's must live in Project.Dto namespace</param>
/// <param name="firstSetFailureData">Data failing to conform to the convention</param>
/// <param name="secondSetFailureTitle">The inverse scenario title, i.e Non-dtos must not live inside Project.Dto namespace</param>
/// <param name="secondSetFailureData">Data failing to conform to the inverse of the convention</param>
void IsSymmetric<TResult>(
string conventionResultTitle, IEnumerable<TResult> conventionFailingData,
string inverseResultTitle, IEnumerable<TResult> inverseFailingData);
string firstSetFailureTitle, IEnumerable<TResult> firstSetFailureData,
string secondSetFailureTitle, IEnumerable<TResult> secondSetFailureData);

/// <summary>
/// A symmetric convention is a convention which also can be applied in reverse. For example
Expand Down
Loading