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
|
package integration
import (
"fmt"
"math"
"strings"
"testing"
"gotest.tools/v3/assert"
)
func TestStrings_Indent(t *testing.T) {
inOutTest(t, `{{ strings.Indent " " "hello world" }}
{{ "hello\nmultiline\nworld" | indent 2 "-" }}
{{ "foo\nbar" | strings.Indent 2 }}
{{"hello\nworld" | strings.Indent 5 | strings.TrimSpace }}
`, ` hello world
--hello
--multiline
--world
foo
bar
hello
world
`)
}
func TestStrings_Repeat(t *testing.T) {
inOutTest(t, `ba{{ strings.Repeat 2 "na" }}`, `banana`)
// use MaxInt so we avoid a negative count on 32-bit systems
input := fmt.Sprintf(`ba{{ strings.Repeat %d "na" }}`, math.MaxInt)
_, _, err := cmd(t, "-i", input).run()
assert.ErrorContains(t, err, `too long: causes overflow`)
_, _, err = cmd(t, "-i", `ba{{ strings.Repeat -1 "na" }}`).run()
assert.ErrorContains(t, err, `negative count`)
}
func TestStrings_Slug(t *testing.T) {
inOutTest(t, `{{ strings.Slug "Hellö, Wôrld! Free @ last..." }}`, `hello-world-free-at-last`)
}
func TestStrings_CaseFuncs(t *testing.T) {
inOutTest(t, `{{ strings.ToLower "HELLO" }}
{{ strings.ToUpper "hello" }}
{{ strings.Title "is there anybody out there?" }}
{{ strings.Title "foo,bar᳇džaz"}}
`,
`hello
HELLO
Is There Anybody Out There?
Foo,Bar᳇Džaz
`)
inOutTest(t, `{{ strings.CamelCase "Hellö, Wôrld! Free @ last..." }}
{{ strings.SnakeCase "Hellö, Wôrld! Free @ last..." }}
{{ strings.KebabCase "Hellö, Wôrld! Free @ last..." }}`, `HellöWôrldFreeLast
Hellö_wôrld_free_last
Hellö-wôrld-free-last`)
}
func TestStrings_WordWrap(t *testing.T) {
out := `There shouldn't be any wrapping of long words or URLs because that would break
things very badly. To wit:
https://example.com/a/super-long/url/that-shouldnt-be?wrapped=for+fear+of#the-breaking-of-functionality
should appear on its own line, regardless of the desired word-wrapping width
that has been set.`
text := strings.ReplaceAll(out, "\n", " ")
in := `{{ print "` + text + `" | strings.WordWrap 80 }}`
inOutTest(t, in, out)
}
|