forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
39 lines (32 loc) · 841 Bytes
/
format_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package arrutil_test
import (
"bytes"
"fmt"
"testing"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestNewFormatter(t *testing.T) {
arr := [2]string{"a", "b"}
str := arrutil.FormatIndent(arr, " ")
assert.Contains(t, str, "\n ")
fmt.Println(str)
str = arrutil.FormatIndent(arr, "")
assert.NotContains(t, str, "\n ")
assert.Eq(t, "[a, b]", str)
fmt.Println(str)
assert.Eq(t, "", arrutil.FormatIndent("invalid", ""))
assert.Eq(t, "[]", arrutil.FormatIndent([]string{}, ""))
sl := []string{"c", "d"}
f := arrutil.NewFormatter(sl)
f.WithFn(func(f *arrutil.ArrFormatter) {
f.Indent = " "
f.ClosePrefix = "-"
})
str = f.String()
assert.Eq(t, "[\n c,\n d\n-]", str)
f = arrutil.NewFormatter(sl)
w := &bytes.Buffer{}
f.FormatTo(w)
assert.Eq(t, "[c, d]", w.String())
}