Skip to content

Commit 4dd1ba9

Browse files
(#123) Improve code coverage for new code
1 parent 81b8e82 commit 4dd1ba9

File tree

3 files changed

+75
-20
lines changed

3 files changed

+75
-20
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
using NUnit.Framework;
4+
using Shouldly;
5+
6+
namespace Stravaig.Extensions.Logging.Diagnostics.Tests;
7+
8+
[TestFixture]
9+
public class TestCaptureLoggerOfTTests
10+
{
11+
[Test]
12+
public void CategoryNameIsBasedOnType()
13+
{
14+
var logger = new TestCaptureLogger<TestCaptureLoggerOfTTests>();
15+
logger.CategoryName.ShouldBe(GetType().FullName);
16+
}
17+
18+
[Test]
19+
[TestCase(LogLevel.Trace)]
20+
[TestCase(LogLevel.Debug)]
21+
[TestCase(LogLevel.Information)]
22+
[TestCase(LogLevel.Warning)]
23+
[TestCase(LogLevel.Error)]
24+
[TestCase(LogLevel.Critical)]
25+
public void IsEnabledTests(LogLevel level)
26+
{
27+
var logger = new TestCaptureLogger<TestCaptureLoggerOfTTests>();
28+
logger.IsEnabled(level).ShouldBeTrue();
29+
}
30+
31+
[Test]
32+
public void ResetRemovesCapturedLogs()
33+
{
34+
var logger = new TestCaptureLogger<TestCaptureLoggerOfTTests>();
35+
logger.LogInformation("Hello");
36+
logger.LogWarning("World");
37+
logger.GetLogs().Count.ShouldBe(2);
38+
39+
logger.Reset();
40+
logger.GetLogs().Count.ShouldBe(0);
41+
}
42+
43+
[Test]
44+
public void GetLogsWithExceptionsWillFilterOutNonExceptionLogs()
45+
{
46+
var logger = new TestCaptureLogger<TestCaptureLoggerOfTTests>();
47+
logger.LogInformation("Hello");
48+
logger.LogWarning(new Exception(), "World");
49+
logger.LogInformation("This is a log.");
50+
logger.LogError(new Exception(), "This has an exception.");
51+
52+
logger.GetLogEntriesWithExceptions().Count.ShouldBe(2);
53+
logger.GetLogs().Count.ShouldBe(4);
54+
}
55+
}

src/Stravaig.Extensions.Logging.Diagnostics.Tests/TestCaptureLoggerTests.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
using NUnit.Framework;
44
using Shouldly;
55

6-
namespace Stravaig.Extensions.Logging.Diagnostics.Tests
6+
namespace Stravaig.Extensions.Logging.Diagnostics.Tests;
7+
8+
[TestFixture]
9+
public class TestCaptureLoggerTests
710
{
8-
[TestFixture]
9-
public class TestCaptureLoggerTests
11+
[Test]
12+
public void LogsWithExceptionOnlyReturnsLogsWithAnExceptionObjectAttached()
1013
{
11-
[Test]
12-
public void LogsWithExceptionOnlyReturnsLogsWithAnExceptionObjectAttached()
13-
{
14-
var logger = new TestCaptureLogger();
15-
16-
logger.LogInformation("One");
17-
logger.LogWarning(new Exception("I'm an exception"), "Two");
18-
logger.LogError(new ApplicationException("I'm an application exception"), "Three");
19-
logger.LogError("Four");
20-
logger.LogCritical(new InvalidOperationException("I'm an invalid operation exception"), "Five");
14+
var logger = new TestCaptureLogger();
15+
16+
logger.LogInformation("One");
17+
logger.LogWarning(new Exception("I'm an exception"), "Two");
18+
logger.LogError(new ApplicationException("I'm an application exception"), "Three");
19+
logger.LogError("Four");
20+
logger.LogCritical(new InvalidOperationException("I'm an invalid operation exception"), "Five");
2121

22-
var logsWithExceptions = logger.GetLogEntriesWithExceptions();
23-
logsWithExceptions.Count.ShouldBe(3);
24-
logsWithExceptions[0].OriginalMessage.ShouldBe("Two");
25-
logsWithExceptions[1].OriginalMessage.ShouldBe("Three");
26-
logsWithExceptions[2].OriginalMessage.ShouldBe("Five");
27-
}
22+
var logsWithExceptions = logger.GetLogEntriesWithExceptions();
23+
logsWithExceptions.Count.ShouldBe(3);
24+
logsWithExceptions[0].OriginalMessage.ShouldBe("Two");
25+
logsWithExceptions[1].OriginalMessage.ShouldBe("Three");
26+
logsWithExceptions[2].OriginalMessage.ShouldBe("Five");
2827
}
29-
}
28+
}

src/Stravaig.Extensions.Logging.Diagnostics.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=LogMessageIsSentenceProblem/@EntryIndexedValue">DO_NOT_SHOW</s:String>
23
<s:Boolean x:Key="/Default/CodeInspection/Highlighting/RunLongAnalysisInSwa/@EntryValue">True</s:Boolean>
34
<s:String x:Key="/Default/CodeInspection/Highlighting/SweaWarningsMode/@EntryValue">ShowAndRun</s:String>
45
<s:Boolean x:Key="/Default/UserDictionary/Words/=Shouldly/@EntryIndexedValue">True</s:Boolean>

0 commit comments

Comments
 (0)