From d2096df82a1a91babeffaf1a0efc1030b28c5c8e Mon Sep 17 00:00:00 2001 From: Maurizio Date: Thu, 17 Aug 2023 20:48:51 +0200 Subject: [PATCH] feat: add `toBeKebabCase` --- src/Mixins/Expectation.php | 12 ++++++++++++ tests/Features/Expect/toBeKebabCase.php | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/Features/Expect/toBeKebabCase.php diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index d128d30b..0686ad80 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -1021,4 +1021,16 @@ final class Expectation return $this; } + + /** + * Asserts that the value is kebab-case. + * + * @return self + */ + public function toBeKebabCase(string $message = ''): self + { + Assert::assertTrue((bool) preg_match('/^[\p{Ll}-]+$/u', (string) $this->value), $message); + + return $this; + } } diff --git a/tests/Features/Expect/toBeKebabCase.php b/tests/Features/Expect/toBeKebabCase.php new file mode 100644 index 00000000..1ca54ddc --- /dev/null +++ b/tests/Features/Expect/toBeKebabCase.php @@ -0,0 +1,23 @@ +toBeKebabCase(); + expect('abc-def')->toBeKebabCase(); + expect('abc_def')->not->toBeKebabCase(); + expect('abcDef')->not->toBeKebabCase(); + expect('AbcDef')->not->toBeKebabCase(); +}); + +test('failures', function () { + expect('Abc')->toBeKebabCase(); +})->throws(ExpectationFailedException::class); + +test('failures with custom message', function () { + expect('Abc')->toBeKebabCase('oh no!'); +})->throws(ExpectationFailedException::class, 'oh no!'); + +test('not failures', function () { + expect('abc-def')->not->toBeKebabCase(); +})->throws(ExpectationFailedException::class);