Skip to content

Commit 9383ff3

Browse files
Justin-LloydMarusyk
authored andcommitted
Created basic xUnit tests (#6)
Initial xUnit test
1 parent 87ecd9d commit 9383ff3

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ src/BenchmarkDotNet.Core/Disassemblers/*
5757
# Cake
5858
tools/**
5959
!tools/packages.config
60-
.dotnet
60+
.dotnet
61+
.vscode/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
11+
<PackageReference Include="xunit" Version="2.4.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Grok.Net\Grok.Net.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

src/Grok.Net.Tests/UnitTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using Xunit;
3+
using GrokNet;
4+
5+
namespace GrokNetTests
6+
{
7+
public class UnitTests
8+
{
9+
[Fact]
10+
public void ParseEmptyTest()
11+
{
12+
string grokPattern = "";
13+
Grok act = new Grok(grokPattern);
14+
string logs = "";
15+
16+
GrokResult grokResult = act.Parse(logs);
17+
18+
Assert.NotNull(grokResult);
19+
Assert.Empty(grokResult);
20+
21+
}
22+
23+
[Fact]
24+
public void PatternCountTest()
25+
{
26+
string grokPattern = "%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}";
27+
Grok act = new Grok(grokPattern);
28+
string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
29+
06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";
30+
31+
GrokResult grokResult = act.Parse(logs);
32+
33+
Assert.Equal(16, grokResult.Count);
34+
}
35+
36+
[Fact]
37+
public void MonthDayPatternTest()
38+
{
39+
Grok act = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}");
40+
string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
41+
06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";
42+
43+
GrokResult grokResult = act.Parse(logs);
44+
45+
Assert.Equal("month", grokResult[0].Key);
46+
Assert.Equal("06", grokResult[0].Value);
47+
Assert.Equal("day", grokResult[1].Key);
48+
Assert.Equal("21", grokResult[1].Value);
49+
Assert.Equal("year", grokResult[2].Key);
50+
Assert.Equal("19", grokResult[2].Value);
51+
Assert.Equal("month", grokResult[8].Key);
52+
Assert.Equal("06", grokResult[8].Value);
53+
Assert.Equal("day", grokResult[9].Key);
54+
Assert.Equal("21", grokResult[9].Value);
55+
Assert.Equal("year", grokResult[10].Key);
56+
Assert.Equal("19", grokResult[10].Value);
57+
}
58+
59+
[Fact]
60+
public void TimePatternTest()
61+
{
62+
Grok act = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}");
63+
string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
64+
06-21-19 21:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";
65+
66+
GrokResult grokResult = act.Parse(logs);
67+
68+
Assert.Equal("21:00:13:589241", grokResult[3].Value);
69+
Assert.Equal("21:00:13:589265", grokResult[11].Value);
70+
}
71+
72+
[Fact]
73+
public void EmailPatternTest()
74+
{
75+
Grok act = new Grok("%{EMAILADDRESS:email}:%{GREEDYDATA:comment}");
76+
string logs = @"[email protected]:Free as in Free Beer";
77+
78+
GrokResult grokResult = act.Parse(logs);
79+
80+
Assert.Equal("[email protected]", grokResult[0].Value);
81+
Assert.Equal("Free as in Free Beer", grokResult[1].Value);
82+
Assert.Equal("email", grokResult[0].Key);
83+
Assert.Equal("comment", grokResult[1].Key);
84+
85+
}
86+
}
87+
}

src/Grok.Net.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
..\build.sh = ..\build.sh
1313
EndProjectSection
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Grok.Net.Tests", "Grok.Net.Tests\Grok.Net.Tests.csproj", "{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -22,6 +24,10 @@ Global
2224
{80980CE7-4509-4CBC-BEE7-E3DFD2213AE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
2325
{80980CE7-4509-4CBC-BEE7-E3DFD2213AE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
2426
{80980CE7-4509-4CBC-BEE7-E3DFD2213AE5}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{F7A8D826-C853-4AB6-9DB9-EFD3D7C11D12}.Release|Any CPU.Build.0 = Release|Any CPU
2531
EndGlobalSection
2632
GlobalSection(SolutionProperties) = preSolution
2733
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)