Skip to content

Commit f2989ef

Browse files
committed
Added Dependency nugets and code
1 parent 1f78a65 commit f2989ef

16 files changed

Lines changed: 11131 additions & 9 deletions

ConventionTests/Class1.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using ApprovalTests;
5+
using ApprovalTests.Core;
6+
using ApprovalTests.Namers;
7+
using NUnit.Framework;
8+
using Approvals = ApprovalTests.Approvals;
9+
10+
namespace ConventionTests
11+
{
12+
public interface IConventionTest
13+
{
14+
string Name { get; }
15+
void Execute();
16+
}
17+
18+
19+
/// <summary>
20+
/// This is where we set what our convention is all about
21+
/// </summary>
22+
public class ConventionData
23+
{
24+
/// <summary>
25+
/// list of assemblies to scan for types that our convention is related to. Can be null, in which case all assemblies starting with 'Als.' will be scanned
26+
/// </summary>
27+
public Assembly[] Assemblies { get; set; }
28+
29+
/// <summary>
30+
/// Descriptive text used for failure message in test. Should explan what is wrong, and how to fix it (how to make types that do not conform to the convention do so).
31+
/// </summary>
32+
public string FailDescription { get; set; }
33+
34+
/// <summary>
35+
/// Specifies that there are valid exceptions to the rule specified by the convention.
36+
/// </summary>
37+
/// <remarks>
38+
/// When set to <c>true</c> will run the test as Approval test so that the exceptional cases can be reviewed and approved.
39+
/// </remarks>
40+
public bool HasApprovedExceptions { get; set; }
41+
42+
/// <summary>
43+
/// This is the convention. The predicate should return <c>true</c> for types that do conform to the convention, and <c>false</c> otherwise
44+
/// </summary>
45+
public Predicate<Type> Must { get; set; }
46+
47+
/// <summary>
48+
/// Predicate that finds types that we want to apply out convention to.
49+
/// </summary>
50+
public Predicate<Type> Types { get; set; }
51+
52+
public Func<Type, string> FailItemDescription { get; set; }
53+
54+
/// <summary>
55+
/// helper method to set <see cref="Assemblies" /> in a more convenient manner.
56+
/// </summary>
57+
/// <param name="assembly"> </param>
58+
/// <returns> </returns>
59+
public ConventionData FromAssembly(params Assembly[] assembly)
60+
{
61+
Assemblies = assembly;
62+
return this;
63+
}
64+
65+
/// <summary>
66+
/// helper method to set <see cref="HasApprovedExceptions" /> in a more convenient manner.
67+
/// </summary>
68+
/// <returns> </returns>
69+
public ConventionData WithApprovedExceptions(string explanation = null)
70+
{
71+
HasApprovedExceptions = true;
72+
return this;
73+
}
74+
}
75+
76+
/// <summary>
77+
/// Base class for convention tests. Inherited types should be put in "/Conventions" folder in test assembly and follow Sentence_naming_convention_with_underscores_indead_of_spaces These tests will be ran by <see
78+
/// cref="ConventionTestsRunner" /> .
79+
/// </summary>
80+
public abstract class ConventionTest : IConventionTest
81+
{
82+
#region IConventionTest Members
83+
84+
public virtual string Name
85+
{
86+
get { return GetType().Name.Replace('_', ' '); }
87+
}
88+
89+
public virtual void Execute()
90+
{
91+
var data = SetUp();
92+
var typesToTest = GetTypesToTest(data);
93+
if (typesToTest.Length == 0)
94+
{
95+
Assert.Inconclusive(
96+
"No types found to apply the convention to. Make sure the Types predicate is correct and that the right assemblies to scan are specified.");
97+
}
98+
var itemDescription = (data.FailItemDescription ?? (t => t.ToString()));
99+
var invalidTypes = Array.FindAll(typesToTest, t => data.Must(t) == false);
100+
var message = (data.FailDescription ?? "Invalid types found") + Environment.NewLine + "\t" +
101+
string.Join(Environment.NewLine + "\t", invalidTypes.Select(itemDescription));
102+
if (data.HasApprovedExceptions)
103+
{
104+
Approve(message);
105+
}
106+
else
107+
{
108+
Assert.AreEqual(0, invalidTypes.Count(), message);
109+
}
110+
}
111+
112+
#endregion
113+
114+
protected virtual Assembly[] GetAssembliesToScan(ConventionData data)
115+
{
116+
if (data.Assemblies != null)
117+
{
118+
return data.Assemblies;
119+
}
120+
var assembly = Assembly.GetCallingAssembly();
121+
var companyName = assembly.FullName.Substring(0, assembly.FullName.IndexOf('.'));
122+
var assemblyNames = assembly.GetReferencedAssemblies();
123+
var applicationAssemblies = Array.FindAll(assemblyNames, n => n.FullName.StartsWith(companyName));
124+
return Array.ConvertAll(applicationAssemblies, Assembly.Load);
125+
}
126+
127+
/// <summary>
128+
/// This is the only method you need to override. Return a <see cref="ConventionData" /> that describes your convention.
129+
/// </summary>
130+
/// <returns> </returns>
131+
protected abstract ConventionData SetUp();
132+
133+
private void Approve(string message)
134+
{
135+
Approvals.Verify(new ApprovalTextWriter(message), new ConventionTestNamer(GetType().Name),
136+
Approvals.GetReporter());
137+
}
138+
139+
protected virtual Type[] GetTypesToTest(ConventionData data)
140+
{
141+
return
142+
GetAssembliesToScan(data).SelectMany(a => a.GetTypes()).Where(data.Types.Invoke).OrderBy(t => t.FullName)
143+
.ToArray();
144+
}
145+
}
146+
147+
public class ConventionTestNamer : UnitTestFrameworkNamer, IApprovalNamer
148+
{
149+
private readonly string name;
150+
151+
public ConventionTestNamer(string name)
152+
{
153+
this.name = name;
154+
}
155+
156+
#region IApprovalNamer Members
157+
158+
string IApprovalNamer.Name
159+
{
160+
get { return name; }
161+
}
162+
163+
#endregion
164+
}
165+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<version>1.0.4</version>
5+
<authors>Krzysztof Kozmic</authors>
6+
<owners>Krzysztof Kozmic</owners>
7+
<dependencies>
8+
<dependency id="ApprovalTests" version="1.9" />
9+
<dependency id="NUnit" version="2.5.10" />
10+
</dependencies>
11+
<id>ConventionTests.NUnit</id>
12+
<title>ConventionTests for NUnit</title>
13+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
14+
<description>My package description.</description>
15+
<summary>Simple code-only package offering some structure to tests validating conventions.</summary>
16+
</metadata>
17+
</package>

ConventionTests/ConventionTests.csproj

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34+
<Reference Include="ApprovalTests">
35+
<HintPath>..\packages\ApprovalTests.1.9\lib\ApprovalTests.dll</HintPath>
36+
</Reference>
37+
<Reference Include="ApprovalUtilities">
38+
<HintPath>..\packages\ApprovalTests.1.9\lib\ApprovalUtilities.dll</HintPath>
39+
</Reference>
40+
<Reference Include="nunit.framework">
41+
<HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath>
42+
</Reference>
3443
<Reference Include="System" />
3544
<Reference Include="System.Core" />
3645
<Reference Include="System.Xml.Linq" />
@@ -40,9 +49,14 @@
4049
<Reference Include="System.Xml" />
4150
</ItemGroup>
4251
<ItemGroup>
43-
<Compile Include="Class1.cs" />
52+
<Compile Include="Conventions\__Run.cs" />
53+
<Compile Include="ConventionTests.NUnit.cs" />
4454
<Compile Include="Properties\AssemblyInfo.cs" />
4555
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="ConventionTests.NUnit.nuspec" />
58+
<None Include="packages.config" />
59+
</ItemGroup>
4660
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4761
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
4862
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using ApprovalTests.Reporters;
5+
using NUnit.Framework;
6+
7+
[assembly: UseReporter(typeof (DiffReporter))]
8+
9+
namespace ConventionTests
10+
{
11+
[TestFixture]
12+
public class ConventionTestsRunner
13+
{
14+
[TestFixtureSetUp]
15+
public static void GlobalSetUp()
16+
{
17+
}
18+
19+
public TestCaseData[] Conventions
20+
{
21+
get
22+
{
23+
var types = GetConventionTypes();
24+
var conventionTests = Array.ConvertAll(types, BuildTestData);
25+
return conventionTests;
26+
}
27+
}
28+
29+
private TestCaseData BuildTestData(Type t)
30+
{
31+
var convention = CreateConvention(t);
32+
return new TestCaseData(convention).SetName(convention.Name);
33+
}
34+
35+
private static IConventionTest CreateConvention(Type t)
36+
{
37+
return (IConventionTest) Activator.CreateInstance(t);
38+
}
39+
40+
private static Type[] GetConventionTypes()
41+
{
42+
var types =
43+
Assembly.GetExecutingAssembly().GetExportedTypes().Where(
44+
IsConventionTest).ToArray();
45+
return types;
46+
}
47+
48+
private static bool IsConventionTest(Type type)
49+
{
50+
return type.IsClass && type.IsAbstract == false && typeof (IConventionTest).IsAssignableFrom(type);
51+
}
52+
53+
[Test]
54+
[TestCaseSource("Conventions")]
55+
public void Run(IConventionTest test)
56+
{
57+
test.Execute();
58+
}
59+
}
60+
}

ConventionTests/Properties/AssemblyInfo.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following
65
// set of attributes. Change these attribute values to modify the information
76
// associated with an assembly.
7+
88
[assembly: AssemblyTitle("ConventionTests")]
99
[assembly: AssemblyDescription("")]
1010
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
1717
// Setting ComVisible to false makes the types in this assembly not visible
1818
// to COM components. If you need to access a type in this assembly from
1919
// COM, set the ComVisible attribute to true on that type.
20+
2021
[assembly: ComVisible(false)]
2122

2223
// The following GUID is for the ID of the typelib if this project is exposed to COM
24+
2325
[assembly: Guid("65eebf47-5385-43ca-9d50-0ebd538c3ad3")]
2426

2527
// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
3234
// You can specify all the values or you can default the Build and Revision Numbers
3335
// by using the '*' as shown below:
3436
// [assembly: AssemblyVersion("1.0.*")]
37+
3538
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
39+
[assembly: AssemblyFileVersion("1.0.0.0")]

ConventionTests/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="ApprovalTests" version="1.9" />
4+
<package id="NUnit" version="2.6.0.12054" />
5+
</packages>
68.9 KB
Binary file not shown.
135 KB
Binary file not shown.
96.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)