package filter

import (
	"testing"

	"github.com/flosch/pongo2"

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

func TestMarkdownFilter(t *testing.T) {
	Convey("set context", t, func() {
		ctx := pongo2.Context{
			"testvar":  "# test",
			"testcode": "```sh\ntest=test\n```",
		}
		Convey("parse template", func() {
			output, err := pongo2.RenderTemplateString("{{ testvar|markdown }}", ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, "<h1>test</h1>\n")

			output, err = pongo2.RenderTemplateString("{{ testcode|markdown }}", ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, `<pre><code class="language-sh">test=test
</code></pre>
`)

			output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"s=monokai" }}`, ctx)
			So(err, ShouldBeNil)
			So(output, ShouldEqual, `<pre style="color:#f8f8f2;background-color:#272822">test<span style="color:#f92672">=</span>test
</pre>`)

			output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"s=" }}`, ctx)
			So(output, ShouldBeEmpty)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "need a syntax sheme name for parameter")

			output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"test=test" }}`, ctx)
			So(output, ShouldBeEmpty)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "unknown parameter")

			output, err = pongo2.RenderTemplateString(`{{ testcode|markdown:"s=monokai,test" }}`, ctx)
			So(output, ShouldBeEmpty)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "invalid parameter: test")
		})
	})
}
