Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix if condition if one of the nodes is undefined while the operator is Or and the other node is true #164

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,26 @@ func (c *compiler) evalInfixExpression(node *ast.InfixExpression) (interface{},

lres, err := c.evalExpression(node.Left)
if err != nil {
return nil, err
if _, ok := err.(*ErrUnknownIdentifier); ok {
lres = false
} else {
return nil, err
}
}
if node.Operator == "&&" {
if !c.isTruthy(lres) {
return false, nil
}
}

rres, err := c.evalExpression(node.Right)
if err != nil {
return nil, err
if _, ok := err.(*ErrUnknownIdentifier); !ok {
return nil, err
} else {
rres = false
}
}

switch node.Operator {
case "&&", "||":
return c.boolsOperator(lres, rres, node.Operator)
Expand Down
54 changes: 54 additions & 0 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,57 @@ func Test_Render_If_Variable_Not_Set(t *testing.T) {
r.NoError(err)
r.Equal("", s)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Is_True(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("path", "cart")
ctx.Set("paths", "cart")
input := `<%= if ( paths == "cart" || (page && page.PageTitle != "cafe") || paths == "cart") { %>hi<%} %>`

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_While_Node_Is_True_Includes_Syntax_Error_Last_Node(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("paths", "cart")
input := `<%= if ( paths == "cart" || pages ^^^ ) { %>hi<%} %>`

_, err := Render(input, ctx)
r.Error(err)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_While_Node_Is_True_Includes_Syntax_Error_First_Node(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("paths", "cart")
input := `<%= if ( paths @#@# "cart" || pages) { %>hi<%} %>`

_, err := Render(input, ctx)
r.Error(err)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Left_Node_Is_True(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("paths", "cart")
input := `<%= if ( paths == "cart" || pages ) { %>hi<%} %>`

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hi", s)
}

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Right_Node_Is_True(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("pages", "cart")
input := `<%= if ( paths == "cart" || pages ) { %>hi<%} %>`

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hi", s)
}