-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharray_test.go
84 lines (74 loc) · 1.95 KB
/
array_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package php
import (
"testing"
"github.com/stephens2424/php/ast"
)
func TestList(t *testing.T) {
testStr := `<?
list($one, $two) = array(1, 2);`
p := NewParser()
a, err := p.Parse("test.php", testStr)
if err != nil {
t.Fatalf("Did not parse list correctly: %s", err)
}
tree := ast.ExpressionStmt{&ast.ListStatement{
Operator: "=",
Assignees: []ast.Assignable{
ast.NewVariable("one"),
ast.NewVariable("two"),
},
Value: &ast.ArrayExpression{
Pairs: []ast.ArrayPair{
{Key: nil, Value: &ast.Literal{Value: "1", Type: ast.Float}},
{Key: nil, Value: &ast.Literal{Value: "2", Type: ast.Float}},
},
},
}}
if !assertEquals(a.Nodes[0], tree) {
t.Fatalf("Array bracked did not parse correctly")
}
}
func TestArrayBracket(t *testing.T) {
testStr := `<?
$arr = ["one", "two"];
$arr2 = ["one" => 1, "two" => 2];`
p := NewParser()
a, err := p.Parse("test.php", testStr)
if err != nil {
t.Fatalf("Did not parse array bracket correctly: %s", err)
}
tree := []ast.Statement{
ast.ExpressionStmt{ast.AssignmentExpression{
Operator: "=",
Assignee: ast.NewVariable("arr"),
Value: &ast.ArrayExpression{
Pairs: []ast.ArrayPair{
{Key: nil, Value: &ast.Literal{Value: `"one"`, Type: ast.String}},
{Key: nil, Value: &ast.Literal{Value: `"two"`, Type: ast.String}},
},
},
}},
ast.ExpressionStmt{ast.AssignmentExpression{
Operator: "=",
Assignee: ast.NewVariable("arr2"),
Value: &ast.ArrayExpression{
Pairs: []ast.ArrayPair{
{
Key: &ast.Literal{Value: `"one"`, Type: ast.String},
Value: &ast.Literal{Value: "1", Type: ast.Float},
},
{
Key: &ast.Literal{Value: `"two"`, Type: ast.String},
Value: &ast.Literal{Value: "2", Type: ast.Float},
},
},
},
}},
}
if !assertEquals(a.Nodes[0], tree[0]) {
t.Fatalf("Array bracked did not parse correctly")
}
if !assertEquals(a.Nodes[1], tree[1]) {
t.Fatalf("Array bracked did not parse correctly")
}
}