From 033c879355f24ff247acb0a10273ceaf842a5200 Mon Sep 17 00:00:00 2001 From: Mark Wilson <23439518+wlsnmrk@users.noreply.github.com> Date: Tue, 21 Jan 2025 10:55:09 -0500 Subject: [PATCH] test: test exception printing for all log types --- Chickensoft.Log.Tests/test/src/FileLogTest.cs | 25 +++++++++++++++---- .../test/src/MultiLogTest.cs | 18 ++++++++++--- .../test/src/TestException.cs | 7 ++++++ .../test/src/TraceLogTest.cs | 25 +++++++++++++++---- 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 Chickensoft.Log.Tests/test/src/TestException.cs diff --git a/Chickensoft.Log.Tests/test/src/FileLogTest.cs b/Chickensoft.Log.Tests/test/src/FileLogTest.cs index 34cd721..4069e78 100644 --- a/Chickensoft.Log.Tests/test/src/FileLogTest.cs +++ b/Chickensoft.Log.Tests/test/src/FileLogTest.cs @@ -20,7 +20,8 @@ public void PrintsMessage() { var log = new FileLog(nameof(FileLogTest), mockWriter.Object); log.Print(_testMsg); mockWriter.Verify(writer => - writer.WriteMessage($"{nameof(FileLogTest)}: {_testMsg}"), Times.Once()); + writer.WriteMessage($"{nameof(FileLogTest)}: {_testMsg}"), + Times.Once()); } [Fact] @@ -29,8 +30,8 @@ public void PrintsError() { var log = new FileLog(nameof(FileLogTest), mockWriter.Object); log.Err(_testMsg); mockWriter.Verify(writer => - writer.WriteError($"ERROR in {nameof(FileLogTest)}: {_testMsg}"), - Times.Once()); + writer.WriteError($"ERROR in {nameof(FileLogTest)}: {_testMsg}"), + Times.Once()); } [Fact] @@ -39,7 +40,21 @@ public void PrintsWarning() { var log = new FileLog(nameof(FileLogTest), mockWriter.Object); log.Warn(_testMsg); mockWriter.Verify(writer => - writer.WriteWarning($"WARNING in {nameof(FileLogTest)}: {_testMsg}"), - Times.Once()); + writer.WriteWarning($"WARNING in {nameof(FileLogTest)}: {_testMsg}"), + Times.Once()); + } + + [Fact] + public void PrintsException() { + var mockWriter = new Mock(); + var log = new FileLog(nameof(FileLogTest), mockWriter.Object); + var e = new TestException(_testMsg); + log.Print(e); + mockWriter.Verify(writer => + writer.WriteError($"ERROR in {nameof(FileLogTest)}: An error occurred."), + Times.Once()); + mockWriter.Verify(writer => + writer.WriteError($"ERROR in {nameof(FileLogTest)}: {e}"), + Times.Once()); } } diff --git a/Chickensoft.Log.Tests/test/src/MultiLogTest.cs b/Chickensoft.Log.Tests/test/src/MultiLogTest.cs index 8a73832..5804201 100644 --- a/Chickensoft.Log.Tests/test/src/MultiLogTest.cs +++ b/Chickensoft.Log.Tests/test/src/MultiLogTest.cs @@ -19,7 +19,7 @@ public void PrintsMessage() { var logs = new List { mockLog1.Object, mockLog2.Object }; var log = new MultiLog(logs); log.Print(_testMsg); - mockLog1.Verify(writer => writer.Print(_testMsg), Times.Once()); + mockLog1.Verify(log => log.Print(_testMsg), Times.Once()); } [Fact] @@ -29,7 +29,7 @@ public void PrintsError() { var logs = new List { mockLog1.Object, mockLog2.Object }; var log = new MultiLog(logs); log.Err(_testMsg); - mockLog1.Verify(writer => writer.Err(_testMsg), Times.Once()); + mockLog1.Verify(log => log.Err(_testMsg), Times.Once()); } [Fact] @@ -39,6 +39,18 @@ public void PrintsWarning() { var logs = new List { mockLog1.Object, mockLog2.Object }; var log = new MultiLog(logs); log.Warn(_testMsg); - mockLog1.Verify(writer => writer.Warn(_testMsg), Times.Once()); + mockLog1.Verify(log => log.Warn(_testMsg), Times.Once()); + } + + [Fact] + public void PrintsException() { + var mockLog1 = new Mock(); + var mockLog2 = new Mock(); + var logs = new List { mockLog1.Object, mockLog2.Object }; + var log = new MultiLog(logs); + var e = new TestException(_testMsg); + log.Print(e); + mockLog1.Verify(log => log.Print(e), Times.Once()); + mockLog2.Verify(log => log.Print(e), Times.Once()); } } diff --git a/Chickensoft.Log.Tests/test/src/TestException.cs b/Chickensoft.Log.Tests/test/src/TestException.cs new file mode 100644 index 0000000..f34bb44 --- /dev/null +++ b/Chickensoft.Log.Tests/test/src/TestException.cs @@ -0,0 +1,7 @@ +namespace Chickensoft.Log.Tests; + +internal sealed class TestException : Exception { + public TestException() { } + public TestException(string msg) : base(msg) { } + public TestException(string msg, Exception inner) : base(msg, inner) { } +} diff --git a/Chickensoft.Log.Tests/test/src/TraceLogTest.cs b/Chickensoft.Log.Tests/test/src/TraceLogTest.cs index 50888fd..296233c 100644 --- a/Chickensoft.Log.Tests/test/src/TraceLogTest.cs +++ b/Chickensoft.Log.Tests/test/src/TraceLogTest.cs @@ -19,7 +19,8 @@ public void PrintsMessage() { var log = new TraceLog(nameof(TraceLogTest), mockWriter.Object); log.Print(_testMsg); mockWriter.Verify(writer => - writer.WriteMessage($"{nameof(TraceLogTest)}: {_testMsg}"), Times.Once()); + writer.WriteMessage($"{nameof(TraceLogTest)}: {_testMsg}"), + Times.Once()); } [Fact] @@ -28,8 +29,8 @@ public void PrintsError() { var log = new TraceLog(nameof(TraceLogTest), mockWriter.Object); log.Err(_testMsg); mockWriter.Verify(writer => - writer.WriteError($"{nameof(TraceLogTest)}: {_testMsg}"), - Times.Once()); + writer.WriteError($"{nameof(TraceLogTest)}: {_testMsg}"), + Times.Once()); } [Fact] @@ -38,7 +39,21 @@ public void PrintsWarning() { var log = new TraceLog(nameof(TraceLogTest), mockWriter.Object); log.Warn(_testMsg); mockWriter.Verify(writer => - writer.WriteWarning($"WARNING in {nameof(TraceLogTest)}: {_testMsg}"), - Times.Once()); + writer.WriteWarning($"WARNING in {nameof(TraceLogTest)}: {_testMsg}"), + Times.Once()); + } + + [Fact] + public void PrintsException() { + var mockWriter = new Mock(); + var log = new TraceLog(nameof(TraceLogTest), mockWriter.Object); + var e = new TestException(_testMsg); + log.Print(e); + mockWriter.Verify(writer => + writer.WriteError($"{nameof(TraceLogTest)}: An error occurred."), + Times.Once()); + mockWriter.Verify(writer => + writer.WriteError($"{nameof(TraceLogTest)}: {e}"), + Times.Once()); } }