github.com/wfusion/gofusion@v1.1.14/common/utils/sqlparser/lexer_test.go (about) 1 package sqlparser_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/wfusion/gofusion/common/utils/sqlparser" 8 ) 9 10 func TestLexer_Lex(t *testing.T) { 11 t.Run("IDENT", func(t *testing.T) { 12 t.Run("Unquoted", func(t *testing.T) { 13 AssertLex(t, `foo_BAR123`, sqlparser.IDENT, `foo_BAR123`) 14 }) 15 t.Run("Quoted", func(t *testing.T) { 16 AssertLex(t, `"crazy ~!#*&# column name"" foo"`, sqlparser.QIDENT, `"crazy ~!#*&# column name" foo"`) 17 }) 18 t.Run("BackQuoted", func(t *testing.T) { 19 AssertLex(t, "`crazy ~!#*&# column name foo`", sqlparser.QIDENT, "`crazy ~!#*&# column name foo`") 20 }) 21 t.Run("NoEndQuote", func(t *testing.T) { 22 AssertLex(t, `"unfinished`, sqlparser.ILLEGAL, `"unfinished`) 23 }) 24 t.Run("x", func(t *testing.T) { 25 AssertLex(t, `x`, sqlparser.IDENT, `x`) 26 }) 27 t.Run("StartingX", func(t *testing.T) { 28 AssertLex(t, `xyz`, sqlparser.IDENT, `xyz`) 29 }) 30 }) 31 32 t.Run("KEYWORD", func(t *testing.T) { 33 AssertLex(t, `BEGIN`, sqlparser.BEGIN, `BEGIN`) 34 }) 35 36 t.Run("STRING", func(t *testing.T) { 37 t.Run("OK", func(t *testing.T) { 38 AssertLex(t, `'this is ''a'' string'`, sqlparser.STRING, `this is 'a' string`) 39 }) 40 t.Run("NoEndQuote", func(t *testing.T) { 41 AssertLex(t, `'unfinished`, sqlparser.ILLEGAL, `'unfinished`) 42 }) 43 }) 44 45 t.Run("MLCOMMENT", func(t *testing.T) { 46 t.Run("OK", func(t *testing.T) { 47 AssertLex(t, `/* this is a multiline comment */`, sqlparser.MLCOMMENT, `this is a multiline comment`) 48 }) 49 t.Run("NoEndQuote", func(t *testing.T) { 50 AssertLex(t, `/* unfinished`, sqlparser.ILLEGAL, `/* unfinished`) 51 }) 52 }) 53 54 t.Run("BLOB", func(t *testing.T) { 55 t.Run("LowerX", func(t *testing.T) { 56 AssertLex(t, `x'0123456789abcdef'`, sqlparser.BLOB, `0123456789abcdef`) 57 }) 58 t.Run("UpperX", func(t *testing.T) { 59 AssertLex(t, `X'0123456789ABCDEF'`, sqlparser.BLOB, `0123456789ABCDEF`) 60 }) 61 t.Run("NoEndQuote", func(t *testing.T) { 62 AssertLex(t, `x'0123`, sqlparser.ILLEGAL, `x'0123`) 63 }) 64 t.Run("BadHex", func(t *testing.T) { 65 AssertLex(t, `x'hello`, sqlparser.ILLEGAL, `x'h`) 66 }) 67 }) 68 69 t.Run("INTEGER", func(t *testing.T) { 70 AssertLex(t, `123`, sqlparser.INTEGER, `123`) 71 }) 72 73 t.Run("FLOAT", func(t *testing.T) { 74 AssertLex(t, `123.456`, sqlparser.FLOAT, `123.456`) 75 AssertLex(t, `.1`, sqlparser.FLOAT, `.1`) 76 AssertLex(t, `123e456`, sqlparser.FLOAT, `123e456`) 77 AssertLex(t, `123E456`, sqlparser.FLOAT, `123E456`) 78 AssertLex(t, `123.456E78`, sqlparser.FLOAT, `123.456E78`) 79 AssertLex(t, `123.E45`, sqlparser.FLOAT, `123.E45`) 80 AssertLex(t, `123E+4`, sqlparser.FLOAT, `123E+4`) 81 AssertLex(t, `123E-4`, sqlparser.FLOAT, `123E-4`) 82 AssertLex(t, `123E`, sqlparser.ILLEGAL, `123E`) 83 AssertLex(t, `123E+`, sqlparser.ILLEGAL, `123E+`) 84 AssertLex(t, `123E-`, sqlparser.ILLEGAL, `123E-`) 85 }) 86 t.Run("BIND", func(t *testing.T) { 87 AssertLex(t, `?'`, sqlparser.BIND, `?`) 88 AssertLex(t, `?123'`, sqlparser.BIND, `?123`) 89 AssertLex(t, `:foo_bar123'`, sqlparser.BIND, `:foo_bar123`) 90 AssertLex(t, `@bar'`, sqlparser.BIND, `@bar`) 91 AssertLex(t, `$baz'`, sqlparser.BIND, `$baz`) 92 }) 93 94 t.Run("EOF", func(t *testing.T) { 95 AssertLex(t, " \n\t\r", sqlparser.EOF, ``) 96 }) 97 98 t.Run("SEMI", func(t *testing.T) { 99 AssertLex(t, ";", sqlparser.SEMI, ";") 100 }) 101 t.Run("LP", func(t *testing.T) { 102 AssertLex(t, "(", sqlparser.LP, "(") 103 }) 104 t.Run("RP", func(t *testing.T) { 105 AssertLex(t, ")", sqlparser.RP, ")") 106 }) 107 t.Run("COMMA", func(t *testing.T) { 108 AssertLex(t, ",", sqlparser.COMMA, ",") 109 }) 110 t.Run("NE", func(t *testing.T) { 111 AssertLex(t, "!=", sqlparser.NE, "!=") 112 }) 113 t.Run("BITNOT", func(t *testing.T) { 114 AssertLex(t, "!", sqlparser.BITNOT, "!") 115 }) 116 t.Run("EQ", func(t *testing.T) { 117 AssertLex(t, "=", sqlparser.EQ, "=") 118 }) 119 t.Run("LE", func(t *testing.T) { 120 AssertLex(t, "<=", sqlparser.LE, "<=") 121 }) 122 t.Run("LSHIFT", func(t *testing.T) { 123 AssertLex(t, "<<", sqlparser.LSHIFT, "<<") 124 }) 125 t.Run("LG", func(t *testing.T) { 126 AssertLex(t, "<>", sqlparser.LG, "<>") 127 }) 128 t.Run("LT", func(t *testing.T) { 129 AssertLex(t, "<", sqlparser.LT, "<") 130 }) 131 t.Run("GE", func(t *testing.T) { 132 AssertLex(t, ">=", sqlparser.GE, ">=") 133 }) 134 t.Run("RSHIFT", func(t *testing.T) { 135 AssertLex(t, ">>", sqlparser.RSHIFT, ">>") 136 }) 137 t.Run("GT", func(t *testing.T) { 138 AssertLex(t, ">", sqlparser.GT, ">") 139 }) 140 t.Run("BITAND", func(t *testing.T) { 141 AssertLex(t, "&", sqlparser.BITAND, "&") 142 }) 143 t.Run("CONCAT", func(t *testing.T) { 144 AssertLex(t, "||", sqlparser.CONCAT, "||") 145 }) 146 t.Run("BITOR", func(t *testing.T) { 147 AssertLex(t, "|", sqlparser.BITOR, "|") 148 }) 149 t.Run("PLUS", func(t *testing.T) { 150 AssertLex(t, "+", sqlparser.PLUS, "+") 151 }) 152 t.Run("MINUS", func(t *testing.T) { 153 AssertLex(t, "-", sqlparser.MINUS, "-") 154 }) 155 t.Run("STAR", func(t *testing.T) { 156 AssertLex(t, "*", sqlparser.STAR, "*") 157 }) 158 t.Run("SLASH", func(t *testing.T) { 159 AssertLex(t, "/", sqlparser.SLASH, "/") 160 }) 161 t.Run("REM", func(t *testing.T) { 162 AssertLex(t, "%", sqlparser.REM, "%") 163 }) 164 t.Run("DOT", func(t *testing.T) { 165 AssertLex(t, ".", sqlparser.DOT, ".") 166 }) 167 t.Run("ILLEGAL", func(t *testing.T) { 168 AssertLex(t, "^", sqlparser.ILLEGAL, "^") 169 }) 170 } 171 172 // AssertLex asserts the value of the first lex to s. 173 func AssertLex(tb testing.TB, s string, expectedTok sqlparser.Token, expectedLit string) { 174 tb.Helper() 175 _, tok, lit := sqlparser.NewLexer(strings.NewReader(s)).Lex() 176 if tok != expectedTok || lit != expectedLit { 177 tb.Fatalf("Lex(%q)=<%s,%s>, want <%s,%s>", s, tok, lit, expectedTok, expectedLit) 178 } 179 }