add better generate

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-03-20 01:33:56 -04:00
parent 3fc6abf56b
commit cdd93563f5
5655 changed files with 1187011 additions and 392 deletions

View file

@ -0,0 +1,84 @@
package templates
import (
"bytes"
"encoding/json"
"strings"
"text/template"
)
// basicFunctions are the set of initial
// functions provided to every template.
var basicFunctions = template.FuncMap{
"json": func(v interface{}) string {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.Encode(v)
// Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String())
},
"split": strings.Split,
"join": strings.Join,
"title": strings.Title,
"lower": strings.ToLower,
"upper": strings.ToUpper,
"pad": padWithSpace,
"truncate": truncateWithLength,
}
// HeaderFunctions are used to created headers of a table.
// This is a replacement of basicFunctions for header generation
// because we want the header to remain intact.
// Some functions like `split` are irrelevant so not added.
var HeaderFunctions = template.FuncMap{
"json": func(v string) string {
return v
},
"title": func(v string) string {
return v
},
"lower": func(v string) string {
return v
},
"upper": func(v string) string {
return v
},
"truncate": func(v string, _ int) string {
return v
},
}
// Parse creates a new anonymous template with the basic functions
// and parses the given format.
func Parse(format string) (*template.Template, error) {
return NewParse("", format)
}
// New creates a new empty template with the provided tag and built-in
// template functions.
func New(tag string) *template.Template {
return template.New(tag).Funcs(basicFunctions)
}
// NewParse creates a new tagged template with the basic functions
// and parses the given format.
func NewParse(tag, format string) (*template.Template, error) {
return New(tag).Parse(format)
}
// padWithSpace adds whitespace to the input if the input is non-empty
func padWithSpace(source string, prefix, suffix int) string {
if source == "" {
return source
}
return strings.Repeat(" ", prefix) + source + strings.Repeat(" ", suffix)
}
// truncateWithLength truncates the source string up to the length provided by the input
func truncateWithLength(source string, length int) string {
if len(source) < length {
return source
}
return source[:length]
}

View file

@ -0,0 +1,89 @@
package templates
import (
"bytes"
"testing"
"github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp"
)
// GitHub #32120
func TestParseJSONFunctions(t *testing.T) {
tm, err := Parse(`{{json .Ports}}`)
assert.NilError(t, err)
var b bytes.Buffer
assert.NilError(t, tm.Execute(&b, map[string]string{"Ports": "0.0.0.0:2->8/udp"}))
want := "\"0.0.0.0:2->8/udp\""
assert.Check(t, is.Equal(want, b.String()))
}
func TestParseStringFunctions(t *testing.T) {
tm, err := Parse(`{{join (split . ":") "/"}}`)
assert.NilError(t, err)
var b bytes.Buffer
assert.NilError(t, tm.Execute(&b, "text:with:colon"))
want := "text/with/colon"
assert.Check(t, is.Equal(want, b.String()))
}
func TestNewParse(t *testing.T) {
tm, err := NewParse("foo", "this is a {{ . }}")
assert.NilError(t, err)
var b bytes.Buffer
assert.NilError(t, tm.Execute(&b, "string"))
want := "this is a string"
assert.Check(t, is.Equal(want, b.String()))
}
func TestParseTruncateFunction(t *testing.T) {
source := "tupx5xzf6hvsrhnruz5cr8gwp"
testCases := []struct {
template string
expected string
}{
{
template: `{{truncate . 5}}`,
expected: "tupx5",
},
{
template: `{{truncate . 25}}`,
expected: "tupx5xzf6hvsrhnruz5cr8gwp",
},
{
template: `{{truncate . 30}}`,
expected: "tupx5xzf6hvsrhnruz5cr8gwp",
},
{
template: `{{pad . 3 3}}`,
expected: " tupx5xzf6hvsrhnruz5cr8gwp ",
},
}
for _, testCase := range testCases {
tm, err := Parse(testCase.template)
assert.NilError(t, err)
t.Run("Non Empty Source Test with template: "+testCase.template, func(t *testing.T) {
var b bytes.Buffer
assert.NilError(t, tm.Execute(&b, source))
assert.Check(t, is.Equal(testCase.expected, b.String()))
})
t.Run("Empty Source Test with template: "+testCase.template, func(t *testing.T) {
var c bytes.Buffer
assert.NilError(t, tm.Execute(&c, ""))
assert.Check(t, is.Equal("", c.String()))
})
t.Run("Nil Source Test with template: "+testCase.template, func(t *testing.T) {
var c bytes.Buffer
assert.Check(t, tm.Execute(&c, nil) != nil)
assert.Check(t, is.Equal("", c.String()))
})
}
}