From 7d6b29756426d17a135db76032f302d60d8a3c92 Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Thu, 12 Apr 2018 16:16:58 +0200 Subject: [PATCH] add FuzzyTest --- .gitignore | 3 + build-abnfgen.sh | 25 +++++++ build.xml | 12 +++- composer.json | 3 +- tests/PHPStan/Parser/FuzzyTest.php | 104 +++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100755 build-abnfgen.sh create mode 100644 tests/PHPStan/Parser/FuzzyTest.php diff --git a/.gitignore b/.gitignore index de4a392c..c67023cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ +/temp +/tools /vendor + /composer.lock diff --git a/build-abnfgen.sh b/build-abnfgen.sh new file mode 100755 index 00000000..a2911aa2 --- /dev/null +++ b/build-abnfgen.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ROOT_DIR="$DIR" + +if [[ ! -d "$ROOT_DIR/tools/abnfgen" ]]; then + rm -rf "$ROOT_DIR/temp/abnfgen" + mkdir -p "$ROOT_DIR/temp/abnfgen" + + wget http://www.quut.com/abnfgen/abnfgen-0.20.tar.gz \ + --output-document "$ROOT_DIR/temp/abnfgen.tar.gz" + + tar xf "$ROOT_DIR/temp/abnfgen.tar.gz" \ + --directory "$ROOT_DIR/temp/abnfgen" \ + --strip-components 1 + + cd "$ROOT_DIR/temp/abnfgen" + ./configure + make + + mkdir -p "$ROOT_DIR/tools/abnfgen" + mv abnfgen "$ROOT_DIR/tools/abnfgen" + rm -rf "$ROOT_DIR/temp/abnfgen" "$ROOT_DIR/temp/abnfgen.tar.gz" +fi diff --git a/build.xml b/build.xml index 93836f7a..f8b6604c 100644 --- a/build.xml +++ b/build.xml @@ -84,7 +84,7 @@ - + + + + + + diff --git a/composer.json b/composer.json index b237dde3..86e544b7 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "phing/phing": "^2.16.0", "phpstan/phpstan-shim": "^0.9.2", "phpunit/phpunit": "^6.3", - "slevomat/coding-standard": "^3.3.0" + "slevomat/coding-standard": "^3.3.0", + "symfony/process": "^3.4 || ^4.0" }, "autoload": { "psr-4": {"PHPStan\\PhpDocParser\\": ["src/"]} diff --git a/tests/PHPStan/Parser/FuzzyTest.php b/tests/PHPStan/Parser/FuzzyTest.php new file mode 100644 index 00000000..a24c7241 --- /dev/null +++ b/tests/PHPStan/Parser/FuzzyTest.php @@ -0,0 +1,104 @@ +lexer = new Lexer(); + $this->typeParser = new TypeParser(); + $this->constExprParser = new ConstExprParser(); + } + + /** + * @dataProvider provideTypeParserData + * @param string $input + */ + public function testTypeParser(string $input) + { + $tokens = new TokenIterator($this->lexer->tokenize($input)); + $this->typeParser->parse($tokens); + + $this->assertSame( + Lexer::TOKEN_END, + $tokens->currentTokenType(), + sprintf('Failed to parse input %s', $input) + ); + } + + public function provideTypeParserData(): Iterator + { + return $this->provideFuzzyInputsData('Type'); + } + + /** + * @dataProvider provideConstExprParserData + * @param string $input + */ + public function testConstExprParser(string $input) + { + $tokens = new TokenIterator($this->lexer->tokenize($input)); + $this->constExprParser->parse($tokens); + + $this->assertSame( + Lexer::TOKEN_END, + $tokens->currentTokenType(), + sprintf('Failed to parse input %s', $input) + ); + } + + public function provideConstExprParserData(): Iterator + { + return $this->provideFuzzyInputsData('ConstantExpr'); + } + + private function provideFuzzyInputsData(string $startSymbol): Iterator + { + $inputsDirectory = sprintf('%s/fuzzy/%s', __DIR__ . '/../../../temp', $startSymbol); + + if (is_dir($inputsDirectory)) { + foreach (glob(sprintf('%s/*.tst', $inputsDirectory)) as $file) { + unlink($file); + } + + } else { + mkdir($inputsDirectory, 0777, true); + } + + $process = new Process([ + __DIR__ . '/../../../tools/abnfgen/abnfgen', + '-lx', + '-n', + '1000', + '-d', + $inputsDirectory, + '-s', + $startSymbol, + __DIR__ . '/../../../doc/grammars/type.abnf', + ]); + + $process->mustRun(); + + foreach (glob(sprintf('%s/*.tst', $inputsDirectory)) as $file) { + $input = file_get_contents($file); + yield [$input]; + } + } + +}