Permalink
Browse files

formatting nspec results as giles sessionresults

  • Loading branch information...
1 parent a496ad8 commit 7d1c1429886de948cade3028e845deb84fc5ebb2 @ericbock ericbock committed Mar 21, 2012
@@ -34,6 +34,9 @@
<Reference Include="Giles.Core">
<HintPath>..\..\Giles.Core\bin\Debug\Giles.Core.dll</HintPath>
</Reference>
+ <Reference Include="NSpec">
+ <HintPath>..\..\..\lib\nspec.0.9.59\lib\NSpec.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -43,6 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="GilesSessionResultsFormatter.cs" />
<Compile Include="NSpecRunner.cs" />
<Compile Include="NSpecTestFrameworkInspector.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -0,0 +1,102 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NSpec;
+using NSpec.Domain;
+using NSpec.Domain.Extensions;
+using NSpec.Domain.Formatters;
+using Giles.Core.Runners;
+
+namespace Giles.Runner.NSpec
+{
+ class GilesSessionResultsFormatter : IFormatter, ILiveFormatter
+ {
+ SessionResults _sessionResults;
+
+ public GilesSessionResultsFormatter(SessionResults sessionResults)
+ {
+ _sessionResults = sessionResults;
+ }
+
+ public void Write(ContextCollection contexts)
+ {
+ var examples = contexts.Examples().Count();
+ var failures = contexts.Failures().Count();
+
+ _sessionResults.SessionRunState = GetSessionRunState(examples, failures);
+
+ contexts.Examples().Each(e => _sessionResults.TestResults.Add(e.ToTestResult()));
+ }
+
+ public void Write(Context context)
+ {
+ if (context.Level == 1) _sessionResults.Messages.Add(Environment.NewLine);
+
+ _sessionResults.Messages.Add(indent.Times(context.Level - 1) + context.Name);
+ }
+
+ public void Write(Example e, int level)
+ {
+ var failure = e.Exception == null ? "" : " - FAILED - {0}".With(e.Exception.CleanMessage());
+
+ var whiteSpace = indent.Times(level);
+
+ var result = e.Pending ? whiteSpace + e.Spec + " - PENDING" : whiteSpace + e.Spec + failure;
+
+ _sessionResults.Messages.Add(result);
+ }
+
+ SessionRunState GetSessionRunState(int examples, int failures)
+ {
+ if (examples == 0) return SessionRunState.NoTests;
+ if (failures > 0) return SessionRunState.Failure;
+ return SessionRunState.Success;
+ }
+
+ const string indent = " ";
+ }
+
+ public static class ExampleExtensions
+ {
+ public static TestResult ToTestResult(this Example example)
+ {
+ TestResult testResult = new TestResult()
+ {
+ Name = example.FullName().Replace("_", " "),
+ TestRunner = TESTRUNNER,
+ State = GetState(example),
+ StackTrace = GetStackTrace(example)
+ };
+ return testResult;
+ }
+
+ static TestState GetState(Example example)
+ {
+ if (example.Pending) return TestState.Ignored;
+ if (example.Passed) return TestState.Passed;
+ return TestState.Failed;
+ }
+
+ static string GetStackTrace(Example example)
+ {
+ if (example.Passed || example.Pending) return String.Empty;
+
+ var stackTrace =
+ example.Exception
+ .GetOrFallback(e => e.StackTrace, "").Split('\n')
+ .Where(l => !internalNameSpaces.Any(l.Contains));
+
+ return stackTrace.Flatten(Environment.NewLine).TrimEnd() + Environment.NewLine;
+ }
+
+ const string TESTRUNNER = "NSPEC";
+ static string[] internalNameSpaces =
+ new[]
+ {
+ "NSpec.Domain",
+ "NSpec.AssertionExtensions",
+ "NUnit.Framework"
+ };
+ }
+}
@@ -3,19 +3,25 @@
using System.Linq;
using System.Text;
using Giles.Core.Runners;
+using System.Reflection;
+using NSpec;
+using NSpec.Domain;
namespace Giles.Runner.NSpec
{
public class NSpecRunner : IFrameworkRunner
{
public IEnumerable<string> RequiredAssemblies()
{
- throw new NotImplementedException();
+ return new[] { Assembly.GetAssembly(typeof(NSpecRunner)).Location, "NSpec.dll" };
}
- public SessionResults RunAssembly(System.Reflection.Assembly assembly)
+ public SessionResults RunAssembly(Assembly assembly)
{
- throw new NotImplementedException();
+ var sessionResults = new SessionResults();
+ var runner = new RunnerInvocation(assembly.Location, String.Empty, new GilesSessionResultsFormatter(sessionResults), false);
+ var runResults = runner.Run();
+ return sessionResults;
}
}
}

0 comments on commit 7d1c142

Please sign in to comment.