github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/model/string_test.go (about)

     1  // Copyright 2020 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 model
    15  
    16  import (
    17  	"github.com/pingcap/check"
    18  	"github.com/pingcap/ticdc/pkg/util/testleak"
    19  )
    20  
    21  type stringSuite struct{}
    22  
    23  var _ = check.Suite(&stringSuite{})
    24  
    25  func (s *stringSuite) TestHolderString(c *check.C) {
    26  	defer testleak.AfterTest(c)()
    27  	testCases := []struct {
    28  		count    int
    29  		expected string
    30  	}{
    31  		{1, "?"},
    32  		{2, "?,?"},
    33  		{10, "?,?,?,?,?,?,?,?,?,?"},
    34  	}
    35  	for _, tc := range testCases {
    36  		s := HolderString(tc.count)
    37  		c.Assert(s, check.Equals, tc.expected)
    38  	}
    39  	// test invalid input
    40  	c.Assert(func() { HolderString(0) }, check.Panics, "strings.Builder.Grow: negative count")
    41  	c.Assert(func() { HolderString(-1) }, check.Panics, "strings.Builder.Grow: negative count")
    42  }
    43  
    44  func (s *stringSuite) TestExtractKeySuffix(c *check.C) {
    45  	defer testleak.AfterTest(c)()
    46  	testCases := []struct {
    47  		input  string
    48  		expect string
    49  		hasErr bool
    50  	}{
    51  		{"/tidb/cdc/capture/info/6a6c6dd290bc8732", "6a6c6dd290bc8732", false},
    52  		{"/tidb/cdc/capture/info/6a6c6dd290bc8732/", "", false},
    53  		{"/tidb/cdc", "cdc", false},
    54  		{"/tidb", "tidb", false},
    55  		{"", "", true},
    56  	}
    57  	for _, tc := range testCases {
    58  		key, err := ExtractKeySuffix(tc.input)
    59  		if tc.hasErr {
    60  			c.Assert(err, check.NotNil)
    61  		} else {
    62  			c.Assert(err, check.IsNil)
    63  			c.Assert(key, check.Equals, tc.expect)
    64  		}
    65  	}
    66  }