1+ using System . ComponentModel . DataAnnotations ;
2+ using System . Text . RegularExpressions ;
3+ using AutoFixture . Xunit2 ;
14using HydraScript . Domain . FrontEnd . Lexer ;
25using HydraScript . Domain . FrontEnd . Lexer . Impl ;
36using HydraScript . Domain . FrontEnd . Lexer . TokenTypes ;
@@ -13,7 +16,7 @@ public class RegexLexerTests
1316
1417 [ Theory ]
1518 [ ClassData ( typeof ( LexerSuccessData ) ) ]
16- public void LexerDoesNotThrowTest ( string text ) =>
19+ public void LexerDoesNotThrowTest ( string text ) =>
1720 Assert . Null ( Record . Exception ( ( ) => _regexLexer . GetTokens ( text ) ) ) ;
1821
1922 [ Theory ]
@@ -31,13 +34,13 @@ public void LexerToStringCorrectTest()
3134 }
3235
3336 [ Fact ]
34- public void EmptyTextTest ( ) =>
37+ public void EmptyTextTest ( ) =>
3538 Assert . NotEmpty ( _regexLexer . GetTokens ( "" ) ) ;
3639
3740 [ Fact ]
3841 public void GetTokensSkipIgnorableTypesTest ( )
3942 {
40- const string text = @ "let x = 1 // int";
43+ const string text = "let x = 1 // int" ;
4144 var tokens = _regexLexer . GetTokens ( text ) ;
4245 Assert . DoesNotContain ( _regexLexer . Structure . FindByTag ( "Comment" ) , tokens . Select ( x => x . Type ) ) ;
4346 }
@@ -49,4 +52,32 @@ public void GetTokens_KeywordInsideIdent_Ident(string input)
4952 var token = tokens . First ( ) ;
5053 token . Type . Should ( ) . Be ( new TokenType ( "Ident" ) ) ;
5154 }
55+
56+ [ Theory , AutoData ]
57+ public void GetTokens_MockedRegex_ValidOutput ( [ MinLength ( 10 ) , MaxLength ( 25 ) ] TokenInput [ ] tokenInputs )
58+ {
59+ var patterns = TokenInput . Pattern . Split ( '|' ) ;
60+
61+ var structure = Substitute . For < IStructure > ( ) ;
62+ var lexer = new RegexLexer ( structure , new TextCoordinateSystemComputer ( ) ) ;
63+ structure . Regex . ReturnsForAnyArgs (
64+ new Regex ( string . Join ( '|' , patterns . Select ( ( x , i ) => $ "(?<TYPE{ i } >{ x } )") ) ) ) ;
65+ var tokenTypes = Enumerable . Range ( 0 , patterns . Length )
66+ . Select ( x => new TokenType ( $ "TYPE{ x } ") )
67+ . ToList ( ) ;
68+
69+ // ReSharper disable once GenericEnumeratorNotDisposed
70+ structure . GetEnumerator ( )
71+ . ReturnsForAnyArgs ( _ => tokenTypes . GetEnumerator ( ) ) ;
72+
73+ var tokens = lexer . GetTokens (
74+ tokenInputs . Aggregate (
75+ TokenInput . AdditiveIdentity ,
76+ ( x , y ) => x + y ) . Value ) ;
77+ for ( var i = 0 ; i < tokenInputs . Length ; i ++ )
78+ {
79+ tokens [ i ] . Value . Should ( ) . BeEquivalentTo ( tokenInputs [ i ] . Value ) ;
80+ tokens [ i ] . Type . Should ( ) . BeOneOf ( tokenTypes ) ;
81+ }
82+ }
5283}
0 commit comments