github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logql/log/pattern/lexer_test.go (about) 1 package pattern 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func Test_Lex(t *testing.T) { 10 for _, tc := range []struct { 11 input string 12 expected []int 13 }{ 14 {`_foo`, []int{LITERAL, LITERAL, LITERAL, LITERAL}}, 15 {`<foo`, []int{LITERAL, LITERAL, LITERAL, LITERAL}}, 16 {`<`, []int{LITERAL}}, 17 {`>`, []int{LITERAL}}, 18 {`<_1foo>`, []int{IDENTIFIER}}, 19 {`<_1foo> bar <buzz>`, []int{IDENTIFIER, LITERAL, LITERAL, LITERAL, LITERAL, LITERAL, IDENTIFIER}}, 20 {`<1foo>`, []int{LITERAL, LITERAL, LITERAL, LITERAL, LITERAL, LITERAL}}, 21 } { 22 tc := tc 23 t.Run(tc.input, func(t *testing.T) { 24 actual := []int{} 25 l := newLexer() 26 l.setData([]byte(tc.input)) 27 for { 28 var lval exprSymType 29 tok := l.Lex(&lval) 30 if tok == 0 { 31 break 32 } 33 actual = append(actual, tok) 34 } 35 assert.Equal(t, toksToStrings(tc.expected), toksToStrings(actual)) 36 assert.Equal(t, tc.expected, actual) 37 }) 38 } 39 } 40 41 func toksToStrings(toks []int) []string { 42 strings := make([]string, len(toks)) 43 for i, tok := range toks { 44 strings[i] = exprToknames[tok-exprPrivate+1] 45 } 46 return strings 47 }