Skip to content

Commit

Permalink
Inline pattern() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Apr 7, 2023
1 parent 0de4993 commit 6d9ad5d
Show file tree
Hide file tree
Showing 93 changed files with 490 additions and 462 deletions.
6 changes: 3 additions & 3 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -1223,15 +1223,15 @@ Added in 0.9.8
* You can now use [`foreach`](https://www.php.net/manual/en/control-structures.foreach.php) on [`match()`], instead
of [`forEach()`]:
```php
foreach (pattern('\d+')->match('127.0.0.1') as $match) {}
foreach (Pattern::of('\d+')->match('127.0.0.1') as $match) {}
```
and also
```php
foreach (pattern('\d+')->match('127.0.0.1')->asInt() as $digit) {}
foreach (Pattern::of('\d+')->match('127.0.0.1')->asInt() as $digit) {}
```
or
```php
foreach (pattern('\d+')->match('127.0.0.1')->all() as $text) {}
foreach (Pattern::of('\d+')->match('127.0.0.1')->all() as $text) {}
```
* Added [`Match.get(string|int)`], which is a shorthand for `Match.group(string|int).text()`.
* Restored `pattern()->match()->test()`/[`fails()`] that were removed in version 0.9.2.
Expand Down
16 changes: 8 additions & 8 deletions test/Feature/CleanRegex/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PatternTest extends TestCase
public function shouldTest_beFalse_forNotMatching()
{
// when
$test = pattern('\d')->test('abc');
$test = Pattern::of('\d')->test('abc');
// then
$this->assertFalse($test);
}
Expand All @@ -38,7 +38,7 @@ public function shouldTest_beFalse_forNotMatching()
public function shouldFails_beTrue_forNotMatched()
{
// when
$fails = pattern('\d')->fails('abc');
$fails = Pattern::of('\d')->fails('abc');
// then
$this->assertTrue($fails);
}
Expand All @@ -49,7 +49,7 @@ public function shouldFails_beTrue_forNotMatched()
public function shouldCount()
{
// when
$count = pattern('\d+')->count('111-222-333');
$count = Pattern::of('\d+')->count('111-222-333');
// then
$this->assertSame(3, $count);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public function shouldTestThrowForMalformedPattern()
public function shouldCount_0_notMatched()
{
// when
$count = pattern('[a-z]+')->count('111-222-333');
$count = Pattern::of('[a-z]+')->count('111-222-333');
// then
$this->assertSame(0, $count);
}
Expand All @@ -106,7 +106,7 @@ public function shouldFilterArray()
'lowercase again',
];
// when
$filtered = pattern('[A-Z][a-z]+')->filter($array);
$filtered = Pattern::of('[A-Z][a-z]+')->filter($array);
// then
$this->assertSame(['Uppercase', 'Uppercase again'], $filtered);
}
Expand All @@ -120,7 +120,7 @@ public function shouldThrowPrettyErrorMessage(): void
$this->expectException(MalformedPatternException::class);
$this->expectExceptionMessage('Two named subpatterns have the same name at offset 21');
// when
pattern('First(?<one>)?(?<one>)?')->test('Test');
Pattern::of('First(?<one>)?(?<one>)?')->test('Test');
}

/**
Expand All @@ -129,7 +129,7 @@ public function shouldThrowPrettyErrorMessage(): void
public function shouldReturn_prune()
{
// when
$result = pattern('\d+[.,]\d+')->prune('Foo for "14,43" and Bar for "2.32"');
$result = Pattern::of('\d+[.,]\d+')->prune('Foo for "14,43" and Bar for "2.32"');
// then
$this->assertSame('Foo for "" and Bar for ""', $result);
}
Expand All @@ -143,7 +143,7 @@ public function shouldThrow_prune_onMalformedPattern()
$this->expectException(MalformedPatternException::class);
$this->expectExceptionMessage('Quantifier does not follow a repeatable item at offset 5');
// when
pattern('Foo **')->prune('Foo bar');
Pattern::of('Foo **')->prune('Foo bar');
}

/**
Expand Down
13 changes: 7 additions & 6 deletions test/Feature/CleanRegex/Split/split/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Test\Feature\CleanRegex\Split\split;

use PHPUnit\Framework\TestCase;
use TRegx\CleanRegex\Pattern;
use TRegx\Exception\MalformedPatternException;

class PatternTest extends TestCase
Expand All @@ -12,7 +13,7 @@ class PatternTest extends TestCase
public function shouldSplitIntoPieces()
{
// when
$pieces = pattern(', ?')->split('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger');
$pieces = Pattern::of(', ?')->split('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger');
// then
$this->assertSame(['Father', 'Mother', 'Maiden', 'Crone', 'Warrior', 'Smith', 'Stranger'], $pieces);
}
Expand All @@ -23,7 +24,7 @@ public function shouldSplitIntoPieces()
public function shouldSplitIntoPieces_withSeparator()
{
// when
$pieces = pattern('([,;]) ?')->split('Foo, Bar; Cat, Door');
$pieces = Pattern::of('([,;]) ?')->split('Foo, Bar; Cat, Door');
// then
$this->assertSame(['Foo', ',', 'Bar', ';', 'Cat', ',', 'Door'], $pieces);
}
Expand All @@ -34,7 +35,7 @@ public function shouldSplitIntoPieces_withSeparator()
public function shouldSplitIntoPieces_withSeparators()
{
// when
$pieces = pattern('(;)(;) ?')->split('Foo;; Bar;; Cat;; Door');
$pieces = Pattern::of('(;)(;) ?')->split('Foo;; Bar;; Cat;; Door');
// then
$this->assertSame(['Foo', ';', ';', 'Bar', ';', ';', 'Cat', ';', ';', 'Door'], $pieces);
}
Expand All @@ -45,7 +46,7 @@ public function shouldSplitIntoPieces_withSeparators()
public function shouldSplitIntoPieces_withSeparators_emptySeparator()
{
// when
$pieces = pattern('()(;) ?')->split('Foo; Bar; Cat; Door');
$pieces = Pattern::of('()(;) ?')->split('Foo; Bar; Cat; Door');
// then
$this->assertSame(['Foo', '', ';', 'Bar', '', ';', 'Cat', '', ';', 'Door'], $pieces);
}
Expand All @@ -56,7 +57,7 @@ public function shouldSplitIntoPieces_withSeparators_emptySeparator()
public function shouldSplitIntoPieces_withOptionalGroup()
{
// when
$pieces = pattern('(,)?(;) ?')->split('One,; Two; Three,; Four');
$pieces = Pattern::of('(,)?(;) ?')->split('One,; Two; Three,; Four');
// then
$this->assertSame(['One', ',', ';', 'Two', null, ';', 'Three', ',', ';', 'Four'], $pieces);
}
Expand All @@ -70,6 +71,6 @@ public function shouldThrowForMalformedPattern()
$this->expectException(MalformedPatternException::class);
$this->expectExceptionMessage('Quantifier does not follow a repeatable item at offset 0');
// when
pattern('+')->split('Foo');
Pattern::of('+')->split('Foo');
}
}
39 changes: 20 additions & 19 deletions test/Feature/CleanRegex/Split/splitEnd/PatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Test\Feature\CleanRegex\Split\splitEnd;

use PHPUnit\Framework\TestCase;
use TRegx\CleanRegex\Pattern;
use TRegx\Exception\MalformedPatternException;

class PatternTest extends TestCase
Expand All @@ -12,7 +13,7 @@ class PatternTest extends TestCase
public function shouldSplitEndIntoPieces_limitEqual()
{
// when
$pieces = pattern(', ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 6);
$pieces = Pattern::of(', ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 6);
// then
$this->assertSame(['Father', 'Mother', 'Maiden', 'Crone', 'Warrior', 'Smith', 'Stranger'], $pieces);
}
Expand All @@ -23,7 +24,7 @@ public function shouldSplitEndIntoPieces_limitEqual()
public function shouldSplitEndIntoPieces_noSplits()
{
// when
$pieces = pattern(', ?')->splitEnd('Bifur, Bofur, Bombur', 0);
$pieces = Pattern::of(', ?')->splitEnd('Bifur, Bofur, Bombur', 0);
// then
$this->assertSame(['Bifur, Bofur, Bombur'], $pieces);
}
Expand All @@ -34,7 +35,7 @@ public function shouldSplitEndIntoPieces_noSplits()
public function shouldSplitEndIntoPieces_singleSplit()
{
// when
$pieces = pattern(', ?')->splitEnd('Bifur, Bofur, Bombur', 1);
$pieces = Pattern::of(', ?')->splitEnd('Bifur, Bofur, Bombur', 1);
// then
$this->assertSame(['Bifur, Bofur', 'Bombur'], $pieces);
}
Expand All @@ -45,7 +46,7 @@ public function shouldSplitEndIntoPieces_singleSplit()
public function shouldSplitEndIntoPieces_twoSplits()
{
// when
$pieces = pattern(', ?')->splitEnd('Bifur, Bofur, Bombur', 2);
$pieces = Pattern::of(', ?')->splitEnd('Bifur, Bofur, Bombur', 2);
// then
$this->assertSame(['Bifur', 'Bofur', 'Bombur'], $pieces);
}
Expand All @@ -56,7 +57,7 @@ public function shouldSplitEndIntoPieces_twoSplits()
public function firstElementEmpty_limit2()
{
// when
$pieces = pattern(', ')->splitEnd(', Bofur, Bombur', 2);
$pieces = Pattern::of(', ')->splitEnd(', Bofur, Bombur', 2);
// then
$this->assertSame(['', 'Bofur', 'Bombur'], $pieces);
}
Expand All @@ -67,7 +68,7 @@ public function firstElementEmpty_limit2()
public function firstElementEmpty_limit3()
{
// when
$pieces = pattern(', ')->splitEnd(', Bofur, Bombur', 3);
$pieces = Pattern::of(', ')->splitEnd(', Bofur, Bombur', 3);
// then
$this->assertSame(['', 'Bofur', 'Bombur'], $pieces);
}
Expand All @@ -78,7 +79,7 @@ public function firstElementEmpty_limit3()
public function shouldSplitEndIntoPieces_threeSplits()
{
// when
$pieces = pattern(', ?')->splitEnd('Bifur, Bofur, Bombur', 3);
$pieces = Pattern::of(', ?')->splitEnd('Bifur, Bofur, Bombur', 3);
// then
$this->assertSame(['Bifur', 'Bofur', 'Bombur'], $pieces);
}
Expand All @@ -89,7 +90,7 @@ public function shouldSplitEndIntoPieces_threeSplits()
public function shouldSplitEndIntoPieces_fourSplits()
{
// when
$pieces = pattern(', ?')->splitEnd('Bifur, Bofur, Bombur', 4);
$pieces = Pattern::of(', ?')->splitEnd('Bifur, Bofur, Bombur', 4);
// then
$this->assertSame(['Bifur', 'Bofur', 'Bombur'], $pieces);
}
Expand All @@ -100,7 +101,7 @@ public function shouldSplitEndIntoPieces_fourSplits()
public function shouldSplitEndIntoPieces_limitExceeding()
{
// when
$pieces = pattern(', ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 7);
$pieces = Pattern::of(', ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 7);
// then
$this->assertSame(['Father', 'Mother', 'Maiden', 'Crone', 'Warrior', 'Smith', 'Stranger'], $pieces);
}
Expand All @@ -111,7 +112,7 @@ public function shouldSplitEndIntoPieces_limitExceeding()
public function shouldSplitEndIntoPieces_limitThree()
{
// when
$pieces = pattern(', ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 3);
$pieces = Pattern::of(', ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 3);
// then
$this->assertSame(['Father, Mother, Maiden, Crone', 'Warrior', 'Smith', 'Stranger'], $pieces);
}
Expand All @@ -122,7 +123,7 @@ public function shouldSplitEndIntoPieces_limitThree()
public function shouldSplitEndIntoPieces_withSeparator_limitThree()
{
// when
$pieces = pattern('(,) ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 3);
$pieces = Pattern::of('(,) ?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 3);
// then
$this->assertSame(['Father, Mother, Maiden, Crone', ',', 'Warrior', ',', 'Smith', ',', 'Stranger'], $pieces);
}
Expand All @@ -134,7 +135,7 @@ public function shouldSplitEndIntoPieces_withSeparator_limitThree()
public function shouldSplitEndIntoPieces_withSeparators_limitThree()
{
// when
$pieces = pattern('(,)( )?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 2);
$pieces = Pattern::of('(,)( )?')->splitEnd('Father, Mother, Maiden, Crone, Warrior, Smith, Stranger', 2);
// then
$this->assertSame(['Father, Mother, Maiden, Crone, Warrior', ',', ' ', 'Smith', ',', ' ', 'Stranger'], $pieces);
}
Expand All @@ -145,7 +146,7 @@ public function shouldSplitEndIntoPieces_withSeparators_limitThree()
public function shouldSplitEndIntoPieces_withEmptySeparator()
{
// when
$pieces = pattern('()(;) ?')->splitEnd('One; Two; Three; Four', 2);
$pieces = Pattern::of('()(;) ?')->splitEnd('One; Two; Three; Four', 2);
// then
$this->assertSame(['One; Two', '', ';', 'Three', '', ';', 'Four'], $pieces);
}
Expand All @@ -156,7 +157,7 @@ public function shouldSplitEndIntoPieces_withEmptySeparator()
public function shouldSplitEndIntoPieces_withUnmatchedSeparator()
{
// when
$pieces = pattern('(,)?(;) ?')->splitEnd('One,; Two; Three,; Four', 2);
$pieces = Pattern::of('(,)?(;) ?')->splitEnd('One,; Two; Three,; Four', 2);
// then
$this->assertSame(['One,; Two', null, ';', 'Three', ',', ';', 'Four'], $pieces);
}
Expand All @@ -167,7 +168,7 @@ public function shouldSplitEndIntoPieces_withUnmatchedSeparator()
public function shouldReturnSubject_onUnmatchedSubject()
{
// when
$pieces = pattern(' ')->splitStart('Bar', 0);
$pieces = Pattern::of(' ')->splitStart('Bar', 0);
// then
$this->assertSame(['Bar'], $pieces);
}
Expand All @@ -181,7 +182,7 @@ public function shouldThrowForMalformedPattern()
$this->expectException(MalformedPatternException::class);
$this->expectExceptionMessage('Quantifier does not follow a repeatable item at offset 0');
// when
pattern('+')->splitEnd('Foo', 1);
Pattern::of('+')->splitEnd('Foo', 1);
}

/**
Expand All @@ -193,7 +194,7 @@ public function shouldThrowForNegativeSplits()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Negative splits: -1');
// when
pattern('invalid)')->splitEnd('Foo', -1);
Pattern::of('invalid)')->splitEnd('Foo', -1);
}

/**
Expand All @@ -205,7 +206,7 @@ public function shouldThrowForNegativeSplitsMinusTwo()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Negative splits: -2');
// when
pattern('invalid)')->splitEnd('Foo', -2);
Pattern::of('invalid)')->splitEnd('Foo', -2);
}

/**
Expand All @@ -214,7 +215,7 @@ public function shouldThrowForNegativeSplitsMinusTwo()
public function shouldLimitMoreSeparatorsLessSplits()
{
// when
$pieces = pattern('(;)(;)')->splitEnd('One;;Two;;Three;;', 5);
$pieces = Pattern::of('(;)(;)')->splitEnd('One;;Two;;Three;;', 5);
// then
$this->assertSame(['One', ';', ';', 'Two', ';', ';', 'Three', ';', ';', ''], $pieces);
}
Expand Down
Loading

0 comments on commit 6d9ad5d

Please sign in to comment.