tolerant-php-parser/tests/CommentsTest.php

79 строки
4.0 KiB
PHP
Исходник Обычный вид История

2016-10-02 23:01:18 +03:00
<?php
2016-12-19 01:49:28 +03:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
2016-10-02 23:01:18 +03:00
use PHPUnit\Framework\TestCase;
// TODO autoload classes
2016-10-02 23:11:45 +03:00
require_once(__DIR__ . "/../lexer.php");
require_once(__DIR__ . "/../parser.php");
2016-10-02 23:11:45 +03:00
require_once(__DIR__ . "/../Token.php");
2016-10-02 23:01:18 +03:00
class CommentsTest extends TestCase {
// TODO test w/ multiple files
const FILENAME = __dir__ . "/cases/commentsFile.php";
const PARSER_POC_FILE = __dir__ . "/cases/parserPocFile.php";
private $lexer;
2016-10-02 23:01:18 +03:00
/**
* TODO not actually a test - just a convenience during initial development
*/
2016-10-02 23:40:16 +03:00
public function testCommentsFile() {
2016-12-27 04:36:20 +03:00
$this->lexer = new \PhpParser\Lexer(file_get_contents(self::FILENAME));
2016-10-13 04:26:42 +03:00
$tokensArray = $this->lexer->getTokensArray();
2016-10-02 23:01:18 +03:00
$expected = array(
2016-11-02 22:19:22 +03:00
new PhpParser\Token(PhpParser\TokenKind::ScriptSectionStartTag, 0, 0, 6),
new PhpParser\Token(PhpParser\TokenKind::Name, 6, 56, 51),
2016-10-03 06:26:01 +03:00
new PhpParser\Token(PhpParser\TokenKind::SlashToken, 57, 58, 2),
2016-10-03 03:21:02 +03:00
new PhpParser\Token(PhpParser\TokenKind::Name, 59, 59, 1),
2016-10-03 06:51:00 +03:00
new PhpParser\Token(PhpParser\TokenKind::SemicolonToken, 60, 60, 1),
2016-10-03 03:21:02 +03:00
new PhpParser\Token(PhpParser\TokenKind::Name, 61, 63, 3),
2016-10-03 06:26:01 +03:00
new PhpParser\Token(PhpParser\TokenKind::SlashEqualsToken, 64, 64, 2),
2016-10-12 01:30:00 +03:00
new PhpParser\Token(PhpParser\TokenKind::DecimalLiteralToken, 66, 66, 1),
2016-10-03 06:51:00 +03:00
new PhpParser\Token(PhpParser\TokenKind::SemicolonToken, 67, 67, 1),
2016-10-03 02:34:50 +03:00
new PhpParser\Token(PhpParser\TokenKind::VariableName, 68, 70, 6),
2016-10-03 06:26:01 +03:00
new PhpParser\Token(PhpParser\TokenKind::AndKeyword, 74, 76, 5),
2016-10-09 01:22:12 +03:00
new PhpParser\Token(PhpParser\TokenKind::Name, 79, 81, 3),
new PhpParser\Token(PhpParser\TokenKind::AsteriskAsteriskToken, 82, 82, 2),
new PhpParser\Token(PhpParser\TokenKind::AsteriskAsteriskEqualsToken, 84, 84, 3),
new PhpParser\Token(PhpParser\TokenKind::EndOfFileToken, 87, 97, 10)
2016-10-02 23:01:18 +03:00
);
foreach ($tokensArray as $index=>$token) {
$this->assertEquals($expected[$index], $token, "Awesome");
}
}
public function testParserPocFile() {
2016-12-27 04:36:20 +03:00
$this->lexer = new \PhpParser\Lexer(file_get_contents(self::PARSER_POC_FILE));
2016-10-13 04:26:42 +03:00
$tokensArray = $this->lexer->getTokensArray();
$expected = array(
2016-11-02 22:19:22 +03:00
new PhpParser\Token(PhpParser\TokenKind::ScriptSectionStartTag, 0, 0, 6),
new PhpParser\Token(PhpParser\TokenKind::ClassKeyword, 6, 7, 6),
new PhpParser\Token(PhpParser\TokenKind::Name, 12, 13, 2),
new PhpParser\Token(PhpParser\TokenKind::OpenBraceToken, 14, 16, 3),
new PhpParser\Token(PhpParser\TokenKind::FunctionKeyword, 17, 56, 47),
new PhpParser\Token(PhpParser\TokenKind::Name, 64, 65, 4),
new PhpParser\Token(PhpParser\TokenKind::OpenParenToken, 68, 68, 1),
new PhpParser\Token(PhpParser\TokenKind::VariableName, 69, 69, 8),
new PhpParser\Token(PhpParser\TokenKind::CloseParenToken, 77, 77, 1),
new PhpParser\Token(PhpParser\TokenKind::OpenBraceToken, 78, 78, 1),
new PhpParser\Token(PhpParser\TokenKind::EchoKeyword, 79, 88, 13),
new PhpParser\Token(PhpParser\TokenKind::VariableName, 92, 92, 8),
new PhpParser\Token(PhpParser\TokenKind::SemicolonToken, 100, 100, 1),
new PhpParser\Token(PhpParser\TokenKind::PublicKeyword, 101, 107, 12),
new PhpParser\Token(PhpParser\TokenKind::VariableName, 113, 114, 3),
new PhpParser\Token(PhpParser\TokenKind::SemicolonToken, 116, 116, 1),
new PhpParser\Token(PhpParser\TokenKind::EndOfFileToken, 117,117,0)
);
foreach ($tokensArray as $index=>$token) {
$this->assertEquals($expected[$index], $token, "Awesome");
}
}
2016-10-02 23:01:18 +03:00
}