github.com/enetx/g@v1.0.80/tests/string_builder_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func TestBuilder(t *testing.T) {
    10  	builder := g.NewBuilder()
    11  
    12  	// Test Write
    13  	builder.Write(g.NewString("hello"))
    14  	expected := "hello"
    15  	if result := builder.String().Std(); result != expected {
    16  		t.Errorf("Write() = %s; want %s", result, expected)
    17  	}
    18  
    19  	// Test WriteBytes
    20  	builder.WriteBytes([]byte(" world"))
    21  	expected = "hello world"
    22  	if result := builder.String().Std(); result != expected {
    23  		t.Errorf("WriteBytes() = %s; want %s", result, expected)
    24  	}
    25  
    26  	// Test WriteByte
    27  	builder.WriteByte('!')
    28  	expected = "hello world!"
    29  	if result := builder.String().Std(); result != expected {
    30  		t.Errorf("WriteByte() = %s; want %s", result, expected)
    31  	}
    32  
    33  	// Test WriteRune
    34  	builder.WriteRune('👋')
    35  	expected = "hello world!👋"
    36  	if result := builder.String().Std(); result != expected {
    37  		t.Errorf("WriteRune() = %s; want %s", result, expected)
    38  	}
    39  
    40  	// Test Len
    41  	if result := builder.Len().Std(); result != len(expected) {
    42  		t.Errorf("Len() = %d; want %d", result, len(expected))
    43  	}
    44  
    45  	// Test Reset
    46  	builder.Reset()
    47  	if result := builder.Len(); result != 0 {
    48  		t.Errorf("After Reset, Len() = %d; want 0", result)
    49  	}
    50  }
    51  
    52  func TestBuilderGrow(t *testing.T) {
    53  	// Create a new Builder
    54  	builder := g.NewBuilder()
    55  
    56  	// Grow the builder
    57  	builder.Grow(10)
    58  
    59  	// Check if the capacity has been increased
    60  	expected := 10
    61  	if result := builder.Cap().Std(); result != expected {
    62  		t.Errorf("Grow(10) = %d; want %d", result, expected)
    63  	}
    64  }
    65  
    66  func TestBuilderCap(t *testing.T) {
    67  	// Create a new Builder
    68  	builder := g.NewBuilder()
    69  
    70  	// Check the initial capacity
    71  	expected := 0
    72  	if result := builder.Cap().Std(); result != expected {
    73  		t.Errorf("Initial Cap() = %d; want %d", result, expected)
    74  	}
    75  }