test alt if statememt

This commit is contained in:
z7zmey 2018-01-12 09:33:34 +02:00
parent 54ecfa2caf
commit 7c05e8b508
2 changed files with 139 additions and 5 deletions

View File

@ -9,7 +9,7 @@ type AltIf struct {
Cond node.Node
Stmt node.Node
ElseIf []node.Node
_else node.Node
Else node.Node
}
// NewAltIf node constuctor
@ -37,8 +37,8 @@ func (n *AltIf) AddElseIf(ElseIf node.Node) node.Node {
return n
}
func (n *AltIf) SetElse(_else node.Node) node.Node {
n._else = _else
func (n *AltIf) SetElse(Else node.Node) node.Node {
n.Else = Else
return n
}
@ -69,9 +69,9 @@ func (n *AltIf) Walk(v node.Visitor) {
}
}
if n._else != nil {
if n.Else != nil {
vv := v.GetChildrenVisitor("else")
n._else.Walk(vv)
n.Else.Walk(vv)
}
v.LeaveNode(n)

View File

@ -0,0 +1,134 @@
package test
import (
"bytes"
"reflect"
"testing"
"github.com/z7zmey/php-parser/node/expr"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
"github.com/z7zmey/php-parser/parser"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
diff := pretty.Compare(expected, actual)
if diff != "" {
t.Errorf("diff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("expected and actual are not equal\n")
}
}
}
func TestAltIf(t *testing.T) {
src := `<?
if ($a) :
endif;
`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestAltElseIf(t *testing.T) {
src := `<?
if ($a) :
elseif ($b):
endif;
`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
ElseIf: []node.Node{
&stmt.AltElseIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestAltElse(t *testing.T) {
src := `<?
if ($a) :
else:
endif;
`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
Else: &stmt.AltElse{
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}
func TestAltElseElseIf(t *testing.T) {
src := `<?
if ($a) :
elseif ($b):
elseif ($c):
else:
endif;
`
expected := &stmt.StmtList{
Stmts: []node.Node{
&stmt.AltIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$a"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
ElseIf: []node.Node{
&stmt.AltElseIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$b"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
&stmt.AltElseIf{
Cond: &expr.Variable{VarName: &node.Identifier{Value: "$c"}},
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
},
Else: &stmt.AltElse{
Stmt: &stmt.StmtList{Stmts: []node.Node{}},
},
},
},
}
actual, _, _ := parser.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}