Skip to content

Commit

Permalink
be very strict about nil identifiers fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Nov 3, 2017
1 parent 9f09c20 commit 081bad1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 35 deletions.
8 changes: 7 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ func (c *compiler) evalIdentifier(node *ast.Identifier) (interface{}, error) {
}
return f.Interface(), nil
}
return c.ctx.Value(node.Value), nil
if c.ctx.Has(node.Value) {
return c.ctx.Value(node.Value), nil
}
if node.Value == "nil" {
return nil, nil
}
return nil, errors.Errorf("could not find identifier '%s'", node.Value)
}

func (c *compiler) evalInfixExpression(node *ast.InfixExpression) (interface{}, error) {
Expand Down
25 changes: 0 additions & 25 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,6 @@ func ExampleRender_customHelperFunctions() {
// <p>i can update</p>
}

func ExampleRender_nilValue() {
html := `<html>
<%= if (names && len(names) > 0) { %>
<ul>
<%= for (n) in names { %>
<li><%= capitalize(n) %></li>
<% } %>
</ul>
<% } else { %>
<h1>Sorry, no names. :(</h1>
<% } %>
</html>`

s, err := Render(html, NewContext())
if err != nil {
log.Fatal(err)
}
fmt.Print(s)
// output: <html>
//
// <h1>Sorry, no names. :(</h1>
//
// </html>
}

func ExampleRender_forIterator() {
html := `<%= for (v) in between(3,6) { %><%=v%><% } %>`

Expand Down
2 changes: 1 addition & 1 deletion for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Test_Render_For_Nil(t *testing.T) {
ctx := NewContext()
ctx.Set("nilValue", nil)
s, err := Render(input, ctx)
r.NoError(err)
r.Error(err)
r.Equal("", s)
}

Expand Down
1 change: 0 additions & 1 deletion functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func Test_Render_Unknown_Function_Call(t *testing.T) {
input := `<p><%= f() %></p>`
_, err := Render(input, NewContext())
r.Error(err)
r.Contains(err.Error(), "f()")
}

func Test_Render_Function_Call_With_Arg(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func Test_Render_If_Or(t *testing.T) {
func Test_Render_If_Nil(t *testing.T) {
r := require.New(t)
input := `<%= if (names && len(names) >= 1) { %>hi<%} %>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("", s)
_, err := Render(input, NewContext())
r.Error(err)
}

func Test_Render_If_Else_Return(t *testing.T) {
Expand Down
18 changes: 14 additions & 4 deletions plush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ func Test_Render_Missing_Variable(t *testing.T) {
r := require.New(t)

input := `<p><%= name %></p>`
s, err := Render(input, NewContext())
r.NoError(err)
r.Equal("<p></p>", s)
_, err := Render(input, NewContext())
r.Error(err)
}

func Test_Render_HTML_Escape(t *testing.T) {
Expand Down Expand Up @@ -318,7 +317,7 @@ func Test_(t *testing.T) {

func Test_Helper_Nil_Arg(t *testing.T) {
r := require.New(t)
input := `<%= foo(none, "k") %><%= foo(nil, "k") %><%= foo(one, "k") %>`
input := `<%= foo(nil, "k") %><%= foo(one, "k") %>`
ctx := NewContextWith(map[string]interface{}{
"one": map[string]string{
"k": "test",
Expand All @@ -335,6 +334,17 @@ func Test_Helper_Nil_Arg(t *testing.T) {
r.Equal("test", s)
}

func Test_UndefinedArg(t *testing.T) {
r := require.New(t)
input := `<%= foo(bar) %>`
ctx := NewContext()
ctx.Set("foo", func(string) {})

_, err := Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "could not find identifier 'bar'")
}

func Test_RunScript(t *testing.T) {
r := require.New(t)
bb := &bytes.Buffer{}
Expand Down

0 comments on commit 081bad1

Please sign in to comment.