feat: add toBeSnakeCase

This commit is contained in:
Maurizio
2023-08-17 20:48:18 +02:00
parent f2e31452f2
commit 4951b1b0f9
2 changed files with 35 additions and 0 deletions

View File

@ -1009,4 +1009,16 @@ final class Expectation
return $this; return $this;
} }
/**
* Asserts that the value is snake_case.
*
* @return self<TValue>
*/
public function toBeSnakeCase(string $message = ''): self
{
Assert::assertTrue((bool) preg_match('/^[\p{Ll}_]+$/u', (string) $this->value), $message);
return $this;
}
} }

View File

@ -0,0 +1,23 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
expect('abc')->toBeSnakeCase();
expect('abc_def')->toBeSnakeCase();
expect('abc-def')->not->toBeSnakeCase();
expect('abcDef')->not->toBeSnakeCase();
expect('AbcDef')->not->toBeSnakeCase();
});
test('failures', function () {
expect('Abc')->toBeSnakeCase();
})->throws(ExpectationFailedException::class);
test('failures with custom message', function () {
expect('Abc')->toBeSnakeCase('oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');
test('not failures', function () {
expect('abc_def')->not->toBeSnakeCase();
})->throws(ExpectationFailedException::class);