Skip to content

Commit

Permalink
#169 fixed NullReferenceException when comparing with a null Level
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Aug 14, 2024
1 parent 1c3c18f commit 863ac3d
Show file tree
Hide file tree
Showing 4 changed files with 496 additions and 475 deletions.
11 changes: 5 additions & 6 deletions src/log4net.Tests/Core/LevelMapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@
#endregion

using log4net.Core;
using log4net.Tests.Layout;

using NUnit.Framework;

namespace log4net.Tests.Core
{
/// <summary>
/// Used for internal unit testing the <see cref="PatternLayoutTest"/> class.
/// Used for internal unit testing the <see cref="LevelMapTest"/> class.
/// </summary>
/// <remarks>
/// Used for internal unit testing the <see cref="PatternLayoutTest"/> class.
/// </remarks>
[TestFixture]
public class LevelMapTest
public sealed class LevelMapTest
{
/// <summary>
/// Tests the creation of a <see cref="LevelMap"/> and calling its <see cref="LevelMap.Clear"/> method
/// </summary>
[Test]
public void LevelMapCreateClear()
{
Expand Down
74 changes: 74 additions & 0 deletions src/log4net.Tests/Core/LevelTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Runtime.CompilerServices;
using log4net.Core;

using NUnit.Framework;

namespace log4net.Tests.Core
{
/// <summary>
/// Used for internal unit testing the <see cref="Level"/> class.
/// </summary>
[TestFixture]
public sealed class LevelTest
{

/// <summary>
/// Tests the comparison between two <see cref="Level"/>s
/// </summary>
[Test]
public void LevelCompare()
{
Level? nullLevel = null;
int? nullInt = null;
Compare(nullInt, nullInt, nullLevel, nullLevel);
Compare(nullInt, Level.Verbose.Value, nullLevel, Level.Verbose);
Compare(Level.Verbose.Value, nullInt, Level.Verbose, nullLevel);
Compare(Level.Verbose.Value, Level.Verbose.Value, Level.Verbose, Level.Verbose);
Compare(Level.Debug.Value, Level.Verbose.Value, Level.Debug, Level.Verbose);
}

private static void Compare(int? leftInt, int? rightInt, Level? left, Level? right,
[CallerArgumentExpression(nameof(left))] string leftName = "",
[CallerArgumentExpression(nameof(right))] string rightName = "")
{
Assert.AreEqual(leftInt < rightInt, left < right, "{0} < {1}", leftName, rightName);
Assert.AreEqual(leftInt > rightInt, left > right, "{0} > {1}", leftName, rightName);
Assert.AreEqual(leftInt <= rightInt, left <= right, "{0} <= {1}", leftName, rightName);
Assert.AreEqual(leftInt >= rightInt, left >= right, "{0} >= {1}", leftName, rightName);
Assert.AreEqual(leftInt == rightInt, left == right, "{0} == {1}", leftName, rightName);
Assert.AreEqual(leftInt != rightInt, left != right, "{0} != {1}", leftName, rightName);
Assert.AreEqual(leftInt?.Equals(rightInt), left?.Equals(right), "{0}?.Equals({1})", leftName, rightName);
if (leftInt is not null)
{
if (rightInt is not null)
{
Assert.AreEqual(leftInt?.CompareTo(rightInt), left?.CompareTo(right!), "{0}?.CompareTo({1})", leftName, rightName);
}
else
{
Assert.Throws<ArgumentNullException>(() => left!.CompareTo(right!));
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/log4net.Tests/log4net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<!-- suppress analyzer mismatch warning -->
<NoWarn>CS8032</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\log4net\Diagnostics\CodeAnalysis\CallerArgumentExpressionAttribute.cs" Link="Diagnostics\CodeAnalysis\CallerArgumentExpressionAttribute.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\log4net\log4net.csproj" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 863ac3d

Please sign in to comment.