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
|
package report
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewTemplate(t *testing.T) {
actual := NewTemplate("TestNewTemplate")
assert.Equal(t, false, actual.isTable)
}
type testStruct struct {
FieldA bool // camel case test
Fieldb bool // no camel case
fieldC bool // nolint // private field
fieldd bool // nolint // private field
}
func TestHeadersSimple(t *testing.T) {
expected := []map[string]string{{
"FieldA": "FIELD A",
"Fieldb": "FIELDB",
"fieldC": "FIELD C",
"fieldd": "FIELDD",
}}
assert.Equal(t, expected, Headers(testStruct{}, nil))
}
func TestHeadersOverride(t *testing.T) {
expected := []map[string]string{{
"FieldA": "FIELD A",
"Fieldb": "FIELD B",
"fieldC": "FIELD C",
"fieldd": "FIELD D",
}}
assert.Equal(t, expected, Headers(testStruct{}, map[string]string{
"Fieldb": "field b",
"fieldd": "field d",
}))
}
func TestNormalizeFormat(t *testing.T) {
testCase := []struct {
input string
expected string
}{
{"{{.ID}}\t{{.ID}}", "{{.ID}}\t{{.ID}}\n"},
{`{{.ID}}\t{{.ID}}`, "{{.ID}}\t{{.ID}}\n"},
{`{{.ID}} {{.ID}}`, "{{.ID}} {{.ID}}\n"},
{`table {{.ID}}\t{{.ID}}`, "{{.ID}}\t{{.ID}}\n"},
{`table {{.ID}} {{.ID}}`, "{{.ID}}\t{{.ID}}\n"},
}
for _, tc := range testCase {
tc := tc
t.Run(tc.input, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expected, NormalizeFormat(tc.input))
})
}
}
func TestTemplate_Parse(t *testing.T) {
testCase := []string{
"table {{.ID}}",
"table {{ .ID}}",
"table {{ .ID}}\n",
"{{range .}}{{.ID}}{{end}}",
`{{range .}}{{.ID}}{{end}}`,
}
var buf bytes.Buffer
for _, tc := range testCase {
tc := tc
t.Run(tc, func(t *testing.T) {
tmpl, e := NewTemplate("TestTemplate").Parse(tc)
assert.NoError(t, e)
err := tmpl.Execute(&buf, [...]map[string]string{{
"ID": "Ident",
}})
assert.NoError(t, err)
assert.Equal(t, "Ident\n", buf.String())
})
buf.Reset()
}
}
func TestTemplate_IsTable(t *testing.T) {
tmpl, e := NewTemplate("TestTemplate").Parse("table {{.ID}}")
assert.NoError(t, e)
assert.True(t, tmpl.isTable)
}
func TestTemplate_trim(t *testing.T) {
tmpl := NewTemplate("TestTemplate")
tmpl, e := tmpl.Funcs(FuncMap{"trim": strings.TrimSpace}).Parse("{{.ID |trim}}")
assert.NoError(t, e)
var buf bytes.Buffer
err := tmpl.Execute(&buf, map[string]string{
"ID": "ident ",
})
assert.NoError(t, err)
assert.Equal(t, "ident\n", buf.String())
}
func TestTemplate_DefaultFuncs(t *testing.T) {
tmpl := NewTemplate("TestTemplate")
// Throw in trim function to ensure default 'join' is still available
tmpl, e := tmpl.Funcs(FuncMap{"trim": strings.TrimSpace}).Parse(`{{join .ID "\n"}}`)
assert.NoError(t, e)
var buf bytes.Buffer
err := tmpl.Execute(&buf, map[string][]string{
"ID": {"ident1", "ident2", "ident3"},
})
assert.NoError(t, err)
assert.Equal(t, "ident1\nident2\nident3\n", buf.String())
}
func TestTemplate_ReplaceFuncs(t *testing.T) {
tmpl := NewTemplate("TestTemplate")
// yes, we're overriding upper with lower :-)
tmpl, e := tmpl.Funcs(FuncMap{"upper": strings.ToLower}).Parse(`{{.ID | lower}}`)
assert.NoError(t, e)
var buf bytes.Buffer
err := tmpl.Execute(&buf, map[string]string{
"ID": "IDENT",
})
assert.NoError(t, err)
assert.Equal(t, "ident\n", buf.String())
}
func TestTemplate_json(t *testing.T) {
tmpl := NewTemplate("TestTemplate")
// yes, we're overriding upper with lower :-)
tmpl, e := tmpl.Parse(`{{json .ID}}`)
assert.NoError(t, e)
var buf bytes.Buffer
err := tmpl.Execute(&buf, map[string][]string{
"ID": {"ident1", "ident2", "ident3"},
})
assert.NoError(t, err)
assert.Equal(t, `["ident1","ident2","ident3"]`+"\n", buf.String())
}
|