github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/berolinaSQL/parser_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package BerolinaSQL
    15  
    16  import (
    17  	"testing"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  )
    21  
    22  var _ = Suite(&testBerolinaSQLSuite{})
    23  
    24  type testBerolinaSQLSuite struct {
    25  }
    26  
    27  func TestT(t *testing.T) {
    28  	TestingT(t)
    29  }
    30  
    31  func (s *testBerolinaSQLSuite) TestSpace(c *C) {
    32  	okBlock := []struct {
    33  		Times    int
    34  		Input    string
    35  		Expected string
    36  	}{
    37  		{0, " 1", "1"},
    38  		{0, "1", "1"},
    39  		{1, "     1", "1"},
    40  		{2, "  1", "1"},
    41  	}
    42  	for _, test := range okBlock {
    43  		rest, err := Space(test.Input, test.Times)
    44  		c.Assert(rest, Equals, test.Expected)
    45  		c.Assert(err, IsNil)
    46  	}
    47  
    48  	errBlock := []struct {
    49  		Times int
    50  		Input string
    51  	}{
    52  		{1, "1"},
    53  		{2, " 1"},
    54  	}
    55  
    56  	for _, test := range errBlock {
    57  		rest, err := Space(test.Input, test.Times)
    58  		c.Assert(rest, Equals, test.Input)
    59  		c.Assert(err, NotNil)
    60  	}
    61  }
    62  
    63  func (s *testBerolinaSQLSuite) TestDigit(c *C) {
    64  	okBlock := []struct {
    65  		Times          int
    66  		Input          string
    67  		ExpectedDigits string
    68  		ExpectedRest   string
    69  	}{
    70  		{0, "123abc", "123", "abc"},
    71  		{1, "123abc", "123", "abc"},
    72  		{2, "123 @)@)", "123", " @)@)"},
    73  		{3, "456 121", "456", " 121"},
    74  	}
    75  
    76  	for _, test := range okBlock {
    77  		digits, rest, err := Digit(test.Input, test.Times)
    78  		c.Assert(digits, Equals, test.ExpectedDigits)
    79  		c.Assert(rest, Equals, test.ExpectedRest)
    80  		c.Assert(err, IsNil)
    81  	}
    82  
    83  	errBlock := []struct {
    84  		Times int
    85  		Input string
    86  	}{
    87  		{1, "int"},
    88  		{2, "1int"},
    89  		{3, "12 int"},
    90  	}
    91  
    92  	for _, test := range errBlock {
    93  		digits, rest, err := Digit(test.Input, test.Times)
    94  		c.Assert(digits, Equals, "")
    95  		c.Assert(rest, Equals, test.Input)
    96  		c.Assert(err, NotNil)
    97  	}
    98  }
    99  
   100  func (s *testBerolinaSQLSuite) TestNumber(c *C) {
   101  	okBlock := []struct {
   102  		Input        string
   103  		ExpectedNum  int
   104  		ExpectedRest string
   105  	}{
   106  		{"123abc", 123, "abc"},
   107  		{"123abc", 123, "abc"},
   108  		{"123 @)@)", 123, " @)@)"},
   109  		{"456 121", 456, " 121"},
   110  	}
   111  	for _, test := range okBlock {
   112  		digits, rest, err := Number(test.Input)
   113  		c.Assert(digits, Equals, test.ExpectedNum)
   114  		c.Assert(rest, Equals, test.ExpectedRest)
   115  		c.Assert(err, IsNil)
   116  	}
   117  
   118  	errBlock := []struct {
   119  		Input string
   120  	}{
   121  		{"int"},
   122  		{"abcint"},
   123  		{"@)@)int"},
   124  	}
   125  
   126  	for _, test := range errBlock {
   127  		digits, rest, err := Number(test.Input)
   128  		c.Assert(digits, Equals, 0)
   129  		c.Assert(rest, Equals, test.Input)
   130  		c.Assert(err, NotNil)
   131  	}
   132  }
   133  
   134  func (s *testBerolinaSQLSuite) TestCharAndAnyChar(c *C) {
   135  	okBlock := []struct {
   136  		Char     byte
   137  		Input    string
   138  		Expected string
   139  	}{
   140  		{'i', "int", "nt"},
   141  		{'1', "1int", "int"},
   142  		{'1', "12 int", "2 int"},
   143  	}
   144  
   145  	for _, test := range okBlock {
   146  		rest, err := Char(test.Input, test.Char)
   147  		c.Assert(rest, Equals, test.Expected)
   148  		c.Assert(err, IsNil)
   149  
   150  		rest, err = AnyChar(test.Input)
   151  		c.Assert(rest, Equals, test.Expected)
   152  		c.Assert(err, IsNil)
   153  	}
   154  
   155  	errBlock := []struct {
   156  		Char  byte
   157  		Input string
   158  	}{
   159  		{'i', "xint"},
   160  		{'1', "x1int"},
   161  		{'1', "x12 int"},
   162  	}
   163  
   164  	for _, test := range errBlock {
   165  		rest, err := Char(test.Input, test.Char)
   166  		c.Assert(rest, Equals, test.Input)
   167  		c.Assert(err, NotNil)
   168  	}
   169  }