From cf91aa3284c27f1a6a5b9650f18a288e7a883f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Tue, 17 Nov 2020 18:50:10 +0100 Subject: [PATCH] Enhancement: Add DataProvider\StringProvider::withWhitespace() --- CHANGELOG.md | 15 ++++++-- README.md | 1 + psalm-baseline.xml | 4 +-- src/DataProvider/StringProvider.php | 36 ++++++++++++++++++- test/Unit/DataProvider/StringProviderTest.php | 25 +++++++++++++ 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dba19a3f..79a44e27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`1.3.1...main`][1.3.1...main]. +For a full diff see [`1.4.0...main`][1.4.0...main]. + +## [`1.4.0`][1.4.0] + +For a full diff see [`1.3.0...1.4.0`][1.3.1...1.4.0]. + +### Added + +* Added `DataProvider\StringProvider::withWhitespace()` ([#374]), by [@localheinz] ## [`1.3.1`][1.3.1] @@ -127,6 +135,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0]. [1.2.0]: https://github.com/ergebnis/test-util/releases/tag/1.2.0 [1.3.0]: https://github.com/ergebnis/test-util/releases/tag/1.3.0 [1.3.1]: https://github.com/ergebnis/test-util/releases/tag/1.3.1 +[1.4.0]: https://github.com/ergebnis/test-util/releases/tag/1.4.0 [0.7.0...0.8.0]: https://github.com/ergebnis/test-util/compare/0.7.0...0.8.0 [0.8.0...0.9.0]: https://github.com/ergebnis/test-util/compare/0.8.0...0.9.0 @@ -137,7 +146,8 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0]. [1.1.0...1.2.0]: https://github.com/ergebnis/test-util/compare/1.1.0...1.2.0 [1.2.0...1.3.0]: https://github.com/ergebnis/test-util/compare/1.2.0...1.3.0 [1.3.0...1.3.1]: https://github.com/ergebnis/test-util/compare/1.3.0...1.3.1 -[1.3.1...main]: https://github.com/ergebnis/test-util/compare/1.3.1...main +[1.3.1...1.4.0]: https://github.com/ergebnis/test-util/compare/1.3.1...1.4.0 +[1.4.0...main]: https://github.com/ergebnis/test-util/compare/1.4.0...main [#118]: https://github.com/ergebnis/test-util/pull/118 [#119]: https://github.com/ergebnis/test-util/pull/119 @@ -156,6 +166,7 @@ For a full diff see [`0.7.0...0.8.0`][0.7.0...0.8.0]. [#343]: https://github.com/ergebnis/test-util/pull/343 [#344]: https://github.com/ergebnis/test-util/pull/344 [#372]: https://github.com/ergebnis/test-util/pull/372 +[#374]: https://github.com/ergebnis/test-util/pull/374 [@ergebnis]: https://github.com/ergebnis [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index adcbaee6..583489c7 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,7 @@ For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\ResourceProviderTe * `empty()` provides an empty `string` * `trimmed()` provides non-empty, non-blank `strings` without leading and trailing whitespace * `untrimmed()` provides non-empty, non-blank `string`s with additional leading and trailing whitespace +* `withWhitespace()` provides non-empty, non-blank, trimmed `string`s containing whitespace For examples, see [`Ergebnis\Test\Util\Test\Unit\DataProvider\StringProviderTest`](test/Unit/DataProvider/StringProviderTest.php). diff --git a/psalm-baseline.xml b/psalm-baseline.xml index ed176990..061d6fd1 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -48,8 +48,8 @@ - - + + \Generator<string, array{0: string}> \Generator<string, array{0: string}> \Generator<string, array{0: string}> \Generator<string, array{0: string}> diff --git a/src/DataProvider/StringProvider.php b/src/DataProvider/StringProvider.php index d16d96d8..cb04e3c0 100644 --- a/src/DataProvider/StringProvider.php +++ b/src/DataProvider/StringProvider.php @@ -70,6 +70,17 @@ public static function untrimmed(): \Generator }); } + /** + * @return \Generator + */ + public static function withWhitespace(): \Generator + { + yield from self::provideDataForValuesWhere(self::values(), static function (string $value): bool { + return \trim($value) === $value + && 1 === \preg_match('/\s/', $value); + }); + } + /** * @return array */ @@ -123,14 +134,37 @@ private static function values(): array }, $whitespaceCharacters) ); + /** @var array $withWhitespaceValues */ + $withWhitespaceValues = \array_combine( + \array_map(static function (string $key): string { + return \sprintf( + 'string-with-whitespace-%s', + $key + ); + }, \array_keys($whitespaceCharacters)), + \array_map(static function (string $whitespaceCharacter) use ($faker): string { + /** @var array $words */ + $words = $faker->words($faker->numberBetween(2, 5)); + + return \implode( + $whitespaceCharacter, + $words + ); + }, $whitespaceCharacters) + ); + return \array_merge( $arbitraryValues, $blankValues, $emptyValues, - $untrimmedValues + $untrimmedValues, + $withWhitespaceValues ); } + /** + * @return array + */ private static function whitespaceCharacters(): array { return [ diff --git a/test/Unit/DataProvider/StringProviderTest.php b/test/Unit/DataProvider/StringProviderTest.php index b56be1e5..f4c5fa43 100644 --- a/test/Unit/DataProvider/StringProviderTest.php +++ b/test/Unit/DataProvider/StringProviderTest.php @@ -51,6 +51,10 @@ public function testArbitraryReturnsGeneratorThatProvidesArbitraryStrings(): voi 'string-untrimmed-line-feed' => Util\DataProvider\Specification\Pattern::create('/^\n{1,5}\w+\n{1,5}$/'), 'string-untrimmed-space' => Util\DataProvider\Specification\Pattern::create('/^\s{1,5}\w+\s{1,5}$/'), 'string-untrimmed-tab' => Util\DataProvider\Specification\Pattern::create('/^\t{1,5}\w+\t{1,5}$/'), + 'string-with-whitespace-carriage-return' => Util\DataProvider\Specification\Pattern::create('/^\w+(\r+\w+){1,4}$/'), + 'string-with-whitespace-line-feed' => Util\DataProvider\Specification\Pattern::create('/^\w+(\n+\w+){1,4}$/'), + 'string-with-whitespace-space' => Util\DataProvider\Specification\Pattern::create('/^\w+(\s+\w+){1,4}$/'), + 'string-with-whitespace-tab' => Util\DataProvider\Specification\Pattern::create('/^\w+(\t+\w+){1,4}$/'), ]; $provider = StringProvider::arbitrary(); @@ -129,6 +133,10 @@ public function testTrimmedReturnsGeneratorThatProvidesStringsThatAreTrimmed(): 'string-arbitrary-word' => Util\DataProvider\Specification\Closure::create(static function (string $value): bool { return '' !== $value && '' !== \trim($value); }), + 'string-with-whitespace-carriage-return' => Util\DataProvider\Specification\Pattern::create('/^\w+(\r+\w+){1,4}$/'), + 'string-with-whitespace-line-feed' => Util\DataProvider\Specification\Pattern::create('/^\w+(\n+\w+){1,4}$/'), + 'string-with-whitespace-space' => Util\DataProvider\Specification\Pattern::create('/^\w+(\s+\w+){1,4}$/'), + 'string-with-whitespace-tab' => Util\DataProvider\Specification\Pattern::create('/^\w+(\t+\w+){1,4}$/'), ]; $provider = StringProvider::trimmed(); @@ -149,4 +157,21 @@ public function testUntrimmedReturnsGeneratorThatProvidesUntrimmedStrings(): voi self::assertProvidesDataSetsForValuesSatisfyingSpecifications($specifications, $provider); } + + public function testWithWhitespaceReturnsGeneratorThatProvidesTrimmedStringsWithWhitespace(): void + { + $specifications = [ + 'string-arbitrary-sentence' => Util\DataProvider\Specification\Closure::create(static function (string $value): bool { + return '' !== $value && '' !== \trim($value); + }), + 'string-with-whitespace-carriage-return' => Util\DataProvider\Specification\Pattern::create('/^\w+(\r+\w+){1,4}$/'), + 'string-with-whitespace-line-feed' => Util\DataProvider\Specification\Pattern::create('/^\w+(\n+\w+){1,4}$/'), + 'string-with-whitespace-space' => Util\DataProvider\Specification\Pattern::create('/^\w+(\s+\w+){1,4}$/'), + 'string-with-whitespace-tab' => Util\DataProvider\Specification\Pattern::create('/^\w+(\t+\w+){1,4}$/'), + ]; + + $provider = StringProvider::withWhitespace(); + + self::assertProvidesDataSetsForValuesSatisfyingSpecifications($specifications, $provider); + } }