Skip to content

Commit

Permalink
Added negations for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed Mar 8, 2024
1 parent a54605f commit 631d413
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/Negate.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ public function letter(): FluentBuilder
return $this->builder;
}

public function letters(): FluentBuilder
{
$this->builder->pushToPattern('[^a-zA-Z]+');

return $this->builder;
}

public function lowerLetter(): FluentBuilder
{
$this->builder->pushToPattern('[^a-z]');

return $this->builder;
}

public function lowerLetters(): FluentBuilder
{
$this->builder->pushToPattern('[^a-z]+');

return $this->builder;
}

/**
* @param string $method
* @param array<int|string, mixed> $arguments
Expand All @@ -32,7 +53,7 @@ public function __call(string $method, array $arguments): FluentBuilder
{
$this->builder->pushToPattern('[^');

$result = $this->builder->{$method}($arguments);
$result = $this->builder->{$method}(...$arguments);

if ($result instanceof FluentBuilder) {
$this->builder->pushToPattern(']');
Expand Down
30 changes: 29 additions & 1 deletion tests/Unit/NegateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,52 @@

declare(strict_types=1);

use Rudashi\FluentBuilder;
use Rudashi\Negate;

it('can create negation')
->expect(negation())
->toBeInstanceOf(Negate::class);

it('can call builder methods')
->expect(negation()->letter())
->toBeInstanceOf(FluentBuilder::class);

it('thrown an exception if the property has no method assigned', function () {
expect(negation()->get());
})->throws(
exception: LogicException::class,
exceptionMessage: 'Method "get" is not extendable by "Negation".'
);

it('can call builder methods')
/**
* Higher-Order methods
*/
it('returns the negation of exactly')
->expect(negation()->exactly('foo bar')->get())
->toBeString()
->toBe('/[^foo bar]/');

it('returns the negation of letter')
->expect(negation()->letter()->get())
->toBeString()
->toBe('/[^a-zA-Z]/');

it('returns the negation of letters')
->expect(negation()->letters()->get())
->toBeString()
->toBe('/[^a-zA-Z]+/');

it('returns the negation of lowerLetter')
->expect(negation()->lowerLetter()->get())
->toBeString()
->toBe('/[^a-z]/');

it('returns the negation of lowerLetters')
->expect(negation()->lowerLetters()->get())
->toBeString()
->toBe('/[^a-z]+/');

it('returns the negation of whitespaces')
->expect(negation()->whitespace()->get())
->toBeString()
Expand Down

0 comments on commit 631d413

Please sign in to comment.