github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/parser/lexer_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package parser 12 13 import ( 14 "reflect" 15 "testing" 16 ) 17 18 func TestLexer(t *testing.T) { 19 // Test the lookahead logic in Lex. 20 testData := []struct { 21 sql string 22 expected []int 23 }{ 24 {`WITH TIME`, []int{WITH_LA, TIME}}, 25 {`WITH ORDINALITY`, []int{WITH_LA, ORDINALITY}}, 26 {`NOT BETWEEN`, []int{NOT_LA, BETWEEN}}, 27 {`NOT IN`, []int{NOT_LA, IN}}, 28 {`NOT SIMILAR`, []int{NOT_LA, SIMILAR}}, 29 {`AS OF SYSTEM TIME`, []int{AS_LA, OF, SYSTEM, TIME}}, 30 } 31 for i, d := range testData { 32 s := makeScanner(d.sql) 33 var scanTokens []sqlSymType 34 for { 35 var lval sqlSymType 36 s.scan(&lval) 37 if lval.id == 0 { 38 break 39 } 40 scanTokens = append(scanTokens, lval) 41 } 42 var l lexer 43 l.init(d.sql, scanTokens, defaultNakedIntType) 44 var lexTokens []int 45 for { 46 var lval sqlSymType 47 id := l.Lex(&lval) 48 if id == 0 { 49 break 50 } 51 lexTokens = append(lexTokens, id) 52 } 53 54 if !reflect.DeepEqual(d.expected, lexTokens) { 55 t.Errorf("%d: %q: expected %d, but found %d", i, d.sql, d.expected, lexTokens) 56 } 57 } 58 59 }