11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using GrokNet ;
45using Xunit ;
@@ -9,7 +10,8 @@ public class UnitTests
910 {
1011 private static Stream ReadCustomFile ( ) =>
1112 File . OpenRead ( $ "Resources{ Path . DirectorySeparatorChar } grok-custom-patterns") ;
12-
13+ private static Stream ReadCustomFileWithInvalidPatterns ( ) =>
14+ File . OpenRead ( $ "Resources{ Path . DirectorySeparatorChar } grok-custom-patterns-invalid") ;
1315 [ Fact ]
1416 public void Parse_Empty_Logs_Not_Throws ( )
1517 {
@@ -127,7 +129,7 @@ public void Parse_IPv4_Pattern()
127129 [ InlineData ( "2001:db8:85a3:0:0:8a2e:370:7334" ) ]
128130 [ InlineData ( "2001:db8:85a3::8a2e:370:7334" ) ]
129131 [ InlineData ( "::1" ) ] // Loopback
130- [ InlineData ( "::" ) ] // Default route
132+ [ InlineData ( "::" ) ] // Default route
131133 public void Parse_IPv6_Pattern ( string ipAddress )
132134 {
133135 // Arrange
@@ -196,29 +198,34 @@ public void Load_Custom_Patterns(string zipcode)
196198 Assert . Equal ( email , grokResult [ 1 ] . Value ) ;
197199 }
198200
199- [ Fact ]
200- public void Load_Wrong_Custom_Patterns ( )
201+ [ Theory ]
202+ [ InlineData ( "122001" ) ]
203+ [ InlineData ( "122 001" ) ]
204+ [ InlineData ( "235 012" ) ]
205+ public void Load_Custom_Patterns_From_IDictionary ( string zipcode )
201206 {
202207 // Arrange
203- const string client = "192.168.1.1" ;
204- const string duration = "1" ;
208+ var customPatterns = new Dictionary < string , string >
209+ {
210+ { "ZIPCODE" , "[1-9]{1}[0-9]{2}\\ s{0,1}[0-9]{3}" } ,
211+ { "FLOAT" , "[+-]?([0-9]*[.,]}?[0-9]+)" }
212+ } ;
213+ const string email = "[email protected] " ; 205214
206- var sut = new Grok ( "%{WRONGPATTERN1:duration }:%{WRONGPATTERN2:client }" , ReadCustomFile ( ) ) ;
215+ var sut = new Grok ( "%{ZIPCODE:zipcode }:%{EMAILADDRESS:email }" , customPatterns ) ;
207216
208- try
209- {
210- // Act
211- GrokResult grokResult = sut . Parse ( $ "{ duration } :{ client } ") ;
212-
213- // Assert (checks if regex is invalid)
214- Assert . Equal ( "" , grokResult [ 0 ] . Value ) ;
215- Assert . Equal ( "" , grokResult [ 1 ] . Value ) ;
216- }
217- catch
218- {
219- // Assert (checks if pattern is invalid)
220- Assert . Throws < FormatException > ( ( ) => sut . Parse ( $ "{ duration } :{ client } ") ) ;
221- }
217+ // Act
218+ GrokResult grokResult = sut . Parse ( $ "{ zipcode } :{ email } ") ;
219+
220+ // Assert
221+ Assert . Equal ( zipcode , grokResult [ 0 ] . Value ) ;
222+ Assert . Equal ( email , grokResult [ 1 ] . Value ) ;
223+ }
224+
225+ [ Fact ]
226+ public void Load_Invalid_Custom_Patterns ( )
227+ {
228+ Assert . Throws < FormatException > ( ( ) => new Grok ( "%{WRONGPATTERN1:duration}:%{WRONGPATTERN2:client}" , ReadCustomFileWithInvalidPatterns ( ) ) ) ;
222229 }
223230
224231 [ Fact ]
@@ -247,7 +254,7 @@ public void Parse_Pattern_With_Type_Should_Parse_To_Specified_Type()
247254 Assert . Equal ( floatValue , grokResult [ 2 ] . Value ) ;
248255 Assert . IsType < double > ( grokResult [ 2 ] . Value ) ; // Float converts to double actually
249256 }
250-
257+
251258 [ Theory ]
252259 [ InlineData ( "INT" , "2147483648" , "int" ) ]
253260 [ InlineData ( "DATESTAMP" , "11-31-2021 02:08:58" , "datetime" ) ]
@@ -264,5 +271,6 @@ public void Parse_With_Type_Parse_Exception_Should_Ignore_Type(string regex, str
264271 Assert . Equal ( nameof ( parse ) , grokResult [ 0 ] . Key ) ;
265272 Assert . Equal ( parse , grokResult [ 0 ] . Value ) ;
266273 }
274+
267275 }
268- }
276+ }
0 commit comments