forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastTests.js
More file actions
140 lines (122 loc) · 4.68 KB
/
astTests.js
File metadata and controls
140 lines (122 loc) · 4.68 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
var should = require("should");
var parser = require('./main');
describe('Test AST structure', function() {
it('test program', function() {
var ast = parser.parseEval('');
ast.kind.should.be.exactly('program');
ast.children.length.should.be.exactly(0);
});
it('test syntax error', function() {
var err = null;
(function(){
try {
var ast = parser.parseCode([
'<?php',
' $a = 1',
' $b = 2' // <-- unexpected $b expecting a ';'
].join('\n'), 'foo.php');
} catch(e) {
err = e;
throw e;
}
}).should.throw(/line\s3/);
err.name.should.be.exactly('SyntaxError');
err.lineNumber.should.be.exactly(3);
err.fileName.should.be.exactly('foo.php');
err.columnNumber.should.be.exactly(1);
});
it('test inline', function() {
var ast = parser.parseCode('Hello <?php echo "World"; ?> !');
ast.children[0].kind.should.be.exactly('inline');
ast.children[2].kind.should.be.exactly('inline');
ast.children[0].value.should.be.exactly('Hello ');
ast.children[2].value.should.be.exactly(' !');
});
it('test magics', function() {
var ast = parser.parseEval('echo __FILE__, __DIR__;');
ast.children[0].arguments[0].kind.should.be.exactly('magic');
ast.children[0].arguments[1].kind.should.be.exactly('magic');
ast.children[0].arguments[0].value.should.be.exactly('__FILE__');
ast.children[0].arguments[1].value.should.be.exactly('__DIR__');
});
it('test shell', function() {
var ast = parser.parseEval('echo `ls -larth`;');
ast.children[0].arguments[0].kind.should.be.exactly('encapsed');
ast.children[0].arguments[0].type.should.be.exactly('shell');
});
it('test clone', function() {
var ast = parser.parseEval('$a = clone $var;');
ast.children[0].right.kind.should.be.exactly('clone');
ast.children[0].right.what.kind.should.be.exactly('variable');
});
it('test echo, isset, unset, empty', function() {
var ast = parser.parseEval([
'echo ($expr) ? "ok" : "ko";',
'print "some text"',
'isset($foo, $bar)',
'unset($var)',
'empty($var)',
''
].join(';\n'));
ast.children[0].kind.should.be.exactly('echo');
ast.children[1].kind.should.be.exactly('print');
ast.children[2].kind.should.be.exactly('isset');
ast.children[3].kind.should.be.exactly('unset');
ast.children[4].kind.should.be.exactly('empty');
});
it('should be variable', function() {
// @todo
});
it('test literals', function() {
// @todo string / numbers / booleans
});
it('test constants', function() {
var ast = parser.parseEval('const FOO = 3.14;');
ast.children[0].kind.should.be.exactly('constant');
ast.children[0].name.should.be.exactly('FOO');
ast.children[0].value.kind.should.be.exactly('number');
ast.children[0].value.value.should.be.exactly('3.14');
});
it('test eval', function() {
var ast = parser.parseEval('eval("return true;");');
ast.children[0].kind.should.be.exactly('eval');
ast.children[0].source.kind.should.be.exactly('string');
ast.children[0].source.value.should.be.exactly('return true;');
});
it('test die/exit', function() {
var ast = parser.parseEval('die("bye");');
ast.children[0].kind.should.be.exactly('exit');
ast.children[0].status.value.should.be.exactly('bye');
ast = parser.parseEval('exit(-1);');
ast.children[0].kind.should.be.exactly('exit');
ast.children[0].status.value.should.be.exactly('-1');
});
it('test coalesce operator', function() {
var ast = parser.parseEval('$var = $a ?? true;');
ast.children[0].right.kind.should.be.exactly('bin');
ast.children[0].right.type.should.be.exactly('??');
ast.children[0].right.left.kind.should.be.exactly('variable');
ast.children[0].right.right.kind.should.be.exactly('boolean');
});
it('test include / require', function() {
var ast = parser.parseEval([
'include "file.php"',
'include_once (PATH . "/file.php")',
'require "req.php"',
'require_once "file.php"',
''
].join(';\n'));
ast.children[0].kind.should.be.exactly('include');
ast.children[0].once.should.be.exactly(false);
ast.children[0].require.should.be.exactly(false);
ast.children[1].kind.should.be.exactly('include');
ast.children[1].once.should.be.exactly(true);
ast.children[1].require.should.be.exactly(false);
ast.children[2].kind.should.be.exactly('include');
ast.children[2].once.should.be.exactly(false);
ast.children[2].require.should.be.exactly(true);
ast.children[3].kind.should.be.exactly('include');
ast.children[3].once.should.be.exactly(true);
ast.children[3].require.should.be.exactly(true);
});
});