Skip to content

Commit

Permalink
Move expression parser to sub-package
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 26, 2017
1 parent d03efed commit 373b2fb
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 507 deletions.
51 changes: 26 additions & 25 deletions chunk_parser_test.go
Expand Up @@ -8,31 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestChunkParser(t *testing.T) {
ctx := Context{map[string]interface{}{
"x": 123,
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
},
}

for i, test := range chunkTests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
tokens := ScanChunks(test.in, "")
// fmt.Println(tokens)
ast, err := Parse(tokens)
require.NoErrorf(t, err, test.in)
// fmt.Println(MustYAML(ast))
buf := new(bytes.Buffer)
err = ast.Render(buf, ctx)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
}

var chunkTests = []struct{ in, expected string }{
{"{{12}}", "12"},
{"{{x}}", "123"},
Expand All @@ -58,4 +33,30 @@ var chunkTests = []struct{ in, expected string }{
{"{%unless false%}0{%elsif true%}1{%else%}2{%endif%}", "0"},
{"{%unless true%}0{%elsif true%}1{%else%}2{%endif%}", "1"},
{"{%unless true%}0{%elsif false%}1{%else%}2{%endif%}", "2"},
// {"{%for a in ar%}{{a}} {{%endfor%}", "first second third "},
}

var chunkTestContext = Context{map[string]interface{}{
"x": 123,
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"title": "Introduction",
},
},
}

func TestChunkParser(t *testing.T) {
for i, test := range chunkTests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
tokens := ScanChunks(test.in, "")
// fmt.Println(tokens)
ast, err := Parse(tokens)
require.NoErrorf(t, err, test.in)
// fmt.Println(MustYAML(ast))
buf := new(bytes.Buffer)
err = ast.Render(buf, chunkTestContext)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})
}
}
6 changes: 5 additions & 1 deletion evaluator.go → expressions/evaluator.go
@@ -1,10 +1,14 @@
package main
package expressions

import (
"fmt"
"reflect"
)

type Context struct {
Variables map[string]interface{}
}

type Expression struct {
value func(Context) (interface{}, error)
}
Expand Down
2 changes: 1 addition & 1 deletion evaluator_test.go → expressions/evaluator_test.go
@@ -1,4 +1,4 @@
package main
package expressions

import (
"fmt"
Expand Down
@@ -1,7 +1,7 @@
//go:generate ragel -Z scanner.rl
//go:generate goyacc expression_parser.y
//go:generate goyacc expressions.y

package main
package expressions

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion expression_parser.y → expressions/expressions.y
@@ -1,5 +1,5 @@
%{
package main
package expressions
import (
"fmt"
"reflect"
Expand Down

0 comments on commit 373b2fb

Please sign in to comment.