Debian Codegraph @

functions_test.go

raw
1 package sprig
2
3 import (
4 "bytes"
5 "fmt"
6 "math/rand"
7 "os"
8 "testing"
9 "text/template"
10 "time"
11
12 "github.com/stretchr/testify/assert"
13 )
14
15 func TestEnv(t *testing.T) {
16 os.Setenv("FOO", "bar")
17 tpl := `{{env "FOO"}}`
18 if err := runt(tpl, "bar"); err != nil {
19 t.Error(err)
20 }
21 }
22
23 func TestExpandEnv(t *testing.T) {
24 os.Setenv("FOO", "bar")
25 tpl := `{{expandenv "Hello $FOO"}}`
26 if err := runt(tpl, "Hello bar"); err != nil {
27 t.Error(err)
28 }
29 }
30
31 func TestBase(t *testing.T) {
32 assert.NoError(t, runt(`{{ base "foo/bar" }}`, "bar"))
33 }
34
35 func TestDir(t *testing.T) {
36 assert.NoError(t, runt(`{{ dir "foo/bar/baz" }}`, "foo/bar"))
37 }
38
39 func TestIsAbs(t *testing.T) {
40 assert.NoError(t, runt(`{{ isAbs "/foo" }}`, "true"))
41 assert.NoError(t, runt(`{{ isAbs "foo" }}`, "false"))
42 }
43
44 func TestClean(t *testing.T) {
45 assert.NoError(t, runt(`{{ clean "/foo/../foo/../bar" }}`, "/bar"))
46 }
47
48 func TestExt(t *testing.T) {
49 assert.NoError(t, runt(`{{ ext "/foo/bar/baz.txt" }}`, ".txt"))
50 }
51
52 func TestSnakeCase(t *testing.T) {
53 assert.NoError(t, runt(`{{ snakecase "FirstName" }}`, "first_name"))
54 assert.NoError(t, runt(`{{ snakecase "HTTPServer" }}`, "http_server"))
55 assert.NoError(t, runt(`{{ snakecase "NoHTTPS" }}`, "no_https"))
56 assert.NoError(t, runt(`{{ snakecase "GO_PATH" }}`, "go_path"))
57 assert.NoError(t, runt(`{{ snakecase "GO PATH" }}`, "go_path"))
58 assert.NoError(t, runt(`{{ snakecase "GO-PATH" }}`, "go_path"))
59 }
60
61 func TestCamelCase(t *testing.T) {
62 assert.NoError(t, runt(`{{ camelcase "http_server" }}`, "HttpServer"))
63 assert.NoError(t, runt(`{{ camelcase "_camel_case" }}`, "_CamelCase"))
64 assert.NoError(t, runt(`{{ camelcase "no_https" }}`, "NoHttps"))
65 assert.NoError(t, runt(`{{ camelcase "_complex__case_" }}`, "_Complex_Case_"))
66 assert.NoError(t, runt(`{{ camelcase "all" }}`, "All"))
67 }
68
69 func TestKebabCase(t *testing.T) {
70 assert.NoError(t, runt(`{{ kebabcase "FirstName" }}`, "first-name"))
71 assert.NoError(t, runt(`{{ kebabcase "HTTPServer" }}`, "http-server"))
72 assert.NoError(t, runt(`{{ kebabcase "NoHTTPS" }}`, "no-https"))
73 assert.NoError(t, runt(`{{ kebabcase "GO_PATH" }}`, "go-path"))
74 assert.NoError(t, runt(`{{ kebabcase "GO PATH" }}`, "go-path"))
75 assert.NoError(t, runt(`{{ kebabcase "GO-PATH" }}`, "go-path"))
76 }
77
78 func TestShuffle(t *testing.T) {
79 defer rand.Seed(time.Now().UnixNano())
80 rand.Seed(1)
81 // Because we're using a random number generator, we need these to go in
82 // a predictable sequence:
83 assert.NoError(t, runt(`{{ shuffle "Hello World" }}`, "rldo HWlloe"))
84 }
85
86 func TestRegex(t *testing.T) {
87 assert.NoError(t, runt(`{{ regexQuoteMeta "1.2.3" }}`, "1\\.2\\.3"))
88 assert.NoError(t, runt(`{{ regexQuoteMeta "pretzel" }}`, "pretzel"))
89 }
90
91 // runt runs a template and checks that the output exactly matches the expected string.
92 func runt(tpl, expect string) error {
93 return runtv(tpl, expect, map[string]string{})
94 }
95
96 // runtv takes a template, and expected return, and values for substitution.
97 //
98 // It runs the template and verifies that the output is an exact match.
99 func runtv(tpl, expect string, vars interface{}) error {
100 fmap := TxtFuncMap()
101 t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))
102 var b bytes.Buffer
103 err := t.Execute(&b, vars)
104 if err != nil {
105 return err
106 }
107 if expect != b.String() {
108 return fmt.Errorf("Expected '%s', got '%s'", expect, b.String())
109 }
110 return nil
111 }
112
113 // runRaw runs a template with the given variables and returns the result.
114 func runRaw(tpl string, vars interface{}) (string, error) {
115 fmap := TxtFuncMap()
116 t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))
117 var b bytes.Buffer
118 err := t.Execute(&b, vars)
119 if err != nil {
120 return "", err
121 }
122 return b.String(), nil
123 }