Debian Codegraph @

precedence_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqlparser
18
19 import (
20 "fmt"
21 "testing"
22 )
23
24 func readable(node Expr) string {
25 switch node := node.(type) {
26 case *OrExpr:
27 return fmt.Sprintf("(%s or %s)", readable(node.Left), readable(node.Right))
28 case *AndExpr:
29 return fmt.Sprintf("(%s and %s)", readable(node.Left), readable(node.Right))
30 case *BinaryExpr:
31 return fmt.Sprintf("(%s %s %s)", readable(node.Left), node.Operator, readable(node.Right))
32 case *IsExpr:
33 return fmt.Sprintf("(%s %s)", readable(node.Expr), node.Operator)
34 default:
35 return String(node)
36 }
37 }
38
39 func TestAndOrPrecedence(t *testing.T) {
40 validSQL := []struct {
41 input string
42 output string
43 }{{
44 input: "select * from a where a=b and c=d or e=f",
45 output: "((a = b and c = d) or e = f)",
46 }, {
47 input: "select * from a where a=b or c=d and e=f",
48 output: "(a = b or (c = d and e = f))",
49 }}
50 for _, tcase := range validSQL {
51 tree, err := Parse(tcase.input)
52 if err != nil {
53 t.Error(err)
54 continue
55 }
56 expr := readable(tree.(*Select).Where.Expr)
57 if expr != tcase.output {
58 t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
59 }
60 }
61 }
62
63 func TestPlusStarPrecedence(t *testing.T) {
64 validSQL := []struct {
65 input string
66 output string
67 }{{
68 input: "select 1+2*3 from a",
69 output: "(1 + (2 * 3))",
70 }, {
71 input: "select 1*2+3 from a",
72 output: "((1 * 2) + 3)",
73 }}
74 for _, tcase := range validSQL {
75 tree, err := Parse(tcase.input)
76 if err != nil {
77 t.Error(err)
78 continue
79 }
80 expr := readable(tree.(*Select).SelectExprs[0].(*AliasedExpr).Expr)
81 if expr != tcase.output {
82 t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
83 }
84 }
85 }
86
87 func TestIsPrecedence(t *testing.T) {
88 validSQL := []struct {
89 input string
90 output string
91 }{{
92 input: "select * from a where a+b is true",
93 output: "((a + b) is true)",
94 }, {
95 input: "select * from a where a=1 and b=2 is true",
96 output: "(a = 1 and (b = 2 is true))",
97 }, {
98 input: "select * from a where (a=1 and b=2) is true",
99 output: "((a = 1 and b = 2) is true)",
100 }}
101 for _, tcase := range validSQL {
102 tree, err := Parse(tcase.input)
103 if err != nil {
104 t.Error(err)
105 continue
106 }
107 expr := readable(tree.(*Select).Where.Expr)
108 if expr != tcase.output {
109 t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
110 }
111 }
112 }