github.com/cloudwego/kitex@v0.9.0/pkg/utils/strings_test.go (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package utils
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/cloudwego/kitex/internal/test"
    24  )
    25  
    26  func TestStringBuilder(t *testing.T) {
    27  	sb := &StringBuilder{}
    28  	sb.Grow(4)
    29  	test.Assert(t, sb.Cap() == 4)
    30  	test.Assert(t, sb.Len() == 0)
    31  	sb.WriteString("1")
    32  	sb.WriteByte('2')
    33  	sb.WriteRune(rune('3'))
    34  	sb.Write([]byte("4"))
    35  	test.Assert(t, sb.Cap() == 4)
    36  	test.Assert(t, sb.Len() == 4)
    37  	test.Assert(t, sb.String() == "1234")
    38  	sb.Reset()
    39  	test.Assert(t, sb.String() == "")
    40  	test.Assert(t, sb.Cap() == 0)
    41  	test.Assert(t, sb.Len() == 0)
    42  
    43  	sb.WithLocked(func(sb *strings.Builder) error {
    44  		sb.Grow(4)
    45  		test.Assert(t, sb.Cap() == 4)
    46  		test.Assert(t, sb.Len() == 0)
    47  		sb.WriteString("1")
    48  		sb.WriteByte('2')
    49  		sb.WriteRune(rune('3'))
    50  		sb.Write([]byte("4"))
    51  		test.Assert(t, sb.Cap() == 4)
    52  		test.Assert(t, sb.Len() == 4)
    53  		test.Assert(t, sb.String() == "1234")
    54  		sb.Reset()
    55  		test.Assert(t, sb.String() == "")
    56  		test.Assert(t, sb.Cap() == 0)
    57  		test.Assert(t, sb.Len() == 0)
    58  		return nil
    59  	})
    60  }