-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Closes #177 and #178 * refactor(formatters): pack `using()` blocks * test(formatters): add test case for issue #177 * test(formatters): XUnitFormatter output XML must start with BOM * chore(test): don't mix TestFixture classes in XUnitFormatter test * fix(formatters): XUnitFormatter throws when no options are set * Closes #176
- Loading branch information
1 parent
a61a3f3
commit 06a1ba4
Showing
2 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using FluentAssertions; | ||
using NSpec.Domain; | ||
using NSpec.Domain.Formatters; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace NSpec.Tests | ||
{ | ||
public abstract class describe_XUnitFormatter_base | ||
{ | ||
protected class xunit_formatter_sample_spec : nspec | ||
{ | ||
void a_context_with_a_pending_example() | ||
{ | ||
it["pending example"] = todo; | ||
} | ||
|
||
void a_context_with_a_grandchild_example() | ||
{ | ||
context["a context with an example"] = () => | ||
{ | ||
it["is passing"] = () => Assert.That(true, Is.True); | ||
}; | ||
} | ||
|
||
void a_context_without_an_example() { } | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class describe_XUnitFormatter : describe_XUnitFormatter_base | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
formatter = new XUnitFormatter(); | ||
|
||
string outDirPath = Path.Combine( | ||
Path.GetTempPath(), | ||
"NSpec.Tests", | ||
nameof(describe_XUnitFormatter)); | ||
|
||
Directory.CreateDirectory(outDirPath); | ||
|
||
outFilePath = Path.Combine( | ||
outDirPath, | ||
Path.ChangeExtension(Path.GetRandomFileName(), "xml")); | ||
|
||
formatter.Options = new Dictionary<string, string>() | ||
{ | ||
{ "file", outFilePath }, | ||
}; | ||
|
||
var invocation = new RunnerInvocation( | ||
dll: typeof(describe_XUnitFormatter).GetTypeInfo().Assembly.Location, | ||
tags: typeof(xunit_formatter_sample_spec).Name, | ||
formatter: formatter, | ||
failFast: false); | ||
|
||
contexts = invocation.Run(); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
if (File.Exists(outFilePath)) | ||
{ | ||
File.Delete(outFilePath); | ||
} | ||
} | ||
|
||
[Test] | ||
public void all_output_is_flushed_to_file() | ||
{ | ||
string actual = File.ReadAllText(outFilePath); | ||
|
||
actual.Should().EndWith("</testsuite></testsuites>\r\n"); | ||
} | ||
|
||
[Test] | ||
public void output_file_starts_with_utf16_bom() | ||
{ | ||
var utf16Encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: true); | ||
|
||
byte[] expected = utf16Encoding.GetPreamble(); | ||
|
||
byte[] actual = new byte[expected.Length]; | ||
|
||
using (var fstream = new FileStream(outFilePath, FileMode.Open)) | ||
{ | ||
fstream.Read(actual, 0, actual.Length); | ||
|
||
actual.ShouldBeEquivalentTo(expected); | ||
} | ||
} | ||
|
||
XUnitFormatter formatter; | ||
string outFilePath; | ||
ContextCollection contexts; | ||
} | ||
|
||
[TestFixture] | ||
public class describe_XUnitFormatter_without_options : describe_XUnitFormatter_base | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
formatter = new XUnitFormatter(); | ||
|
||
Run(typeof(xunit_formatter_sample_spec)); | ||
} | ||
|
||
[Test] | ||
public void writing_does_not_throw() | ||
{ | ||
formatter.Write(contextCollection); | ||
} | ||
|
||
// TODO refactor this with WhenRunningSpecs.when_running_specs | ||
|
||
protected void Run(params Type[] types) | ||
{ | ||
var tagsFilter = new Tags().Parse(""); | ||
|
||
var builder = new ContextBuilder(new SpecFinder(types), tagsFilter, new DefaultConventions()); | ||
|
||
contextCollection = builder.Contexts(); | ||
|
||
contextCollection.Build(); | ||
} | ||
|
||
XUnitFormatter formatter; | ||
ContextCollection contextCollection; | ||
} | ||
} |