package filter

import (
	"math"
	"testing"

	"github.com/flosch/pongo2"

	. "github.com/smartystreets/goconvey/convey"
)

func TestJSONFilter(t *testing.T) {
	Convey("set context", t, func() {
		ctx := pongo2.Context{
			"teststr": "test",
			"testmap": map[string]interface{}{
				"float": 1.23,
				"int":   5,
				"str":   "test",
			},
			"testerr": math.Inf(1),
		}
		Convey("parse template", func() {
			output, err := pongo2.RenderTemplateString("{{ teststr|safe|json }}", ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, `"test"`)

			output, err = pongo2.RenderTemplateString("{{ testmap|safe|json }}", ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, `{"float":1.23,"int":5,"str":"test"}`)

			output, err = pongo2.RenderTemplateString(`{{ testmap|safe|json:"pretty" }}`, ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, `{
    "float": 1.23,
    "int": 5,
    "str": "test"
}`)

			output, err = pongo2.RenderTemplateString("{{ testnil|safe|json }}", ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, `null`)

			output, err = pongo2.RenderTemplateString("{{ testerr|safe|json }}", ctx)
			So(err, ShouldNotBeNil)
			So(output, ShouldEqual, ``)

		})
	})
}
