forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings_test.go
196 lines (158 loc) · 4.12 KB
/
strings_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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package funcs
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestReplaceAll(t *testing.T) {
sf := &StringFuncs{}
assert.Equal(t, "Replaced",
sf.ReplaceAll("Orig", "Replaced", "Orig"))
assert.Equal(t, "ReplacedReplaced",
sf.ReplaceAll("Orig", "Replaced", "OrigOrig"))
}
func TestIndent(t *testing.T) {
sf := &StringFuncs{}
testdata := []struct {
args []interface{}
out string
}{
{[]interface{}{"foo\nbar\nbaz"}, " foo\n bar\n baz"},
{[]interface{}{" ", "foo\nbar\nbaz"}, " foo\n bar\n baz"},
{[]interface{}{3, "-", "foo\nbar\nbaz"}, "---foo\n---bar\n---baz"},
{[]interface{}{3, "foo\nbar\nbaz"}, " foo\n bar\n baz"},
}
for _, d := range testdata {
out, err := sf.Indent(d.args...)
assert.NoError(t, err)
assert.Equal(t, d.out, out)
}
}
func TestTrimPrefix(t *testing.T) {
sf := &StringFuncs{}
assert.Equal(t, "Bar",
sf.TrimPrefix("Foo", "FooBar"))
}
func TestTrunc(t *testing.T) {
sf := &StringFuncs{}
assert.Equal(t, "", sf.Trunc(5, ""))
assert.Equal(t, "", sf.Trunc(0, nil))
assert.Equal(t, "123", sf.Trunc(3, 123456789))
assert.Equal(t, "hello, world", sf.Trunc(-1, "hello, world"))
}
func TestAbbrev(t *testing.T) {
sf := &StringFuncs{}
_, err := sf.Abbrev()
assert.Error(t, err)
_, err = sf.Abbrev("foo")
assert.Error(t, err)
s, err := sf.Abbrev(3, "foo")
assert.NoError(t, err)
assert.Equal(t, "foo", s)
s, err = sf.Abbrev(2, 6, "foobar")
assert.NoError(t, err)
assert.Equal(t, "foobar", s)
s, err = sf.Abbrev(6, 9, "foobarbazquxquux")
assert.NoError(t, err)
assert.Equal(t, "...baz...", s)
}
func TestSlug(t *testing.T) {
sf := &StringFuncs{}
s := sf.Slug(nil)
assert.Equal(t, "nil", s)
s = sf.Slug(0)
assert.Equal(t, "0", s)
s = sf.Slug(1.85e-5)
assert.Equal(t, "1-85e-05", s)
s = sf.Slug("Hello, World!")
assert.Equal(t, "hello-world", s)
s = sf.Slug("[email protected]")
assert.Equal(t, "fooatexample-com", s)
s = sf.Slug("rock & roll!")
assert.Equal(t, "rock-and-roll", s)
s = sf.Slug("[email protected]")
assert.Equal(t, "fooatexample-com", s)
s = sf.Slug(`100%`)
assert.Equal(t, "100", s)
}
func TestSort(t *testing.T) {
sf := &StringFuncs{}
in := []string{"foo", "bar", "baz"}
out := []string{"bar", "baz", "foo"}
assert.Equal(t, out, must(sf.Sort(in)))
assert.Equal(t, out, must(sf.Sort([]interface{}{"foo", "bar", "baz"})))
}
func TestQuote(t *testing.T) {
sf := &StringFuncs{}
testdata := []struct {
in interface{}
out string
}{
{``, `""`},
{`foo`, `"foo"`},
{nil, `"nil"`},
{123.4, `"123.4"`},
{`hello "world"`, `"hello \"world\""`},
{`it's its`, `"it's its"`},
}
for _, d := range testdata {
assert.Equal(t, d.out, sf.Quote(d.in))
}
}
func TestShellQuote(t *testing.T) {
sf := &StringFuncs{}
testdata := []struct {
in interface{}
out string
}{
// conventional cases are covered in gompstrings.ShellQuote() tests
// we cover only cases that require type conversion or array/slice combining here
{nil, `'nil'`},
{123.4, `'123.4'`},
// array and slice cases
{[]string{}, ``},
{[]string{"", ""}, `'' ''`},
{[...]string{"one'two", "three four"}, `'one'"'"'two' 'three four'`},
{[]string{"one'two", "three four"}, `'one'"'"'two' 'three four'`},
}
for _, d := range testdata {
assert.Equal(t, d.out, sf.ShellQuote(d.in))
}
}
func TestSquote(t *testing.T) {
sf := &StringFuncs{}
testdata := []struct {
in interface{}
out string
}{
{``, `''`},
{`foo`, `'foo'`},
{nil, `'nil'`},
{123.4, `'123.4'`},
{`hello "world"`, `'hello "world"'`},
{`it's its`, `'it''s its'`},
}
for _, d := range testdata {
assert.Equal(t, d.out, sf.Squote(d.in))
}
}
func TestRuneCount(t *testing.T) {
sf := &StringFuncs{}
n, err := sf.RuneCount("")
assert.NoError(t, err)
assert.Equal(t, 0, n)
n, err = sf.RuneCount("foo")
assert.NoError(t, err)
assert.Equal(t, 3, n)
n, err = sf.RuneCount("foo", "bar")
assert.NoError(t, err)
assert.Equal(t, 6, n)
n, err = sf.RuneCount(42, true)
assert.NoError(t, err)
assert.Equal(t, 6, n)
n, err = sf.RuneCount("😂\U0001F602")
assert.NoError(t, err)
assert.Equal(t, 2, n)
n, err = sf.RuneCount("\U0001F600", 3.14)
assert.NoError(t, err)
assert.Equal(t, 5, n)
}