Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ src/BenchmarkDotNet.Core/Disassemblers/*
# Cake
tools/**
!tools/packages.config
.dotnet
.dotnet
.vscode/
19 changes: 19 additions & 0 deletions src/Grok.Net.Tests/Grok.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Grok.Net\Grok.Net.csproj" />
</ItemGroup>

</Project>
87 changes: 87 additions & 0 deletions src/Grok.Net.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using Xunit;
using GrokNet;

namespace GrokNetTests
{
public class UnitTests
{
[Fact]
public void ParseEmptyTest()
{
string grokPattern = "";
Grok act = new Grok(grokPattern);
string logs = "";

GrokResult grokResult = act.Parse(logs);

Assert.NotNull(grokResult);
Assert.Empty(grokResult);

}

[Fact]
public void PatternCountTest()
{
string grokPattern = "%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}";
Grok act = new Grok(grokPattern);
string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";

GrokResult grokResult = act.Parse(logs);

Assert.Equal(16, grokResult.Count);
}

[Fact]
public void MonthDayPatternTest()
{
Grok act = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}");
string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";

GrokResult grokResult = act.Parse(logs);

Assert.Equal("month", grokResult[0].Key);
Assert.Equal("06", grokResult[0].Value);
Assert.Equal("day", grokResult[1].Key);
Assert.Equal("21", grokResult[1].Value);
Assert.Equal("year", grokResult[2].Key);
Assert.Equal("19", grokResult[2].Value);
Assert.Equal("month", grokResult[8].Key);
Assert.Equal("06", grokResult[8].Value);
Assert.Equal("day", grokResult[9].Key);
Assert.Equal("21", grokResult[9].Value);
Assert.Equal("year", grokResult[10].Key);
Assert.Equal("19", grokResult[10].Value);
}

[Fact]
public void TimePatternTest()
{
Grok act = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}");
string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";

GrokResult grokResult = act.Parse(logs);

Assert.Equal("21:00:13:589241", grokResult[3].Value);
Assert.Equal("21:00:13:589265", grokResult[11].Value);
}

[Fact]
public void EmailPatternTest()
{
Grok act = new Grok("%{EMAILADDRESS:email}:%{GREEDYDATA:comment}");
string logs = @"[email protected]:Free as in Free Beer";

GrokResult grokResult = act.Parse(logs);

Assert.Equal("[email protected]", grokResult[0].Value);
Assert.Equal("Free as in Free Beer", grokResult[1].Value);
Assert.Equal("email", grokResult[0].Key);
Assert.Equal("comment", grokResult[1].Key);

}
}
}
6 changes: 6 additions & 0 deletions src/Grok.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\build.sh = ..\build.sh
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grok.Net.Tests", "Grok.Net.Tests\Grok.Net.Tests.csproj", "{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -22,6 +24,10 @@ Global
{80980CE7-4509-4CBC-BEE7-E3DFD2213AE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80980CE7-4509-4CBC-BEE7-E3DFD2213AE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80980CE7-4509-4CBC-BEE7-E3DFD2213AE5}.Release|Any CPU.Build.0 = Release|Any CPU
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down