|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tokenizer |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "reflect" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/ajitpratap0/GoSQLX/internal/tokscratch" |
| 24 | + "github.com/ajitpratap0/GoSQLX/pkg/models" |
| 25 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 26 | +) |
| 27 | + |
| 28 | +// TestStoreToken_CopiesEveryField guards storeToken's field-by-field copy: a |
| 29 | +// field added to models.Token must be copied too, or tokens from the bridged |
| 30 | +// readers would silently lose it. |
| 31 | +func TestStoreToken_CopiesEveryField(t *testing.T) { |
| 32 | + var v models.Token |
| 33 | + rv := reflect.ValueOf(&v).Elem() |
| 34 | + for i := 0; i < rv.NumField(); i++ { |
| 35 | + f := rv.Field(i) |
| 36 | + switch f.Kind() { |
| 37 | + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 38 | + f.SetInt(int64(i + 1)) |
| 39 | + case reflect.String: |
| 40 | + f.SetString("v") |
| 41 | + case reflect.Bool: |
| 42 | + f.SetBool(true) |
| 43 | + case reflect.Pointer: |
| 44 | + f.Set(reflect.New(f.Type().Elem())) |
| 45 | + default: |
| 46 | + t.Fatalf("models.Token.%s has kind %s: extend this test and storeToken", rv.Type().Field(i).Name, f.Kind()) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + var out models.Token |
| 51 | + if err := storeToken(&out, v, nil); err != nil { |
| 52 | + t.Fatalf("storeToken: %v", err) |
| 53 | + } |
| 54 | + if !reflect.DeepEqual(out, v) { |
| 55 | + t.Fatalf("storeToken copied %+v, want %+v", out, v) |
| 56 | + } |
| 57 | + |
| 58 | + want := errors.New("boom") |
| 59 | + out = models.Token{} |
| 60 | + if err := storeToken(&out, v, want); err != want { |
| 61 | + t.Fatalf("storeToken error = %v, want %v", err, want) |
| 62 | + } |
| 63 | + if out != (models.Token{}) { |
| 64 | + t.Fatalf("storeToken wrote %+v on error, want no write", out) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// TestTokenize_GrowsPastEstimate covers the slot growth path: "{}" pairs are |
| 69 | +// one prescan word but two tokens each, so the capacity estimate falls short |
| 70 | +// and the token slice must grow while tokens are built in place. |
| 71 | +func TestTokenize_GrowsPastEstimate(t *testing.T) { |
| 72 | + const pairs = 200 |
| 73 | + sql := strings.Repeat("{}", pairs) |
| 74 | + |
| 75 | + probe, err := New() |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("New: %v", err) |
| 78 | + } |
| 79 | + probe.load([]byte(sql), sql) |
| 80 | + if est := probe.prescan(); est >= 2*pairs+1 { |
| 81 | + t.Fatalf("precondition: prescan estimate %d covers all %d tokens", est, 2*pairs+1) |
| 82 | + } |
| 83 | + |
| 84 | + check := func(t *testing.T, got []models.TokenWithSpan) { |
| 85 | + t.Helper() |
| 86 | + if len(got) != 2*pairs+1 { |
| 87 | + t.Fatalf("got %d tokens, want %d", len(got), 2*pairs+1) |
| 88 | + } |
| 89 | + for i, tw := range got[:2*pairs] { |
| 90 | + want := models.TokenWithSpan{ |
| 91 | + Token: models.Token{Type: models.TokenTypeLBrace, Value: "{"}, |
| 92 | + Start: models.Location{Line: 1, Column: i + 1}, |
| 93 | + End: models.Location{Line: 1, Column: i + 2}, |
| 94 | + } |
| 95 | + if i%2 == 1 { |
| 96 | + want.Token = models.Token{Type: models.TokenTypeRBrace, Value: "}"} |
| 97 | + } |
| 98 | + if tw != want { |
| 99 | + t.Fatalf("token %d = %+v, want %+v", i, tw, want) |
| 100 | + } |
| 101 | + } |
| 102 | + if eof := got[2*pairs].Token; eof.Type != models.TokenTypeEOF { |
| 103 | + t.Fatalf("last token = %+v, want EOF", eof) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + t.Run("public", func(t *testing.T) { |
| 108 | + tkz := GetTokenizer() |
| 109 | + defer PutTokenizer(tkz) |
| 110 | + got, err := tkz.TokenizeString(sql) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("TokenizeString: %v", err) |
| 113 | + } |
| 114 | + check(t, got) |
| 115 | + }) |
| 116 | + t.Run("scratch", func(t *testing.T) { |
| 117 | + tkz := GetTokenizer() |
| 118 | + defer PutTokenizer(tkz) |
| 119 | + for round := 0; round < 2; round++ { // the second round reuses tokBuf |
| 120 | + got, err := tokscratch.String(tkz, sql) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("round %d: %v", round, err) |
| 123 | + } |
| 124 | + check(t, got) |
| 125 | + } |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +// TestTokenize_ErrorLeavesSpareSlotsZero checks the tokBuf invariant |
| 130 | +// (tokBuf[len:cap] is zero) after a reader fails in the slot it was given, |
| 131 | +// for each reader family, and that the next call on the same tokenizer still |
| 132 | +// matches the public API exactly. The tokenize loop zeroes a failed slot, so |
| 133 | +// the invariant holds even for a reader that writes part of a token before |
| 134 | +// it fails. |
| 135 | +func TestTokenize_ErrorLeavesSpareSlotsZero(t *testing.T) { |
| 136 | + tests := []struct { |
| 137 | + name string |
| 138 | + dialect keywords.SQLDialect |
| 139 | + sql string |
| 140 | + }{ |
| 141 | + {"single quoted string", "", "SELECT a, 'unterminated"}, |
| 142 | + {"double quoted identifier", "", `SELECT a, "unterminated`}, |
| 143 | + {"backtick identifier", keywords.DialectMySQL, "SELECT a, `unterminated"}, |
| 144 | + {"bracket identifier", keywords.DialectSQLServer, "SELECT a, [unterminated"}, |
| 145 | + {"unicode escape unsupported", keywords.DialectOracle, "SELECT a, U&'x'"}, |
| 146 | + {"unicode escape bad", keywords.DialectPostgreSQL, `SELECT a, U&'\zz'`}, |
| 147 | + {"escape string", keywords.DialectPostgreSQL, `SELECT a, E'unterminated`}, |
| 148 | + {"national string", keywords.DialectMySQL, "SELECT a, N'unterminated"}, |
| 149 | + {"triple quoted string", keywords.DialectBigQuery, "SELECT a, '''unterminated"}, |
| 150 | + {"invalid number", "", "SELECT a, 1abc"}, |
| 151 | + {"unexpected character", "", "SELECT a, §"}, |
| 152 | + } |
| 153 | + const after = "SELECT b, 'ok', \"q\", 1.5 FROM t WHERE c >= 2" |
| 154 | + for _, tc := range tests { |
| 155 | + t.Run(tc.name, func(t *testing.T) { |
| 156 | + tkz := GetTokenizer() |
| 157 | + defer PutTokenizer(tkz) |
| 158 | + tkz.SetDialect(tc.dialect) |
| 159 | + pub, err := NewWithDialect(tc.dialect) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("NewWithDialect: %v", err) |
| 162 | + } |
| 163 | + |
| 164 | + _, wantErr := pub.TokenizeString(tc.sql) |
| 165 | + if wantErr == nil { |
| 166 | + t.Fatalf("precondition: %q tokenizes without error", tc.sql) |
| 167 | + } |
| 168 | + _, gotErr := tokscratch.String(tkz, tc.sql) |
| 169 | + if gotErr == nil || gotErr.Error() != wantErr.Error() { |
| 170 | + t.Fatalf("scratch error = %v, want %v", gotErr, wantErr) |
| 171 | + } |
| 172 | + spare := tkz.tokBuf[len(tkz.tokBuf):cap(tkz.tokBuf)] |
| 173 | + for i := range spare { |
| 174 | + if spare[i] != (models.TokenWithSpan{}) { |
| 175 | + t.Fatalf("tokBuf[%d] = %+v after the error, want zero", len(tkz.tokBuf)+i, spare[i]) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + want, err := pub.TokenizeString(after) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("public TokenizeString: %v", err) |
| 182 | + } |
| 183 | + got, err := tokscratch.String(tkz, after) |
| 184 | + if err != nil { |
| 185 | + t.Fatalf("scratch after the error: %v", err) |
| 186 | + } |
| 187 | + if !reflect.DeepEqual(got, want) { |
| 188 | + t.Fatalf("scratch after the error:\n got %+v\nwant %+v", got, want) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments