forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayTests.js
More file actions
202 lines (159 loc) · 7.44 KB
/
arrayTests.js
File metadata and controls
202 lines (159 loc) · 7.44 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
var should = require("should");
var parser = require('../src/index');
describe('Array without keys', function() {
it('deference array', function () {
var ast = parser.parseEval([
'$a = [',
'"a", "b"',
']($foo)[$foo->bar()[1]]->foo()'
].join('\r'), {
parser: {
// debug: true
}
});
// @todo console.log(ast);
});
describe('of strings', function () {
// Get result from parser
var ast = parser.parseEval('array("item1", "item2", "item3")');
it('should be of type array', function () {
ast.children[0].kind.should.be.exactly("array");
});
it('should have correct number of items', function () {
ast.children[0].items.length.should.be.exactly(3);
});
it('should have correct item types and values', function () {
ast.children[0].items[0].value.kind.should.be.exactly("string");
ast.children[0].items[0].value.value.should.be.exactly("item1");
ast.children[0].items[1].value.kind.should.be.exactly("string");
ast.children[0].items[1].value.value.should.be.exactly("item2");
ast.children[0].items[2].value.kind.should.be.exactly("string");
ast.children[0].items[2].value.value.should.be.exactly("item3");
});
});
describe('of numbers', function () {
// Get result from parser
var ast = parser.parseEval('array(1, 2.5, 0x1000)');
it('should be of type array', function () {
ast.children[0].kind.should.be.exactly("array");
});
it('should have correct number of items', function () {
ast.children[0].items.length.should.be.exactly(3);
});
it('should have correct item types and values', function () {
ast.children[0].items[0].value.kind.should.be.exactly("number");
ast.children[0].items[0].value.value.should.be.exactly('1');
ast.children[0].items[1].value.kind.should.be.exactly("number");
ast.children[0].items[1].value.value.should.be.exactly('2.5');
ast.children[0].items[2].value.kind.should.be.exactly("number");
ast.children[0].items[2].value.value.should.be.exactly('0x1000');
});
});
describe('of strings and numbers', function () {
// Get result from parser
var ast = parser.parseEval('array(1, "item2", 3, "item4")');
it('should be of type array', function () {
ast.children[0].kind.should.be.exactly("array");
});
it('should have correct number of items', function () {
ast.children[0].items.length.should.be.exactly(4);
});
it('should have correct item types and values', function () {
ast.children[0].items[0].value.kind.should.be.exactly("number");
ast.children[0].items[0].value.value.should.be.exactly('1');
ast.children[0].items[1].value.kind.should.be.exactly("string");
ast.children[0].items[1].value.value.should.be.exactly("item2");
ast.children[0].items[2].value.kind.should.be.exactly("number");
ast.children[0].items[2].value.value.should.be.exactly('3');
ast.children[0].items[3].value.kind.should.be.exactly("string");
ast.children[0].items[3].value.value.should.be.exactly("item4");
});
});
describe('of variables', function () {
// Get result from parser
var ast = parser.parseEval('array($obj1, $obj2, $obj3)');
it('should be of type array', function () {
ast.children[0].kind.should.be.exactly("array");
});
it('should have correct number of items', function () {
ast.children[0].items.length.should.be.exactly(3);
});
it('should have correct item types and values', function () {
ast.children[0].items[0].value.kind.should.be.exactly("variable");
ast.children[0].items[0].value.name.should.be.exactly("obj1");
ast.children[0].items[1].value.kind.should.be.exactly("variable");
ast.children[0].items[1].value.name.should.be.exactly("obj2");
ast.children[0].items[2].value.kind.should.be.exactly("variable");
ast.children[0].items[2].value.name.should.be.exactly("obj3");
});
});
// TODO -- Check this is valid PHP + check AST output is correct
describe('of objects', function () {
// Get result from parser
var ast = parser.parseEval('[new foo(), new stdClass(), new bar()]');
it('should be of type array', function () {
ast.children[0].kind.should.be.exactly("array");
});
it('should have correct number of items', function () {
ast.children[0].items.length.should.be.exactly(3);
});
it('should have correct item types and values', function () {
ast.children[0].items[0].value.kind.should.be.exactly("new");
ast.children[0].items[0].value.what.name.should.be.exactly("foo");
ast.children[0].items[1].value.kind.should.be.exactly("new");
ast.children[0].items[1].value.what.name.should.be.exactly("stdClass");
ast.children[0].items[2].value.kind.should.be.exactly("new");
ast.children[0].items[2].value.what.name.should.be.exactly("bar");
});
});
describe('of arrays', function () {
// Get result from parser
var ast = parser.parseEval([
'array(',
' array("item1", "item2"),',
' array("item3", "item4"),',
' array("item5", "item6")',
')'
].join('\n'));
it('should be of type array', function () {
ast.children[0].kind.should.be.exactly("array");
});
it('should have correct number of items', function () {
ast.children[0].items.length.should.be.exactly(3);
});
it('should have correct item types and values', function () {
ast.children[0].items[0].value.kind.should.be.exactly("array");
ast.children[0].items[0].value.items.length.should.be.exactly(2);
ast.children[0].items[0].value.items[0].value.value.should.be.exactly("item1");
ast.children[0].items[0].value.items[1].value.value.should.be.exactly("item2");
ast.children[0].items[1].value.kind.should.be.exactly("array");
ast.children[0].items[1].value.items.length.should.be.exactly(2);
ast.children[0].items[1].value.items[0].value.value.should.be.exactly("item3");
ast.children[0].items[1].value.items[1].value.value.should.be.exactly("item4");
ast.children[0].items[2].value.kind.should.be.exactly("array");
ast.children[0].items[2].value.items.length.should.be.exactly(2);
ast.children[0].items[2].value.items[0].value.value.should.be.exactly("item5");
ast.children[0].items[2].value.items[1].value.value.should.be.exactly("item6");
});
});
describe('mixed tests / coverage', function() {
it('test empty array', function() {
var ast = parser.parseEval('$a = []; $b = array();');
ast.children[0].right.items.length.should.be.exactly(0);
ast.children[1].right.items.length.should.be.exactly(0);
});
it('test short form / keys', function() {
var ast = parser.parseEval('[0 => &$foo, $bar => "foobar"];');
ast.children[0].items.length.should.be.exactly(2);
ast.children[0].shortForm.should.be.exactly(true);
ast.children[0].items[0].key.kind.should.be.exactly('number');
ast.children[0].items[0].value.kind.should.be.exactly('variable');
ast.children[0].items[0].value.byref.should.be.exactly(true);
ast.children[0].items[0].value.name.should.be.exactly('foo');
ast.children[0].items[0].value.byref.should.be.exactly(true);
ast.children[0].items[1].key.kind.should.be.exactly('variable');
ast.children[0].items[1].key.name.should.be.exactly('bar');
ast.children[0].items[1].key.byref.should.be.exactly(false);
});
});
});