github.com/Nigel2392/go-datastructures@v1.1.5/stringbuilder/strinbuilder_test.go (about)

     1  package stringbuilder_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/Nigel2392/go-datastructures/stringbuilder"
     8  )
     9  
    10  func TestStringBuilder(t *testing.T) {
    11  	var sb = stringbuilder.StringBuilder{}
    12  	sb.Append("Hello")
    13  	sb.Append(" ")
    14  	sb.Append("World")
    15  	sb.Prepend(" ")
    16  	sb.Prepend("World")
    17  	sb.Prepend(" ")
    18  	sb.Prepend("Hello")
    19  
    20  	if sb.String() != "Hello World Hello World" {
    21  		t.Fatal("Expected \"Hello World World Hello\" but got ", sb.String())
    22  	}
    23  
    24  	if sb.Len() != len("Hello World Hello World") {
    25  		t.Fatal("Expected length ", len("Hello World Hello World"), " but got ", sb.Len())
    26  	}
    27  }
    28  
    29  func TestStringBuilder2(t *testing.T) {
    30  	var sb = stringbuilder.StringBuilder{}
    31  	sb.WriteString("Hello")
    32  	sb.WriteString(" ")
    33  	sb.WriteString("World")
    34  	sb.WriteString(" ")
    35  	sb.WriteString("Hello")
    36  	sb.WriteString(" ")
    37  	sb.WriteString("World")
    38  
    39  	var regularSB = strings.Builder{}
    40  	var written, err = sb.WriteTo(&regularSB)
    41  	if err != nil {
    42  		t.Fatal("Error executing stringbuilder.WriteTo: ", err)
    43  	}
    44  
    45  	if written != int64(len("Hello World Hello World")) {
    46  		t.Fatal("Expected ", len("Hello World Hello World"), " bytes written but got ", written)
    47  	}
    48  }
    49  
    50  func TestReset(t *testing.T) {
    51  	sb := stringbuilder.StringBuilder{}
    52  	sb.Append("Hello")
    53  	sb.Append(" ")
    54  	sb.Append("World")
    55  	sb.Reset()
    56  
    57  	if sb.String() != "" {
    58  		t.Fatal("Expected empty string but got ", sb.String())
    59  	}
    60  
    61  	if sb.Len() != 0 {
    62  		t.Fatal("Expected length 0 but got ", sb.Len())
    63  	}
    64  }