forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopTests.js
More file actions
131 lines (124 loc) · 4.41 KB
/
loopTests.js
File metadata and controls
131 lines (124 loc) · 4.41 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
var should = require("should");
var parser = require('../src/index');
describe('Test loops statements (for, while)', function() {
describe('Test while', function() {
var ast = parser.parseEval([
'while(true) {',
' echo "go";',
'}',
'while(true):',
' echo "short";',
'endwhile;'
].join('\n'), {
parser: { debug: false }
});
it('test default form', function() {
// enclosed form
ast.children[0].kind.should.be.exactly('while');
ast.children[0].shortForm.should.be.exactly(false);
ast.children[0].test.kind.should.be.exactly('boolean');
ast.children[0].body.kind.should.be.exactly('block');
ast.children[0].body.children.length.should.be.exactly(1);
});
it('test short form', function() {
// short form
ast.children[1].kind.should.be.exactly('while');
ast.children[1].shortForm.should.be.exactly(true);
ast.children[1].test.kind.should.be.exactly('boolean');
ast.children[1].body.kind.should.be.exactly('block');
ast.children[1].body.children.length.should.be.exactly(1);
});
});
describe('Test do', function() {
var ast = parser.parseEval([
'do {',
' echo "something";',
'} while(true);'
].join('\n'), {
parser: { debug: false }
});
it('test statement', function() {
ast.children[0].kind.should.be.exactly('do');
ast.children[0].test.kind.should.be.exactly('boolean');
ast.children[0].body.kind.should.be.exactly('block');
ast.children[0].body.children.length.should.be.exactly(1);
});
});
describe('Test for', function() {
var ast = parser.parseEval([
'for($i = 0, $b = 0; $i < 10, $ok; $i++)',
'echo $i;',
'for(;;):',
'echo $ok;',
'continue 5;',
'continue(5);',
'endfor;'
].join('\n'), {
parser: { debug: false }
});
it('test kind', function() {
ast.children[0].kind.should.be.exactly('for');
ast.children[0].shortForm.should.be.exactly(false);
ast.children[1].kind.should.be.exactly('for');
ast.children[1].shortForm.should.be.exactly(true);
});
it('test init args', function() {
ast.children[0].init.length.should.be.exactly(2);
ast.children[0].init[0].left.name.should.be.exactly('i');
ast.children[0].init[1].left.name.should.be.exactly('b');
ast.children[1].init.length.should.be.exactly(0);
});
it('check test args', function() {
ast.children[0].test.length.should.be.exactly(2);
ast.children[1].test.length.should.be.exactly(0);
// @todo ast.children[0].test[0]
ast.children[0].test[1].kind.should.be.exactly('variable');
});
it('test increment', function() {
// @todo
});
it('test body', function() {
// @todo
});
});
describe('Test foreach', function() {
var ast = parser.parseEval([
'foreach(&$foo as $v)',
'echo "$k -> $v\n";',
'foreach([[1,2], [3,4]] as list($a, $b) => [$c, $d]):',
'echo "$a -> $b\n";',
'endforeach;'
].join('\n'), {
parser: { debug: false }
});
it('test kind & form', function() {
ast.children[0].kind.should.be.exactly('foreach');
ast.children[0].shortForm.should.be.exactly(false);
ast.children[1].kind.should.be.exactly('foreach');
ast.children[1].shortForm.should.be.exactly(true);
});
it('test source', function() {
ast.children[0].source.kind.should.be.exactly('variable');
ast.children[0].source.byref.should.be.exactly(true);
ast.children[0].source.name.should.be.exactly('foo');
ast.children[1].source.kind.should.be.exactly('array');
ast.children[1].source.items.length.should.be.exactly(2);
});
it('test key', function() {
should.equal(ast.children[0].key, null);
ast.children[1].key.kind.should.be.exactly('list');
ast.children[1].key.arguments.length.should.be.exactly(2);
});
it('test value', function() {
ast.children[0].value.kind.should.be.exactly('variable');
ast.children[0].value.name.should.be.exactly('v');
ast.children[1].value.kind.should.be.exactly('array');
ast.children[1].value.shortForm.should.be.exactly(true);
});
it('test body', function() {
ast.children[0].body.kind.should.be.exactly('echo');
ast.children[1].body.kind.should.be.exactly('block');
ast.children[1].body.children[0].kind.should.be.exactly('echo');
});
});
});