Skip to content

Commit 102fcde

Browse files
committed
#169 fixed NullReferenceException when comparing with a null Level
1 parent 1c3c18f commit 102fcde

File tree

5 files changed

+494
-477
lines changed

5 files changed

+494
-477
lines changed

src/log4net.Tests/Core/LevelMapTest.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,20 @@
1818
#endregion
1919

2020
using log4net.Core;
21-
using log4net.Tests.Layout;
2221

2322
using NUnit.Framework;
2423

2524
namespace log4net.Tests.Core
2625
{
2726
/// <summary>
28-
/// Used for internal unit testing the <see cref="PatternLayoutTest"/> class.
27+
/// Used for internal unit testing the <see cref="LevelMap"/> class.
2928
/// </summary>
30-
/// <remarks>
31-
/// Used for internal unit testing the <see cref="PatternLayoutTest"/> class.
32-
/// </remarks>
3329
[TestFixture]
34-
public class LevelMapTest
30+
public sealed class LevelMapTest
3531
{
32+
/// <summary>
33+
/// Tests the creation of a <see cref="LevelMap"/> and calling its <see cref="LevelMap.Clear"/> method
34+
/// </summary>
3635
[Test]
3736
public void LevelMapCreateClear()
3837
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#region Apache License
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one or more
4+
// contributor license agreements. See the NOTICE file distributed with
5+
// this work for additional information regarding copyright ownership.
6+
// The ASF licenses this file to you under the Apache License, Version 2.0
7+
// (the "License"); you may not use this file except in compliance with
8+
// the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#endregion
19+
20+
using System;
21+
using System.Runtime.CompilerServices;
22+
using log4net.Core;
23+
24+
using NUnit.Framework;
25+
26+
namespace log4net.Tests.Core
27+
{
28+
/// <summary>
29+
/// Used for internal unit testing the <see cref="Level"/> class.
30+
/// </summary>
31+
[TestFixture]
32+
public sealed class LevelTest
33+
{
34+
/// <summary>
35+
/// Tests the comparison between two <see cref="Level"/>s
36+
/// </summary>
37+
[Test]
38+
public void LevelCompare()
39+
{
40+
Level? nullLevel = null;
41+
int? nullInt = null;
42+
Compare(nullInt, nullInt, nullLevel, nullLevel);
43+
Compare(nullInt, Level.Verbose.Value, nullLevel, Level.Verbose);
44+
Compare(Level.Verbose.Value, nullInt, Level.Verbose, nullLevel);
45+
Compare(Level.Verbose.Value, Level.Verbose.Value, Level.Verbose, Level.Verbose);
46+
Compare(Level.Debug.Value, Level.Verbose.Value, Level.Debug, Level.Verbose);
47+
}
48+
49+
private static void Compare(int? leftInt, int? rightInt, Level? left, Level? right,
50+
[CallerArgumentExpression(nameof(left))] string leftName = "",
51+
[CallerArgumentExpression(nameof(right))] string rightName = "")
52+
{
53+
Assert.AreEqual(leftInt < rightInt, left < right, "{0} < {1}", leftName, rightName);
54+
Assert.AreEqual(leftInt > rightInt, left > right, "{0} > {1}", leftName, rightName);
55+
Assert.AreEqual(leftInt <= rightInt, left <= right, "{0} <= {1}", leftName, rightName);
56+
Assert.AreEqual(leftInt >= rightInt, left >= right, "{0} >= {1}", leftName, rightName);
57+
Assert.AreEqual(leftInt == rightInt, left == right, "{0} == {1}", leftName, rightName);
58+
Assert.AreEqual(leftInt != rightInt, left != right, "{0} != {1}", leftName, rightName);
59+
Assert.AreEqual(leftInt?.Equals(rightInt), left?.Equals(right), "{0}?.Equals({1})", leftName, rightName);
60+
if (leftInt is not null)
61+
{
62+
if (rightInt is not null)
63+
{
64+
Assert.AreEqual(leftInt?.CompareTo(rightInt), left?.CompareTo(right!), "{0}?.CompareTo({1})", leftName, rightName);
65+
}
66+
else
67+
{
68+
Assert.Throws<ArgumentNullException>(() => left!.CompareTo(right!));
69+
}
70+
}
71+
}
72+
}
73+
}

src/log4net.Tests/log4net.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<!-- suppress analyzer mismatch warning -->
1414
<NoWarn>CS8032</NoWarn>
1515
</PropertyGroup>
16+
<ItemGroup>
17+
<Compile Include="..\log4net\Diagnostics\CodeAnalysis\CallerArgumentExpressionAttribute.cs" Link="Diagnostics\CodeAnalysis\CallerArgumentExpressionAttribute.cs" />
18+
</ItemGroup>
1619
<ItemGroup>
1720
<ProjectReference Include="..\log4net\log4net.csproj" />
1821
</ItemGroup>

0 commit comments

Comments
 (0)