Skip to content

Adds convention reason to conventions, useful for the living documentati... #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2013
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace TestStack.ConventionTests.Autofac
{
using global::Autofac;
using global::Autofac.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using global::Autofac;
using global::Autofac.Core;

public class CanResolveAllRegisteredServices : IConvention<AutofacRegistrations>
{
Expand Down Expand Up @@ -37,6 +37,14 @@ public void Execute(AutofacRegistrations data, IConventionResultContext result)
result.Is("Can resolve all types registered with Autofac", failingTypes);
}

public string ConventionReason
{
get
{
return "Container resolution failings are runtime exceptions, this convention allows you to detect missing registrations faster!";
}
}

private IEnumerable<Type> GetGenericFactoryTypes(AutofacRegistrations data, IComponentRegistration componentRegistration)
{
return from ctorParameter in data.GetRegistrationCtorParameters(componentRegistration)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace TestStack.ConventionTests.Autofac
{
using System.Collections.Generic;
using global::Autofac.Core;
using System.Collections.Generic;
using TestStack.ConventionTests.ConventionData;

public class ServicesShouldOnlyHaveDependenciesWithLesserLifetime : IConvention<AutofacRegistrations>
Expand Down Expand Up @@ -37,5 +37,10 @@ public void Execute(AutofacRegistrations data, IConventionResultContext result)

result.Is("Components should not depend on with greater lifetimes", exceptions);
}

public string ConventionReason
{
get { return @"When classes with larger lifetimes depend on classes with smaller lifetimes this often indicates an error and can lead to subtle bugs"; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public void Execute(FakeData data, IConventionResultContext result)
{
result.Is("Header", new[] {"Different"});
}

public string ConventionReason
{
get { return "Because fail.."; }
}
}
}
}
2 changes: 2 additions & 0 deletions TestStack.ConventionTests.Tests/ConventionFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public void Execute(Types data, IConventionResultContext result)
// Oops, I forgot to set the result
}

public string ConventionReason { get { return "Convention does not set result for testing"; } }

bool IsBroken(Type type)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ from item in GetItemTypes(collection)
result.Is("Some title", collectionToItemLookup);
}

public string ConventionReason
{
get { return "Test convention"; }
}

IEnumerable<Type> GetItemTypes(Type type)
{
return from @interface in type.GetInterfaces()
Expand Down
1 change: 0 additions & 1 deletion TestStack.ConventionTests/Convention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using TestStack.ConventionTests.ConventionData;
using TestStack.ConventionTests.Internal;
using TestStack.ConventionTests.Reporting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public void Execute(Types data, IConventionResultContext result)
result.Is("Types must have a default constructor",
data.TypesToVerify.Where(t => t.HasDefaultConstructor() == false));
}

public string ConventionReason
{
get { return "This convention is useful when classes need to be proxied (nHibernate/Entity Framework entities), which need a public or protected constructor"; }
}
}
}
5 changes: 5 additions & 0 deletions TestStack.ConventionTests/Conventions/AllMethodsAreVirtual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ public void Execute(Types data, IConventionResultContext result)
{
result.Is("Methods must be virtual", data.TypesToVerify.SelectMany(t => t.NonVirtualMethods()));
}

public string ConventionReason
{
get { return "This convention is useful when classes need to be proxied (nHibernate entities), which need members to be virtual"; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public void Execute(Types data, IConventionResultContext result)
data.TypesToVerify);
}

public string ConventionReason
{
get { return "To simplify project structure and allow developers to know where this type of class should live in the project"; }
}

bool TypeLivesInSpecifiedNamespace(Type t)
{
return t.Namespace == null || t.Namespace.StartsWith(namespaceToCheck);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public void Execute(ProjectFileItems data, IConventionResultContext result)
string.Format("{0} Files must be embedded resources", FileExtension),
data.Items.Where(s => s.FilePath.EndsWith(FileExtension) && s.ReferenceType != "EmbeddedResource"));
}

public string ConventionReason { get { return "Many files are added as 'Content' to visual studio projects, this convention enforces files with an extension are correctly set as Embedded Resources"; } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public void Execute(Types data, IConventionResultContext result)
GetControllerTypeName() + "s must be suffixed with Controller", typesWhichDoNotEndInController,
"Types named *Controller must inherit from ApiController or Controller",
controllersWhichDoNotInheritFromController);
}

}

public string ConventionReason { get { return "This convention detects when Mvc Controllers are likely misconfigured and do not follow Mvc conventions"; } }

protected virtual string GetControllerTypeName()
{
return "Mvc Controller";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public void Execute(ProjectReferences data, IConventionResultContext result)
data.References.Where(IsBinOrObjReference));
}

public string ConventionReason { get { return "Referencing assemblies from the bin/obj folder is normally due to a broken reference, this convention detects these issues"; } }

static bool IsBinOrObjReference(ProjectReference reference)
{
return Regex.IsMatch(reference.ReferencedPath, AssemblyReferencingObjRegex, RegexOptions.IgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ public void Execute(Types data, IConventionResultContext result)
result.Is("ViewModels (types named *{0}) should inherit from INotifyPropertyChanged",
failingData);
}

public string ConventionReason
{
get
{
return "In different scenarios, WPF can hold onto ViewModels if they do not inherit from INotifyPropertyChanged";
}
}
}
}
1 change: 1 addition & 0 deletions TestStack.ConventionTests/IConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IConvention<in T> where T : IConventionData
{
void Execute(T data, IConventionResultContext result);
string ConventionReason { get; }
}
}
7 changes: 5 additions & 2 deletions TestStack.ConventionTests/Internal/ConventionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ConventionContext : IConventionResultContext, IConventionFormatCont
readonly IList<IResultsProcessor> processors;
readonly ITestResultProcessor testResultProcessor;
readonly IList<ConventionResult> results = new List<ConventionResult>();
string conventionReason;
bool resultSet;

public ConventionContext(string dataDescription, IList<IReportDataFormatter> formatters,
Expand Down Expand Up @@ -65,6 +66,7 @@ void IConventionResultContext.Is<TResult>(string resultTitle, IEnumerable<TResul
results.Add(new ConventionResult(
typeof (TResult),
resultTitle,
conventionReason,
dataDescription,
failingData.ToObjectArray()));
}
Expand All @@ -76,11 +78,11 @@ void IConventionResultContext.IsSymmetric<TResult>(
resultSet = true;
results.Add(new ConventionResult(
typeof (TResult), firstSetFailureTitle,
dataDescription,
conventionReason, dataDescription,
firstSetFailureData.ToObjectArray()));
results.Add(new ConventionResult(
typeof (TResult), secondSetFailureTitle,
dataDescription,
conventionReason, dataDescription,
secondSetFailureData.ToObjectArray()));
}

Expand All @@ -102,6 +104,7 @@ void IConventionResultContext.IsSymmetric<TResult>(
public void Execute<TDataSource>(IConvention<TDataSource> convention, TDataSource data)
where TDataSource : IConventionData
{
conventionReason = convention.ConventionReason;
if (!data.HasData)
throw new ConventionSourceInvalidException(String.Format("{0} has no data", data.Description));
convention.Execute(data, this);
Expand Down
5 changes: 4 additions & 1 deletion TestStack.ConventionTests/Internal/ConventionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

public class ConventionResult
{
public ConventionResult(Type dataType, string conventionTitle, string dataDescription, object[] data)
public ConventionResult(Type dataType, string conventionTitle, string conventionReason, string dataDescription, object[] data)
{
ConventionReason = conventionReason;
DataType = dataType;
ConventionTitle = conventionTitle;
DataDescription = dataDescription;
Expand All @@ -15,6 +16,7 @@ public ConventionResult(Type dataType, string conventionTitle, string dataDescri

public Type DataType { get; private set; }
public string ConventionTitle { get; private set; }
public string ConventionReason { get; private set; }
public string DataDescription { get; private set; }
public object[] Data { get; private set; }

Expand All @@ -23,6 +25,7 @@ public bool HasData
get { return Data.Any(); }
}


protected bool Equals(ConventionResult other)
{
return DataType == other.DataType && string.Equals(ConventionTitle, other.ConventionTitle) && string.Equals(DataDescription, other.DataDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ protected override string Process(IConventionFormatContext context, IEnumerable<
html.RenderBeginTag(HtmlTextWriterTag.H4);
html.Write(conventionResult.ConventionTitle);
html.RenderEndTag();
html.AddAttribute("style", "margin-left:20px;");
html.RenderBeginTag(HtmlTextWriterTag.Div);
html.Write(conventionResult.ConventionReason);
html.RenderEndTag();
if (conventionResult.Data.Any())
{
html.AddAttribute("style", "margin-left:20px;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ protected override string Process(IConventionFormatContext context, IEnumerable<

foreach (var conventionResult in conventionReport)
{
sb.Append(" - ");
sb.AppendLine(conventionResult.ConventionTitle);
sb.Append(" - **");
sb.Append(conventionResult.ConventionTitle);
sb.AppendLine("** ");
sb.AppendLine(conventionResult.ConventionReason);
}
}

Expand Down