Skip to content

Commit 46d1bda

Browse files
author
Jake Ginnivan
committed
Using specific exceptions for errors
1 parent 51977bf commit 46d1bda

11 files changed

+71
-73
lines changed

TestStack.ConventionTests.Tests/ProjectBasedConventions.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,39 @@
99
using TestStack.ConventionTests.Tests.Properties;
1010

1111
[TestFixture]
12-
[UseReporter(typeof (DiffReporter))]
12+
[UseReporter(typeof(DiffReporter))]
1313
public class ProjectBasedConventions
1414
{
15-
public ProjectBasedConventions()
15+
Project project;
16+
IProjectProvider projectProvider;
17+
18+
[SetUp]
19+
public void Setup()
1620
{
17-
Convention.Settings.AssertInclunclusive = Assert.Inconclusive;
18-
Convention.Settings.AssertZero = (v, m) => Assert.AreEqual(0, v, m);
21+
projectProvider = Substitute.For<IProjectProvider>();
22+
var projectLocator = Substitute.For<IProjectLocator>();
23+
project = new Project(typeof(ProjectBasedConventions).Assembly, projectProvider, projectLocator);
1924
}
2025

2126
[Test]
2227
public void ReferencingBinObj()
2328
{
24-
var projectProvider = Substitute.For<IProjectProvider>();
25-
var projectLocator = Substitute.For<IProjectLocator>();
2629
projectProvider
2730
.LoadProjectDocument(Arg.Any<string>())
2831
.Returns(XDocument.Parse(Resources.ProjectFileWithBinReference));
2932

30-
Convention.Is(new ProjectDoesNotReferenceDllsFromBinOrObjDirectories(),
31-
new Project(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator));
33+
Convention.Is(new ProjectDoesNotReferenceDllsFromBinOrObjDirectories(), project);
3234
}
33-
35+
3436
[Test]
3537
public void ScriptsNotEmbeddedResources()
3638
{
37-
var projectProvider = Substitute.For<IProjectProvider>();
38-
var projectLocator = Substitute.For<IProjectLocator>();
39+
project.Includes = i => i.EndsWith(".sql");
3940
projectProvider
4041
.LoadProjectDocument(Arg.Any<string>())
4142
.Returns(XDocument.Parse(Resources.ProjectFileWithInvalidSqlScriptFile));
4243

43-
Convention.Is(new FilesAreEmbeddedResources(),
44-
new Project(typeof (ProjectBasedConventions).Assembly, projectProvider, projectLocator)
45-
{
46-
Includes = i => i.EndsWith(".sql")
47-
});
44+
Convention.Is(new FilesAreEmbeddedResources(), project);
4845
}
4946
}
5047
}

TestStack.ConventionTests.Tests/TypeBasedConventions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@ public class TypeBasedConventions
1313

1414
public TypeBasedConventions()
1515
{
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-
2016
var itemsToVerify = typeof (SampleDomainClass).Assembly.GetTypes();
2117
nhibernateEntities = new Types
2218
{
23-
ApplicableTypes = itemsToVerify,
24-
HasApprovedExceptions = false
19+
ApplicableTypes = itemsToVerify
2520
};
2621
}
2722

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
namespace TestStack.ConventionTests
22
{
3-
using System;
3+
using System.Diagnostics;
44
using ApprovalTests;
5+
using ApprovalTests.Core.Exceptions;
56

67
public static class Convention
78
{
8-
public static readonly ConventionSettings Settings = new ConventionSettings();
9-
109
public static void Is<TData>(IConvention<TData> convention, TData data) where TData : IConventionData
1110
{
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-
}
11+
data.ThrowIfHasInvalidSource();
1812
var result = convention.Execute(data);
19-
if (result.IsConclusive == false)
20-
{
21-
Settings.AssertInclunclusive(result.Message);
22-
return;
23-
}
2413
if (data.HasApprovedExceptions)
2514
{
2615
// should we encapsulate Approvals behind Settings?
27-
Approvals.Verify(result.Message);
16+
try
17+
{
18+
Approvals.Verify(result.Message);
19+
20+
Trace.WriteLine(string.Format("{0} has approved exceptions:\r\n\r\n{1}", convention.GetType().Name, result.Message));
21+
}
22+
catch (ApprovalException ex)
23+
{
24+
throw new ConventionFailedException("Approved exceptions for convention differs, see inner exception for more information", ex);
25+
}
2826
return;
2927
}
30-
Settings.AssertZero(result.InvalidResultsCount, result.Message);
31-
}
32-
33-
public class ConventionSettings
34-
{
35-
public Action<String> AssertInclunclusive;
3628

37-
public Action<int, string> AssertZero;
38-
39-
public ConventionSettings()
40-
{
41-
// TODO: initialize the type;
42-
}
29+
throw new ConventionFailedException(result.Message);
4330
}
4431
}
4532
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace TestStack.ConventionTests
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
[Serializable]
7+
public class ConventionFailedException : Exception
8+
{
9+
public ConventionFailedException() { }
10+
public ConventionFailedException(string message) : base(message) { }
11+
public ConventionFailedException(string message, Exception inner) : base(message, inner) { }
12+
protected ConventionFailedException(SerializationInfo info, StreamingContext context)
13+
: base(info, context) { }
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace TestStack.ConventionTests.Conventions
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
[Serializable]
7+
public class ConventionSourceInvalidException : Exception
8+
{
9+
public ConventionSourceInvalidException() { }
10+
public ConventionSourceInvalidException(string message) : base(message) { }
11+
public ConventionSourceInvalidException(string message, Exception inner) : base(message, inner) { }
12+
protected ConventionSourceInvalidException(
13+
SerializationInfo info,
14+
StreamingContext context)
15+
: base(info, context) { }
16+
}
17+
}

TestStack.ConventionTests/Conventions/Project.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public Project(Assembly assembly, IProjectProvider projectProvider, IProjectLoca
2222

2323
public IProjectProvider ProjectProvider { get; private set; }
2424

25-
public bool HasValidSource
25+
public void ThrowIfHasInvalidSource()
2626
{
27-
get { return ProjectLocator.ResolveProjectFilePath(Assembly) != null; }
27+
if (ProjectLocator.ResolveProjectFilePath(Assembly) == null)
28+
throw new ConventionSourceInvalidException("Cannot resolve project file for assembly {0}");
2829
}
2930

3031
public bool HasApprovedExceptions { get; set; }

TestStack.ConventionTests/Conventions/ProjectDoesNotReferenceDllsFromBinOrObjDirectories.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public ConventionResult Execute(Project data)
1616
return ConventionResult.For(invalid, "Some invalid references found.", (r, m) => m.AppendLine("\t" + r));
1717
}
1818

19-
2019
static bool IsBinOrObjReference(string reference)
2120
{
2221
return Regex.IsMatch(reference, AssemblyReferencingObjRegex, RegexOptions.IgnoreCase);

TestStack.ConventionTests/Conventions/Types.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace TestStack.ConventionTests.Conventions
22
{
33
using System;
4-
using System.Linq;
4+
using TestStack.ConventionTests.Internal;
55

66
/// <summary>
77
/// This is where we set what our convention is all about.
@@ -13,9 +13,10 @@ public class Types : IConventionData
1313

1414
public bool HasApprovedExceptions { get; set; }
1515

16-
public bool HasValidSource
16+
public void ThrowIfHasInvalidSource()
1717
{
18-
get { return ApplicableTypes.Any(); }
18+
if (ApplicableTypes.None())
19+
throw new ConventionSourceInvalidException("You must supply types to verify");
1920
}
2021
}
2122
}

TestStack.ConventionTests/IConventionData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public interface IConventionData
44
{
5-
bool HasValidSource { get; }
5+
void ThrowIfHasInvalidSource();
66
bool HasApprovedExceptions { get; }
77
}
88
}

TestStack.ConventionTests/Internal/ConventionResult.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@
88
public class ConventionResult
99
{
1010
public string Message { get; set; }
11-
public bool IsConclusive { get; set; }
12-
// TODO: perhaps name it better so that it doesn't get confused with System.Exception and related concepts
13-
public int InvalidResultsCount { get; set; }
1411

1512
public static ConventionResult For<TResult>(IEnumerable<TResult> items,
1613
string header,
1714
Action<TResult, StringBuilder> itemDescriptor)
1815
{
1916
var array = items.ToArray();
20-
var result = new ConventionResult
21-
{
22-
InvalidResultsCount = array.Length,
23-
IsConclusive = true
24-
};
17+
var result = new ConventionResult();
2518
if (array.None())
2619
{
2720
return result;
@@ -36,14 +29,5 @@ public static ConventionResult For<TResult>(IEnumerable<TResult> items,
3629
result.Message = message.ToString();
3730
return result;
3831
}
39-
40-
public static ConventionResult Inconclusive(string message)
41-
{
42-
return new ConventionResult
43-
{
44-
Message = message,
45-
IsConclusive = false
46-
};
47-
}
4832
}
4933
}

0 commit comments

Comments
 (0)