function streamEqual($stream1, $stream2) { if (count($stream1) != count($stream2)) { return false; } foreach ($stream1 as $i => $token) { if (!tokenEqual($stream1[$i], $stream2[$i])) { return false; } } return true; }
<?php require './test.php'; require '../src/TokenStream.php'; $tokenStream = new TokenStream("<?php\ndie();"); group('get', function () use($tokenStream) { test(streamEqual($tokenStream->get(1, 2), array(new Token(T_EXIT, 'die'), new Token(T_OPEN_ROUND, '('))), true, 'get(int, int)'); }); group('extract', function () use($tokenStream) { $tokenStream = clone $tokenStream; test(streamEqual($tokenStream->extract(1, 2), array(new Token(T_EXIT, 'die'), new Token(T_OPEN_ROUND, '('))), true, 'return value of extract(int, int)'); test(streamEqual($tokenStream->get(0, count($tokenStream) - 1), array(new Token(T_OPEN_TAG, "<?php\n"), new Token(T_CLOSE_ROUND, ')'), new Token(T_SEMICOLON, ';'))), true, 'stream change of extract(int, int)'); test(tokenEqual($tokenStream->extract(2), new Token(T_SEMICOLON, ';')), true, 'return value of extract(int)'); test(streamEqual($tokenStream->get(0, count($tokenStream) - 1), array(new Token(T_OPEN_TAG, "<?php\n"), new Token(T_CLOSE_ROUND, ')'))), true, 'stream change of extract(int)'); }); group('append', function () use($tokenStream) { $tokenStream = clone $tokenStream; $tokenStream->append(array(new Token(T_WHITESPACE, "\n"), '{', '}', array(';'))); test((string) $tokenStream, "<?php\ndie();\n{};", 'append token array'); $tokenStream->append(';'); test((string) $tokenStream, "<?php\ndie();\n{};;", 'append single token'); }); group('insert', function () use($tokenStream) { $tokenStream = clone $tokenStream; $tokenStream->insert(1, array('{', '}', array(';', new Token(T_WHITESPACE, "\n")))); test((string) $tokenStream, "<?php\n{};\ndie();", 'insert token array'); $tokenStream->insert(4, ';'); test((string) $tokenStream, "<?php\n{};;\ndie();", 'insert single token'); });
$token->undefined; }, 'InvalidArgumentException', 'getting undefined property'); test($token->type, T_OPEN_ROUND, 'getting property: type'); test($token->content, '(', 'getting property: content'); test($token->line, 0, 'getting property: line'); test($token->id, 1, 'getting property: id'); test($token->name, "'('", 'getting property: name (char token)'); test($funcToken->name, 'T_FUNCTION', 'getting property: name ("real" token)'); test((string) $token, '(', '(string) $token'); }); group('magic setters', function () use($token, $funcToken) { testException(function () use($token) { $token->undefined = 'a'; }, 'InvalidArgumentException', 'setting undefined property'); testException(function () use($token) { $token->id = 1; }, 'InvalidArgumentException', 'setting id not allowed'); testException(function () use($token) { $token->name = 1; }, 'InvalidArgumentException', 'setting name not allowed'); $token = clone $token; $token->type = T_FUNCTION; $token->content = 'function'; $token->line = 1; test(tokenEqual($token, $funcToken), true, 'setting properties'); }); group('Token->is()', function () use($token) { test($token->is(T_OPEN_ROUND), true, 'is(token)'); test($token->is(array(T_CLOSE_ROUND, T_OPEN_ROUND)), true, 'is(array(token, token))'); test($token->is(T_CLOSE_ROUND, T_OPEN_ROUND), true, 'is(token, token)'); });