github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/pkg/util/uitl_test.go (about)

     1  package util
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestSpliceErrors(t *testing.T) {
    11  	err1 := errors.New("this is error 1")
    12  	err2 := errors.New("this is error 2")
    13  	err3 := errors.New("this is error 3")
    14  
    15  	const head = "[\n"
    16  	var line1 = fmt.Sprintf("  %s\n", err1)
    17  	var line2 = fmt.Sprintf("  %s\n", err2)
    18  	var line3 = fmt.Sprintf("  %s\n", err3)
    19  	const tail = "]\n"
    20  
    21  	sliceOutput := SpliceErrors([]error{err1, err2, err3})
    22  	if strings.Index(sliceOutput, head) != 0 ||
    23  		strings.Index(sliceOutput, line1) != len(head) ||
    24  		strings.Index(sliceOutput, line2) != len(head+line1) ||
    25  		strings.Index(sliceOutput, line3) != len(head+line1+line2) ||
    26  		strings.Index(sliceOutput, tail) != len(head+line1+line2+line3) {
    27  		t.Error("the func format the multiple elements error slice unexpected")
    28  		return
    29  	}
    30  
    31  	if SpliceErrors([]error{}) != "" || SpliceErrors(nil) != "" {
    32  		t.Error("the func format the zero-length error slice unexpected")
    33  		return
    34  	}
    35  }