mirror of
https://github.com/danog/liquid.git
synced 2024-11-30 08:39:01 +01:00
Tests
This commit is contained in:
parent
2353433e66
commit
fd230ed2cc
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
@ -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
1
tags/testdata/include_target_2.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
{% if test %}{{ var }}{% endif %}
|
Loading…
Reference in New Issue
Block a user