Skip to content

Commit 1bbd7b5

Browse files
committed
fixing unittests
1 parent b3b95ac commit 1bbd7b5

14 files changed

+359
-170
lines changed

src/ColumnizerLib.UnitTests/ColumnTests.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,8 @@ public void Column_NullCharReplacement ()
8888
FullValue = "asdf\0".AsMemory()
8989
};
9090

91-
//Switch between the different implementation for the windows versions
92-
//Not that great solution but currently I'm out of ideas, I know that currently
93-
//only one implementation depending on the windows version is executed
94-
if (Environment.Version >= Version.Parse("6.2"))
95-
{
96-
Assert.That(column.DisplayValue, Is.EqualTo("asdf␀"));
97-
}
98-
else
99-
{
100-
Assert.That(column.DisplayValue, Is.EqualTo("asdf "));
101-
}
102-
103-
Assert.That(column.FullValue, Is.EqualTo("asdf\0"));
91+
Assert.That(column.DisplayValue.ToString(), Is.EqualTo("asdf "));
92+
Assert.That(column.FullValue.ToString(), Is.EqualTo("asdf\0"));
10493
}
10594

10695
[Test]
@@ -112,7 +101,7 @@ public void Column_TabReplacement ()
112101
FullValue = "asdf\t".AsMemory()
113102
};
114103

115-
Assert.That(column.DisplayValue, Is.EqualTo("asdf "));
116-
Assert.That(column.FullValue, Is.EqualTo("asdf\t"));
104+
Assert.That(column.DisplayValue.ToString(), Is.EqualTo("asdf "));
105+
Assert.That(column.FullValue.ToString(), Is.EqualTo("asdf\t"));
117106
}
118107
}

src/LogExpert.Tests/BufferShiftTest.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public void Boot ()
2525
}
2626

2727
[Test]
28-
[TestCase(ReaderType.Pipeline)]
2928
[TestCase(ReaderType.System)]
3029
//[TestCase(ReaderType.Legacy)] Legacy Reader does not Support this
3130
public void TestShiftBuffers1 (ReaderType readerType)
@@ -64,11 +63,9 @@ public void TestShiftBuffers1 (ReaderType readerType)
6463
var oldCount = lil.Count;
6564

6665
// Simulate rollover
67-
//
6866
files = RolloverSimulation(files, "*$J(.)", false);
6967

7068
// Simulate rollover detection
71-
//
7269
_ = reader.ShiftBuffers();
7370

7471
lil = reader.GetLogFileInfoList();
@@ -78,7 +75,6 @@ public void TestShiftBuffers1 (ReaderType readerType)
7875
Assert.That(reader.LineCount, Is.EqualTo(linesPerFile * lil.Count));
7976

8077
// Check if rollover'd file names have been handled by LogfileReader
81-
//
8278
Assert.That(lil.Count, Is.EqualTo(files.Count));
8379
enumerator = files.GetEnumerator();
8480
_ = enumerator.MoveNext();
@@ -90,9 +86,7 @@ public void TestShiftBuffers1 (ReaderType readerType)
9086
_ = enumerator.MoveNext();
9187
}
9288

93-
// Check if file buffers have correct files. Assuming here that one buffer fits for a
94-
// complete file
95-
//
89+
// Check if file buffers have correct files. Assuming here that one buffer fits for a complete file
9690
enumerator = files.GetEnumerator();
9791
_ = enumerator.MoveNext();
9892

@@ -108,7 +102,6 @@ public void TestShiftBuffers1 (ReaderType readerType)
108102
}
109103

110104
// Checking file content
111-
//
112105
enumerator = files.GetEnumerator();
113106
_ = enumerator.MoveNext();
114107
_ = enumerator.MoveNext(); // move to 2nd entry. The first file now contains 2nd file's content (because rollover)
@@ -118,8 +111,8 @@ public void TestShiftBuffers1 (ReaderType readerType)
118111
for (i = 0; i < logBuffers.Count - 2; ++i)
119112
{
120113
var logBuffer = logBuffers[i];
121-
var line = logBuffer.GetLineOfBlock(0);
122-
Assert.That(line.FullLine.Contains(enumerator.Current, StringComparison.Ordinal));
114+
var line = logBuffer.GetLineMemoryOfBlock(0);
115+
Assert.That(line.FullLine.Span.Contains(enumerator.Current.AsSpan(), StringComparison.Ordinal));
123116
_ = enumerator.MoveNext();
124117
}
125118

@@ -128,18 +121,16 @@ public void TestShiftBuffers1 (ReaderType readerType)
128121
for (; i < logBuffers.Count; ++i)
129122
{
130123
var logBuffer = logBuffers[i];
131-
var line = logBuffer.GetLineOfBlock(0);
132-
Assert.That(line.FullLine.Contains(enumerator.Current, StringComparison.Ordinal));
124+
var line = logBuffer.GetLineMemoryOfBlock(0);
125+
Assert.That(line.FullLine.Span.Contains(enumerator.Current.AsSpan(), StringComparison.Ordinal));
133126
}
134127

135128
oldCount = lil.Count;
136129

137130
// Simulate rollover again - now latest file will be deleted (simulates logger's rollover history limit)
138-
//
139131
files = RolloverSimulation(files, "*$J(.)", true);
140132

141133
// Simulate rollover detection
142-
//
143134
_ = reader.ShiftBuffers();
144135
lil = reader.GetLogFileInfoList();
145136

@@ -148,10 +139,9 @@ public void TestShiftBuffers1 (ReaderType readerType)
148139
Assert.That(reader.LineCount, Is.EqualTo(linesPerFile * lil.Count));
149140

150141
// Check first line to see if buffers are correct
151-
//
152-
var firstLine = reader.GetLogLine(0);
142+
var firstLine = reader.GetLogLineMemory(0);
153143
var names = new string[files.Count];
154144
files.CopyTo(names, 0);
155-
Assert.That(firstLine.FullLine.Contains(names[2], StringComparison.Ordinal));
145+
Assert.That(firstLine.FullLine.Span.Contains(names[2].AsSpan(), StringComparison.Ordinal));
156146
}
157147
}

src/LogExpert.Tests/CSVColumnizerTest.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Reflection;
2+
13
using ColumnizerLib;
24

35
using LogExpert.Core.Classes.Log;
@@ -11,9 +13,39 @@ namespace LogExpert.Tests;
1113
[TestFixture]
1214
public class CSVColumnizerTest
1315
{
14-
[TestCase(@".\TestData\organizations-10000.csv", new[] { "Index", "Organization Id", "Name", "Website", "Country", "Description", "Founded", "Industry", "Number of employees" }, ReaderType.Pipeline)]
15-
[TestCase(@".\TestData\organizations-1000.csv", new[] { "Index", "Organization Id", "Name", "Website", "Country", "Description", "Founded", "Industry", "Number of employees" }, ReaderType.Pipeline)]
16-
[TestCase(@".\TestData\people-10000.csv", new[] { "Index", "User Id", "First Name", "Last Name", "Sex", "Email", "Phone", "Date of birth", "Job Title" }, ReaderType.Pipeline)]
16+
[SetUp]
17+
public void Setup ()
18+
{
19+
// Reset singleton for testing (same pattern as PluginRegistryTests)
20+
ResetPluginRegistrySingleton();
21+
22+
// Initialize plugin registry with proper test directory
23+
var testDataPath = Path.Join(Path.GetTempPath(), "LogExpertTests", Guid.NewGuid().ToString());
24+
_ = Directory.CreateDirectory(testDataPath);
25+
26+
var pluginRegistry = PluginRegistry.PluginRegistry.Create(testDataPath, 250);
27+
28+
// Verify the local file system plugin is registered
29+
var localPlugin = pluginRegistry.FindFileSystemForUri(@"C:\test.txt");
30+
Assert.That(localPlugin, Is.Not.Null, "Local file system plugin not registered!");
31+
}
32+
33+
[TearDown]
34+
public void TearDown ()
35+
{
36+
ResetPluginRegistrySingleton();
37+
}
38+
39+
/// <summary>
40+
/// Uses reflection to reset the singleton instance for testing.
41+
/// This ensures each test starts with a fresh PluginRegistry state.
42+
/// </summary>
43+
private static void ResetPluginRegistrySingleton ()
44+
{
45+
var instanceField = typeof(PluginRegistry.PluginRegistry).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
46+
instanceField?.SetValue(null, null);
47+
}
48+
1749
[TestCase(@".\TestData\organizations-10000.csv", new[] { "Index", "Organization Id", "Name", "Website", "Country", "Description", "Founded", "Industry", "Number of employees" }, ReaderType.System)]
1850
[TestCase(@".\TestData\organizations-1000.csv", new[] { "Index", "Organization Id", "Name", "Website", "Country", "Description", "Founded", "Industry", "Number of employees" }, ReaderType.System)]
1951
[TestCase(@".\TestData\people-10000.csv", new[] { "Index", "User Id", "First Name", "Last Name", "Sex", "Email", "Phone", "Date of birth", "Job Title" }, ReaderType.System)]
@@ -32,6 +64,6 @@ public void Instantiat_CSVFile_BuildCorrectColumnizer (string filename, string[]
3264
}
3365

3466
var expectedResult = string.Join(",", expectedHeaders);
35-
Assert.That(logline.LogLine.FullLine, Is.EqualTo(expectedResult));
67+
Assert.That(logline.LogLine.FullLine.ToString(), Is.EqualTo(expectedResult));
3668
}
37-
}
69+
}

src/LogExpert.Tests/ColumnizerPickerTest.cs

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Reflection;
2+
13
using ColumnizerLib;
24

35
using LogExpert.Core.Classes.Columnizer;
@@ -20,14 +22,36 @@ public class ColumnizerPickerTest
2022
[SetUp]
2123
public void Setup ()
2224
{
23-
// Ensure plugin registry is initialized with default plugins
24-
var pluginRegistry = PluginRegistry.PluginRegistry.Instance;
25+
// Reset singleton for testing (same pattern as PluginRegistryTests)
26+
ResetPluginRegistrySingleton();
27+
28+
// Initialize plugin registry with proper test directory
29+
var testDataPath = Path.Join(Path.GetTempPath(), "LogExpertTests", Guid.NewGuid().ToString());
30+
_ = Directory.CreateDirectory(testDataPath);
31+
32+
var pluginRegistry = PluginRegistry.PluginRegistry.Create(testDataPath, 250);
2533

2634
// Verify the local file system plugin is registered
2735
var localPlugin = pluginRegistry.FindFileSystemForUri(@"C:\test.txt");
2836
Assert.That(localPlugin, Is.Not.Null, "Local file system plugin not registered!");
2937
}
3038

39+
[TearDown]
40+
public void TearDown ()
41+
{
42+
ResetPluginRegistrySingleton();
43+
}
44+
45+
/// <summary>
46+
/// Uses reflection to reset the singleton instance for testing.
47+
/// This ensures each test starts with a fresh PluginRegistry state.
48+
/// </summary>
49+
private static void ResetPluginRegistrySingleton ()
50+
{
51+
var instanceField = typeof(PluginRegistry.PluginRegistry).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
52+
instanceField?.SetValue(null, null);
53+
}
54+
3155
[TestCase("Square Bracket Columnizer", "30/08/2018 08:51:42.712 [TRACE] [a] hello", "30/08/2018 08:51:42.712 [DATAIO] [b] world", null, null, null)]
3256
[TestCase("Square Bracket Columnizer", "30/08/2018 08:51:42.712 [TRACE] hello", "30/08/2018 08:51:42.712 [DATAIO][] world", null, null, null)]
3357
[TestCase("Square Bracket Columnizer", "", "30/08/2018 08:51:42.712 [TRACE] hello", "30/08/2018 08:51:42.712 [TRACE] hello", "[DATAIO][b][c] world", null)]
@@ -38,42 +62,49 @@ public void FindColumnizer_ReturnCorrectColumnizer (string expectedColumnizerNam
3862

3963
Mock<IAutoLogLineMemoryColumnizerCallback> autoLogLineColumnizerCallbackMock = new();
4064

41-
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLine(0)).Returns(new TestLogLine()
65+
// Mock GetLogLineMemory() which returns ILogLineMemory
66+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(0)).Returns(new TestLogLineMemory()
4267
{
43-
FullLine = line0,
68+
FullLine = line0?.AsMemory() ?? ReadOnlyMemory<char>.Empty,
4469
LineNumber = 0
4570
});
4671

47-
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLine(1)).Returns(new TestLogLine()
72+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(1)).Returns(new TestLogLineMemory()
4873
{
49-
FullLine = line1,
74+
FullLine = line1?.AsMemory() ?? ReadOnlyMemory<char>.Empty,
5075
LineNumber = 1
5176
});
5277

53-
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLine(2)).Returns(new TestLogLine()
78+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(2)).Returns(new TestLogLineMemory()
5479
{
55-
FullLine = line2,
80+
FullLine = line2?.AsMemory() ?? ReadOnlyMemory<char>.Empty,
5681
LineNumber = 2
5782
});
5883

59-
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLine(3)).Returns(new TestLogLine()
84+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(3)).Returns(new TestLogLineMemory()
6085
{
61-
FullLine = line3,
86+
FullLine = line3?.AsMemory() ?? ReadOnlyMemory<char>.Empty,
6287
LineNumber = 3
6388
});
64-
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLine(4)).Returns(new TestLogLine()
89+
90+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(4)).Returns(new TestLogLineMemory()
6591
{
66-
FullLine = line4,
92+
FullLine = line4?.AsMemory() ?? ReadOnlyMemory<char>.Empty,
6793
LineNumber = 4
6894
});
6995

96+
// Mock for additional sampled lines that ColumnizerPicker checks
97+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(5)).Returns((ILogLineMemory)null);
98+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(25)).Returns((ILogLineMemory)null);
99+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(100)).Returns((ILogLineMemory)null);
100+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(200)).Returns((ILogLineMemory)null);
101+
_ = autoLogLineColumnizerCallbackMock.Setup(a => a.GetLogLineMemory(400)).Returns((ILogLineMemory)null);
102+
70103
var result = ColumnizerPicker.FindMemoryColumnizer(path, autoLogLineColumnizerCallbackMock.Object, PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers);
71104

72105
Assert.That(result.GetName(), Is.EqualTo(expectedColumnizerName));
73106
}
74107

75-
[TestCase(@".\TestData\JsonColumnizerTest_01.txt", typeof(JsonCompactColumnizer.JsonCompactColumnizer), ReaderType.Pipeline)]
76-
[TestCase(@".\TestData\SquareBracketColumnizerTest_02.txt", typeof(SquareBracketColumnizer), ReaderType.Pipeline)]
77108
[TestCase(@".\TestData\JsonColumnizerTest_01.txt", typeof(JsonCompactColumnizer.JsonCompactColumnizer), ReaderType.System)]
78109
[TestCase(@".\TestData\SquareBracketColumnizerTest_02.txt", typeof(SquareBracketColumnizer), ReaderType.System)]
79110
public void FindReplacementForAutoColumnizer_ValidTextFile_ReturnCorrectColumnizer (string fileName, Type columnizerType, ReaderType readerType)
@@ -88,7 +119,7 @@ public void FindReplacementForAutoColumnizer_ValidTextFile_ReturnCorrectColumniz
88119
_ = autoColumnizer.Setup(a => a.GetName()).Returns("Auto Columnizer");
89120

90121
// TODO: When DI container is ready, we can mock this set up.
91-
PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers.Add(new JsonCompactColumnizer.JsonCompactColumnizer() as ILogLineMemoryColumnizer);
122+
PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers.Add(new JsonCompactColumnizer.JsonCompactColumnizer());
92123
var result = ColumnizerPicker.FindReplacementForAutoMemoryColumnizer(fileName, reader, autoColumnizer.Object, PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers);
93124

94125
Assert.That(columnizerType, Is.EqualTo(result.GetType()));
@@ -98,7 +129,7 @@ public void FindReplacementForAutoColumnizer_ValidTextFile_ReturnCorrectColumniz
98129
public void DecideColumnizerByName_WhenReaderIsNotReady_ReturnCorrectColumnizer (string fileName, Type columnizerType)
99130
{
100131
// TODO: When DI container is ready, we can mock this set up.
101-
PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers.Add(new JsonCompactColumnizer.JsonCompactColumnizer() as ILogLineMemoryColumnizer);
132+
PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers.Add(new JsonCompactColumnizer.JsonCompactColumnizer());
102133
var result = ColumnizerPicker.DecideMemoryColumnizerByName(fileName, PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers);
103134

104135
Assert.That(columnizerType, Is.EqualTo(result.GetType()));
@@ -109,19 +140,29 @@ public void DecideColumnizerByName_WhenReaderIsNotReady_ReturnCorrectColumnizer
109140
public void DecideColumnizerByName_ValidTextFile_ReturnCorrectColumnizer (string columnizerName, Type columnizerType)
110141
{
111142
// TODO: When DI container is ready, we can mock this set up.
112-
PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers.Add(new JsonColumnizer.JsonColumnizer() as ILogLineMemoryColumnizer);
143+
PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers.Add(new JsonColumnizer.JsonColumnizer());
113144

114145
var result = ColumnizerPicker.DecideMemoryColumnizerByName(columnizerName, PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers);
115146

116147
Assert.That(columnizerType, Is.EqualTo(result.GetType()));
117148
}
118149

119-
private class TestLogLine : ILogLine
150+
/// <summary>
151+
/// Test helper class that implements ILogLineMemory for mocking log lines.
152+
/// </summary>
153+
private class TestLogLineMemory : ILogLineMemory
120154
{
121-
public string Text => FullLine;
122-
123-
public string FullLine { get; set; }
155+
public ReadOnlyMemory<char> FullLine { get; set; }
124156

125157
public int LineNumber { get; set; }
158+
159+
// Explicit implementation for ILogLine.FullLine (string version)
160+
string ILogLine.FullLine => FullLine.ToString();
161+
162+
// Explicit implementation for ITextValue.Text
163+
string ITextValue.Text => FullLine.ToString();
164+
165+
// Explicit implementation for ITextValueMemory.Text (ReadOnlyMemory<char> version)
166+
ReadOnlyMemory<char> ITextValueMemory.Text => FullLine;
126167
}
127168
}

src/LogExpert.Tests/Helpers/RegexHelperTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void IsValidPattern_WithValidPattern_ShouldReturnTrue ()
140140

141141
// Assert
142142
Assert.That(isValid, Is.True);
143-
Assert.That(error, Is.Null);
143+
Assert.That(error, Is.Empty);
144144
}
145145

146146
[Test]

0 commit comments

Comments
 (0)