Skip to content

Commit

Permalink
add FuzzyTest
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Apr 12, 2018
1 parent 958806c commit 7d6b297
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,2 +1,5 @@
/temp
/tools
/vendor

/composer.lock
25 changes: 25 additions & 0 deletions 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
12 changes: 11 additions & 1 deletion build.xml
Expand Up @@ -84,7 +84,7 @@
</exec>
</target>

<target name="tests">
<target name="tests" depends="build-abnfgen">
<exec
executable="vendor/bin/phpunit"
logoutput="true"
Expand All @@ -111,4 +111,14 @@
<arg path="tests"/>
</exec>
</target>

<target name="build-abnfgen">
<exec
executable="./build-abnfgen.sh"
logoutput="true"
passthru="true"
checkreturn="true"
>
</exec>
</target>
</project>
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -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/"]}
Expand Down
104 changes: 104 additions & 0 deletions tests/PHPStan/Parser/FuzzyTest.php
@@ -0,0 +1,104 @@
<?php declare(strict_types = 1);

namespace PHPStan\PhpDocParser\Parser;

use Iterator;
use PHPStan\PhpDocParser\Lexer\Lexer;
use Symfony\Component\Process\Process;

class FuzzyTest extends \PHPUnit\Framework\TestCase
{

/** @var Lexer */
private $lexer;

/** @var TypeParser */
private $typeParser;

/** @var ConstExprParser */
private $constExprParser;

protected function setUp()
{
parent::setUp();
$this->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];
}
}

}

0 comments on commit 7d6b297

Please sign in to comment.