Skip to content

Commit 2a7683c

Browse files
committed
#168 added tests for LevelMapping
1 parent 5b0116b commit 2a7683c

File tree

10 files changed

+321
-222
lines changed

10 files changed

+321
-222
lines changed

src/log4net.Tests/Core/LevelMapTest.cs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,47 @@
2121

2222
using NUnit.Framework;
2323

24-
namespace log4net.Tests.Core
24+
namespace log4net.Tests.Core;
25+
26+
/// <summary>
27+
/// Used for internal unit testing the <see cref="LevelMap"/> class.
28+
/// </summary>
29+
[TestFixture]
30+
public sealed class LevelMapTest
2531
{
2632
/// <summary>
27-
/// Used for internal unit testing the <see cref="LevelMap"/> class.
33+
/// Tests the creation of a <see cref="LevelMap"/> and calling its <see cref="LevelMap.Clear"/> method
2834
/// </summary>
29-
[TestFixture]
30-
public sealed class LevelMapTest
35+
[Test]
36+
public void LevelMapCreateClear()
3137
{
32-
/// <summary>
33-
/// Tests the creation of a <see cref="LevelMap"/> and calling its <see cref="LevelMap.Clear"/> method
34-
/// </summary>
35-
[Test]
36-
public void LevelMapCreateClear()
37-
{
38-
var map = new LevelMap();
39-
LevelCollection allLevels = map.AllLevels;
40-
Assert.AreEqual(0, allLevels.Count);
41-
Assert.IsNull(map["nonexistent"]);
38+
var map = new LevelMap();
39+
LevelCollection allLevels = map.AllLevels;
40+
Assert.AreEqual(0, allLevels.Count);
41+
Assert.IsNull(map["nonexistent"]);
4242

43-
map.Add("level1234", 1234, "displayName");
44-
allLevels = map.AllLevels;
45-
Assert.AreEqual(1, allLevels.Count);
46-
Assert.AreEqual("level1234", allLevels[0].Name);
47-
Assert.AreEqual("displayName", allLevels[0].DisplayName);
48-
Assert.AreEqual(1234, allLevels[0].Value);
49-
Level? level1234 = map["level1234"];
50-
Assert.IsNotNull(level1234);
51-
Assert.AreSame(level1234, allLevels[0]);
43+
map.Add("level1234", 1234, "displayName");
44+
allLevels = map.AllLevels;
45+
Assert.AreEqual(1, allLevels.Count);
46+
Assert.AreEqual("level1234", allLevels[0].Name);
47+
Assert.AreEqual("displayName", allLevels[0].DisplayName);
48+
Assert.AreEqual(1234, allLevels[0].Value);
49+
Level? level1234 = map["level1234"];
50+
Assert.IsNotNull(level1234);
51+
Assert.AreSame(level1234, allLevels[0]);
5252

53-
Level lookupLevel = map.LookupWithDefault(level1234!);
54-
Assert.AreSame(level1234, lookupLevel);
53+
Level lookupLevel = map.LookupWithDefault(level1234!);
54+
Assert.AreSame(level1234, lookupLevel);
5555

56-
var otherLevel = new Level(5678, "level5678", "display");
57-
lookupLevel = map.LookupWithDefault(otherLevel);
58-
Assert.AreSame(otherLevel, lookupLevel);
59-
Assert.AreSame(otherLevel, map["LEVEL5678"]);
56+
var otherLevel = new Level(5678, "level5678", "display");
57+
lookupLevel = map.LookupWithDefault(otherLevel);
58+
Assert.AreSame(otherLevel, lookupLevel);
59+
Assert.AreSame(otherLevel, map["LEVEL5678"]);
6060

61-
map.Clear();
62-
allLevels = map.AllLevels;
63-
Assert.AreEqual(0, allLevels.Count);
64-
Assert.IsNull(map["level1234"]);
65-
Assert.IsNull(map["LEVEL5678"]);
66-
}
61+
map.Clear();
62+
allLevels = map.AllLevels;
63+
Assert.AreEqual(0, allLevels.Count);
64+
Assert.IsNull(map["level1234"]);
65+
Assert.IsNull(map["LEVEL5678"]);
6766
}
6867
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.Collections.Generic;
22+
using System.Linq;
23+
using System.Reflection;
24+
using log4net.Core;
25+
using log4net.Util;
26+
using NUnit.Framework;
27+
28+
namespace log4net.Tests.Core;
29+
30+
/// <summary>
31+
/// Used for internal unit testing the <see cref="LevelMapping"/> class.
32+
/// </summary>
33+
[TestFixture]
34+
public sealed class LevelMappingTest
35+
{
36+
/// <inheritdoc/>
37+
private sealed class MappingEntry : LevelMappingEntry
38+
{
39+
/// <inheritdoc/>
40+
internal MappingEntry(Level level) => Level = level;
41+
42+
/// <inheritdoc/>
43+
public override string ToString() => $"{Level?.Value} - {Level?.Name}";
44+
}
45+
46+
/// <summary>
47+
/// Tests the sorting of the entries
48+
/// </summary>
49+
[Test]
50+
public void SortEntriesTest()
51+
{
52+
MappingEntry[] unsorted = [
53+
new(Level.Info),
54+
new(Level.Off),
55+
new(Level.Emergency),
56+
new(Level.Error),
57+
new(Level.Alert),
58+
new(Level.All),
59+
new(Level.Critical),
60+
new(Level.Debug),
61+
new(Level.Fatal),
62+
new(Level.Fine),
63+
new(Level.Finer),
64+
new(Level.Finest),
65+
new(Level.Log4Net_Debug),
66+
new(Level.Notice),
67+
new(Level.Severe),
68+
new(Level.Trace),
69+
new(Level.Verbose),
70+
new(Level.Warn)
71+
];
72+
LevelMapping mapping = new();
73+
foreach (MappingEntry entry in unsorted)
74+
{
75+
mapping.Add(entry);
76+
}
77+
78+
List<MappingEntry> withoutDuplicates = unsorted.GroupBy(entry => entry.Level!.Value)
79+
.Select(group => group.Last()).ToList();
80+
81+
List<LevelMappingEntry> sorted = (List<LevelMappingEntry>)typeof(LevelMapping)
82+
.GetMethod("SortEntries", BindingFlags.NonPublic | BindingFlags.Instance)!
83+
.Invoke(mapping, Array.Empty<object>())!;
84+
85+
CollectionAssert.AreEquivalent(withoutDuplicates, sorted);
86+
CollectionAssert.AreNotEqual(withoutDuplicates, sorted);
87+
88+
int lowestLevelSeen = int.MaxValue;
89+
foreach (LevelMappingEntry entry in sorted)
90+
{
91+
Assert.IsTrue(lowestLevelSeen >= entry.Level!.Value, entry.Level.Name);
92+
lowestLevelSeen = entry.Level!.Value;
93+
}
94+
Assert.AreEqual(Level.All.Value, lowestLevelSeen);
95+
}
96+
97+
/// <summary>
98+
/// Tests the <see cref="LevelMapping.Lookup(Level?)"/> method
99+
/// </summary>
100+
[Test]
101+
public void LookupTest()
102+
{
103+
LevelMapping mapping = new();
104+
mapping.Add(new MappingEntry(Level.Info));
105+
mapping.Add(new MappingEntry(Level.Off));
106+
mapping.Add(new MappingEntry(Level.Emergency));
107+
mapping.Add(new MappingEntry(Level.Warn));
108+
109+
Assert.IsNull(mapping.Lookup(Level.Info)?.Level);
110+
111+
mapping.ActivateOptions();
112+
113+
Assert.AreEqual(Level.Info, mapping.Lookup(Level.Info)?.Level);
114+
Assert.AreEqual(Level.Off, mapping.Lookup(Level.Off)?.Level);
115+
Assert.AreEqual(Level.Emergency, mapping.Lookup(Level.Emergency)?.Level);
116+
Assert.AreEqual(Level.Warn, mapping.Lookup(Level.Warn)?.Level);
117+
Assert.AreEqual(Level.Warn, mapping.Lookup(Level.Error)?.Level);
118+
Assert.IsNull(mapping.Lookup(Level.Fine)?.Level);
119+
Assert.AreEqual(Level.Emergency, mapping.Lookup(Level.Log4Net_Debug)?.Level);
120+
Assert.IsNull(mapping.Lookup(Level.Trace)?.Level);
121+
Assert.AreEqual(Level.Warn, mapping.Lookup(Level.Alert)?.Level);
122+
Assert.IsNull(mapping.Lookup(Level.All)?.Level);
123+
}
124+
}

src/log4net.Tests/Core/LevelTest.cs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,49 @@
2323

2424
using NUnit.Framework;
2525

26-
namespace log4net.Tests.Core
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
2733
{
2834
/// <summary>
29-
/// Used for internal unit testing the <see cref="Level"/> class.
35+
/// Tests the comparison between two <see cref="Level"/>s
3036
/// </summary>
31-
[TestFixture]
32-
public sealed class LevelTest
37+
[Test]
38+
public void LevelCompare()
3339
{
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-
}
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+
}
4848

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 = "")
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)
5261
{
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)
62+
if (rightInt is not null)
63+
{
64+
Assert.AreEqual(leftInt?.CompareTo(rightInt), left?.CompareTo(right!), "{0}?.CompareTo({1})", leftName, rightName);
65+
}
66+
else
6167
{
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-
}
68+
Assert.Throws<ArgumentNullException>(() => left!.CompareTo(right!));
7069
}
7170
}
7271
}

0 commit comments

Comments
 (0)