forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionTests.js
More file actions
94 lines (85 loc) · 3.05 KB
/
functionTests.js
File metadata and controls
94 lines (85 loc) · 3.05 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
var should = require("should");
var parser = require('../src/index');
describe('Function tests', function() {
var ast = parser.parseEval([
'function &foo($a = 1, callable $b, ?array &...$params) : ?boolean {}',
'$a = function &($b) use(&$c, $d) : array {',
' return true;',
'};',
'$b = foo(...[1, null, 1, 2, 3]);'
].join('\n'));
it('test variadic error', function () {
var astErr = parser.parseEval([
'$b = foo(...[1, 2, 3], $a);'
].join('\n'), {
parser: {
suppressErrors: true
}
});
var msg = 'Unexpected argument after a variadic argument on line 1';
astErr.errors.length.should.be.exactly(1);
astErr.errors[0].line.should.be.exactly(1);
astErr.errors[0].message.should.be.exactly(msg);
});
it('test description', function () {
// Get result from parser
ast.children[0].kind.should.be.exactly('function');
ast.children[0].name.should.be.exactly('foo');
ast.children[0].byref.should.be.exactly(true);
ast.children[0].nullable.should.be.exactly(true);
ast.children[0].type.name.should.be.exactly('boolean');
});
it('test arguments', function () {
/*
// 1st param
var arg1 = args[0];
arg1[0].should.be.exactly('param');
arg1[1].should.be.exactly('$a');
should.equal(arg1[2], null);
arg1[3][0].should.be.exactly('number');
arg1[3][1].should.be.exactly('1');
arg1[4].should.be.exactly(false, 'not byref');
arg1[5].should.be.exactly(false, 'not variadic');
// 2nd param
var arg2 = args[1];
arg2[0].should.be.exactly('param');
arg2[1].should.be.exactly('$params');
arg2[2][0].should.be.exactly('array');
should.equal(arg2[3], null);
arg2[4].should.be.exactly(true, 'byref');
arg2[5].should.be.exactly(true, 'variadic');
*/
});
it('test closure', function () {
var fn = ast.children[1].right;
fn.kind.should.be.exactly('closure');
fn.byref.should.be.exactly(true);
fn.uses.length.should.be.exactly(2);
fn.uses[0].kind.should.be.exactly('variable');
fn.uses[0].name.should.be.exactly('c');
fn.uses[0].byref.should.be.exactly(true);
fn.uses[1].kind.should.be.exactly('variable');
fn.uses[1].name.should.be.exactly('d');
fn.uses[1].byref.should.be.exactly(false);
fn.arguments.length.should.be.exactly(1);
fn.arguments[0].kind.should.be.exactly('parameter');
fn.arguments[0].name.should.be.exactly('b');
fn.arguments[0].byref.should.be.exactly(false);
fn.type.kind.should.be.exactly('identifier');
fn.type.name.should.be.exactly('\\array');
fn.nullable.should.be.exactly(false);
fn.body.kind.should.be.exactly('block');
});
it('test static closure', function() {
// from expr
var ast = parser.parseEval('$a = static function() {};');
var fn = ast.children[0].right;
fn.kind.should.be.exactly('closure');
fn.isStatic.should.be.exactly(true);
// from statement
ast = parser.parseEval('static function() {};');
fn = ast.children[0];
fn.kind.should.be.exactly('closure');
fn.isStatic.should.be.exactly(true);
});
});