diff --git a/engine_test.go b/engine_test.go index 334d674..4a56258 100644 --- a/engine_test.go +++ b/engine_test.go @@ -66,6 +66,17 @@ func TestEngine_ParseAndRenderString_struct(t *testing.T) { require.Equal(t, "hello", str) } +func TestEngine_ParseAndRender_errors(t *testing.T) { + _, err := NewEngine().ParseAndRenderString("{{ syntax error }}", emptyBindings) + require.Error(t, err) + _, err = NewEngine().ParseAndRenderString("{% if %}", emptyBindings) + require.Error(t, err) + _, err = NewEngine().ParseAndRenderString("{% undefined_tag %}", emptyBindings) + require.Error(t, err) + _, err = NewEngine().ParseAndRenderString("{% a | undefined_filter %}", emptyBindings) + require.Error(t, err) +} + func BenchmarkEngine_Parse(b *testing.B) { engine := NewEngine() buf := new(bytes.Buffer) diff --git a/liquid.go b/liquid.go index 9f59d3a..c8843eb 100644 --- a/liquid.go +++ b/liquid.go @@ -24,6 +24,8 @@ type Bindings map[string]interface{} type Renderer func(render.Context) (string, error) // SourceError records an error with a source location and optional cause. +// +// SourceError does not depend on, but is compatible with, the causer interface of https://github.com/pkg/errors. type SourceError interface { error Cause() error diff --git a/liquid_test.go b/liquid_test.go deleted file mode 100644 index 23143ba..0000000 --- a/liquid_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package liquid - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestIsTemplateError(t *testing.T) { - _, err := NewEngine().ParseAndRenderString("{{ syntax error }}", emptyBindings) - require.Error(t, err) - _, err = NewEngine().ParseAndRenderString("{% if %}", emptyBindings) - require.Error(t, err) - _, err = NewEngine().ParseAndRenderString("{% undefined_tag %}", emptyBindings) - require.Error(t, err) - _, err = NewEngine().ParseAndRenderString("{% a | undefined_filter %}", emptyBindings) - require.Error(t, err) -} diff --git a/tags/include_tag_test.go b/tags/include_tag_test.go index 1698dba..cd4be29 100644 --- a/tags/include_tag_test.go +++ b/tags/include_tag_test.go @@ -11,23 +11,42 @@ import ( "github.com/stretchr/testify/require" ) -var includeTestBindings = map[string]interface{}{} +var includeTestBindings = map[string]interface{}{ + "test": true, + "var": "value", +} func TestIncludeTag(t *testing.T) { config := render.NewConfig() loc := parser.SourceLoc{Pathname: "testdata/include_source.html", LineNo: 1} AddStandardTags(config) - ast, err := config.Compile(`{% include "include_target.html" %}`, loc) + // basic functionality + root, err := config.Compile(`{% include "include_target.html" %}`, loc) require.NoError(t, err) buf := new(bytes.Buffer) - err = render.Render(ast, buf, includeTestBindings, config) + err = render.Render(root, buf, includeTestBindings, config) require.NoError(t, err) require.Equal(t, "include target", strings.TrimSpace(buf.String())) - ast, err = config.Compile(`{% include 10 %}`, loc) + // tag and variable + root, err = config.Compile(`{% include "include_target_2.html" %}`, loc) require.NoError(t, err) - err = render.Render(ast, ioutil.Discard, includeTestBindings, config) + buf = new(bytes.Buffer) + err = render.Render(root, buf, includeTestBindings, config) + require.NoError(t, err) + require.Equal(t, "value", strings.TrimSpace(buf.String())) + + // errors + root, err = config.Compile(`{% include 10 %}`, loc) + require.NoError(t, err) + err = render.Render(root, ioutil.Discard, includeTestBindings, config) require.Error(t, err) require.Contains(t, err.Error(), "requires a string") + + root, err = config.Compile(`{% include "missing_file.html" %}`, loc) + require.NoError(t, err) + err = render.Render(root, ioutil.Discard, includeTestBindings, config) + require.Error(t, err) + require.Contains(t, err.Error(), "no such file") } diff --git a/tags/testdata/include_target_2.html b/tags/testdata/include_target_2.html new file mode 100644 index 0000000..519b97a --- /dev/null +++ b/tags/testdata/include_target_2.html @@ -0,0 +1 @@ +{% if test %}{{ var }}{% endif %} \ No newline at end of file