forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatementTests.js
More file actions
212 lines (189 loc) · 6.88 KB
/
statementTests.js
File metadata and controls
212 lines (189 loc) · 6.88 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var should = require("should");
var parser = require('../src/index');
describe('Test statements', function() {
it('test goto label', function() {
var ast = parser.parseEval([
'start:',
' $i++;',
'goto start;'
].join('\n'), {
parser: { debug: false }
});
ast.children[0].kind.should.be.exactly('label');
ast.children[0].name.should.be.exactly('start');
ast.children[1].kind.should.be.exactly('post');
ast.children[2].kind.should.be.exactly('goto');
ast.children[2].label.should.be.exactly('start');
});
it('test global', function() {
var ast = parser.parseEval([
'function foo() {',
' global $a, $b;',
'}'
].join('\n'), {
parser: { debug: false }
});
var expr = ast.children[0].body.children[0];
expr.kind.should.be.exactly('global');
expr.items[0].kind.should.be.exactly('variable');
expr.items[0].name.should.be.exactly('a');
expr.items[1].kind.should.be.exactly('variable');
expr.items[1].name.should.be.exactly('b');
});
it('test halt statement', function() {
var ast = parser.parseEval([
'$a = 1;',
'__halt_compiler();',
'$b = 1;'
].join('\n'), {
parser: { debug: false }
});
ast.children.length.should.be.exactly(2);
ast.children[0].kind.should.be.exactly('assign');
ast.children[1].kind.should.be.exactly('halt');
ast.children[1].after.should.be.exactly('\n$b = 1;');
// test the inner error
(function() {
var ast = parser.parseEval([
'if (true) {',
' __halt_compiler();',
'}'
].join('\n'));
}).should.throw(/line\s2/);
// test the fallback
ast = parser.parseEval([
'if (true) {',
' __halt_compiler();',
'}',
'$b = 1;'
].join('\n'), {
parser: { suppressErrors: true }
});
ast.children.length.should.be.exactly(2);
ast.errors.length.should.be.exactly(1);
ast.children[0].kind.should.be.exactly('if');
ast.children[0].body.children[0].kind.should.be.exactly('halt');
ast.children[0].body.children[0].after.should.be.exactly('\n}\n$b = 1;');
ast.children[1].kind.should.be.exactly('assign');
});
it('test static', function() {
var ast = parser.parseEval([
'function foo() {',
' static $a, $b = 5;',
'}',
'static $sVar1 = 11;'
].join('\n'), {
parser: { debug: false }
});
// test function multi statements
var expr = ast.children[0].body.children[0];
expr.kind.should.be.exactly('static');
expr.items.length.should.be.exactly(2);
expr.items[0].kind.should.be.exactly('variable');
expr.items[1].kind.should.be.exactly('assign');
// test single statement
ast.children[1].kind.should.be.exactly('static');
ast.children[1].items.length.should.be.exactly(1);
ast.children[1].items[0].kind.should.be.exactly('assign');
ast.children[1].items[0].left.kind.should.be.exactly('variable');
ast.children[1].items[0].left.name.should.be.exactly('sVar1');
ast.children[1].items[0].right.kind.should.be.exactly('number');
ast.children[1].items[0].right.value.should.be.exactly('11');
});
it('test declare', function() {
var ast = parser.parseEval([
'declare(ticks=1);',
'$a = 1;',
'declare(ticks=2,encoding="ISO-8859-1");',
'$b = 1;',
'declare(ticks=1) {',
' $c = 2;',
'}',
'declare(encoding="UTF-8"):',
' $d = 3;',
'enddeclare;',
'$e = 4;' // <- single statement
].join('\n'), {
parser: { debug: false }
});
ast.children.length.should.be.exactly(5);
ast.children[0].kind.should.be.exactly('declare');
ast.children[0].mode.should.be.exactly('none');
ast.children[0].children.length.should.be.exactly(1);
ast.children[0].children[0].left.name.should.be.exactly('a');
ast.children[0].what.ticks.kind.should.be.exactly('number');
ast.children[0].what.ticks.value.should.be.exactly('1');
ast.children[1].kind.should.be.exactly('declare');
ast.children[1].mode.should.be.exactly('none');
ast.children[1].children.length.should.be.exactly(1);
ast.children[1].children[0].left.name.should.be.exactly('b');
ast.children[1].what.ticks.kind.should.be.exactly('number');
ast.children[1].what.ticks.value.should.be.exactly('2');
ast.children[1].what.encoding.kind.should.be.exactly('string');
ast.children[1].what.encoding.value.should.be.exactly('ISO-8859-1');
ast.children[2].kind.should.be.exactly('declare');
ast.children[2].mode.should.be.exactly('block');
ast.children[2].children.length.should.be.exactly(1);
ast.children[2].children[0].left.name.should.be.exactly('c');
ast.children[2].what.ticks.kind.should.be.exactly('number');
ast.children[2].what.ticks.value.should.be.exactly('1');
ast.children[3].kind.should.be.exactly('declare');
ast.children[3].mode.should.be.exactly('short');
ast.children[3].children.length.should.be.exactly(1);
ast.children[3].children[0].left.name.should.be.exactly('d');
ast.children[3].what.encoding.kind.should.be.exactly('string');
ast.children[3].what.encoding.value.should.be.exactly('UTF-8');
ast.children[4].kind.should.be.exactly('assign');
});
it('test try', function() {
var ast = parser.parseEval([
'try {',
' foo();',
'} catch(FooError|BarError $err) {',
' var_dump($err);',
' throw $err;',
'} finally {',
' clean();',
'}'
].join('\n'), {
parser: { debug: false }
});
var expr = ast.children[0];
expr.kind.should.be.exactly('try');
expr.body.kind.should.be.exactly('block');
expr.body.children[0].kind.should.be.exactly('call');
expr.body.children[0].what.name.should.be.exactly('foo');
expr.catches.length.should.be.exactly(1);
// test catch
var catchExpr = expr.catches[0];
catchExpr.kind.should.be.exactly('catch');
catchExpr.what.length.should.be.exactly(2);
catchExpr.what[0].name.should.be.exactly('FooError');
catchExpr.what[1].name.should.be.exactly('BarError');
catchExpr.variable.kind.should.be.exactly('variable');
catchExpr.variable.name.should.be.exactly('err');
// test the throw statement
catchExpr.body.kind.should.be.exactly('block');
catchExpr.body.children.length.should.be.exactly(2);
catchExpr.body.children[1].kind.should.be.exactly('throw');
catchExpr.body.children[1].what.kind.should.be.exactly('variable');
catchExpr.body.children[1].what.name.should.be.exactly('err');
// always block
expr.always.kind.should.be.exactly('block');
});
it('test inner statements', function() {
var ast = parser.parseEval([
'if (true) {',
' function foo() {}',
' abstract class foo {}',
' final class foo {}',
' class foo {}',
' trait foo {}',
' interface foo {}',
'}'
].join('\n'), {
parser: { debug: false }
});
// @todo : do assert
});
});