-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoop_test.go
166 lines (160 loc) · 3.72 KB
/
oop_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package php
import (
"testing"
"github.com/stephens2424/php/ast"
)
func TestClass(t *testing.T) {
testStr := `<?php
abstract class TestClass {
public $myProp;
protected $myProp2;
const my_const = "test";
private $arr = array("one", "two");
abstract public function method0($arg);
public function method1($arg) {
echo $arg;
}
private function method2(TestClass $arg, $arg2 = false) {
echo $arg;
return $arg;
}
}`
p := NewParser()
p.disableScoping = true
p.Debug = true
a, err := p.Parse("test.php", testStr)
if err != nil {
t.Fatal(err)
}
if len(a.Nodes) != 1 {
t.Fatalf("Class did not correctly parse")
}
tree := &ast.Class{
Name: "TestClass",
Constants: []*ast.Constant{
{
Name: "my_const",
Value: &ast.Literal{Type: ast.String, Value: `"test"`},
},
},
Properties: []*ast.Property{
{
Visibility: ast.Public,
Name: "$myProp",
},
{
Visibility: ast.Protected,
Name: "$myProp2",
},
{
Visibility: ast.Private,
Name: "$arr",
Initialization: &ast.ArrayExpression{
Pairs: []ast.ArrayPair{
{Value: &ast.Literal{Type: ast.String, Value: `"one"`}},
{Value: &ast.Literal{Type: ast.String, Value: `"two"`}},
},
},
},
},
Methods: []*ast.Method{
{
Visibility: ast.Public,
FunctionStmt: &ast.FunctionStmt{
FunctionDefinition: &ast.FunctionDefinition{
Name: "method0",
Arguments: []*ast.FunctionArgument{
{
Variable: ast.NewVariable("arg"),
},
},
},
},
},
{
Visibility: ast.Public,
FunctionStmt: &ast.FunctionStmt{
FunctionDefinition: &ast.FunctionDefinition{
Name: "method1",
Arguments: []*ast.FunctionArgument{
{
Variable: ast.NewVariable("arg"),
},
},
},
Body: &ast.Block{
Statements: []ast.Statement{
ast.Echo(ast.NewVariable("arg")),
},
},
},
},
{
Visibility: ast.Private,
FunctionStmt: &ast.FunctionStmt{
FunctionDefinition: &ast.FunctionDefinition{
Name: "method2",
Arguments: []*ast.FunctionArgument{
{
TypeHint: "TestClass",
Variable: ast.NewVariable("arg"),
},
{
Variable: ast.NewVariable("arg2"),
Default: &ast.Literal{Type: ast.Boolean, Value: "false"},
},
},
},
Body: &ast.Block{
Statements: []ast.Statement{
ast.Echo(ast.NewVariable("arg")),
&ast.ReturnStmt{Expression: ast.NewVariable("arg")},
},
},
},
},
},
}
if !assertEquals(a.Nodes[0], tree) {
t.Fatalf("Class did not parse correctly")
}
}
func TestExtraModifiers(t *testing.T) {
testStr := `<?
class myclass {
public public function test() {
}
}`
p := NewParser()
p.disableScoping = true
_, err := p.Parse("test.php", testStr)
if err == nil || len(err.(ParseErrorList)) != 1 {
t.Fatalf("Did not correctly error that a function has two public modifiers")
}
}
func TestInstantiation(t *testing.T) {
testStr := `<?
$obj = new Obj::$classes['obj']($arg);`
p := NewParser()
p.disableScoping = true
a, err := p.Parse("test.php", testStr)
if err != nil {
t.Fatalf("Did not parse instantiation correctly: %s", err)
}
tree := ast.ExpressionStmt{ast.AssignmentExpression{
Operator: "=",
Assignee: ast.NewVariable("obj"),
Value: &ast.NewExpression{
Class: ast.NewClassExpression("Obj", &ast.ArrayLookupExpression{
Array: ast.NewVariable("classes"),
Index: &ast.Literal{Type: ast.String, Value: `'obj'`},
}),
Arguments: []ast.Expression{
ast.NewVariable("arg"),
},
},
}}
if !assertEquals(a.Nodes[0], tree) {
t.Fatalf("Instantiation did not parse correctly")
}
}