tpl: Extend Jsonify to support optional indent parameter

Fixes #5040
This commit is contained in:
Cameron Moore 2020-03-21 10:15:12 -05:00 committed by Bjørn Erik Pedersen
parent 7eba37ae9b
commit 1bc93021e3
3 changed files with 38 additions and 9 deletions

View file

@ -83,17 +83,22 @@ func TestJsonify(t *testing.T) {
ns := New()
for _, test := range []struct {
indent []interface{}
v interface{}
expect interface{}
}{
{[]string{"a", "b"}, template.HTML(`["a","b"]`)},
{tstNoStringer{}, template.HTML("{}")},
{nil, template.HTML("null")},
{nil, []string{"a", "b"}, template.HTML(`["a","b"]`)},
{[]interface{}{" "}, []string{"a", "b"}, template.HTML("[\n \"a\",\n \"b\"\n]")},
{nil, tstNoStringer{}, template.HTML("{}")},
{nil, nil, template.HTML("null")},
// errors
{math.NaN(), false},
{nil, math.NaN(), false},
{[]interface{}{tstNoStringer{}}, []string{"a", "b"}, false},
} {
result, err := ns.Jsonify(test.v)
args := append(test.indent, test.v)
result, err := ns.Jsonify(args...)
if b, ok := test.expect.(bool); ok && !b {
c.Assert(err, qt.Not(qt.IsNil))