Skip to content

Commit 51977bf

Browse files
author
Jake Ginnivan
committed
Merge pull request #16 from kkozmic/APIv2Spike
another iteration of API experiments
2 parents bb1f773 + ebd2864 commit 51977bf

25 files changed

+351
-330
lines changed
Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,50 @@
11
namespace TestStack.ConventionTests.Tests
22
{
33
using System.Xml.Linq;
4-
using ApprovalTests;
54
using ApprovalTests.Reporters;
65
using NSubstitute;
76
using NUnit.Framework;
87
using TestStack.ConventionTests.Conventions;
9-
using TestStack.ConventionTests.Helpers;
8+
using TestStack.ConventionTests.Internal;
109
using TestStack.ConventionTests.Tests.Properties;
1110

1211
[TestFixture]
13-
[UseReporter(typeof(DiffReporter))]
12+
[UseReporter(typeof (DiffReporter))]
1413
public class ProjectBasedConventions
1514
{
15+
public ProjectBasedConventions()
16+
{
17+
Convention.Settings.AssertInclunclusive = Assert.Inconclusive;
18+
Convention.Settings.AssertZero = (v, m) => Assert.AreEqual(0, v, m);
19+
}
20+
1621
[Test]
1722
public void ReferencingBinObj()
1823
{
19-
// Actual syntax will be (when not testing):
20-
//
21-
// Convention.Is<ProjectDoesNotReferenceDllsFromBinOrObjDirectories>(new[] { typeof(ProjectBasedConventions).Assembly });
22-
//
23-
2424
var projectProvider = Substitute.For<IProjectProvider>();
2525
var projectLocator = Substitute.For<IProjectLocator>();
2626
projectProvider
2727
.LoadProjectDocument(Arg.Any<string>())
2828
.Returns(XDocument.Parse(Resources.ProjectFileWithBinReference));
2929

30-
var convention = new ProjectDoesNotReferenceDllsFromBinOrObjDirectories(projectLocator, projectProvider);
31-
32-
var exception = Assert.Throws<ConventionFailedException>(() =>
33-
Convention.Is(convention, new[] { typeof(ProjectBasedConventions).Assembly }));
34-
Approvals.Verify(exception.Message);
30+
Convention.Is(new ProjectDoesNotReferenceDllsFromBinOrObjDirectories(),
31+
new Project(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator));
3532
}
36-
33+
3734
[Test]
3835
public void ScriptsNotEmbeddedResources()
3936
{
40-
// Actual syntax will be (when not testing):
41-
//
42-
// Convention.Is<FilesAreEmbeddedResources>(new[] { typeof(ProjectBasedConventions).Assembly }, i => i.EndsWith(".sql"));
43-
//
44-
4537
var projectProvider = Substitute.For<IProjectProvider>();
4638
var projectLocator = Substitute.For<IProjectLocator>();
4739
projectProvider
4840
.LoadProjectDocument(Arg.Any<string>())
4941
.Returns(XDocument.Parse(Resources.ProjectFileWithInvalidSqlScriptFile));
5042

51-
var convention = new FilesAreEmbeddedResources(projectLocator, projectProvider);
52-
53-
var exception = Assert.Throws<ConventionFailedException>(() =>
54-
Convention.Is(convention, new[] { typeof(ProjectBasedConventions).Assembly }, i => i.EndsWith(".sql")));
55-
Approvals.Verify(exception.Message);
43+
Convention.Is(new FilesAreEmbeddedResources(),
44+
new Project(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator)
45+
{
46+
Includes = i => i.EndsWith(".sql")
47+
});
5648
}
5749
}
5850
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
namespace TestStack.ConventionTests.Tests
22
{
3-
using System;
4-
using ApprovalTests;
53
using ApprovalTests.Reporters;
64
using NUnit.Framework;
75
using TestAssembly;
86
using TestStack.ConventionTests.Conventions;
97

108
[TestFixture]
11-
[UseReporter(typeof(DiffReporter))]
9+
[UseReporter(typeof (DiffReporter))]
1210
public class TypeBasedConventions
1311
{
14-
readonly Type[] itemsToVerify;
12+
readonly Types nhibernateEntities;
1513

1614
public TypeBasedConventions()
1715
{
18-
itemsToVerify = typeof(SampleDomainClass).Assembly.GetTypes();
16+
// TODO: This should go to some sort of autodiscovery mechanism so users don't have to see this shit
17+
Convention.Settings.AssertInclunclusive = Assert.Inconclusive;
18+
Convention.Settings.AssertZero = (v, m) => Assert.AreEqual(0, v, m);
19+
20+
var itemsToVerify = typeof (SampleDomainClass).Assembly.GetTypes();
21+
nhibernateEntities = new Types
22+
{
23+
ApplicableTypes = itemsToVerify,
24+
HasApprovedExceptions = false
25+
};
1926
}
2027

2128
[Test]
22-
public void all_methods_are_virtual()
29+
public void all_classes_have_default_constructor()
2330
{
24-
var exception = Assert.Throws<ConventionFailedException>(() => Convention.Is<AllMethodsAreVirtual>(itemsToVerify));
25-
26-
Approvals.Verify(exception.Message);
31+
Convention.Is(new AllClassesHaveDefaultConstructor(), nhibernateEntities);
2732
}
2833

2934
[Test]
30-
public void all_classes_have_default_constructor()
35+
public void all_methods_are_virtual()
3136
{
32-
var exception = Assert.Throws<ConventionFailedException>(() => Convention.Is<AllClassesHaveDefaultConstructor>(itemsToVerify));
33-
34-
Approvals.Verify(exception.Message);
37+
Convention.Is(new AllMethodsAreVirtual(), nhibernateEntities);
3538
}
3639
}
3740
}
Lines changed: 27 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,45 @@
11
namespace TestStack.ConventionTests
22
{
33
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Reflection;
7-
using System.Text;
4+
using ApprovalTests;
85

96
public static class Convention
107
{
11-
public static void Is<T, T2>(IEnumerable<T2> itemsToVerify) where T : ConventionData<T2>, new()
12-
{
13-
Is(new T(), itemsToVerify);
14-
}
15-
public static void Is<T, T2>(IEnumerable<T2> itemsToVerify, Func<string, bool> filter)
16-
where T : ConventionData<T2>, IRuntimeFilter<string>, new()
17-
{
18-
Is(new T(), itemsToVerify);
19-
}
20-
public static void Is<T, T2, TItem>(IEnumerable<T2> itemsToVerify, Func<TItem, bool> filter)
21-
where T : ConventionData<T2>, IRuntimeFilter<TItem>, new()
22-
{
23-
Is(new T(), itemsToVerify);
24-
}
25-
26-
//Item.Is<ConventionData<Type>>
27-
public static void Is<T>(IEnumerable<Type> itemsToVerify) where T : ConventionData<Type>, new()
28-
{
29-
Is(new T(), itemsToVerify);
30-
}
31-
public static void Is<T>(IEnumerable<Type> itemsToVerify, Func<string, bool> filter) where T : ConventionData<Type>, IRuntimeFilter<string>, new()
32-
{
33-
Is(new T(), itemsToVerify, filter);
34-
}
35-
public static void Is<T, TItem>(IEnumerable<Type> itemsToVerify, Func<TItem, bool> filter) where T : ConventionData<Type>, IRuntimeFilter<TItem>, new()
36-
{
37-
Is(new T(), itemsToVerify, filter);
38-
}
8+
public static readonly ConventionSettings Settings = new ConventionSettings();
399

40-
//Item.Is<ConventionData<Assembly>>
41-
public static void Is<T>(IEnumerable<Assembly> itemsToVerify) where T : ConventionData<Assembly>, new()
10+
public static void Is<TData>(IConvention<TData> convention, TData data) where TData : IConventionData
4211
{
43-
Is(new T(), itemsToVerify);
44-
}
45-
public static void Is<T>(IEnumerable<Assembly> itemsToVerify, Func<string, bool> filter) where T : ConventionData<Assembly>, IRuntimeFilter<string>, new()
46-
{
47-
Is(new T(), itemsToVerify, filter);
48-
}
49-
public static void Is<T, TItem>(IEnumerable<Assembly> itemsToVerify, Func<TItem, bool> filter) where T : ConventionData<Assembly>, IRuntimeFilter<TItem>, new()
50-
{
51-
Is(new T(), itemsToVerify, filter);
52-
}
53-
54-
public static void Is<T>(ConventionData<T> convention, IEnumerable<T> itemsToVerify)
55-
{
56-
var results = Result(convention, itemsToVerify);
57-
if (!string.IsNullOrEmpty(results))
58-
throw new ConventionFailedException(results);
59-
}
60-
61-
public static void Is<TConvention, T>(TConvention convention, IEnumerable<T> itemsToVerify, Func<string, bool> itemFilter)
62-
where TConvention : ConventionData<T>, IRuntimeFilter<string>
63-
{
64-
Is<TConvention, T, string>(convention, itemsToVerify, itemFilter);
12+
if (data.HasValidSource == false)
13+
{
14+
// TODO: this would have to have a more reasonable and helpful message...
15+
Settings.AssertInclunclusive("No valid source in " + data);
16+
return;
17+
}
18+
var result = convention.Execute(data);
19+
if (result.IsConclusive == false)
20+
{
21+
Settings.AssertInclunclusive(result.Message);
22+
return;
23+
}
24+
if (data.HasApprovedExceptions)
25+
{
26+
// should we encapsulate Approvals behind Settings?
27+
Approvals.Verify(result.Message);
28+
return;
29+
}
30+
Settings.AssertZero(result.InvalidResultsCount, result.Message);
6531
}
6632

67-
public static void Is<TConvention, T, TItem>(TConvention convention, IEnumerable<T> itemsToVerify, Func<TItem, bool> itemFilter)
68-
where TConvention : ConventionData<T>, IRuntimeFilter<TItem>
33+
public class ConventionSettings
6934
{
70-
var results = Result(convention, itemsToVerify, itemFilter);
71-
if (!string.IsNullOrEmpty(results))
72-
throw new ConventionFailedException(results);
73-
}
35+
public Action<String> AssertInclunclusive;
7436

75-
public static string Result<T>(ConventionData<T> convention, IEnumerable<T> itemsToVerify)
76-
{
77-
var message = new StringBuilder();
78-
var invalidItems = itemsToVerify.Where(i => !convention.Must(i)).ToArray();
79-
if (!invalidItems.Any()) return null;
37+
public Action<int, string> AssertZero;
8038

81-
message.AppendLine(convention.Description ?? "Convention has failing items");
82-
foreach (var invalidType in invalidItems)
39+
public ConventionSettings()
8340
{
84-
message.Append('\t');
85-
convention.ItemDescription(invalidType, message);
41+
// TODO: initialize the type;
8642
}
87-
88-
return message.ToString();
89-
}
90-
91-
public static string Result<TConvention, T, TItem>(TConvention convention, IEnumerable<T> itemsToVerify, Func<TItem, bool> itemFilter)
92-
where TConvention : ConventionData<T>, IRuntimeFilter<TItem>
93-
{
94-
convention.SetFilter(itemFilter);
95-
return Result(convention, itemsToVerify);
9643
}
9744
}
9845
}
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
11
namespace TestStack.ConventionTests
22
{
33
using System;
4+
using System.Text;
45

56
/// <summary>
67
/// This is where we set what our convention is all about
78
/// </summary>
8-
public class ConventionData : ConventionData<Type>
9+
public class ConventionData<TItem>
910
{
11+
public static readonly Predicate<TItem> None = _ => false;
12+
readonly Action<TItem, StringBuilder> defaultItemDescription = DefaultItemDescriptionMethod;
13+
14+
public ConventionData()
15+
{
16+
Must = None;
17+
OrderBy = HashCode;
18+
ItemDescription = defaultItemDescription;
19+
}
20+
21+
public Func<TItem, object> OrderBy { get; set; }
22+
23+
/// <summary>
24+
/// Descriptive text used for failure message in test. Should explan what is wrong, and how to fix it (how to make
25+
/// types that do not conform to the convention do so).
26+
/// </summary>
27+
public string Description { get; set; }
28+
29+
/// <summary>
30+
/// This is the convention. The predicate should return <c>true</c> for types that do conform to the convention, and
31+
/// <c>false</c> otherwise
32+
/// </summary>
33+
public Predicate<TItem> Must { get; set; }
34+
35+
public Action<TItem, StringBuilder> ItemDescription { get; set; }
36+
37+
static void DefaultItemDescriptionMethod(TItem item, StringBuilder message)
38+
{
39+
message.AppendLine(ReferenceEquals(item, null) ? "<<null>>" : item.ToString());
40+
}
41+
42+
object HashCode(TItem arg)
43+
{
44+
if (ReferenceEquals(arg, null))
45+
{
46+
return 0;
47+
}
48+
return arg.GetHashCode();
49+
}
1050
}
1151
}

TestStack.ConventionTests/ConventionData`1.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

TestStack.ConventionTests/ConventionFailedException.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)