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
1 change: 1 addition & 0 deletions ConventionTests.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantArgumentNameForLiteralExpression/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=all/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="all"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_IMPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;ALWAYS_IMPLICIT&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_IMPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CSReorderTypeMembers&gt;True&lt;/CSReorderTypeMembers&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">all</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EXPLICIT_PRIVATE_MODIFIER/@EntryValue">False</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Header
------
Passed: 'Header' for 'Fake data'
--------------------------------

Approved Exception
With approved exceptions:
Approved Exception
13 changes: 10 additions & 3 deletions TestStack.ConventionTests.Tests/ConventionAssertionClassTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace TestStack.ConventionTests.Tests
{
using System.Collections.Generic;
using ApprovalTests.Reporters;
using NUnit.Framework;
using TestStack.ConventionTests.Internal;
Expand All @@ -21,16 +22,22 @@ public void approval_mismatch()

public class FakeData : IConventionData
{
public void EnsureHasNonEmptySource()
public string Description { get { return "Fake data"; } }

public bool HasData { get { return true; } }

public ConventionReportFailure Format(string failingData)
{
return new ConventionReportFailure(failingData);
}
}

public class FailingConvention : IConvention<FakeData>
{
public ConventionResult Execute(FakeData data)
public string ConventionTitle { get { return "Header"; } }
public IEnumerable<object> GetFailingData(FakeData data)
{
return ConventionResult.For(new[] { "" }, "Header", (s, builder) => builder.AppendLine("Different"));
return new[] { "Different" };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Some invalid assembly references found in TestStack.ConventionTests.Tests
-------------------------------------------------------------------------
Failed: 'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests'
-----------------------------------------------------------------------------------------------------------

bin\Debug\ApprovalTests.dll
bin\Debug\ApprovalTests.dll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Some invalid assembly references found in TestStack.ConventionTests.Tests
-------------------------------------------------------------------------
Passed: 'Project must not reference dlls from bin or obj directories' for 'TestStack.ConventionTests.Tests'
-----------------------------------------------------------------------------------------------------------

bin\Debug\ApprovalTests.dll
With approved exceptions:
bin\Debug\ApprovalTests.dll
14 changes: 4 additions & 10 deletions TestStack.ConventionTests.Tests/ProjectBasedConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ public void scripts_not_embedded_resources()
.Returns(XDocument.Parse(Resources.ProjectFileWithInvalidSqlScriptFile));

var projectLocator = Substitute.For<IProjectLocator>();
var project = new ProjectFiles(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator)
{
Items = i => i.FilePath.EndsWith(".sql")
};
var ex = Assert.Throws<ConventionFailedException>(() => Convention.Is(new FilesAreEmbeddedResources(), project));
var project = new ProjectFiles(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator);
var ex = Assert.Throws<ConventionFailedException>(() => Convention.Is(new FilesAreEmbeddedResources(".sql"), project));

Approvals.Verify(ex.Message);
}
Expand All @@ -70,15 +67,12 @@ public void scripts_not_embedded_resources()
public void scripts_not_embedded_resources_with_approved_exceptions()
{
var projectLocator = Substitute.For<IProjectLocator>();
var project = new ProjectFiles(typeof(ProjectBasedConventions).Assembly, projectProvider, projectLocator)
{
Items = i => i.FilePath.EndsWith(".sql")
};
var project = new ProjectFiles(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator);
projectProvider
.LoadProjectDocument(Arg.Any<string>())
.Returns(XDocument.Parse(Resources.ProjectFileWithInvalidSqlScriptFile));

Convention.IsWithApprovedExeptions(new FilesAreEmbeddedResources(), project);
Convention.IsWithApprovedExeptions(new FilesAreEmbeddedResources(".sql"), project);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The following files which should be embedded resources:
-------------------------------------------------------
Failed: '.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests'
-------------------------------------------------------------------------------------

Scripts\Script2.sql
Scripts\Script2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
The following files which should be embedded resources:
-------------------------------------------------------
Passed: '.sql Files must be embedded resources' for 'TestStack.ConventionTests.Tests'
-------------------------------------------------------------------------------------

Scripts\Script2.sql
With approved exceptions:
Scripts\Script2.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The following types do not have default constructor
---------------------------------------------------
Failed: 'Types must have a default constructor' for 'nHibernate Entitites'
--------------------------------------------------------------------------

TestAssembly.ClassWithNoDefaultCtor
TestAssembly.ClassWithPrivateDefaultCtor
TestAssembly.ClassWithPrivateDefaultCtor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The following types do not have default constructor
---------------------------------------------------
Passed: 'Types must have a default constructor' for 'nHibernate Entitites'
--------------------------------------------------------------------------

With approved exceptions:
TestAssembly.ClassWithNoDefaultCtor
TestAssembly.ClassWithPrivateDefaultCtor
TestAssembly.ClassWithPrivateDefaultCtor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The following methods are not virtual.
--------------------------------------
Failed: 'Methods must be virtual' for 'nHibernate Entitites'
------------------------------------------------------------

TestAssembly.SampleDomainClass.TestNonVirtual
TestAssembly.SampleDomainClass.TestNonVirtual
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
The following methods are not virtual.
--------------------------------------
Passed: 'Methods must be virtual' for 'nHibernate Entitites'
------------------------------------------------------------

TestAssembly.SampleDomainClass.TestNonVirtual
With approved exceptions:
TestAssembly.SampleDomainClass.TestNonVirtual
6 changes: 3 additions & 3 deletions TestStack.ConventionTests.Tests/TypeBasedConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TypeBasedConventions
public TypeBasedConventions()
{
var itemsToVerify = typeof (SampleDomainClass).Assembly.GetTypes();
nhibernateEntities = new Types
nhibernateEntities = new Types("nHibernate Entitites")
{
TypesToVerify = itemsToVerify
};
Expand Down Expand Up @@ -54,7 +54,7 @@ public void all_methods_are_virtual_wth_approved_exceptions()
[Test]
public void dtos_exists_in_dto_namespace()
{
var types = new Types
var types = new Types("TestAssembly types")
{
TypesToVerify = new[] { typeof(SomeDto), typeof(BlahDto), typeof(AnotherClass)}
};
Expand All @@ -67,7 +67,7 @@ public void dtos_exists_in_dto_namespace()
[Test]
public void dtos_exists_in_dto_namespace_wth_approved_exceptions()
{
var types = new Types
var types = new Types("TestAssembly types")
{
TypesToVerify = new[] { typeof(SomeDto), typeof(BlahDto), typeof(AnotherClass) }
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Dtos must be under the 'TestAssembly.Dtos' namespace
----------------------------------------------------
Failed: 'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
---------------------------------------------------------------------------------------

TestAssembly.SomeDto


Non-Dtos must not be under the 'TestAssembly.Dtos' namespace
------------------------------------------------------------
Failed: 'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
-----------------------------------------------------------------------------------------------

TestAssembly.Dtos.AnotherClass
TestAssembly.Dtos.AnotherClass
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
Dtos must be under the 'TestAssembly.Dtos' namespace
----------------------------------------------------
Passed: 'Dtos must be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
---------------------------------------------------------------------------------------

With approved exceptions:
TestAssembly.SomeDto


Non-Dtos must not be under the 'TestAssembly.Dtos' namespace
------------------------------------------------------------

TestAssembly.Dtos.AnotherClass

Passed: 'Non-Dtos must not be under the 'TestAssembly.Dtos' namespace' for 'TestAssembly types'
-----------------------------------------------------------------------------------------------

With approved exceptions:
TestAssembly.Dtos.AnotherClass
135 changes: 123 additions & 12 deletions TestStack.ConventionTests/Convention.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,147 @@
namespace TestStack.ConventionTests
{
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ApprovalTests;
using ApprovalTests.Core.Exceptions;
using TestStack.ConventionTests.Conventions;
using TestStack.ConventionTests.Internal;
using TestStack.ConventionTests.Reporting;

public static class Convention
{
public static void Is<TData>(IConvention<TData> convention, TData data) where TData : IConventionData
static readonly HtmlReportRenderer HtmlRenderer = new HtmlReportRenderer(AssemblyDirectory);
static readonly List<ResultInfo> Reports = new List<ResultInfo>();

static Convention()
{
Formatters = new List<IReportDataFormatter>
{
new TypeDataFormatter(),
new ProjectReferenceFormatter(),
new ProjectFileFormatter(),
new MethodInfoDataFormatter(),
new StringDataFormatter()
};
}

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

public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
where TDataSource : IConventionData
{
data.EnsureHasNonEmptySource();
var result = convention.Execute(data);
Is(convention, data, new ConventionResultExceptionReporter());
}

public static void Is<TDataSource>(IConvention<TDataSource> convention, TDataSource data, IConventionReportRenderer reporter)
where TDataSource : IConventionData
{
try
{
var conventionResult = Executor.GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
Reports.Add(conventionResult);

if (result.Failed)
throw new ConventionFailedException(result.Message);
new ConventionReportTraceRenderer().Render(conventionResult);
reporter.Render(conventionResult);
}
finally
{
HtmlRenderer.Render(Reports.ToArray());
}
}

public static void IsWithApprovedExeptions<TData>(IConvention<TData> convention, TData data) where TData : IConventionData
public static void IsWithApprovedExeptions<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
where TDataSource : IConventionData
{
data.EnsureHasNonEmptySource();
var result = convention.Execute(data);
var conventionResult = Executor.GetConventionReportWithApprovedExeptions(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
Reports.Add(conventionResult);

// should we encapsulate Approvals behind Settings?
try
{
Approvals.Verify(result.Message);
var conventionReportTextRenderer = new ConventionReportTextRenderer();
conventionReportTextRenderer.Render(conventionResult);
Approvals.Verify(conventionReportTextRenderer.Output);

Trace.WriteLine(string.Format("{0} has approved exceptions:\r\n\r\n{1}", convention.GetType().Name, result.Message));
new ConventionReportTraceRenderer().Render(conventionResult);
}
catch (ApprovalException ex)
{
throw new ConventionFailedException("Approved exceptions for convention differs\r\n\r\n"+ex.Message, ex);
}
finally
{
HtmlRenderer.Render(Reports.ToArray());
}
}

public static void Is<TDataSource>(ISymmetricConvention<TDataSource> convention, TDataSource data)
where TDataSource : IConventionData
{
Is(convention, data, new ConventionResultExceptionReporter());
}

public static void Is<TDataSource>(ISymmetricConvention<TDataSource> convention, TDataSource data, IConventionReportRenderer reporter)
where TDataSource : IConventionData
{
try
{
var conventionResult = Executor.GetConventionReport(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
var inverseConventionResult = Executor.GetConventionReport(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);

Reports.Add(conventionResult);
Reports.Add(inverseConventionResult);

new ConventionReportTraceRenderer().Render(conventionResult, inverseConventionResult);
reporter.Render(conventionResult, inverseConventionResult);
}
finally
{
HtmlRenderer.Render(Reports.ToArray());
}
}

public static void IsWithApprovedExeptions<TDataSource>(ISymmetricConvention<TDataSource> convention, TDataSource data)
where TDataSource : IConventionData
{
var conventionResult = Executor.GetConventionReportWithApprovedExeptions(convention.ConventionTitle, convention.GetFailingData(data).ToArray(), data);
var inverseConventionResult = Executor.GetConventionReportWithApprovedExeptions(convention.InverseTitle, convention.GetFailingInverseData(data).ToArray(), data);
Reports.Add(conventionResult);
Reports.Add(inverseConventionResult);

try
{
//Render both, with approved exceptions included
var conventionReportTextRenderer = new ConventionReportTextRenderer();
conventionReportTextRenderer.Render(conventionResult, inverseConventionResult);
Approvals.Verify(conventionReportTextRenderer.Output);

// Trace on success
new ConventionReportTraceRenderer().Render(conventionResult, inverseConventionResult);
}
catch (ApprovalException ex)
{
throw new ConventionFailedException("Approved exceptions for convention differs\r\n\r\n" + ex.Message, ex);
}
finally
{
HtmlRenderer.Render(Reports.ToArray());
}
}

// http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in#answer-283917
static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
}
}
Loading