aboutsummaryrefslogtreecommitdiff
path: root/acid_test.go
blob: e8bd5c2a7cbd8fc2c89614e4a522138a01923880 (plain)
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
package main

import (
	"bytes"
	"testing"
	ttemplate "text/template"
	"time"
)

func TestTemplateQuote(t *testing.T) {
	// Ideally, we should back-parse it using sh syntax.
	// This is an unnecessarily fragile test.
	for _, test := range []struct {
		input, output string
	}{
		{`!!'!$`, `'!!'\''!$'`},
		{``, `""`},
		{`${var}`, `"\${var}"`},
		{"`cat`", "\"\\`cat\\`\""},
		{`"魚\"`, `"\"魚\\\""`},
	} {
		var b bytes.Buffer
		err := ttemplate.Must(ttemplate.New("test").
			Funcs(shellFuncs).Parse("{{quote .}}")).Execute(&b, test.input)
		if err != nil {
			t.Errorf("template execution error: %s\n", err)
		}
		if b.String() != test.output {
			t.Errorf("%q should be quoted os %q, not %q\n",
				test.input, test.output, b.String())
		}
	}
}

func TestShortDurationString(t *testing.T) {
	for _, test := range []struct {
		d      time.Duration
		expect string
	}{
		{72 * time.Hour, "3d"},
		{-3 * time.Hour, "-3h"},
		{12 * time.Minute, "12m"},
		{time.Millisecond, "0s"},
	} {
		if sd := shortDurationString(test.d); sd != test.expect {
			t.Errorf("%s = %s; want %s\n", test.d, sd, test.expect)
		}
	}
}