Skip to content

Commit

Permalink
test: test exception printing for all log types
Browse files Browse the repository at this point in the history
  • Loading branch information
wlsnmrk committed Jan 21, 2025
1 parent ad30366 commit 033c879
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
25 changes: 20 additions & 5 deletions Chickensoft.Log.Tests/test/src/FileLogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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<FileLog.IWriter>();
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());
}
}
18 changes: 15 additions & 3 deletions Chickensoft.Log.Tests/test/src/MultiLogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void PrintsMessage() {
var logs = new List<ILog> { 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]
Expand All @@ -29,7 +29,7 @@ public void PrintsError() {
var logs = new List<ILog> { 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]
Expand All @@ -39,6 +39,18 @@ public void PrintsWarning() {
var logs = new List<ILog> { 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<ILog>();
var mockLog2 = new Mock<ILog>();
var logs = new List<ILog> { 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());
}
}
7 changes: 7 additions & 0 deletions Chickensoft.Log.Tests/test/src/TestException.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
25 changes: 20 additions & 5 deletions Chickensoft.Log.Tests/test/src/TraceLogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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<TraceLog.IWriter>();
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());
}
}

0 comments on commit 033c879

Please sign in to comment.