-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.go
More file actions
143 lines (112 loc) · 2.86 KB
/
builder_test.go
File metadata and controls
143 lines (112 loc) · 2.86 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package strlang
import (
"testing"
)
func TestBuilder_Block(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.Block("if true {", func() {
b.WriteStringln("return false")
}, "}")
expected := "" +
"if true {\n" +
" return false\n" +
"}\n"
return t, expected, b.String()
})
}
func TestBuilder_Block_Nested(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.Block("if a {", func() {
b.Block("if b {", func() {
b.WriteStringln("return c")
}, "}")
}, "}")
expected := "" +
"if a {\n" +
" if b {\n" +
" return c\n" +
" }\n" +
"}\n"
return t, expected, b.String()
})
}
func TestBuilder_WriteStringln(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteStringln("Hello, world!", 2)
b.WriteStringln("It's a test :)")
expected := "" +
"Hello, world!\n\n" +
"It's a test :)\n"
return t, expected, b.String()
})
}
func TestBuilder_Indent(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteStringln("Hello, world!")
b.Indent()
b.WriteStringln("==>")
b.Indent(2)
b.WriteStringln("==>")
expected := "" +
"Hello, world!\n" +
" ==>\n" +
" ==>\n"
return t, expected, b.String()
})
}
func TestBuilder_StripIndent(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteStringln("Hello, world!")
b.Indent(5)
b.WriteStringln("==>")
b.StripIndent()
b.WriteStringln("==>")
b.StripIndent(2)
b.WriteStringln("==>")
expected := "" +
"Hello, world!\n" +
" ==>\n" +
" ==>\n" +
" ==>\n"
return t, expected, b.String()
})
}
func TestBuilder_TrimLeft(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteStringln(">>>>>>>>>>Hello, world!")
b.TrimLeft(">")
expected := "Hello, world!\n"
return t, expected, b.String()
})
}
func TestBuilder_TrimRight(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteStringln("Hello, world!", 30)
b.TrimRight("\n")
expected := "Hello, world!"
return t, expected, b.String()
})
}
func TestBuilder_WriteNoIdentString(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteNoIdentString("John Doe: ")
b.WriteStringln("Hello, world!")
expected := "John Doe: Hello, world!\n"
return t, expected, b.String()
})
}
func TestBuilder_WriteString(t *testing.T) {
testBuilderInit(func(b *Builder) (*testing.T, string, string) {
b.WriteString("Hello, world!")
expected := "Hello, world!"
return t, expected, b.String()
})
}
func testBuilderInit(fn func(b *Builder) (t *testing.T, expected, given string)) {
b := NewBuilder()
b.SetIndentChar(" ")
t, expected, given := fn(b)
if given != expected {
t.Errorf("Expected %q but got %q", expected, given)
}
}