github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/dialect/postgresql/scanner_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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 postgresql
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    22  )
    23  
    24  func TestLiteralID(t *testing.T) {
    25  	testcases := []struct {
    26  		in  string
    27  		id  int
    28  		out string
    29  	}{{
    30  		in:  "`aa`",
    31  		id:  ID,
    32  		out: "aa",
    33  	}, {
    34  		in:  "```a```",
    35  		id:  ID,
    36  		out: "`a`",
    37  	}, {
    38  		in:  "`a``b`",
    39  		id:  ID,
    40  		out: "a`b",
    41  	}, {
    42  		in:  "`a``b`c",
    43  		id:  ID,
    44  		out: "a`b",
    45  	}, {
    46  		in:  "`a``b",
    47  		id:  LEX_ERROR,
    48  		out: "a`b",
    49  	}, {
    50  		in:  "`a``b``",
    51  		id:  LEX_ERROR,
    52  		out: "a`b`",
    53  	}, {
    54  		in:  "``",
    55  		id:  LEX_ERROR,
    56  		out: "",
    57  	}}
    58  
    59  	for _, tcase := range testcases {
    60  		s := NewScanner(dialect.MYSQL, tcase.in)
    61  		id, out := s.Scan()
    62  		if tcase.id != id || string(out) != tcase.out {
    63  			t.Errorf("Scan(%s): %d, %s, want %d, %s", tcase.in, id, out, tcase.id, tcase.out)
    64  		}
    65  	}
    66  }
    67  
    68  func tokenName(id int) string {
    69  	if id == STRING {
    70  		return "STRING"
    71  	} else if id == LEX_ERROR {
    72  		return "LEX_ERROR"
    73  	}
    74  	return fmt.Sprintf("%d", id)
    75  }
    76  
    77  func TestString(t *testing.T) {
    78  	testcases := []struct {
    79  		in   string
    80  		id   int
    81  		want string
    82  	}{{
    83  		in:   "''",
    84  		id:   STRING,
    85  		want: "",
    86  	}, {
    87  		in:   "''''",
    88  		id:   STRING,
    89  		want: "'",
    90  	}, {
    91  		in:   "'hello'",
    92  		id:   STRING,
    93  		want: "hello",
    94  	}, {
    95  		in:   "'\\n'",
    96  		id:   STRING,
    97  		want: "\n",
    98  	}, {
    99  		in:   "'\\nhello\\n'",
   100  		id:   STRING,
   101  		want: "\nhello\n",
   102  	}, {
   103  		in:   "'a''b'",
   104  		id:   STRING,
   105  		want: "a'b",
   106  	}, {
   107  		in:   "'a\\'b'",
   108  		id:   STRING,
   109  		want: "a'b",
   110  	}, {
   111  		in:   "'\\'",
   112  		id:   LEX_ERROR,
   113  		want: "'",
   114  	}, {
   115  		in:   "'",
   116  		id:   LEX_ERROR,
   117  		want: "",
   118  	}, {
   119  		in:   "'hello\\'",
   120  		id:   LEX_ERROR,
   121  		want: "hello'",
   122  	}, {
   123  		in:   "'hello",
   124  		id:   LEX_ERROR,
   125  		want: "hello",
   126  	}, {
   127  		in:   "'hello\\",
   128  		id:   LEX_ERROR,
   129  		want: "hello",
   130  	}, {
   131  		in:   "'C:\\Program Files(x86)'",
   132  		id:   STRING,
   133  		want: "C:Program Files(x86)",
   134  	}, {
   135  		in:   "'C:\\\\Program Files(x86)'",
   136  		id:   STRING,
   137  		want: "C:\\Program Files(x86)",
   138  	}}
   139  
   140  	for _, tcase := range testcases {
   141  		id, got := NewScanner(dialect.MYSQL, tcase.in).Scan()
   142  		if tcase.id != id || string(got) != tcase.want {
   143  			t.Errorf("Scan(%q) = (%s, %q), want (%s, %q)", tcase.in, tokenName(id), got, tokenName(tcase.id), tcase.want)
   144  		}
   145  	}
   146  }
   147  
   148  func TestBuffer(t *testing.T) {
   149  	testcases := []struct {
   150  		in   string
   151  		id   int
   152  		want string
   153  	}{{
   154  		in:   "'webapp'@'localhost'",
   155  		id:   STRING,
   156  		want: "webapp",
   157  	}}
   158  
   159  	for _, tcase := range testcases {
   160  		id, got := NewScanner(dialect.MYSQL, tcase.in).Scan()
   161  		if tcase.id != id || string(got) != tcase.want {
   162  			t.Errorf("Scan(%q) = (%s, %q), want (%s, %q)", tcase.in, tokenName(id), got, tokenName(tcase.id), tcase.want)
   163  		}
   164  	}
   165  }