Skip to content

Commit

Permalink
Return error when if condition contains and Or condition and a syntax…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
Mido-sys authored and sio4 committed Oct 1, 2022
1 parent 16c7852 commit bc8587c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
18 changes: 9 additions & 9 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,25 +453,25 @@ func (c *compiler) evalInfixExpression(node *ast.InfixExpression) (interface{},
lres, err := c.evalExpression(node.Left)
if err != nil {
if _, ok := err.(*ErrUnknownIdentifier); ok {
return false, nil
lres = false
} else {
return nil, err
}
return nil, err
}
if node.Operator == "&&" {
if !c.isTruthy(lres) {
return false, nil
}
}
if node.Operator == "||" {
if c.isTruthy(lres) {
return true, 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
47 changes: 43 additions & 4 deletions if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,52 @@ func Test_Render_If_Variable_Not_Set(t *testing.T) {

func Test_Render_If_Variable_Not_Set_But_Or_Condition_Is_True(t *testing.T) {
r := require.New(t)
type page struct {
PageTitle string
}
ctx := NewContext()
ctx.Set("path", "cart")
ctx.Set("paths", "cart")
input := `<%= if ( path == "pagePath" || (page && page.PageTitle != "cafe") || paths == "cart") { %>hi<%} %>`
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)
Expand Down

0 comments on commit bc8587c

Please sign in to comment.