GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sqlparser/ast_copy_on_rewrite_test.go at master · smlxl/sqlparser
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
Expand file tree/ Copy pathast_copy_on_rewrite_test.goMore file actions 128 lines (110 loc) · 3.65 KB / Copy pathast_copy_on_rewrite_test.goFile metadata and controls128 lines (110 loc) · 3.65 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 /* Copyright 2023 The Vitess Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package sqlparser import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestCopyOnRewrite(t *testing.T) { parser := NewTestParser() // rewrite an expression without changing the original expr, err := parser.ParseExpr("a = b") require.NoError(t, err) out := CopyOnRewrite(expr, nil, func(cursor *CopyOnWriteCursor) { col, ok := cursor.Node().(*ColName) if !ok { return } if col.Name.EqualString("a") { cursor.Replace(NewIntLiteral("1")) } }, nil) assert.Equal(t, "a = b", String(expr)) assert.Equal(t, "1 = b", String(out)) } func TestCopyOnRewriteDeeper(t *testing.T) { parser := NewTestParser() // rewrite an expression without changing the original. the changed happens deep in the syntax tree, // here we are testing that all ancestors up to the root are cloned correctly expr, err := parser.ParseExpr("a + b * c = 12") require.NoError(t, err) var path []string out := CopyOnRewrite(expr, nil, func(cursor *CopyOnWriteCursor) { col, ok := cursor.Node().(*ColName) if !ok { return } if col.Name.EqualString("c") { cursor.Replace(NewIntLiteral("1")) } }, func(before, _ SQLNode) { path = append(path, String(before)) }) assert.Equal(t, "a + b * c = 12", String(expr)) assert.Equal(t, "a + b * 1 = 12", String(out)) expected := []string{ // this are all the nodes that we need to clone when changing the `c` node "c", "b * c", "a + b * c", "a + b * c = 12", } assert.Equal(t, expected, path) } func TestDontCopyWithoutRewrite(t *testing.T) { parser := NewTestParser() // when no rewriting happens, we want the original back expr, err := parser.ParseExpr("a = b") require.NoError(t, err) out := CopyOnRewrite(expr, nil, func(cursor *CopyOnWriteCursor) {}, nil) assert.Same(t, expr, out) } func TestStopTreeWalk(t *testing.T) { parser := NewTestParser() // stop walking down part of the AST original := "a = b + c" expr, err := parser.ParseExpr(original) require.NoError(t, err) out := CopyOnRewrite(expr, func(node, parent SQLNode) bool { _, ok := node.(*BinaryExpr) return !ok }, func(cursor *CopyOnWriteCursor) { col, ok := cursor.Node().(*ColName) if !ok { return } cursor.Replace(NewStrLiteral(col.Name.String())) }, nil) assert.Equal(t, original, String(expr)) assert.Equal(t, "'a' = b + c", String(out)) // b + c are unchanged since they are under the + (*BinaryExpr) } func TestStopTreeWalkButStillVisit(t *testing.T) { parser := NewTestParser() // here we are asserting that even when we stop at the binary expression, we still visit it in the post visitor original := "1337 = b + c" expr, err := parser.ParseExpr(original) require.NoError(t, err) out := CopyOnRewrite(expr, func(node, parent SQLNode) bool { _, ok := node.(*BinaryExpr) return !ok }, func(cursor *CopyOnWriteCursor) { switch cursor.Node().(type) { case *BinaryExpr: cursor.Replace(NewStrLiteral("johnny was here")) case *ColName: t.Errorf("should not visit ColName in the post") } }, nil) assert.Equal(t, original, String(expr)) assert.Equal(t, "1337 = 'johnny was here'", String(out)) // b + c are replaced } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |