Skip to content

Commit f4b6461

Browse files
committed
Merge pull request #45 from JakeGinnivan/DetectWhenConventionDoesNotSetResult
Fixed issue #38 - should detect when result not set
2 parents f1becb5 + 225e214 commit f4b6461

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace TestStack.ConventionTests.Tests
2+
{
3+
using System;
4+
using System.Linq;
5+
using NUnit.Framework;
6+
using TestStack.ConventionTests.ConventionData;
7+
using TestStack.ConventionTests.Internal;
8+
9+
[TestFixture]
10+
public class ConventionFixture
11+
{
12+
[Test]
13+
public void ShouldThrowWhenConventionDoesNotSetResult()
14+
{
15+
Assert.Throws<ResultNotSetException>(() =>
16+
Convention.Is(new CustomConventionWhichDoesNothing(), Types.InAssemblyOf<ConventionFixture>()));
17+
}
18+
19+
// ReSharper disable once UnusedVariable
20+
class CustomConventionWhichDoesNothing : IConvention<Types>
21+
{
22+
public void Execute(Types data, IConventionResultContext result)
23+
{
24+
var failingTypes = data.TypesToVerify.Where(IsBroken);
25+
// Oops, I forgot to set the result
26+
}
27+
28+
bool IsBroken(Type type)
29+
{
30+
return true;
31+
}
32+
}
33+
}
34+
}

TestStack.ConventionTests.Tests/TestStack.ConventionTests.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Autofac\TestTypes\IBar.cs" />
6262
<Compile Include="Autofac\TestTypes\IFoo.cs" />
6363
<Compile Include="ConventionAssertionClassTests.cs" />
64+
<Compile Include="ConventionFixture.cs" />
6465
<Compile Include="CsvReportTests.cs" />
6566
<Compile Include="MvcConventions.cs" />
6667
<Compile Include="ProjectBasedConventions.cs" />

TestStack.ConventionTests/Internal/ConventionContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class ConventionContext : IConventionResultContext, IConventionFormatCont
1313
readonly IList<IResultsProcessor> processors;
1414
readonly ITestResultProcessor testResultProcessor;
1515
readonly IList<ConventionResult> results = new List<ConventionResult>();
16+
bool resultSet;
1617

1718
public ConventionContext(string dataDescription, IList<IReportDataFormatter> formatters,
1819
IList<IResultsProcessor> processors, ITestResultProcessor testResultProcessor)
@@ -59,6 +60,7 @@ IReportDataFormatter GetReportDataFormatterFor(object data)
5960

6061
void IConventionResultContext.Is<TResult>(string resultTitle, IEnumerable<TResult> failingData)
6162
{
63+
resultSet = true;
6264
// ReSharper disable PossibleMultipleEnumeration
6365
results.Add(new ConventionResult(
6466
typeof (TResult),
@@ -71,6 +73,7 @@ void IConventionResultContext.IsSymmetric<TResult>(
7173
string firstSetFailureTitle, IEnumerable<TResult> firstSetFailureData,
7274
string secondSetFailureTitle, IEnumerable<TResult> secondSetFailureData)
7375
{
76+
resultSet = true;
7477
results.Add(new ConventionResult(
7578
typeof (TResult), firstSetFailureTitle,
7679
dataDescription,
@@ -103,6 +106,9 @@ public void Execute<TDataSource>(IConvention<TDataSource> convention, TDataSourc
103106
throw new ConventionSourceInvalidException(String.Format("{0} has no data", data.Description));
104107
convention.Execute(data, this);
105108

109+
if (!resultSet)
110+
throw new ResultNotSetException("{0} did not set a result, conventions must always set a result");
111+
106112
foreach (IResultsProcessor resultsProcessor in processors)
107113
{
108114
resultsProcessor.Process(this, ConventionResults);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace TestStack.ConventionTests.Internal
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
[Serializable]
7+
public class ResultNotSetException : Exception
8+
{
9+
public ResultNotSetException() { }
10+
public ResultNotSetException(string message) : base(message) { }
11+
public ResultNotSetException(string message, Exception inner) : base(message, inner) { }
12+
13+
protected ResultNotSetException(
14+
SerializationInfo info,
15+
StreamingContext context)
16+
: base(info, context)
17+
{
18+
}
19+
}
20+
}

TestStack.ConventionTests/TestStack.ConventionTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="Reporting\TestResult.cs" />
116116
<Compile Include="Reporting\StringDataFormatter.cs" />
117117
<Compile Include="Reporting\TypeDataFormatter.cs" />
118+
<Compile Include="Internal\ResultNotSetException.cs" />
118119
</ItemGroup>
119120
<ItemGroup>
120121
<None Include="packages.config" />

0 commit comments

Comments
 (0)