github.com/XiaoMi/Gaea@v1.2.5/parser/format/format_test.go (about)

     1  // Copyright 2015 PingCAP, 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 format
    15  
    16  import (
    17  	"bytes"
    18  	"io/ioutil"
    19  	"strings"
    20  	"testing"
    21  
    22  	. "github.com/pingcap/check"
    23  
    24  	"github.com/XiaoMi/Gaea/util/testleak"
    25  )
    26  
    27  func TestT(t *testing.T) {
    28  	CustomVerboseFlag = true
    29  	TestingT(t)
    30  }
    31  
    32  var _ = Suite(&testFormatSuite{})
    33  var _ = Suite(&testRestoreCtxSuite{})
    34  
    35  type testFormatSuite struct {
    36  }
    37  
    38  func checkFormat(c *C, f Formatter, buf *bytes.Buffer, str, expect string) {
    39  	_, err := f.Format(str, 3)
    40  	c.Assert(err, IsNil)
    41  	b, err := ioutil.ReadAll(buf)
    42  	c.Assert(err, IsNil)
    43  	c.Assert(string(b), Equals, expect)
    44  }
    45  
    46  func (s *testFormatSuite) TestFormat(c *C) {
    47  	defer testleak.AfterTest(c)()
    48  	str := "abc%d%%e%i\nx\ny\n%uz\n"
    49  	buf := &bytes.Buffer{}
    50  	f := IndentFormatter(buf, "\t")
    51  	expect := `abc3%e
    52  	x
    53  	y
    54  z
    55  `
    56  	checkFormat(c, f, buf, str, expect)
    57  
    58  	str = "abc%d%%e%i\nx\ny\n%uz\n%i\n"
    59  	buf = &bytes.Buffer{}
    60  	f = FlatFormatter(buf)
    61  	expect = "abc3%e x y z\n "
    62  	checkFormat(c, f, buf, str, expect)
    63  }
    64  
    65  type testRestoreCtxSuite struct {
    66  }
    67  
    68  func (s *testRestoreCtxSuite) TestRestoreCtx(c *C) {
    69  	testCases := []struct {
    70  		flag   RestoreFlags
    71  		expect string
    72  	}{
    73  		{0, "key`.'\"Word\\ str`.'\"ing\\ na`.'\"Me\\"},
    74  		{RestoreStringSingleQuotes, "key`.'\"Word\\ 'str`.''\"ing\\' na`.'\"Me\\"},
    75  		{RestoreStringDoubleQuotes, "key`.'\"Word\\ \"str`.'\"\"ing\\\" na`.'\"Me\\"},
    76  		{RestoreStringEscapeBackslash, "key`.'\"Word\\ str`.'\"ing\\\\ na`.'\"Me\\"},
    77  		{RestoreKeyWordUppercase, "KEY`.'\"WORD\\ str`.'\"ing\\ na`.'\"Me\\"},
    78  		{RestoreKeyWordLowercase, "key`.'\"word\\ str`.'\"ing\\ na`.'\"Me\\"},
    79  		{RestoreNameUppercase, "key`.'\"Word\\ str`.'\"ing\\ NA`.'\"ME\\"},
    80  		{RestoreNameLowercase, "key`.'\"Word\\ str`.'\"ing\\ na`.'\"me\\"},
    81  		{RestoreNameDoubleQuotes, "key`.'\"Word\\ str`.'\"ing\\ \"na`.'\"\"Me\\\""},
    82  		{RestoreNameBackQuotes, "key`.'\"Word\\ str`.'\"ing\\ `na``.'\"Me\\`"},
    83  		{DefaultRestoreFlags, "KEY`.'\"WORD\\ 'str`.''\"ing\\' `na``.'\"Me\\`"},
    84  		{RestoreStringSingleQuotes | RestoreStringDoubleQuotes, "key`.'\"Word\\ 'str`.''\"ing\\' na`.'\"Me\\"},
    85  		{RestoreKeyWordUppercase | RestoreKeyWordLowercase, "KEY`.'\"WORD\\ str`.'\"ing\\ na`.'\"Me\\"},
    86  		{RestoreNameUppercase | RestoreNameLowercase, "key`.'\"Word\\ str`.'\"ing\\ NA`.'\"ME\\"},
    87  		{RestoreNameDoubleQuotes | RestoreNameBackQuotes, "key`.'\"Word\\ str`.'\"ing\\ \"na`.'\"\"Me\\\""},
    88  	}
    89  	var sb strings.Builder
    90  	for _, testCase := range testCases {
    91  		sb.Reset()
    92  		ctx := NewRestoreCtx(testCase.flag, &sb)
    93  		ctx.WriteKeyWord("key`.'\"Word\\")
    94  		ctx.WritePlain(" ")
    95  		ctx.WriteString("str`.'\"ing\\")
    96  		ctx.WritePlain(" ")
    97  		ctx.WriteName("na`.'\"Me\\")
    98  		c.Assert(sb.String(), Equals, testCase.expect, Commentf("case: %#v", testCase))
    99  	}
   100  }