github.com/waldiirawan/apm-agent-go/v2@v2.2.2/sqlutil/scanner_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package sqlutil 19 20 import ( 21 "encoding/json" 22 "io/ioutil" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/require" 27 ) 28 29 type test struct { 30 Name string `json:"name"` 31 Comment string `json:"comment,omitempty"` 32 Input string `json:"input"` 33 Tokens []struct { 34 Kind string `json:"kind"` 35 Text string `json:"text"` 36 } `json:"tokens,omitempty"` 37 } 38 39 func TestScanner(t *testing.T) { 40 var tests []test 41 data, err := ioutil.ReadFile("../internal/testdata/json-specs/sql_token_examples.json") 42 require.NoError(t, err) 43 err = json.Unmarshal(data, &tests) 44 require.NoError(t, err) 45 46 for _, test := range tests { 47 t.Run(test.Name, func(t *testing.T) { 48 msgFormat := "%s" 49 args := []interface{}{test.Input} 50 if test.Comment != "" { 51 msgFormat += " (%s)" 52 args = append(args, test.Comment) 53 } 54 55 s := NewScanner(test.Input) 56 for _, tok := range test.Tokens { 57 if !assert.True(t, s.Scan()) { 58 return 59 } 60 assert.Equal(t, tok.Kind, s.Token().String()) 61 assert.Equal(t, tok.Text, s.Text()) 62 } 63 assert.False(t, s.Scan()) 64 }) 65 } 66 }