1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Morphism\Parse; |
4
|
|
|
|
5
|
|
|
use Graze\Morphism\Test\Parse\TestCase; |
6
|
|
|
|
7
|
|
|
class TokenTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
public function testIsEof() |
10
|
|
|
{ |
11
|
|
|
$eof = new Token('EOF'); |
12
|
|
|
$this->assertTrue($eof->isEof()); |
13
|
|
|
|
14
|
|
|
$notEof = new Token('number', '123'); |
15
|
|
|
$this->assertFalse($notEof->isEof()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testEq() |
19
|
|
|
{ |
20
|
|
|
$token = new Token('string', 'test'); |
21
|
|
|
|
22
|
|
|
$this->assertTrue($token->eq('string', 'test')); |
23
|
|
|
$this->assertTrue($token->eq('string', 'Test')); |
24
|
|
|
$this->assertTrue($token->eq('string', 'TEST')); |
25
|
|
|
|
26
|
|
|
$this->assertFalse($token->eq('string', 'testtest')); |
27
|
|
|
$this->assertFalse($token->eq('number', 'test')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testMakeStringEscapes() |
31
|
|
|
{ |
32
|
|
|
$sq = "'"; |
33
|
|
|
$bs = "\\"; |
34
|
|
|
|
35
|
|
|
$escaped = Token::fromString( |
36
|
|
|
"bcd{$bs}{$bs}" . |
37
|
|
|
"cde{$bs}0" . |
38
|
|
|
"def{$bs}b" . |
39
|
|
|
"efg{$bs}n" . |
40
|
|
|
"fgh{$bs}r" . |
41
|
|
|
"ghi{$bs}t" . |
42
|
|
|
"hij{$bs}z" . |
43
|
|
|
"xyz", |
44
|
|
|
$sq |
45
|
|
|
); |
46
|
|
|
$this->assertTrue( |
47
|
|
|
$escaped->eq( |
48
|
|
|
'string', |
49
|
|
|
"bcd{$bs}" . |
50
|
|
|
"cde" . chr(0) . |
51
|
|
|
"def" . chr(8) . |
52
|
|
|
"efg" . chr(10) . |
53
|
|
|
"fgh" . chr(13) . |
54
|
|
|
"ghi" . chr(9) . |
55
|
|
|
"hij" . chr(26) . |
56
|
|
|
"xyz" |
57
|
|
|
), |
58
|
|
|
"all escape sequences are correctly processed" |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testMakeStringNonEscapes() |
63
|
|
|
{ |
64
|
|
|
$sq = "'"; |
65
|
|
|
$dq = '"'; |
66
|
|
|
$bs = "\\"; |
67
|
|
|
|
68
|
|
|
$unescaped = Token::fromString( |
69
|
|
|
"ijk{$bs}a" . |
70
|
|
|
"jkl{$bs}f" . |
71
|
|
|
"klm{$bs}${sq}" . |
72
|
|
|
"lmn{$bs}${dq}" . |
73
|
|
|
"mno{$bs}?" . |
74
|
|
|
"nop{$bs}176" . |
75
|
|
|
"opq{$bs}x7e" . |
76
|
|
|
"xyz", |
77
|
|
|
$sq |
78
|
|
|
); |
79
|
|
|
$this->assertTrue( |
80
|
|
|
$unescaped->eq( |
81
|
|
|
'string', |
82
|
|
|
"ijk" . "a" . |
83
|
|
|
"jkl" . "f" . |
84
|
|
|
"klm" . "${sq}" . |
85
|
|
|
"lmn" . "${dq}" . |
86
|
|
|
"mno" . "?" . |
87
|
|
|
"nop" . "176" . |
88
|
|
|
"opq" . "x7e" . |
89
|
|
|
"xyz" |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testMakeStringQuotes() |
95
|
|
|
{ |
96
|
|
|
$sq = "'"; |
97
|
|
|
|
98
|
|
|
foreach ([ |
99
|
|
|
"" => "", |
100
|
|
|
"{$sq}${sq}" => "{$sq}", |
101
|
|
|
"abc{$sq}${sq}" => "abc{$sq}", |
102
|
|
|
"{$sq}${sq}abc" => "{$sq}abc", |
103
|
|
|
] as $arg => $result) { |
104
|
|
|
$token = Token::fromString($arg, $sq); |
105
|
|
|
$this->assertTrue($token->eq('string', $result)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$token = Token::fromString("{$sq}${sq}", $sq); |
109
|
|
|
$this->assertTrue($token->eq('string', "{$sq}")); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testMakeIdentifier() |
113
|
|
|
{ |
114
|
|
|
$bs = "\\"; |
115
|
|
|
|
116
|
|
|
$token = Token::fromIdentifier( |
117
|
|
|
"abc``def" . |
118
|
|
|
"{$bs}0" . |
119
|
|
|
"{$bs}b" . |
120
|
|
|
"{$bs}n" . |
121
|
|
|
"{$bs}r" . |
122
|
|
|
"{$bs}t" . |
123
|
|
|
"{$bs}z" . |
124
|
|
|
"" |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$this->assertTrue( |
128
|
|
|
$token->eq( |
129
|
|
|
'identifier', |
130
|
|
|
'abc`def' . |
131
|
|
|
"{$bs}0" . |
132
|
|
|
"{$bs}b" . |
133
|
|
|
"{$bs}n" . |
134
|
|
|
"{$bs}r" . |
135
|
|
|
"{$bs}t" . |
136
|
|
|
"{$bs}z" . |
137
|
|
|
"" |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testEscapeStringNull() |
143
|
|
|
{ |
144
|
|
|
$this->assertSame('NULL', Token::escapeString(null)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testEscapeString() |
148
|
|
|
{ |
149
|
|
|
$bs = "\\"; |
150
|
|
|
$sq = "'"; |
151
|
|
|
$nul = chr(0); |
152
|
|
|
$lf = chr(10); |
153
|
|
|
$cr = chr(13); |
154
|
|
|
|
155
|
|
|
$this->assertSame( |
156
|
|
|
"{$sq}" . |
157
|
|
|
"abc''" . |
158
|
|
|
"def{$bs}0" . |
159
|
|
|
"ghi{$bs}n" . |
160
|
|
|
"jkl{$bs}r" . |
161
|
|
|
"mno{$bs}{$bs}" . |
162
|
|
|
"pqr" . |
163
|
|
|
"{$sq}", |
164
|
|
|
Token::escapeString( |
165
|
|
|
"abc{$sq}" . |
166
|
|
|
"def{$nul}" . |
167
|
|
|
"ghi{$lf}" . |
168
|
|
|
"jkl{$cr}" . |
169
|
|
|
"mno{$bs}" . |
170
|
|
|
"pqr" |
171
|
|
|
) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @dataProvider providerEscapeIdentifier |
177
|
|
|
* @param bool $quoteNames |
178
|
|
|
* @param string $arg |
179
|
|
|
* @param string $expected |
180
|
|
|
*/ |
181
|
|
|
public function testEscapeIdentifier($quoteNames, $arg, $expected) |
182
|
|
|
{ |
183
|
|
|
Token::setQuoteNames($quoteNames); |
184
|
|
|
$this->assertSame($expected, Token::escapeIdentifier($arg)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function providerEscapeIdentifier() |
191
|
|
|
{ |
192
|
|
|
return [ |
193
|
|
|
[true, '', '``' ], |
194
|
|
|
[true, 'a', '`a`' ], |
195
|
|
|
[true, 'abc def', '`abc def`' ], |
196
|
|
|
[true, 'abc`def', '`abc``def`'], |
197
|
|
|
|
198
|
|
|
[false, '', '' ], |
199
|
|
|
[false, 'a', 'a' ], |
200
|
|
|
[false, 'abc def', 'abc def' ], |
201
|
|
|
[false, 'abc`def', 'abc`def' ], |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @dataProvider providerAsXyz |
207
|
|
|
* @param string $method |
208
|
|
|
* @param string $type |
209
|
|
|
* @param string $arg |
210
|
|
|
* @param mixed $expected |
211
|
|
|
*/ |
212
|
|
|
public function testAsXyz($method, $type, $arg, $expected) |
213
|
|
|
{ |
214
|
|
|
$token = new Token($type, $arg); |
215
|
|
|
$this->assertSame($expected, $token->$method()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function providerAsXyz() |
222
|
|
|
{ |
223
|
|
|
return [ |
224
|
|
|
['asString', 'string', 'abc', 'abc' ], |
225
|
|
|
['asString', 'string', '', '' ], |
226
|
|
|
|
227
|
|
|
['asString', 'number', '123', '123' ], |
228
|
|
|
['asString', 'number', '0123', '123' ], |
229
|
|
|
['asString', 'number', '-0123', '-123' ], |
230
|
|
|
['asString', 'number', '1.2300', '1.2300' ], |
231
|
|
|
['asString', 'number', '1.234567890123', '1.234567890123' ], |
232
|
|
|
['asString', 'number', '-1.23', '-1.23' ], |
233
|
|
|
['asString', 'number', '+1.23', '1.23' ], |
234
|
|
|
['asString', 'number', '.23', '0.23' ], |
235
|
|
|
['asString', 'number', '', '0' ], |
236
|
|
|
|
237
|
|
|
// TODO - work is needed on Token::asString to make these tests parse: |
|
|
|
|
238
|
|
|
// ['asString', 'number', '1.234e1', '12.34' ], |
|
|
|
|
239
|
|
|
// ['asString', 'number', '1.234000e15', '1.234e15' ], |
|
|
|
|
240
|
|
|
// ['asString', 'number', '0.999999e15', '999999000000000' ], |
|
|
|
|
241
|
|
|
// ['asString', 'number', '-1.234000e15', '-1.234e15' ], |
|
|
|
|
242
|
|
|
// ['asString', 'number', '-0.999999e15', '-999999000000000'], |
|
|
|
|
243
|
|
|
// ['asString', 'number', '1.0001e-15', '0.0000000000000010001' ], |
|
|
|
|
244
|
|
|
// ['asString', 'number', '0.999e-16', '9.99e-16' ], |
|
|
|
|
245
|
|
|
// ['asString', 'number', '-1.0001e-15', '-0.0000000000000010001' ], |
|
|
|
|
246
|
|
|
// ['asString', 'number', '-0.999e-16', '-9.99e-16' ], |
|
|
|
|
247
|
|
|
|
248
|
|
|
['asString', 'hex', '68656c6c6f21', 'hello!' ], |
249
|
|
|
['asString', 'bin', '0111111000100011', '~#' ], |
250
|
|
|
|
251
|
|
|
['asNumber', 'string', '123', 123 ], |
252
|
|
|
['asNumber', 'number', '456', 456 ], |
253
|
|
|
['asNumber', 'hex', 'fffe', 65534 ], |
254
|
|
|
['asNumber', 'bin', '10100101', 165 ], |
255
|
|
|
|
256
|
|
|
['asDate', 'string', '0', '0000-00-00'], |
257
|
|
|
['asDate', 'string', '0000-00-00', '0000-00-00'], |
258
|
|
|
['asDate', 'string', '1970-08-12', '1970-08-12'], |
259
|
|
|
['asDate', 'string', '2001-12-31', '2001-12-31'], |
260
|
|
|
|
261
|
|
|
['asTime', 'string', '0', '00:00:00' ], |
262
|
|
|
['asTime', 'string', '00:00:00', '00:00:00' ], |
263
|
|
|
['asTime', 'string', '17:45:59', '17:45:59' ], |
264
|
|
|
|
265
|
|
|
['asDateTime', 'string', '0', '0000-00-00 00:00:00'], |
266
|
|
|
['asDateTime', 'string', '0000-00-00', '0000-00-00 00:00:00'], |
267
|
|
|
['asDateTime', 'string', '1970-08-12', '1970-08-12 00:00:00'], |
268
|
|
|
['asDateTime', 'string', '2001-12-31', '2001-12-31 00:00:00'], |
269
|
|
|
['asDateTime', 'string', '0000-00-00 00:00:00', '0000-00-00 00:00:00'], |
270
|
|
|
['asDateTime', 'string', '1970-08-12 13:34:45', '1970-08-12 13:34:45'], |
271
|
|
|
['asDateTime', 'string', '2001-12-31 23:59:59', '2001-12-31 23:59:59'], |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @dataProvider providerAsXyzError |
277
|
|
|
* @expectedException \Exception |
278
|
|
|
* @param string $method |
279
|
|
|
* @param string $type |
280
|
|
|
* @param string $arg |
281
|
|
|
*/ |
282
|
|
|
public function testAsXyzError($method, $type, $arg) |
283
|
|
|
{ |
284
|
|
|
(new Token($type, $arg))->$method(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
|
|
public function providerAsXyzError() |
291
|
|
|
{ |
292
|
|
|
return [ |
293
|
|
|
['asDate', 'string', 'abc' ], |
294
|
|
|
['asDate', 'string', '1970' ], |
295
|
|
|
['asDate', 'string', '19700812' ], |
296
|
|
|
['asDate', 'string', '1970/08/12'], |
297
|
|
|
|
298
|
|
|
['asTime', 'string', 'abc' ], |
299
|
|
|
['asTime', 'string', '000000' ], |
300
|
|
|
['asTime', 'string', '1745' ], |
301
|
|
|
['asTime', 'string', '174559' ], |
302
|
|
|
|
303
|
|
|
['asDateTime', 'string', 'abc' ], |
304
|
|
|
['asDateTime', 'string', '19700812' ], |
305
|
|
|
['asDateTime', 'string', '1970/08/12' ], |
306
|
|
|
['asDateTime', 'string', '197008120000' ], |
307
|
|
|
['asDateTime', 'string', '19700812000000'], |
308
|
|
|
|
309
|
|
|
['asString', 'symbol', 'abc' ], |
310
|
|
|
|
311
|
|
|
['asNumber', 'symbol', 'abc' ], |
312
|
|
|
]; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param Token $token |
317
|
|
|
* @param string $expected |
318
|
|
|
* @dataProvider provideToDebugString |
319
|
|
|
*/ |
320
|
|
|
public function testToDebugString(Token $token, $expected) |
321
|
|
|
{ |
322
|
|
|
$this->assertEquals($expected, $token->toDebugString()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return array |
327
|
|
|
*/ |
328
|
|
|
public function provideToDebugString() |
329
|
|
|
{ |
330
|
|
|
$text = 'foo'; |
331
|
|
|
|
332
|
|
|
return [ |
333
|
|
|
// Token, Expected output |
334
|
|
|
[new Token(Token::BIN, $text), 'bin[foo]'], |
335
|
|
|
[new Token(Token::COMMENT, $text), 'comment[foo]'], |
336
|
|
|
[new Token(Token::CONDITIONAL_END, $text), 'conditional-end[foo]'], |
337
|
|
|
[new Token(Token::CONDITIONAL_START, $text), 'conditional-start[foo]'], |
338
|
|
|
[new Token(Token::EOF, $text), 'EOF[foo]'], |
339
|
|
|
[new Token(Token::HEX, $text), 'hex[foo]'], |
340
|
|
|
[new Token(Token::IDENTIFIER, $text), 'identifier[foo]'], |
341
|
|
|
[new Token(Token::NUMBER, $text), 'number[foo]'], |
342
|
|
|
[new Token(Token::SYMBOL, $text), 'symbol[foo]'], |
343
|
|
|
[new Token(Token::WHITESPACE, $text), 'whitespace[foo]'], |
344
|
|
|
]; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|