Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 10, 2017
1 parent b269138 commit bfad047
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 48 deletions.
6 changes: 6 additions & 0 deletions evaluator/convert_test.go
Expand Up @@ -64,6 +64,12 @@ func TestConvert_map(t *testing.T) {
require.Equal(t, "value", m["key"])
}

func TestConvert_map_key_error(t *testing.T) {
typ := reflect.TypeOf(map[string]int{})
_, err := Convert(map[interface{}]interface{}{"key": "value"}, typ)
require.Error(t, err)
}

func TestConvert_map_synonym(t *testing.T) {
type VariableMap map[interface{}]interface{}
typ := reflect.TypeOf(map[string]string{})
Expand Down
72 changes: 72 additions & 0 deletions render/context_test.go
@@ -0,0 +1,72 @@
package render

import (
"bytes"
"fmt"
"io"
"testing"

"github.com/stretchr/testify/require"
)

func addContextTestTags(s Config) {
s.AddBlock("eval").Renderer(func(w io.Writer, c Context) error {
v, err := c.EvaluateString(c.TagArgs())
if err != nil {
return err
}
_, err = w.Write([]byte(fmt.Sprint(v)))
return err
})
s.AddBlock("parse").Compiler(func(c BlockNode) (func(io.Writer, Context) error, error) {
a := c.Args
return func(w io.Writer, c Context) error {
_, err := w.Write([]byte(a))
return err
}, nil
})
s.AddTag("tag_name", func(string) (func(io.Writer, Context) error, error) {
return func(w io.Writer, c Context) error {
_, err := w.Write([]byte(c.TagName()))
return err
}, nil
})
s.AddTag("expand_arg", func(string) (func(w io.Writer, c Context) error, error) {
return func(w io.Writer, c Context) error {
s, err := c.ExpandTagArg()
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
}, nil
})
}

var contextTests = []struct{ in, out string }{
{`{% parse args %}{% endparse %}`, "args"},
{`{% eval x %}{% endeval %}`, "123"},
{`{% expand_arg x %}`, "x"},
{`{% expand_arg {{x}} %}`, "123"},
{`{% tag_name %}`, "tag_name"},
}

var contextTestBindings = map[string]interface{}{
"x": 123,
}

func TestContext(t *testing.T) {
cfg := NewConfig()
addContextTestTags(cfg)
context := newNodeContext(contextTestBindings, cfg)
for i, test := range contextTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
ast, err := cfg.Compile(test.in)
require.NoErrorf(t, err, test.in)
buf := new(bytes.Buffer)
err = renderNode(ast, buf, context)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.out, buf.String(), test.in)
})
}
}
58 changes: 11 additions & 47 deletions render/render_test.go
Expand Up @@ -11,37 +11,6 @@ import (
)

func addRenderTestTags(s Config) {
s.AddBlock("parse").Compiler(func(c BlockNode) (func(io.Writer, Context) error, error) {
a := c.Args
return func(w io.Writer, c Context) error {
_, err := w.Write([]byte(a))
return err
}, nil
})
s.AddBlock("eval").Renderer(func(w io.Writer, c Context) error {
v, err := c.EvaluateString(c.TagArgs())
if err != nil {
return err
}
_, err = w.Write([]byte(fmt.Sprint(v)))
return err
})
s.AddTag("tag_name", func(string) (func(io.Writer, Context) error, error) {
return func(w io.Writer, c Context) error {
_, err := w.Write([]byte(c.TagName()))
return err
}, nil
})
s.AddTag("expand_arg", func(string) (func(w io.Writer, c Context) error, error) {
return func(w io.Writer, c Context) error {
s, err := c.ExpandTagArg()
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
}, nil
})
s.AddBlock("err2").Compiler(func(c BlockNode) (func(io.Writer, Context) error, error) {
return func(w io.Writer, c Context) error {
return fmt.Errorf("stage 2 error")
Expand All @@ -53,12 +22,7 @@ var renderTests = []struct{ in, out string }{
{`{{ 12 }}`, "12"},
{`{{ x }}`, "123"},
{`{{ page.title }}`, "Introduction"},
{`{{ ar[1] }}`, "second"},
{`{% parse args %}{% endparse %}`, "args"},
{`{% eval x %}{% endeval %}`, "123"},
{`{% expand_arg x %}`, "x"},
{`{% expand_arg {{x}} %}`, "123"},
{`{% tag_name %}`, "tag_name"},
{`{{ array[1] }}`, "second"},
}

var renderErrorTests = []struct{ in, out string }{
Expand All @@ -67,7 +31,8 @@ var renderErrorTests = []struct{ in, out string }{
}

var renderTestBindings = map[string]interface{}{
"x": 123,
"x": 123,
"array": []string{"first", "second", "third"},
"obj": map[string]interface{}{
"a": 1,
},
Expand All @@ -87,19 +52,18 @@ var renderTestBindings = map[string]interface{}{
{"weight": 3},
{"weight": nil},
},
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
}

func TestRender(t *testing.T) {
settings := NewConfig()
addRenderTestTags(settings)
context := newNodeContext(renderTestBindings, settings)
cfg := NewConfig()
addRenderTestTags(cfg)
context := newNodeContext(renderTestBindings, cfg)
for i, test := range renderTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
ast, err := settings.Compile(test.in)
ast, err := cfg.Compile(test.in)
require.NoErrorf(t, err, test.in)
buf := new(bytes.Buffer)
err = renderNode(ast, buf, context)
Expand All @@ -110,12 +74,12 @@ func TestRender(t *testing.T) {
}

func TestRenderErrors(t *testing.T) {
settings := NewConfig()
addRenderTestTags(settings)
context := newNodeContext(renderTestBindings, settings)
cfg := NewConfig()
addRenderTestTags(cfg)
context := newNodeContext(renderTestBindings, cfg)
for i, test := range renderErrorTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
ast, err := settings.Compile(test.in)
ast, err := cfg.Compile(test.in)
require.NoErrorf(t, err, test.in)
err = renderNode(ast, ioutil.Discard, context)
require.Errorf(t, err, test.in)
Expand Down
22 changes: 21 additions & 1 deletion tags/loop_test.go
Expand Up @@ -17,7 +17,7 @@ var loopTests = []struct{ in, expected string }{
{`{% for a in array limit:2 %}{{ a }}.{% endfor %}`, "first.second."},
{`{% for a in array offset:1 %}{{ a }}.{% endfor %}`, "second.third."},
{`{% for a in array reversed limit:1 %}{{ a }}.{% endfor %}`, "third."},
// TODO investigate how these combine; does it depend on the order
// TODO investigate how these combine; does it depend on the order?
// {`{% for a in array reversed offset:1 %}{{ a }}.{% endfor %}`, "second.first."},
// {`{% for a in array limit:1 offset:1 %}{{ a }}.{% endfor %}`, "second."},
// {`{% for a in array reversed limit:1 offset:1 %}{{ a }}.{% endfor %}`, "second."},
Expand Down Expand Up @@ -58,6 +58,11 @@ var loopTests = []struct{ in, expected string }{
{`{% for a in hash %}{{ a }}{% endfor %}`, "a"},
}

var loopErrorTests = []struct{ in, expected string }{
{`{% break %}`, "break outside a loop"},
{`{% continue %}`, "continue outside a loop"},
}

var loopTestBindings = map[string]interface{}{
"array": []string{"first", "second", "third"},
"hash": map[string]interface{}{"a": 1},
Expand All @@ -77,3 +82,18 @@ func TestLoopTag(t *testing.T) {
})
}
}

func TestLoopTag_errors(t *testing.T) {
config := render.NewConfig()
AddStandardTags(config)
for i, test := range loopErrorTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
ast, err := config.Compile(test.in)
require.NoErrorf(t, err, test.in)
buf := new(bytes.Buffer)
err = render.Render(ast, buf, loopTestBindings, config)
require.Errorf(t, err, test.in)
require.Containsf(t, err.Error(), test.expected, test.in)
})
}
}

0 comments on commit bfad047

Please sign in to comment.