Skip to content

Commit

Permalink
#168 added tests for LevelMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Aug 16, 2024
1 parent 5b0116b commit 2a7683c
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 222 deletions.
69 changes: 34 additions & 35 deletions src/log4net.Tests/Core/LevelMapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,47 @@

using NUnit.Framework;

namespace log4net.Tests.Core
namespace log4net.Tests.Core;

/// <summary>
/// Used for internal unit testing the <see cref="LevelMap"/> class.
/// </summary>
[TestFixture]
public sealed class LevelMapTest
{
/// <summary>
/// Used for internal unit testing the <see cref="LevelMap"/> class.
/// Tests the creation of a <see cref="LevelMap"/> and calling its <see cref="LevelMap.Clear"/> method
/// </summary>
[TestFixture]
public sealed class LevelMapTest
[Test]
public void LevelMapCreateClear()
{
/// <summary>
/// Tests the creation of a <see cref="LevelMap"/> and calling its <see cref="LevelMap.Clear"/> method
/// </summary>
[Test]
public void LevelMapCreateClear()
{
var map = new LevelMap();
LevelCollection allLevels = map.AllLevels;
Assert.AreEqual(0, allLevels.Count);
Assert.IsNull(map["nonexistent"]);
var map = new LevelMap();
LevelCollection allLevels = map.AllLevels;
Assert.AreEqual(0, allLevels.Count);
Assert.IsNull(map["nonexistent"]);

map.Add("level1234", 1234, "displayName");
allLevels = map.AllLevels;
Assert.AreEqual(1, allLevels.Count);
Assert.AreEqual("level1234", allLevels[0].Name);
Assert.AreEqual("displayName", allLevels[0].DisplayName);
Assert.AreEqual(1234, allLevels[0].Value);
Level? level1234 = map["level1234"];
Assert.IsNotNull(level1234);
Assert.AreSame(level1234, allLevels[0]);
map.Add("level1234", 1234, "displayName");
allLevels = map.AllLevels;
Assert.AreEqual(1, allLevels.Count);
Assert.AreEqual("level1234", allLevels[0].Name);
Assert.AreEqual("displayName", allLevels[0].DisplayName);
Assert.AreEqual(1234, allLevels[0].Value);
Level? level1234 = map["level1234"];
Assert.IsNotNull(level1234);
Assert.AreSame(level1234, allLevels[0]);

Level lookupLevel = map.LookupWithDefault(level1234!);
Assert.AreSame(level1234, lookupLevel);
Level lookupLevel = map.LookupWithDefault(level1234!);
Assert.AreSame(level1234, lookupLevel);

var otherLevel = new Level(5678, "level5678", "display");
lookupLevel = map.LookupWithDefault(otherLevel);
Assert.AreSame(otherLevel, lookupLevel);
Assert.AreSame(otherLevel, map["LEVEL5678"]);
var otherLevel = new Level(5678, "level5678", "display");
lookupLevel = map.LookupWithDefault(otherLevel);
Assert.AreSame(otherLevel, lookupLevel);
Assert.AreSame(otherLevel, map["LEVEL5678"]);

map.Clear();
allLevels = map.AllLevels;
Assert.AreEqual(0, allLevels.Count);
Assert.IsNull(map["level1234"]);
Assert.IsNull(map["LEVEL5678"]);
}
map.Clear();
allLevels = map.AllLevels;
Assert.AreEqual(0, allLevels.Count);
Assert.IsNull(map["level1234"]);
Assert.IsNull(map["LEVEL5678"]);
}
}
124 changes: 124 additions & 0 deletions src/log4net.Tests/Core/LevelMappingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#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.Collections.Generic;
using System.Linq;
using System.Reflection;
using log4net.Core;
using log4net.Util;
using NUnit.Framework;

namespace log4net.Tests.Core;

/// <summary>
/// Used for internal unit testing the <see cref="LevelMapping"/> class.
/// </summary>
[TestFixture]
public sealed class LevelMappingTest
{
/// <inheritdoc/>
private sealed class MappingEntry : LevelMappingEntry
{
/// <inheritdoc/>
internal MappingEntry(Level level) => Level = level;

/// <inheritdoc/>
public override string ToString() => $"{Level?.Value} - {Level?.Name}";
}

/// <summary>
/// Tests the sorting of the entries
/// </summary>
[Test]
public void SortEntriesTest()
{
MappingEntry[] unsorted = [
new(Level.Info),
new(Level.Off),
new(Level.Emergency),
new(Level.Error),
new(Level.Alert),
new(Level.All),
new(Level.Critical),
new(Level.Debug),
new(Level.Fatal),
new(Level.Fine),
new(Level.Finer),
new(Level.Finest),
new(Level.Log4Net_Debug),
new(Level.Notice),
new(Level.Severe),
new(Level.Trace),
new(Level.Verbose),
new(Level.Warn)
];
LevelMapping mapping = new();
foreach (MappingEntry entry in unsorted)
{
mapping.Add(entry);
}

List<MappingEntry> withoutDuplicates = unsorted.GroupBy(entry => entry.Level!.Value)
.Select(group => group.Last()).ToList();

List<LevelMappingEntry> sorted = (List<LevelMappingEntry>)typeof(LevelMapping)
.GetMethod("SortEntries", BindingFlags.NonPublic | BindingFlags.Instance)!
.Invoke(mapping, Array.Empty<object>())!;

CollectionAssert.AreEquivalent(withoutDuplicates, sorted);
CollectionAssert.AreNotEqual(withoutDuplicates, sorted);

int lowestLevelSeen = int.MaxValue;
foreach (LevelMappingEntry entry in sorted)
{
Assert.IsTrue(lowestLevelSeen >= entry.Level!.Value, entry.Level.Name);
lowestLevelSeen = entry.Level!.Value;
}
Assert.AreEqual(Level.All.Value, lowestLevelSeen);
}

/// <summary>
/// Tests the <see cref="LevelMapping.Lookup(Level?)"/> method
/// </summary>
[Test]
public void LookupTest()
{
LevelMapping mapping = new();
mapping.Add(new MappingEntry(Level.Info));
mapping.Add(new MappingEntry(Level.Off));
mapping.Add(new MappingEntry(Level.Emergency));
mapping.Add(new MappingEntry(Level.Warn));

Assert.IsNull(mapping.Lookup(Level.Info)?.Level);

mapping.ActivateOptions();

Assert.AreEqual(Level.Info, mapping.Lookup(Level.Info)?.Level);
Assert.AreEqual(Level.Off, mapping.Lookup(Level.Off)?.Level);
Assert.AreEqual(Level.Emergency, mapping.Lookup(Level.Emergency)?.Level);
Assert.AreEqual(Level.Warn, mapping.Lookup(Level.Warn)?.Level);
Assert.AreEqual(Level.Warn, mapping.Lookup(Level.Error)?.Level);
Assert.IsNull(mapping.Lookup(Level.Fine)?.Level);
Assert.AreEqual(Level.Emergency, mapping.Lookup(Level.Log4Net_Debug)?.Level);
Assert.IsNull(mapping.Lookup(Level.Trace)?.Level);
Assert.AreEqual(Level.Warn, mapping.Lookup(Level.Alert)?.Level);
Assert.IsNull(mapping.Lookup(Level.All)?.Level);
}
}
73 changes: 36 additions & 37 deletions src/log4net.Tests/Core/LevelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,49 @@

using NUnit.Framework;

namespace log4net.Tests.Core
namespace log4net.Tests.Core;

/// <summary>
/// Used for internal unit testing the <see cref="Level"/> class.
/// </summary>
[TestFixture]
public sealed class LevelTest
{
/// <summary>
/// Used for internal unit testing the <see cref="Level"/> class.
/// Tests the comparison between two <see cref="Level"/>s
/// </summary>
[TestFixture]
public sealed class LevelTest
[Test]
public void LevelCompare()
{
/// <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);
}
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 = "")
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)
{
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
{
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!));
}
Assert.Throws<ArgumentNullException>(() => left!.CompareTo(right!));
}
}
}
Expand Down
Loading

0 comments on commit 2a7683c

Please sign in to comment.