diff --git a/src/ApprovalTests.NUnit3/ApprovalTests.NUnit3.csproj b/src/ApprovalTests.NUnit3/ApprovalTests.NUnit3.csproj new file mode 100644 index 00000000..e44696ee --- /dev/null +++ b/src/ApprovalTests.NUnit3/ApprovalTests.NUnit3.csproj @@ -0,0 +1,17 @@ + + + + net48;net8.0 + full + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ApprovalTests.NUnit3/ApprovalTestsConfig.cs b/src/ApprovalTests.NUnit3/ApprovalTestsConfig.cs new file mode 100644 index 00000000..2d2846ae --- /dev/null +++ b/src/ApprovalTests.NUnit3/ApprovalTestsConfig.cs @@ -0,0 +1 @@ +[assembly: UseReporter(typeof(DiffReporter))] \ No newline at end of file diff --git a/src/ApprovalTests.NUnit3/GlobalUsings.cs b/src/ApprovalTests.NUnit3/GlobalUsings.cs new file mode 100644 index 00000000..7704c5d8 --- /dev/null +++ b/src/ApprovalTests.NUnit3/GlobalUsings.cs @@ -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; \ No newline at end of file diff --git a/src/ApprovalTests.NUnit3/NUnitReporterTest.TestReporter.approved.txt b/src/ApprovalTests.NUnit3/NUnitReporterTest.TestReporter.approved.txt new file mode 100644 index 00000000..782edd32 --- /dev/null +++ b/src/ApprovalTests.NUnit3/NUnitReporterTest.TestReporter.approved.txt @@ -0,0 +1 @@ +World \ No newline at end of file diff --git a/src/ApprovalTests.NUnit3/NUnitReporterTest.cs b/src/ApprovalTests.NUnit3/NUnitReporterTest.cs new file mode 100644 index 00000000..81617761 --- /dev/null +++ b/src/ApprovalTests.NUnit3/NUnitReporterTest.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/ApprovalTests.NUnit3/NUnitReporterWithCleanup.cs b/src/ApprovalTests.NUnit3/NUnitReporterWithCleanup.cs new file mode 100644 index 00000000..5be9f64a --- /dev/null +++ b/src/ApprovalTests.NUnit3/NUnitReporterWithCleanup.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/ApprovalTests.Tests/ApprovalTests.Tests.csproj b/src/ApprovalTests.Tests/ApprovalTests.Tests.csproj index aa292a46..1fbed8a9 100644 --- a/src/ApprovalTests.Tests/ApprovalTests.Tests.csproj +++ b/src/ApprovalTests.Tests/ApprovalTests.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/ApprovalTests.Tests/FileApproverTests.cs b/src/ApprovalTests.Tests/FileApproverTests.cs index 097ce01a..3ac276af 100644 --- a/src/ApprovalTests.Tests/FileApproverTests.cs +++ b/src/ApprovalTests.Tests/FileApproverTests.cs @@ -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] @@ -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(fileApprover); + ClassicAssert.IsInstanceOf(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); } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/GlobalUsings.cs b/src/ApprovalTests.Tests/GlobalUsings.cs index 53518803..7cd0e6a6 100644 --- a/src/ApprovalTests.Tests/GlobalUsings.cs +++ b/src/ApprovalTests.Tests/GlobalUsings.cs @@ -24,4 +24,5 @@ global using iText.Kernel.Pdf; global using iText.Layout; global using iText.Layout.Element; -global using NUnit.Framework; \ No newline at end of file +global using NUnit.Framework; +global using NUnit.Framework.Legacy; \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Namer/AdditionalInformationTests.cs b/src/ApprovalTests.Tests/Namer/AdditionalInformationTests.cs index f4d65d82..5a4a24f8 100644 --- a/src/ApprovalTests.Tests/Namer/AdditionalInformationTests.cs +++ b/src/ApprovalTests.Tests/Namer/AdditionalInformationTests.cs @@ -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] @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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"); } } @@ -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); } } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Namer/ApprovalResultsTest.cs b/src/ApprovalTests.Tests/Namer/ApprovalResultsTest.cs index f6074ac1..7913ddb8 100644 --- a/src/ApprovalTests.Tests/Namer/ApprovalResultsTest.cs +++ b/src/ApprovalTests.Tests/Namer/ApprovalResultsTest.cs @@ -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() { diff --git a/src/ApprovalTests.Tests/Namer/ApprovalsFilenameTest.cs b/src/ApprovalTests.Tests/Namer/ApprovalsFilenameTest.cs index 3a4b6cdd..4cae9152 100644 --- a/src/ApprovalTests.Tests/Namer/ApprovalsFilenameTest.cs +++ b/src/ApprovalTests.Tests/Namer/ApprovalsFilenameTest.cs @@ -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] diff --git a/src/ApprovalTests.Tests/Namer/NunitStackTraceNamerTests.cs b/src/ApprovalTests.Tests/Namer/NunitStackTraceNamerTests.cs index a954ea17..f4e5096d 100644 --- a/src/ApprovalTests.Tests/Namer/NunitStackTraceNamerTests.cs +++ b/src/ApprovalTests.Tests/Namer/NunitStackTraceNamerTests.cs @@ -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] @@ -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); } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Namer/StackTraceParsers/StackTraceParserTests.cs b/src/ApprovalTests.Tests/Namer/StackTraceParsers/StackTraceParserTests.cs index b864d5c0..fd55fa32 100644 --- a/src/ApprovalTests.Tests/Namer/StackTraceParsers/StackTraceParserTests.cs +++ b/src/ApprovalTests.Tests/Namer/StackTraceParsers/StackTraceParserTests.cs @@ -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. diff --git a/src/ApprovalTests.Tests/Pdf/PdfTest.cs b/src/ApprovalTests.Tests/Pdf/PdfTest.cs index 655f5385..8bbd8c5a 100644 --- a/src/ApprovalTests.Tests/Pdf/PdfTest.cs +++ b/src/ApprovalTests.Tests/Pdf/PdfTest.cs @@ -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(""" @@ -46,7 +46,7 @@ NO IDS DECLARED public void TestPdf_ScrubberIdsNotMatch(string input) { var matchPositions = PdfScrubber.FindIds(input).ToList(); - Assert.AreEqual(0, matchPositions.Count); + ClassicAssert.AreEqual(0, matchPositions.Count); } [Test] @@ -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()); } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Persistence/AsyncSaverTest.cs b/src/ApprovalTests.Tests/Persistence/AsyncSaverTest.cs index 7e2fea3e..61fc81f2 100644 --- a/src/ApprovalTests.Tests/Persistence/AsyncSaverTest.cs +++ b/src/ApprovalTests.Tests/Persistence/AsyncSaverTest.cs @@ -6,7 +6,7 @@ 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] @@ -14,7 +14,7 @@ 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] @@ -22,6 +22,6 @@ 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")); } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Reporters/AssemblyLevelTest.cs b/src/ApprovalTests.Tests/Reporters/AssemblyLevelTest.cs index cedfd3cf..64b21ad1 100644 --- a/src/ApprovalTests.Tests/Reporters/AssemblyLevelTest.cs +++ b/src/ApprovalTests.Tests/Reporters/AssemblyLevelTest.cs @@ -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()); } } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Reporters/FirstWorkingReporterTest.cs b/src/ApprovalTests.Tests/Reporters/FirstWorkingReporterTest.cs index 231424f2..cff973d5 100644 --- a/src/ApprovalTests.Tests/Reporters/FirstWorkingReporterTest.cs +++ b/src/ApprovalTests.Tests/Reporters/FirstWorkingReporterTest.cs @@ -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] @@ -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 { @@ -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); } } diff --git a/src/ApprovalTests.Tests/Reporters/MultiReporterTest.cs b/src/ApprovalTests.Tests/Reporters/MultiReporterTest.cs index aea7f432..c373fec3 100644 --- a/src/ApprovalTests.Tests/Reporters/MultiReporterTest.cs +++ b/src/ApprovalTests.Tests/Reporters/MultiReporterTest.cs @@ -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); + ClassicAssert.AreEqual("a,r", b.CalledWith); + ClassicAssert.IsInstanceOf(exception); } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Reporters/NUnitReporterTest.cs b/src/ApprovalTests.Tests/Reporters/NUnitReporterTest.cs index a2006a55..bfafdb74 100644 --- a/src/ApprovalTests.Tests/Reporters/NUnitReporterTest.cs +++ b/src/ApprovalTests.Tests/Reporters/NUnitReporterTest.cs @@ -7,7 +7,7 @@ public class NUnitReporterTest public void TestNunitIsWorking() { Approvals.SetCaller(); - Assert.IsTrue(NUnitReporter.INSTANCE.IsWorkingInThisEnvironment("default.txt")); + ClassicAssert.IsTrue(NUnit4Reporter.INSTANCE.IsWorkingInThisEnvironment("default.txt")); } [Test] @@ -21,12 +21,10 @@ public void TestReporter() Approvals.Verify("Hello"); } } - catch (Exception e) + catch (AssertionException e) { - 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, - e.Message); + var expectedMessage = string.Format(" Assert.That(actual, Is.EqualTo(expected)){0} String lengths are both 5. Strings differ at index 0.{0} Expected: \"World\"{0} But was: \"Hello\"{0} -----------^{0}", Environment.NewLine); + ClassicAssert.AreEqual(expectedMessage, e.Message); } } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Reporters/NUnitReporterWithCleanup.cs b/src/ApprovalTests.Tests/Reporters/NUnitReporterWithCleanup.cs index 5f9ef4aa..437f9396 100644 --- a/src/ApprovalTests.Tests/Reporters/NUnitReporterWithCleanup.cs +++ b/src/ApprovalTests.Tests/Reporters/NUnitReporterWithCleanup.cs @@ -1,4 +1,4 @@ -public class NUnitReporterWithCleanup : NUnitReporter +public class NUnitReporterWithCleanup : NUnit4Reporter { public override void Report(string approved, string received) { diff --git a/src/ApprovalTests.Tests/Reporters/ReporterFactoryTest.cs b/src/ApprovalTests.Tests/Reporters/ReporterFactoryTest.cs index 292fc9ed..2128c51f 100644 --- a/src/ApprovalTests.Tests/Reporters/ReporterFactoryTest.cs +++ b/src/ApprovalTests.Tests/Reporters/ReporterFactoryTest.cs @@ -11,14 +11,14 @@ static IEnumerable GetSingletonReporterTypes() } static void SubMethod() => - Assert.AreEqual(typeof(MethodLevelReporter), Approvals.GetReporter().GetType()); + ClassicAssert.AreEqual(typeof(MethodLevelReporter), Approvals.GetReporter().GetType()); [Test] public void TestClassLevel() { using (Approvals.SetFrontLoadedReporter(ReportWithoutFrontLoading.INSTANCE)) { - Assert.AreEqual(typeof(ClassLevelReporter), Approvals.GetReporter().GetType()); + ClassicAssert.AreEqual(typeof(ClassLevelReporter), Approvals.GetReporter().GetType()); } } @@ -28,7 +28,7 @@ public void TestMethodOverride() { using (Approvals.SetFrontLoadedReporter(ReportWithoutFrontLoading.INSTANCE)) { - Assert.AreEqual(typeof(MethodLevelReporter), Approvals.GetReporter().GetType()); + ClassicAssert.AreEqual(typeof(MethodLevelReporter), Approvals.GetReporter().GetType()); } } @@ -39,7 +39,7 @@ public async Task TestAsyncMethodOverride() await Task.Delay(1); using (Approvals.SetFrontLoadedReporter(ReportWithoutFrontLoading.INSTANCE)) { - Assert.AreEqual(typeof(MethodLevelReporter), Approvals.GetReporter().GetType()); + ClassicAssert.AreEqual(typeof(MethodLevelReporter), Approvals.GetReporter().GetType()); } } @@ -59,7 +59,7 @@ public void TestMultipleReporters() { using (Approvals.SetFrontLoadedReporter(ReportWithoutFrontLoading.INSTANCE)) { - Assert.AreEqual(typeof(MultiReporter), Approvals.GetReporter().GetType()); + ClassicAssert.AreEqual(typeof(MultiReporter), Approvals.GetReporter().GetType()); } } @@ -69,7 +69,10 @@ public void TestSingletonOnAllReporters() using (Approvals.SetFrontLoadedReporter(ReportWithoutFrontLoading.INSTANCE)) { var reporters = GetSingletonReporterTypes(); - foreach (var r in reporters) Assert.IsInstanceOf(r, UseReporterAttribute.GetSingleton(r), $"Please add\npublic static readonly {r.FullName} INSTANCE = new {r.FullName}();"); + foreach (var r in reporters) + { + ClassicAssert.IsInstanceOf(r, UseReporterAttribute.GetSingleton(r), $"Please add\npublic static readonly {r.FullName} INSTANCE = new {r.FullName}();"); + } } } } diff --git a/src/ApprovalTests.Tests/Reporters/ReporterTest.cs b/src/ApprovalTests.Tests/Reporters/ReporterTest.cs index 8a904211..04ae4a5a 100644 --- a/src/ApprovalTests.Tests/Reporters/ReporterTest.cs +++ b/src/ApprovalTests.Tests/Reporters/ReporterTest.cs @@ -8,7 +8,7 @@ public void Testname() { var old = Environment.GetEnvironmentVariable(NCrunchReporter.EnvironmentVariable); Environment.SetEnvironmentVariable(NCrunchReporter.EnvironmentVariable, "1"); - Assert.IsTrue(NCrunchReporter.INSTANCE.IsWorkingInThisEnvironment("a.txt")); + ClassicAssert.IsTrue(NCrunchReporter.INSTANCE.IsWorkingInThisEnvironment("a.txt")); Environment.SetEnvironmentVariable(NCrunchReporter.EnvironmentVariable, old); } diff --git a/src/ApprovalTests.Tests/StackTraceScrubberTest.cs b/src/ApprovalTests.Tests/StackTraceScrubberTest.cs index 28f89707..43f6a5b9 100644 --- a/src/ApprovalTests.Tests/StackTraceScrubberTest.cs +++ b/src/ApprovalTests.Tests/StackTraceScrubberTest.cs @@ -5,20 +5,20 @@ public class StackTraceScrubberTest public void TestDashedPath() { const string Path = @"C:\code\ApprovalTests - Net\Persistence\Datasets\DatasetTest.cs"; - Assert.AreEqual("...\\DatasetTest.cs", StackTraceScrubber.ScrubPaths(Path)); + ClassicAssert.AreEqual("...\\DatasetTest.cs", StackTraceScrubber.ScrubPaths(Path)); } [Test] public void TestDashedPathOnMac() { const string Path = "/Users/approver/code/ApprovalTests - Net/Persistence/Datasets/DatasetTest.cs"; - Assert.AreEqual(".../DatasetTest.cs", StackTraceScrubber.ScrubPaths(Path)); + ClassicAssert.AreEqual(".../DatasetTest.cs", StackTraceScrubber.ScrubPaths(Path)); } [Test] public void TestKeyValuePair() { const string Value = "name: File.foo"; - Assert.AreEqual(Value, StackTraceScrubber.ScrubPaths(Value)); + ClassicAssert.AreEqual(Value, StackTraceScrubber.ScrubPaths(Value)); } } \ No newline at end of file diff --git a/src/ApprovalTests.Tests/Writers/WriterFactoryTests.cs b/src/ApprovalTests.Tests/Writers/WriterFactoryTests.cs index f5a399e5..8c08887e 100644 --- a/src/ApprovalTests.Tests/Writers/WriterFactoryTests.cs +++ b/src/ApprovalTests.Tests/Writers/WriterFactoryTests.cs @@ -6,17 +6,17 @@ public class WriterFactoryTests [Test] public void TestTextWriter() { - Assert.IsInstanceOf(WriterFactory.CreateTextWriter("foo")); + ClassicAssert.IsInstanceOf(WriterFactory.CreateTextWriter("foo")); WriterFactory.SetTextWriterCreator(t => new MyTextWriter(t)); - Assert.IsInstanceOf(WriterFactory.CreateTextWriter("foo")); + ClassicAssert.IsInstanceOf(WriterFactory.CreateTextWriter("foo")); } [Test] public void TestTextWriterWithExtension() { - Assert.IsInstanceOf(WriterFactory.CreateTextWriter("foo", ".txt")); + ClassicAssert.IsInstanceOf(WriterFactory.CreateTextWriter("foo", ".txt")); WriterFactory.SetTextWriterCreator((t, e) => new MyTextWriter(t, e)); - Assert.IsInstanceOf(WriterFactory.CreateTextWriter("foo", "txt")); + ClassicAssert.IsInstanceOf(WriterFactory.CreateTextWriter("foo", "txt")); } } diff --git a/src/ApprovalTests.Xunit2/NonNUnitReporterTest.cs b/src/ApprovalTests.Xunit2/NonNUnitReporterTest.cs index 33c72c78..61d95fb2 100644 --- a/src/ApprovalTests.Xunit2/NonNUnitReporterTest.cs +++ b/src/ApprovalTests.Xunit2/NonNUnitReporterTest.cs @@ -4,7 +4,7 @@ public class NonNUnitReporterTest public void TestNunitIsNotWorkingFromXUnit() { Approvals.SetCaller(); - Assert.False(NUnitReporter.INSTANCE.IsWorkingInThisEnvironment("default.txt")); + Assert.False(NUnit4Reporter.INSTANCE.IsWorkingInThisEnvironment("default.txt")); } [Fact] diff --git a/src/ApprovalTests.sln b/src/ApprovalTests.sln index e6f88391..b9a33f59 100644 --- a/src/ApprovalTests.sln +++ b/src/ApprovalTests.sln @@ -22,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApprovalUtilities", "Approv EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApprovalTests.Xunit2", "ApprovalTests.Xunit2\ApprovalTests.Xunit2.csproj", "{E9BD0B96-01C4-4B25-ADFD-3A630970D267}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApprovalTests.NUnit3", "ApprovalTests.NUnit3\ApprovalTests.NUnit3.csproj", "{33F31A8B-82DC-4450-9F67-8C707364E894}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -52,6 +54,10 @@ Global {E9BD0B96-01C4-4B25-ADFD-3A630970D267}.Debug|Any CPU.Build.0 = Debug|Any CPU {E9BD0B96-01C4-4B25-ADFD-3A630970D267}.Release|Any CPU.ActiveCfg = Release|Any CPU {E9BD0B96-01C4-4B25-ADFD-3A630970D267}.Release|Any CPU.Build.0 = Release|Any CPU + {33F31A8B-82DC-4450-9F67-8C707364E894}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {33F31A8B-82DC-4450-9F67-8C707364E894}.Debug|Any CPU.Build.0 = Debug|Any CPU + {33F31A8B-82DC-4450-9F67-8C707364E894}.Release|Any CPU.ActiveCfg = Release|Any CPU + {33F31A8B-82DC-4450-9F67-8C707364E894}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/ApprovalTests/Reporters/TestFrameworks/AssertReporter.cs b/src/ApprovalTests/Reporters/TestFrameworks/AssertReporter.cs index fe3a1f5b..5e957c1f 100644 --- a/src/ApprovalTests/Reporters/TestFrameworks/AssertReporter.cs +++ b/src/ApprovalTests/Reporters/TestFrameworks/AssertReporter.cs @@ -31,7 +31,7 @@ public void AssertEqual(string approvedContent, string receivedContent) { try { - var type = Type.GetType(assertClass); + var type = Type.GetType(assertClass, true); var parameters = new[] { approvedContent, receivedContent }; InvokeEqualsMethod(type, parameters); } diff --git a/src/ApprovalTests/Reporters/TestFrameworks/FrameworkAssertReporter.cs b/src/ApprovalTests/Reporters/TestFrameworks/FrameworkAssertReporter.cs index 68f5c389..c821a6d6 100644 --- a/src/ApprovalTests/Reporters/TestFrameworks/FrameworkAssertReporter.cs +++ b/src/ApprovalTests/Reporters/TestFrameworks/FrameworkAssertReporter.cs @@ -5,8 +5,10 @@ public class FrameworkAssertReporter : FirstWorkingReporter public static readonly FrameworkAssertReporter INSTANCE = new(); public FrameworkAssertReporter() - : base(MsTestReporter.INSTANCE, - NUnitReporter.INSTANCE, + : base( + MsTestReporter.INSTANCE, + NUnit4Reporter.INSTANCE, + NUnit3Reporter.INSTANCE, XUnit2Reporter.INSTANCE) { } diff --git a/src/ApprovalTests/Reporters/TestFrameworks/NUnitReporter.cs b/src/ApprovalTests/Reporters/TestFrameworks/NUnit3Reporter.cs similarity index 56% rename from src/ApprovalTests/Reporters/TestFrameworks/NUnitReporter.cs rename to src/ApprovalTests/Reporters/TestFrameworks/NUnit3Reporter.cs index 5196e656..ea31a9c9 100644 --- a/src/ApprovalTests/Reporters/TestFrameworks/NUnitReporter.cs +++ b/src/ApprovalTests/Reporters/TestFrameworks/NUnit3Reporter.cs @@ -1,10 +1,10 @@ namespace ApprovalTests.Reporters.TestFrameworks; -public class NUnitReporter : AssertReporter +public class NUnit3Reporter : AssertReporter { - public readonly static NUnitReporter INSTANCE = new(); + public readonly static NUnit3Reporter INSTANCE = new(); - public NUnitReporter() + public NUnit3Reporter() : base("NUnit.Framework.Assert, nunit.framework", "AreEqual", NUnitStackTraceParser.Attribute) { } diff --git a/src/ApprovalTests/Reporters/TestFrameworks/NUnit4Reporter.cs b/src/ApprovalTests/Reporters/TestFrameworks/NUnit4Reporter.cs new file mode 100644 index 00000000..f4d8cf48 --- /dev/null +++ b/src/ApprovalTests/Reporters/TestFrameworks/NUnit4Reporter.cs @@ -0,0 +1,27 @@ +namespace ApprovalTests.Reporters.TestFrameworks; + +public class NUnit4Reporter : AssertReporter +{ + public readonly static NUnit4Reporter INSTANCE = new(); + static readonly Lazy isNUnit4 = new(IsisNUnit4); + + static bool IsisNUnit4() + { + return AppDomain + .CurrentDomain + .GetAssemblies() + .Any(_ => + { + var name = _.FullName; + return name.Contains("nunit.framework.legacy"); + }); + } + + public override bool IsWorkingInThisEnvironment(string forFile) => + base.IsWorkingInThisEnvironment(forFile) && isNUnit4.Value; + + public NUnit4Reporter() + : base("NUnit.Framework.Legacy.ClassicAssert, nunit.framework.legacy", "AreEqual", NUnitStackTraceParser.Attribute) + { + } +} \ No newline at end of file