github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/format/format_test.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 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 // Copyright 2015 PingCAP, Inc. 16 // 17 // Licensed under the Apache License, Version 2.0 (the "License"); 18 // you may not use this file except in compliance with the License. 19 // You may obtain a copy of the License at 20 // 21 // http://www.apache.org/licenses/LICENSE-2.0 22 // 23 // Unless required by applicable law or agreed to in writing, software 24 // distributed under the License is distributed on an "AS IS" BASIS, 25 // See the License for the specific language governing permissions and 26 // limitations under the License. 27 28 package format 29 30 import ( 31 "bytes" 32 "io/ioutil" 33 "strings" 34 "testing" 35 36 "github.com/stretchr/testify/require" 37 ) 38 39 func checkFormat(t *testing.T, f Formatter, buf *bytes.Buffer, str, expect string) { 40 _, err := f.Format(str, 3) 41 require.NoError(t, err) 42 b, err := ioutil.ReadAll(buf) 43 require.NoError(t, err) 44 require.Equal(t, expect, string(b)) 45 } 46 47 func TestFormat(t *testing.T) { 48 t.Parallel() 49 str := "abc%d%%e%i\nx\ny\n%uz\n" 50 buf := &bytes.Buffer{} 51 f := IndentFormatter(buf, "\t") 52 expect := `abc3%e 53 x 54 y 55 z 56 ` 57 checkFormat(t, f, buf, str, expect) 58 59 str = "abc%d%%e%i\nx\ny\n%uz\n%i\n" 60 buf = &bytes.Buffer{} 61 f = FlatFormatter(buf) 62 expect = "abc3%e x y z\n " 63 checkFormat(t, f, buf, str, expect) 64 } 65 66 func TestRestoreCtx(t *testing.T) { 67 t.Parallel() 68 testCases := []struct { 69 flag RestoreFlags 70 expect string 71 }{ 72 {0, "key`.'\"Word\\ str`.'\"ing\\ na`.'\"Me\\"}, 73 {RestoreStringSingleQuotes, "key`.'\"Word\\ 'str`.''\"ing\\' na`.'\"Me\\"}, 74 {RestoreStringDoubleQuotes, "key`.'\"Word\\ \"str`.'\"\"ing\\\" na`.'\"Me\\"}, 75 {RestoreStringEscapeBackslash, "key`.'\"Word\\ str`.'\"ing\\\\ na`.'\"Me\\"}, 76 {RestoreKeyWordUppercase, "KEY`.'\"WORD\\ str`.'\"ing\\ na`.'\"Me\\"}, 77 {RestoreKeyWordLowercase, "key`.'\"word\\ str`.'\"ing\\ na`.'\"Me\\"}, 78 {RestoreNameUppercase, "key`.'\"Word\\ str`.'\"ing\\ NA`.'\"ME\\"}, 79 {RestoreNameLowercase, "key`.'\"Word\\ str`.'\"ing\\ na`.'\"me\\"}, 80 {RestoreNameDoubleQuotes, "key`.'\"Word\\ str`.'\"ing\\ \"na`.'\"\"Me\\\""}, 81 {RestoreNameBackQuotes, "key`.'\"Word\\ str`.'\"ing\\ `na``.'\"Me\\`"}, 82 {DefaultRestoreFlags, "KEY`.'\"WORD\\ 'str`.''\"ing\\' `na``.'\"Me\\`"}, 83 {RestoreStringSingleQuotes | RestoreStringDoubleQuotes, "key`.'\"Word\\ 'str`.''\"ing\\' na`.'\"Me\\"}, 84 {RestoreKeyWordUppercase | RestoreKeyWordLowercase, "KEY`.'\"WORD\\ str`.'\"ing\\ na`.'\"Me\\"}, 85 {RestoreNameUppercase | RestoreNameLowercase, "key`.'\"Word\\ str`.'\"ing\\ NA`.'\"ME\\"}, 86 {RestoreNameDoubleQuotes | RestoreNameBackQuotes, "key`.'\"Word\\ str`.'\"ing\\ \"na`.'\"\"Me\\\""}, 87 } 88 var sb strings.Builder 89 for _, testCase := range testCases { 90 sb.Reset() 91 ctx := NewRestoreCtx(testCase.flag, &sb) 92 ctx.WriteKeyWord("key`.'\"Word\\") 93 ctx.WritePlain(" ") 94 ctx.WriteString("str`.'\"ing\\") 95 ctx.WritePlain(" ") 96 ctx.WriteName("na`.'\"Me\\") 97 require.Equalf(t, testCase.expect, sb.String(), "case: %#v", testCase) 98 } 99 } 100 101 func TestRestoreSpecialComment(t *testing.T) { 102 t.Parallel() 103 var sb strings.Builder 104 sb.Reset() 105 ctx := NewRestoreCtx(RestoreTiDBSpecialComment, &sb) 106 ctx.WriteWithSpecialComments("fea_id", func() { 107 ctx.WritePlain("content") 108 }) 109 require.Equal(t, "/*T![fea_id] content */", sb.String()) 110 111 sb.Reset() 112 ctx.WriteWithSpecialComments("", func() { 113 ctx.WritePlain("shard_row_id_bits") 114 }) 115 require.Equal(t, "/*T! shard_row_id_bits */", sb.String()) 116 }