-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GivenALogger.cs
100 lines (79 loc) · 3.36 KB
/
GivenALogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using FluentAssertions;
using Microsoft.Build.Framework;
using Xunit;
namespace Microsoft.NET.Build.Tasks.UnitTests
{
public class GivenALogger
{
private sealed class TestLogger : Logger
{
public List<Message> Messages { get; } = new List<Message>();
protected override void LogCore(in Message message) => Messages.Add(message);
}
[Fact]
public void ItLogsWarnings()
{
var logger = new TestLogger();
logger.LogWarning("NETSDK1234: Hello, {0}!", "world");
logger.LogWarning("NETSDK4567: Goodbye, {0} {1}.", "cruel", "world");
logger.Messages.Should().Equal(
new Message(MessageLevel.Warning, "Hello, world!", code: "NETSDK1234"),
new Message(MessageLevel.Warning, "Goodbye, cruel world.", code: "NETSDK4567"));
}
[Fact]
public void ItLogsErrors()
{
var logger = new TestLogger();
logger.LogError("NETSDK9898: Uh oh! {0}", ":(");
logger.Messages.Should().Equal(
new Message(MessageLevel.Error, "Uh oh! :(", code: "NETSDK9898"));
}
[Fact]
public void ItLogsMessages()
{
var logger = new TestLogger();
logger.LogMessage("Normal importance by default {0} {1} {2}", "a", "b", "c");
logger.LogMessage(MessageImportance.Low, "Low importance {0} {1} {2}", "x", "y", "z");
logger.LogMessage(MessageImportance.Normal, "Explicit normal importance");
logger.LogMessage(MessageImportance.High, "High importance");
logger.Messages.Should().Equal(
new Message(MessageLevel.NormalImportance, "Normal importance by default a b c"),
new Message(MessageLevel.LowImportance, "Low importance x y z"),
new Message(MessageLevel.NormalImportance, "Explicit normal importance"),
new Message(MessageLevel.HighImportance, "High importance"));
}
[Fact]
public void ItIndicatesIfErrorsWereLogged()
{
var logger = new TestLogger();
logger.HasLoggedErrors.Should().BeFalse();
logger.LogWarning("NETSDK0000: _");
logger.HasLoggedErrors.Should().BeFalse();
logger.LogMessage("_");
logger.HasLoggedErrors.Should().BeFalse();
logger.LogError("NETSDK0000: _");
logger.HasLoggedErrors.Should().BeTrue();
logger.LogWarning("NETSDK0000: _");
logger.HasLoggedErrors.Should().BeTrue();
logger.LogMessage("_");
logger.HasLoggedErrors.Should().BeTrue();
logger.LogError("NETSDK0000: _");
logger.HasLoggedErrors.Should().BeTrue();
}
[Fact]
public void ItEnforcesErrorCodesInDebug()
{
var logger = new TestLogger();
Action logWithoutErrorCode = () => logger.LogError("No error code");
#if DEBUG
logWithoutErrorCode.Should().Throw<ArgumentException>();
#else
logWithoutErrorCode();
logger.Messages.Should().Equal(
new Message(MessageLevel.Error, "No error code", code: default));
#endif
}
}
}