Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified build.ps1
100644 → 100755
Empty file.
69 changes: 69 additions & 0 deletions src/LogExpert.Core.UnitTests/Classes/DateTimeParser/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using LogExpert.Core.Classes.DateTimeParser;

using NUnit.Framework;

namespace LogExpert.Core.UnitTests.Classes.DateTimeParser;

[TestFixture]
public class ParserTests
{
[Test]
public void ParseSections_EmptyString_ReturnsEmptyList()
{
var result = Parser.ParseSections("", out var syntaxError);

Assert.That(result, Is.Not.Null);
Assert.That(result.Count, Is.EqualTo(0));
Assert.That(syntaxError, Is.False);
}

[Test]
public void ParseSections_SimpleFormatString_ReturnsSections()
{
var result = Parser.ParseSections("yyyy-MM-dd", out var syntaxError);

Assert.That(result, Is.Not.Null);
Assert.That(syntaxError, Is.False);
// Don't assert specific count as implementation details may vary
}

[Test]
public void ParseSections_ValidInput_DoesNotSetSyntaxError()
{
Parser.ParseSections("HH:mm:ss", out var syntaxError);

Assert.That(syntaxError, Is.False);
}

[Test]
public void ParseSections_NullInput_ThrowsException()
{
// Test that the parser throws an exception for null input
Assert.Throws<NullReferenceException>(() => Parser.ParseSections(null, out var syntaxError));
}

[TestCase("yyyy")]
[TestCase("MM")]
[TestCase("dd")]
[TestCase("HH:mm")]
[TestCase("yyyy-MM-dd HH:mm:ss")]
public void ParseSections_CommonFormats_DoesNotThrow(string formatString)
{
Assert.DoesNotThrow(() =>
{
var result = Parser.ParseSections(formatString, out var syntaxError);
Assert.That(result, Is.Not.Null);
});
}

[Test]
public void ParseSections_ComplexFormat_HandlesCorrectly()
{
const string complexFormat = "yyyy-MM-dd'T'HH:mm:ss.fff";

var result = Parser.ParseSections(complexFormat, out var syntaxError);

Assert.That(result, Is.Not.Null);
// Parser should handle complex formats without throwing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using LogExpert.Core.Classes.DateTimeParser;

using NUnit.Framework;

using System.Collections.Generic;

namespace LogExpert.Core.UnitTests.Classes.DateTimeParser;

[TestFixture]
public class SectionTests
{
[Test]
public void Constructor_Default_InitializesCorrectly()
{
var section = new Section();

Assert.That(section.SectionIndex, Is.EqualTo(0));
Assert.That(section.GeneralTextDateDurationParts, Is.Null);
}

[Test]
public void SectionIndex_SetAndGet_WorksCorrectly()
{
var section = new Section();
const int expectedIndex = 5;

section.SectionIndex = expectedIndex;

Assert.That(section.SectionIndex, Is.EqualTo(expectedIndex));
}

[Test]
public void GeneralTextDateDurationParts_SetAndGet_WorksCorrectly()
{
var section = new Section();
var parts = new List<string> { "yyyy", "MM", "dd" };

section.GeneralTextDateDurationParts = parts;

Assert.That(section.GeneralTextDateDurationParts, Is.EqualTo(parts));
Assert.That(section.GeneralTextDateDurationParts, Is.Not.Null);
Assert.That(section.GeneralTextDateDurationParts.Count, Is.EqualTo(3));
}

[Test]
public void GeneralTextDateDurationParts_SetToNull_AllowsNullValue()
{
var section = new Section
{
GeneralTextDateDurationParts = new List<string> { "test" }
};

section.GeneralTextDateDurationParts = null;

Assert.That(section.GeneralTextDateDurationParts, Is.Null);
}

[Test]
public void Properties_IndependentlyModifiable_DoNotAffectEachOther()
{
var section = new Section();
const int index = 10;
var parts = new List<string> { "HH", "mm", "ss" };

section.SectionIndex = index;
section.GeneralTextDateDurationParts = parts;

Assert.That(section.SectionIndex, Is.EqualTo(index));
Assert.That(section.GeneralTextDateDurationParts, Is.EqualTo(parts));
}
}
88 changes: 88 additions & 0 deletions src/LogExpert.Core.UnitTests/Classes/DateTimeParser/TokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using LogExpert.Core.Classes.DateTimeParser;

using NUnit.Framework;

using System;

namespace LogExpert.Core.UnitTests.Classes.DateTimeParser;

[TestFixture]
public class TokenTests
{
[TestCase("y", true)]
[TestCase("yyyy", true)]
[TestCase("Y", true)]
[TestCase("YYYY", true)]
[TestCase("m", true)]
[TestCase("MM", true)]
[TestCase("M", true)]
[TestCase("mm", true)]
[TestCase("d", true)]
[TestCase("dd", true)]
[TestCase("D", true)]
[TestCase("DD", true)]
[TestCase("s", true)]
[TestCase("ss", true)]
[TestCase("S", true)]
[TestCase("SS", true)]
[TestCase("h", true)]
[TestCase("hh", true)]
[TestCase("H", true)]
[TestCase("HH", true)]
[TestCase("tt", true)]
[TestCase("TT", true)]
public void IsDatePart_ValidDateTokens_ReturnsTrue(string token, bool expected)
{
var result = Token.IsDatePart(token);

Assert.That(result, Is.EqualTo(expected));
}

[TestCase("x", false)]
[TestCase("z", false)]
[TestCase("xyz", false)]
[TestCase("abc", false)]
[TestCase("123", false)]
[TestCase("-", false)]
[TestCase(":", false)]
[TestCase(" ", false)]
[TestCase("", false)]
[TestCase("ttt", false)]
[TestCase("t", false)]
public void IsDatePart_InvalidTokens_ReturnsFalse(string token, bool expected)
{
var result = Token.IsDatePart(token);

Assert.That(result, Is.EqualTo(expected));
}

[Test]
public void IsDatePart_NullToken_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => Token.IsDatePart(null));
}

[TestCase("Y")]
[TestCase("M")]
[TestCase("D")]
[TestCase("S")]
[TestCase("H")]
[TestCase("TT")]
public void IsDatePart_CaseInsensitive_ReturnsTrue(string token)
{
var result = Token.IsDatePart(token);

Assert.That(result, Is.True);
}

[TestCase("yy")]
[TestCase("yyyy")]
[TestCase("yMd")]
[TestCase("hmmss")]
public void IsDatePart_ComplexDateTokens_ReturnsTrue(string token)
{
var result = Token.IsDatePart(token);

Assert.That(result, Is.True);
}
}
148 changes: 148 additions & 0 deletions src/LogExpert.Core.UnitTests/Classes/ParamParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using LogExpert.Core.Classes;

using Moq;

using NUnit.Framework;

namespace LogExpert.Core.UnitTests.Classes;

[TestFixture]
public class ParamParserTests
{
private Mock<ILogLine> _mockLogLine;

[SetUp]
public void SetUp()
{
_mockLogLine = new Mock<ILogLine>();
_mockLogLine.Setup(x => x.FullLine).Returns("Sample log line content");
}

[Test]
public void ReplaceParams_LineNumber_ReplacesLParameter()
{
var parser = new ParamParser("Line number: %L");

var result = parser.ReplaceParams(_mockLogLine.Object, 42, @"C:\test\file.txt");

Assert.That(result, Is.EqualTo("Line number: 42"));
}

[Test]
public void ReplaceParams_FileName_ReplacesNParameter()
{
var parser = new ParamParser("File: %N");
var fileName = "file.txt";

var result = parser.ReplaceParams(_mockLogLine.Object, 1, fileName);

Assert.That(result, Is.EqualTo("File: file.txt"));
}

[Test]
public void ReplaceParams_FilePath_ReplacesPParameter()
{
var parser = new ParamParser("Path: %P");
var tempFile = Path.Combine(Path.GetTempPath(), "test", "file.txt");

var result = parser.ReplaceParams(_mockLogLine.Object, 1, tempFile);

Assert.That(result, Contains.Substring("Path: "));
Assert.That(result, Contains.Substring("test"));
}

[Test]
public void ReplaceParams_FullPath_ReplacesFParameter()
{
var parser = new ParamParser("Full path: %F");
var fileName = "file.txt";

var result = parser.ReplaceParams(_mockLogLine.Object, 1, fileName);

Assert.That(result, Contains.Substring("Full path: "));
Assert.That(result, Contains.Substring("file.txt"));
}

[Test]
public void ReplaceParams_Extension_ReplacesEParameter()
{
var parser = new ParamParser("Extension: %E");
var fileName = "file.txt";

var result = parser.ReplaceParams(_mockLogLine.Object, 1, fileName);

Assert.That(result, Is.EqualTo("Extension: .txt"));
}

[Test]
public void ReplaceParams_NameWithoutExtension_ReplacesMParameter()
{
var parser = new ParamParser("Name without ext: %M");
var fileName = "file.txt";

var result = parser.ReplaceParams(_mockLogLine.Object, 1, fileName);

Assert.That(result, Is.EqualTo("Name without ext: file"));
}

[Test]
public void ReplaceParams_FileWithSpaces_QuotesPath()
{
var parser = new ParamParser("Path: %P");
var tempFile = Path.Combine(Path.GetTempPath(), "test folder", "file.txt");

var result = parser.ReplaceParams(_mockLogLine.Object, 1, tempFile);

Assert.That(result, Contains.Substring("Path: "));
// Should contain quotes if path has spaces
if (tempFile.Contains(' '))
{
Assert.That(result, Contains.Substring("\""));
}
}

[Test]
public void ReplaceParams_UnixPath_HandlesCorrectly()
{
var parser = new ParamParser("File: %N, Path: %P");
var fileName = "/home/user/file.txt";

var result = parser.ReplaceParams(_mockLogLine.Object, 1, fileName);

Assert.That(result, Contains.Substring("File: file.txt"));
Assert.That(result, Contains.Substring("Path: "));
}

[Test]
public void ReplaceParams_MultipleParameters_ReplacesAll()
{
var parser = new ParamParser("Line %L: %N at %P");
var fileName = "app.log";

var result = parser.ReplaceParams(_mockLogLine.Object, 123, fileName);

Assert.That(result, Contains.Substring("Line 123:"));
Assert.That(result, Contains.Substring("app.log"));
Assert.That(result, Contains.Substring("at "));
}

[Test]
public void StripExtension_VariousFileNames_ReturnsCorrectResult()
{
Assert.That(ParamParser.StripExtension("file.txt"), Is.EqualTo("file"));
Assert.That(ParamParser.StripExtension("archive.tar.gz"), Is.EqualTo("archive.tar"));
Assert.That(ParamParser.StripExtension("file"), Is.EqualTo("fil")); // This is the actual behavior when no extension
Assert.That(ParamParser.StripExtension("file."), Is.EqualTo("file"));
}

[Test]
public void Constructor_StoresTemplate()
{
const string template = "Test template %L";
var parser = new ParamParser(template);

// Test that the template is used correctly
var result = parser.ReplaceParams(_mockLogLine.Object, 1, "test.txt");
Assert.That(result, Contains.Substring("Test template"));
}
}
Loading