Skip to content

Commit

Permalink
Nunit 4 (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed May 11, 2024
1 parent 8c36848 commit e0ad2ae
Show file tree
Hide file tree
Showing 31 changed files with 174 additions and 75 deletions.
17 changes: 17 additions & 0 deletions src/ApprovalTests.NUnit3/ApprovalTests.NUnit3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net8.0</TargetFrameworks>
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<ProjectReference Include="..\ApprovalTests\ApprovalTests.csproj" />
<ProjectReference Include="..\ApprovalUtilities\ApprovalUtilities.csproj" />
</ItemGroup>
<Import Project="$(ProjectDir)..\ApprovalTests\build\ApprovalTests.targets" />
<Import Project="$(ProjectDir)..\ApprovalTests\build\ApprovalTests.props" />
</Project>
1 change: 1 addition & 0 deletions src/ApprovalTests.NUnit3/ApprovalTestsConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: UseReporter(typeof(DiffReporter))]
5 changes: 5 additions & 0 deletions src/ApprovalTests.NUnit3/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using ApprovalTests;
global using ApprovalTests.Reporters;
global using ApprovalTests.Reporters.TestFrameworks;
global using NUnit.Framework;
global using NUnit.Framework.Internal;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
World
30 changes: 30 additions & 0 deletions src/ApprovalTests.NUnit3/NUnitReporterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[TestFixture]
public class NUnitReporterTest
{
[Test]
public void TestNunitIsWorking()
{
Approvals.SetCaller();
Assert.IsTrue(NUnit3Reporter.INSTANCE.IsWorkingInThisEnvironment("default.txt"));
}

[Test]
[UseReporter(typeof(NUnitReporterWithCleanup))]
public void TestReporter()
{
try
{
using (new TestExecutionContext.IsolatedContext())
{
Approvals.Verify("Hello");
}
}
catch (AssertionException exception)
{
var expectedMessage = string.Format(" String lengths are both 5. Strings differ at index 0.{0} Expected: \"World\"{0} But was: \"Hello\"{0} -----------^{0}", Environment.NewLine);
Assert.AreEqual(
expectedMessage,
exception.Message);
}
}
}
14 changes: 14 additions & 0 deletions src/ApprovalTests.NUnit3/NUnitReporterWithCleanup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class NUnitReporterWithCleanup : NUnit3Reporter
{
public override void Report(string approved, string received)
{
try
{
base.Report(approved, received);
}
finally
{
File.Delete(received);
}
}
}
2 changes: 1 addition & 1 deletion src/ApprovalTests.Tests/ApprovalTests.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.4" />
<PackageReference Include="MarkdownSnippets.MsBuild" Version="27.0.2" PrivateAssets="all" />
<PackageReference Include="itext7" Version="8.0.4" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
Expand Down
6 changes: 3 additions & 3 deletions src/ApprovalTests.Tests/FileApproverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void LineEndingsAreIgnored()
File.WriteAllText(approvedFile, "Foo\nBar");
File.WriteAllText(receivedFile, "Foo\r\nBar");
var fileApprover = new FileApprover(null, null, true).Approve(approvedFile, receivedFile);
Assert.IsNull(fileApprover);
ClassicAssert.IsNull(fileApprover);
}

[Test]
Expand All @@ -34,13 +34,13 @@ public void LineEndingAreNotIgnored()
File.WriteAllText(approvedFile, "Foo\nBar");
File.WriteAllText(receivedFile, "Foo\r\nBar");
var fileApprover = new FileApprover(null, null).Approve(approvedFile, receivedFile);
Assert.IsInstanceOf<ApprovalMismatchException>(fileApprover);
ClassicAssert.IsInstanceOf<ApprovalMismatchException>(fileApprover);
}

static void AssertApprover(string receivedFile, string approvedFile, bool expected)
{
var basePath = PathUtilities.GetDirectoryForCaller();
var fileApprover = new FileApprover(null, null).Approve(basePath + approvedFile, basePath + receivedFile);
Assert.AreEqual(expected, fileApprover == null);
ClassicAssert.AreEqual(expected, fileApprover == null);
}
}
3 changes: 2 additions & 1 deletion src/ApprovalTests.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
global using iText.Kernel.Pdf;
global using iText.Layout;
global using iText.Layout.Element;
global using NUnit.Framework;
global using NUnit.Framework;
global using NUnit.Framework.Legacy;
14 changes: 7 additions & 7 deletions src/ApprovalTests.Tests/Namer/AdditionalInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void UniqueForRuntime()
public void WithoutExtraInfo()
{
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual("AdditionalInformationTests.WithoutExtraInfo", name);
ClassicAssert.AreEqual("AdditionalInformationTests.WithoutExtraInfo", name);
}

[Test]
Expand All @@ -32,7 +32,7 @@ public void WithScenarioData()
using (ApprovalResults.ForScenario("ScenarioName"))
{
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual("AdditionalInformationTests.WithScenarioData.ForScenario.ScenarioName", name);
ClassicAssert.AreEqual("AdditionalInformationTests.WithScenarioData.ForScenario.ScenarioName", name);
}
}

Expand All @@ -43,7 +43,7 @@ public async Task WithScenarioDataAsync()
{
await Task.Delay(10);
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual("AdditionalInformationTests.WithScenarioDataAsync.ForScenario.asyncScenario", name);
ClassicAssert.AreEqual("AdditionalInformationTests.WithScenarioDataAsync.ForScenario.asyncScenario", name);
}
}

Expand All @@ -54,7 +54,7 @@ public void WithScenarioDataScrubsInvalidChars()
using (ApprovalResults.ForScenario("invalid/chars"))
{
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual(
ClassicAssert.AreEqual(
"AdditionalInformationTests.WithScenarioDataScrubsInvalidChars.ForScenario.invalid_chars", name);
}
}
Expand All @@ -66,7 +66,7 @@ public void WithMultiplePartScenarioData(string a, string b)
using (ApprovalResults.ForScenario(a, b))
{
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual("AdditionalInformationTests.WithMultiplePartScenarioData.ForScenario.foo.bar", name);
ClassicAssert.AreEqual("AdditionalInformationTests.WithMultiplePartScenarioData.ForScenario.foo.bar", name);
}
}

Expand All @@ -78,7 +78,7 @@ public void TestMultipleNames()
using (ApprovalResults.ForScenario("machineName"))
{
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual(name,
ClassicAssert.AreEqual(name,
"AdditionalInformationTests.TestMultipleNames.ForScenario.scenario.ForScenario.machineName");
}
}
Expand All @@ -91,7 +91,7 @@ public class NestedClassTests
public void WithNestedClass()
{
var name = Approvals.GetDefaultNamer().Name;
Assert.AreEqual("AdditionalInformationTests.NestedClassTests.WithNestedClass", name);
ClassicAssert.AreEqual("AdditionalInformationTests.NestedClassTests.WithNestedClass", name);
}
}
}
2 changes: 1 addition & 1 deletion src/ApprovalTests.Tests/Namer/ApprovalResultsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void TestUniqueNames()

[Test]
public void TestEasyNames() =>
Assert.AreEqual("Windows 7", ApprovalResults.TransformEasyOsName("Microsoft Windows 7 Professional N"));
ClassicAssert.AreEqual("Windows 7", ApprovalResults.TransformEasyOsName("Microsoft Windows 7 Professional N"));

public void SampleUniqueForOs()
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApprovalTests.Tests/Namer/ApprovalsFilenameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public void TestMachineSpecificName()
var approvalsFilename = ApprovalsFilename.Parse(@"..\Email\EmailTest.Testname.Microsoft_Windows_10_Education.approved.eml");
// end-snippet
Approvals.Verify(approvalsFilename);
Assert.True(approvalsFilename.IsMachineSpecific);
ClassicAssert.True(approvalsFilename.IsMachineSpecific);
}

[Test]
Expand Down
8 changes: 4 additions & 4 deletions src/ApprovalTests.Tests/Namer/NunitStackTraceNamerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ public class NunitStackTraceNamerTests
public void TestApprovalName()
{
var name = new UnitTestFrameworkNamer().Name;
Assert.AreEqual("NunitStackTraceNamerTests.TestApprovalName", name);
ClassicAssert.AreEqual("NunitStackTraceNamerTests.TestApprovalName", name);
}

[Test]
public void TestSourcePath()
{
var path = Approvals.GetDefaultNamer().SourcePath;
Assert.IsNotEmpty(path);
ClassicAssert.IsNotEmpty(path);
var fullPath = path.ToLower() + Path.DirectorySeparatorChar + GetType().Name + ".cs";
Assert.IsTrue(File.Exists(fullPath), fullPath + " does not exist");
ClassicAssert.IsTrue(File.Exists(fullPath), fullPath + " does not exist");
}

[Test]
Expand All @@ -25,6 +25,6 @@ public void TestCaseAttributes(string caseName)
{
NamerFactory.AdditionalInformation = caseName;
var name = new UnitTestFrameworkNamer().Name;
Assert.AreEqual("NunitStackTraceNamerTests.TestCaseAttributes." + caseName, name);
ClassicAssert.AreEqual("NunitStackTraceNamerTests.TestCaseAttributes." + caseName, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ public void Parse_UsingStaticInitialize_DontThrowInvalidOperationException()
}
catch (InvalidOperationException e)
{
Assert.Fail(
"InvalidOperationException when trying to parse stacktrace. " +
"This is caused by the parser collection not being thread-safe. " +
"Original exception message : {0} and stacktrace : {1}",
e.Message,
e.StackTrace
);
Assert.Fail($"InvalidOperationException when trying to parse stacktrace. This is caused by the parser collection not being thread-safe. Original exception message : {e.Message} and stacktrace : {e.StackTrace}");
}
// Because the current stacktrace passed to the parse method doesn't contains any trace of a compliant stacktrace parser
// it's normal that we receive an exception here so let's ignore it.
Expand Down
8 changes: 4 additions & 4 deletions src/ApprovalTests.Tests/Pdf/PdfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ arbitrary content
public void TestPdf_ScrubberIdsMatch(string input)
{
var matchPositions = PdfScrubber.FindIds(input).ToList();
Assert.AreEqual(2, matchPositions.Count);
ClassicAssert.AreEqual(2, matchPositions.Count);

Assert.IsTrue(matchPositions.All(pos => input[pos.start - 1] == '<' && input[pos.start + pos.length] == '>'));
ClassicAssert.IsTrue(matchPositions.All(pos => input[pos.start - 1] == '<' && input[pos.start + pos.length] == '>'));
}

[TestCase("""
Expand All @@ -46,7 +46,7 @@ public void TestPdf_ScrubberIdsMatch(string input)
public void TestPdf_ScrubberIdsNotMatch(string input)
{
var matchPositions = PdfScrubber.FindIds(input).ToList();
Assert.AreEqual(0, matchPositions.Count);
ClassicAssert.AreEqual(0, matchPositions.Count);
}

[Test]
Expand Down Expand Up @@ -128,6 +128,6 @@ public void TestPdf_ScrubberFindAllReplacementsInFile()

using var fileStream = File.OpenRead(pdf);
var matches = PdfScrubber.FindReplacements(fileStream);
Assert.AreEqual(3, matches.Count());
ClassicAssert.AreEqual(3, matches.Count());
}
}
6 changes: 3 additions & 3 deletions src/ApprovalTests.Tests/Persistence/AsyncSaverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ public void TestAsyncWrapperSave()
{
using var f = new TempFile("stuff");
var s = new FileSaver(f.File);
Assert.AreEqual("hello", s.ToAsync().Save("hello").Result);
ClassicAssert.AreEqual("hello", s.ToAsync().Save("hello").Result);
}

[Test]
public void TestTrueAsyncSave()
{
using var f = new TempFile("stuff");
var s = new FileAsyncSaver(f.File);
Assert.AreEqual("hello", s.Save("hello").Result);
ClassicAssert.AreEqual("hello", s.Save("hello").Result);
}

[Test]
public void TestNonAsyncWrapper()
{
using var f = new TempFile("stuff");
var s = new FileAsyncSaver(f.File);
Assert.AreEqual("hello", s.ToSynchronous().Save("hello"));
ClassicAssert.AreEqual("hello", s.ToSynchronous().Save("hello"));
}
}
2 changes: 1 addition & 1 deletion src/ApprovalTests.Tests/Reporters/AssemblyLevelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public void TestClassLevel()
{
using (Approvals.SetFrontLoadedReporter(ReportWithoutFrontLoading.INSTANCE))
{
Assert.AreEqual(typeof(DiffReporter), Approvals.GetReporter().GetType());
ClassicAssert.AreEqual(typeof(DiffReporter), Approvals.GetReporter().GetType());
}
}
}
18 changes: 9 additions & 9 deletions src/ApprovalTests.Tests/Reporters/FirstWorkingReporterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public void TestCallsFirstAndOnlyFirst()
var c = new RecordingReporter(true);

var reporter = new FirstWorkingReporter(a, b, c);
Assert.IsTrue(reporter.IsWorkingInThisEnvironment("default.txt"));
ClassicAssert.IsTrue(reporter.IsWorkingInThisEnvironment("default.txt"));
reporter.Report("a", "b");
Assert.IsNull(a.CalledWith);
Assert.AreEqual("a,b", b.CalledWith);
Assert.IsNull(c.CalledWith);
ClassicAssert.IsNull(a.CalledWith);
ClassicAssert.AreEqual("a,b", b.CalledWith);
ClassicAssert.IsNull(c.CalledWith);
}

[Test]
Expand All @@ -28,7 +28,7 @@ public void TestException()
try
{
var ex = ExceptionUtilities.GetException(() => new DiffReporter().Report("received.notreal", "received.notreal"));
Assert.AreEqual("Could not find a diff tool for extension: .notreal", ex.Message);
ClassicAssert.AreEqual("Could not find a diff tool for extension: .notreal", ex.Message);
}
finally
{
Expand All @@ -43,10 +43,10 @@ public void TestCleanup()
var cleanup2 = new MockCleanup();
var r = new FirstWorkingReporter(cleanup1, new QuietReporter(), cleanup2);
r.CleanUp("a", "r");
Assert.AreEqual("a", cleanup1.approved);
Assert.AreEqual("a", cleanup2.approved);
Assert.AreEqual("r", cleanup1.received);
Assert.AreEqual("r", cleanup2.received);
ClassicAssert.AreEqual("a", cleanup1.approved);
ClassicAssert.AreEqual("a", cleanup2.approved);
ClassicAssert.AreEqual("r", cleanup1.received);
ClassicAssert.AreEqual("r", cleanup2.received);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/ApprovalTests.Tests/Reporters/MultiReporterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ public void TestMultiReporter()
var b = new RecordingReporter();
var multi = new MultiReporter(a, b);
multi.Report("a", "r");
Assert.AreEqual("a,r", a.CalledWith);
Assert.AreEqual("a,r", b.CalledWith);
ClassicAssert.AreEqual("a,r", a.CalledWith);
ClassicAssert.AreEqual("a,r", b.CalledWith);
}

[Test]
public void TestCallAfterException()
{
var a = new NUnitReporter();
var a = new NUnit4Reporter();
var b = new RecordingReporter();
var multi = new MultiReporter(a, b);
var exception = ExceptionUtilities.GetException(() => multi.Report("a", "r"));
Assert.AreEqual("a,r", b.CalledWith);
Assert.IsInstanceOf<Exception>(exception);
ClassicAssert.AreEqual("a,r", b.CalledWith);
ClassicAssert.IsInstanceOf<Exception>(exception);
}
}

0 comments on commit e0ad2ae

Please sign in to comment.