Skip to content

Instantly share code, notes, and snippets.

@drgomesp
Last active January 4, 2016 15:09
Show Gist options
  • Save drgomesp/8638571 to your computer and use it in GitHub Desktop.
Save drgomesp/8638571 to your computer and use it in GitHub Desktop.
The base test class for the Greppy regular expressions library
<?php
namespace Greppy;
function p()
{
return Pattern::create();
}
class PatternTest extends \PHPUnit_Framework_TestCase
{
protected function assertPreConditions()
{
$this->assertTrue(class_exists($class = 'Greppy\Pattern'),
'Class not found: ' . $class);
}
public function providePatterns()
{
return [
[p()->any , '.'],
[p()->literal('f/oo') , 'f\/oo'],
[p()->range('A', 'Z') , '[A-Z]'],
[p()->capture->alternatives(['foo', 'bar']) , '(foo|bar)'],
[p()->word , '\w'],
[p()->nonWord , '\W'],
[p()->whitespace , '\s'],
[p()->nonWhitespace , '\S'],
[p()->digit , '\d'],
[p()->nonDigit , '\D'],
[p()->any->zeroOrMore , '.*'],
[p()->any->oneOrMore , '.+'],
[p()->any->optional , '.?'],
[p()->any->repeatInterval(2, 4) , '.{2,4}'],
[p()->any->until->digit , '.*\d'],
[p()->any->until->capture->digit , '.*(\d)'],
[p()->capture->digit->capture->any , '(\d)(.)'],
[p()->line(p()->any) , '/^.$/'],
];
}
/**
* @dataProvider providePatterns
*/
public function testRandomPatterns($greppyPattern, $pattern)
{
$this->assertEquals($pattern, (string) $greppyPattern);
}
public function provideXMLShadowParsingPatterns()
{
return [
[$textScanningExpression = p()->not('<')->more , '[^<]+'],
[$untilHyphen = p()->not('-')->until->literal('-') , '[^-]*\-'],
[$untilTwoHyphens =
$untilHyphen .
p()->silent(
p()->not('-') .
$untilHyphen
) .
p()->until->literal('-')
, '[^-]*\-(?:[^-][^-]*\-)*\-'],
];
}
/**
* @dataProvider provideXMLShadowParsingPatterns
*/
public function testXMLShadowParsingPatterns($greppyPattern, $pattern)
{
$this->assertEquals($pattern, (string) $greppyPattern);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment