From 5cc4b9d001e2f4e47fc92ffed004158cdf2a9411 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Tue, 19 Mar 2019 21:25:14 +0100 Subject: [PATCH] added tests --- .gitignore | 5 +++-- build/ci/.drone.yml | 13 ++++++++++- pkg/filter/dump_test.go | 24 ++++++++++++++++++++ pkg/filter/json_test.go | 50 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 pkg/filter/dump_test.go create mode 100644 pkg/filter/json_test.go diff --git a/.gitignore b/.gitignore index dcc0cc6..ac0f37b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -html/ -build/dist \ No newline at end of file +/html/ +/build/dist +/coverage.out diff --git a/build/ci/.drone.yml b/build/ci/.drone.yml index 41d3713..db5e62e 100644 --- a/build/ci/.drone.yml +++ b/build/ci/.drone.yml @@ -6,7 +6,7 @@ workspace: path: src/gitbase.de/apairon/mark2web steps: - - name: build for linux + - name: test image: golang:latest environment: CGO_ENABLED: 0 @@ -15,6 +15,17 @@ steps: commands: - git submodule update --init --recursive - git fetch --tags + - go test -v -coverprofile coverage.out ./pkg/* + when: + event: [ push, tag ] + + - name: build for linux + image: golang:latest + environment: + CGO_ENABLED: 0 + GOOS: linux + GOARCH: amd64 + commands: - scripts/build.sh when: event: [ push, tag ] diff --git a/pkg/filter/dump_test.go b/pkg/filter/dump_test.go new file mode 100644 index 0000000..4925345 --- /dev/null +++ b/pkg/filter/dump_test.go @@ -0,0 +1,24 @@ +package filter + +import ( + "testing" + + "github.com/davecgh/go-spew/spew" + + "github.com/flosch/pongo2" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestDumpFilter(t *testing.T) { + Convey("set context", t, func() { + ctx := pongo2.Context{ + "testvar": "test", + } + Convey("parse template", func() { + output, err := pongo2.RenderTemplateString("{{ testvar|safe|dump }}", ctx) + So(err, ShouldBeNil) + So(output, ShouldEqual, spew.Sdump("test")) + }) + }) +} diff --git a/pkg/filter/json_test.go b/pkg/filter/json_test.go new file mode 100644 index 0000000..2d1bcbe --- /dev/null +++ b/pkg/filter/json_test.go @@ -0,0 +1,50 @@ +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, ``) + + }) + }) +}