1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 06:08:57 +01:00
This commit is contained in:
Oliver Steele 2017-07-25 08:16:30 -04:00
parent 2353433e66
commit fd230ed2cc
5 changed files with 38 additions and 23 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)
}

View File

@ -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")
}

1
tags/testdata/include_target_2.html vendored Normal file
View File

@ -0,0 +1 @@
{% if test %}{{ var }}{% endif %}