This repository has been archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserTest.php
405 lines (352 loc) · 14.5 KB
/
ParserTest.php
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/*
* This file is part of the Typhax package.
*
* Copyright © 2012 Erin Millard
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eloquent\Typhax\Parser;
use Eloquent\Cosmos\ClassName;
use Eloquent\Typhax\Lexer\Lexer;
use Eloquent\Typhax\Type\AndType;
use Eloquent\Typhax\Type\ArrayType;
use Eloquent\Typhax\Type\BooleanType;
use Eloquent\Typhax\Type\CallableType;
use Eloquent\Typhax\Type\ExtensionType;
use Eloquent\Typhax\Type\FloatType;
use Eloquent\Typhax\Type\IntegerType;
use Eloquent\Typhax\Type\MixedType;
use Eloquent\Typhax\Type\NullType;
use Eloquent\Typhax\Type\NumericType;
use Eloquent\Typhax\Type\ObjectType;
use Eloquent\Typhax\Type\OrType;
use Eloquent\Typhax\Type\ResourceType;
use Eloquent\Typhax\Type\StreamType;
use Eloquent\Typhax\Type\StringType;
use Eloquent\Typhax\Type\StringableType;
use Eloquent\Typhax\Type\TraversableType;
use Eloquent\Typhax\Type\TupleType;
use Eloquent\Typhax\Type\Type;
use PHPUnit_Framework_TestCase;
use ReflectionObject;
class ParserTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
parent::setUp();
$this->_lexer = new Lexer;
}
public function parserData()
{
$data = array();
$source = ' foo ';
$position = 6;
$expected = new ObjectType(ClassName::fromString('foo'));
$data['Basic example'] = array($expected, $position, $source);
$source = ' foo < bar > ';
$position = 14;
$expected = new TraversableType(
new ObjectType(ClassName::fromString('foo')),
new MixedType,
new ObjectType(ClassName::fromString('bar'))
);
$data['Simple traversable 1'] = array($expected, $position, $source);
$source = ' foo < bar , baz > ';
$position = 20;
$expected = new TraversableType(
new ObjectType(ClassName::fromString('foo')),
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz'))
);
$data['Simple traversable 2'] = array($expected, $position, $source);
$source = ' foo < bar , baz < qux , doom > > ';
$position = 35;
$expected = new TraversableType(
new ObjectType(ClassName::fromString('foo')),
new ObjectType(ClassName::fromString('bar')),
new TraversableType(
new ObjectType(ClassName::fromString('baz')),
new ObjectType(ClassName::fromString('qux')),
new ObjectType(ClassName::fromString('doom'))
)
);
$data['Nested subtypes'] = array($expected, $position, $source);
$source = ' foo | bar ';
$position = 12;
$expected = new OrType(array(
new ObjectType(ClassName::fromString('foo')),
new ObjectType(ClassName::fromString('bar')),
));
$data['Basic composite OR'] = array($expected, $position, $source);
$source = ' foo + bar ';
$position = 12;
$expected = new AndType(array(
new ObjectType(ClassName::fromString('foo')),
new ObjectType(ClassName::fromString('bar')),
));
$data['Basic composite AND'] = array($expected, $position, $source);
$source = ' foo + bar + baz ';
$position = 18;
$expected = new AndType(array(
new ObjectType(ClassName::fromString('foo')),
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz')),
));
$data['Chained composite AND'] = array($expected, $position, $source);
$source = ' foo | bar + baz ';
$position = 18;
$expected = new OrType(array(
new ObjectType(ClassName::fromString('foo')),
new AndType(array(
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz')),
))
));
$data['Composite precedence'] = array($expected, $position, $source);
$source = ' foo < bar | baz , qux | doom > ';
$position = 33;
$expected = new TraversableType(
new ObjectType(ClassName::fromString('foo')),
new OrType(array(
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz')),
)),
new OrType(array(
new ObjectType(ClassName::fromString('qux')),
new ObjectType(ClassName::fromString('doom')),
))
);
$data['Composite types nested inside traversable'] = array($expected, $position, $source);
$source = ' tuple < bar | baz , qux | doom > ';
$position = 35;
$expected = new TupleType(array(
new OrType(array(
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz')),
)),
new OrType(array(
new ObjectType(ClassName::fromString('qux')),
new ObjectType(ClassName::fromString('doom')),
)),
));
$data['Composite types nested inside tuple'] = array($expected, $position, $source);
$source = ' foo | bar < baz , qux > ';
$position = 25;
$expected = new OrType(array(
new ObjectType(ClassName::fromString('foo')),
new TraversableType(
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz')),
new ObjectType(ClassName::fromString('qux'))
),
));
$data['Traversable type nested inside composite'] = array($expected, $position, $source);
$source = ' boolean | callable | float | integer | null | numeric | object | string | stringable | mixed ';
$position = 95;
$expected = new OrType(array(
new BooleanType,
new CallableType,
new FloatType,
new IntegerType,
new NullType,
new NumericType,
new ObjectType,
new StringType,
new StringableType,
new MixedType,
));
$data['Instrinsic types'] = array($expected, $position, $source);
$source = ' bool | callback | double | int | long | number | real | scalar ';
$position = 65;
$expected = new OrType(array(
new BooleanType,
new CallableType,
new FloatType,
new IntegerType,
new IntegerType,
new OrType(array(
new IntegerType,
new FloatType,
)),
new FloatType,
new OrType(array(
new IntegerType,
new FloatType,
new StringType,
new BooleanType,
)),
));
$data['Intrinsic aliases'] = array($expected, $position, $source);
$source = ' array ';
$position = 8;
$expected = new TraversableType(
new ArrayType,
new MixedType,
new MixedType
);
$data['Array'] = array($expected, $position, $source);
$source = ' tuple < foo , bar , baz > ';
$position = 28;
$expected = new TupleType(array(
new ObjectType(ClassName::fromString('foo')),
new ObjectType(ClassName::fromString('bar')),
new ObjectType(ClassName::fromString('baz')),
));
$data['Tuple type'] = array($expected, $position, $source);
$source = ' resource ';
$position = 11;
$expected = new ResourceType;
$data['Resource type'] = array($expected, $position, $source);
$source = ' resource { ofType : foo } ';
$position = 28;
$expected = new ResourceType('foo');
$data['Resource type with ofType attribute'] = array($expected, $position, $source);
$source = ' resource { ofType : stream } ';
$position = 31;
$expected = new ResourceType('stream');
$data['Resource type with ofType attribute of reserved type name'] = array($expected, $position, $source);
$source = ' stream ';
$position = 9;
$expected = new StreamType;
$data['Stream type'] = array($expected, $position, $source);
$source = ' stream { readable: true, writable: false } ';
$position = 45;
$expected = new StreamType(true, false);
$data['Stream type with attributes'] = array($expected, $position, $source);
$source = ' stream { writable: true } ';
$position = 28;
$expected = new StreamType(null, true);
$data['Stream type with only writable attribute'] = array($expected, $position, $source);
$source = ' foo bar ';
$position = 6;
$expected = new ObjectType(ClassName::fromString('foo'));
$data["Don't parse past end of type"] = array($expected, $position, $source);
$source = ' : Foo\Bar ';
$position = 12;
$expected = new ExtensionType(ClassName::fromString('Foo\Bar'), array());
$data["Parse extension type"] = array($expected, $position, $source);
$source = ' : Foo\Bar { foo: bar }';
$position = 24;
$expected = new ExtensionType(ClassName::fromString('Foo\Bar'), array('foo' => 'bar'));
$data["Parse extension type with attributes"] = array($expected, $position, $source);
return $data;
}
/**
* @dataProvider parserData
*/
public function testParser(Type $expected, $position, $source)
{
$tokens = $this->_lexer->tokens($source);
$parser = new Parser;
$this->assertEquals($expected, $parser->parse($source, $actualPosition));
$this->assertSame($position, $actualPosition);
}
public function parserFailureData()
{
$data = array();
// #0: Empty string
$source = '';
$expectedClass = __NAMESPACE__.'\Exception\UnexpectedTokenException';
$expectedMessage = 'Unexpected END at position 0. Expected one of STRING, TYPE_NAME, NULL, COLON.';
$data[] = array($expectedClass, $expectedMessage, $source);
// #1: Whitespace string
$source = ' ';
$expectedClass = __NAMESPACE__.'\Exception\UnexpectedTokenException';
$expectedMessage = 'Unexpected END at position 2. Expected one of STRING, TYPE_NAME, NULL, COLON.';
$data[] = array($expectedClass, $expectedMessage, $source);
// #2: Empty type list
$source = ' foo < > ';
$expectedClass = __NAMESPACE__.'\Exception\UnexpectedTokenException';
$expectedMessage = 'Unexpected GREATER_THAN at position 8. Expected one of STRING, TYPE_NAME, NULL, COLON.';
$data[] = array($expectedClass, $expectedMessage, $source);
// #3: Empty attributes
$source = ' resource { } ';
$expectedClass = __NAMESPACE__.'\Exception\UnexpectedTokenException';
$expectedMessage = 'Unexpected BRACE_CLOSE at position 13. Expected one of STRING, STRING_QUOTED, TYPE_NAME, INTEGER, FLOAT, NULL, BOOLEAN_TRUE, BOOLEAN_FALSE, BRACE_OPEN, SQUARE_BRACKET_OPEN.';
$data[] = array($expectedClass, $expectedMessage, $source);
// #4: Unsupported attributes
$source = ' resource { foo : bar } ';
$expectedClass = __NAMESPACE__.'\Exception\UnsupportedAttributeException';
$expectedMessage = "Unsupported attribute at position 13. Type 'resource' does not support attribute 'foo'.";
$data[] = array($expectedClass, $expectedMessage, $source);
// #5: Unsupported attributes position calculation check
$source = ' resource { foobar : baz } ';
$expectedClass = __NAMESPACE__.'\Exception\UnsupportedAttributeException';
$expectedMessage = "Unsupported attribute at position 13. Type 'resource' does not support attribute 'foobar'.";
$data[] = array($expectedClass, $expectedMessage, $source);
// #6: Non-traversable as primary in a traversable
$source = ' string < foo > ';
$expectedClass = __NAMESPACE__.'\Exception\UnexpectedTokenException';
$expectedMessage = 'Unexpected LESS_THAN at position 9. Expected one of BRACE_OPEN, PLUS, PIPE.';
$data[] = array($expectedClass, $expectedMessage, $source);
// #7: Traversable with too many types in type list
$source = ' foo < bar , baz , qux , doom , splat > ';
$expectedClass = __NAMESPACE__.'\Exception\UnexpectedTokenException';
$expectedMessage = 'Unexpected COMMA at position 18. Expected GREATER_THAN.';
$data[] = array($expectedClass, $expectedMessage, $source);
return $data;
}
/**
* @dataProvider parserFailureData
*/
public function testParserFailure($expectedClass, $expectedMessage, $source)
{
$parser = new Parser;
$this->setExpectedException($expectedClass, $expectedMessage);
$parser->parse($source);
}
public function hashData()
{
$data = array();
// #0: Basic hash
$source = ' { foo : "bar" , \'baz\' : 666 , qux : .666 , doom : null , splat : true , pop : false } ';
$expected = array(
'foo' => 'bar',
'baz' => 666,
'qux' => .666,
'doom' => null,
'splat' => true,
'pop' => false,
);
$data[] = array($expected, $source);
// #1: Nested hash and array
$source = ' { foo : { bar : baz } , qux : [ doom, [ splat , pop ] ] } ';
$expected = array(
'foo' => array(
'bar' => 'baz',
),
'qux' => array(
'doom',
array(
'splat',
'pop',
),
),
);
$data[] = array($expected, $source);
// #2: Empty hash and array
$source = ' { foo : { } , bar : [ ] } ';
$expected = array(
'foo' => array(),
'bar' => array(),
);
$data[] = array($expected, $source);
return $data;
}
/**
* @dataProvider hashData
*/
public function testParseHash(array $expected, $source)
{
$tokens = $this->_lexer->tokens($source);
$parser = new Parser;
$reflector = new ReflectionObject($parser);
$method = $reflector->getMethod('parseHash');
$method->setAccessible(true);
$arguments = array(&$tokens);
$actual = $method->invokeArgs($parser, $arguments);
$this->assertEquals($expected, $actual);
}
}