From d9ea378819cf33f32902b8aeba2a83628208129e Mon Sep 17 00:00:00 2001 From: "johannes.pichler" Date: Sat, 20 Jun 2020 09:47:40 +0200 Subject: [PATCH] Only append numbers when data set desc is the same --- src/Datasets.php | 27 +++++++++++++++++++----- tests/.snapshots/success.txt | 40 +++++++++++++++++++----------------- tests/Features/Datasets.php | 4 +++- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index 309ca51a..203ce741 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -74,14 +74,31 @@ final class Datasets $data = iterator_to_array($data); } - $namedData = []; - $number = 1; + $dataSetDescriptions = []; + $dataSetValues = []; + foreach ($data as $values) { $values = is_array($values) ? $values : [$values]; - $name = $description . sprintf(' #%d', $number) . self::getDataSetDescription($values); - $namedData[$name] = $values; - $number++; + $dataSetDescriptions[] = self::getDataSetDescription($values); + $dataSetValues[] = $values; + } + + $namedData = []; + $valueIndex = 0; + + foreach (array_count_values($dataSetDescriptions) as $dataSetDescription => $count) { + if ($count === 1) { + $name = $description . $dataSetDescription; + $namedData[$name] = $dataSetValues[$valueIndex]; + } else { + for ($i = 0; $i < $count; $i++) { + $name = $description . $dataSetDescription . sprintf(' #%d', $i + 1); + $namedData[$name] = $dataSetValues[$valueIndex + $i]; + } + } + + $valueIndex += $count; } return $namedData; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index c85f178e..5c74bcf3 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -22,28 +22,30 @@ ✓ it throws exception if dataset already exist ✓ it sets closures ✓ it sets arrays - ✓ it gets bound to test case object #1 with ('a') - ✓ it gets bound to test case object #2 with ('b') - ✓ it truncates the description #1 with ('FoooFoooFoooFoooFoooFoooFoooF...ooFooo') - ✓ lazy datasets #1 with (1) - ✓ lazy datasets #2 with (2) + ✓ it gets bound to test case object with ('a') + ✓ it gets bound to test case object with ('b') + ✓ it truncates the description with ('FoooFoooFoooFoooFoooFoooFoooF...ooFooo') + ✓ lazy datasets with (1) + ✓ lazy datasets with (2) ✓ lazy datasets did the job right - ✓ eager datasets #1 with (1) - ✓ eager datasets #2 with (2) + ✓ eager datasets with (1) + ✓ eager datasets with (2) ✓ eager datasets did the job right - ✓ lazy registered datasets #1 with (1) - ✓ lazy registered datasets #2 with (2) + ✓ lazy registered datasets with (1) + ✓ lazy registered datasets with (2) ✓ lazy registered datasets did the job right - ✓ eager registered datasets #1 with (1) - ✓ eager registered datasets #2 with (2) + ✓ eager registered datasets with (1) + ✓ eager registered datasets with (2) ✓ eager registered datasets did the job right - ✓ eager wrapped registered datasets #1 with (1) - ✓ eager wrapped registered datasets #2 with (2) + ✓ eager wrapped registered datasets with (1) + ✓ eager wrapped registered datasets with (2) ✓ eager registered wrapped datasets did the job right - ✓ lazy named datasets #1 with (Bar Object (...)) - ✓ it creates unique test case names #1 with ('Name 1', Pest\Plugin Object (), true) - ✓ it creates unique test case names #2 with ('Name 1', Pest\Plugin Object (), true) - ✓ it creates unique test case names #3 with ('Name 1', Pest\Plugin Object (), false) + ✓ lazy named datasets with (Bar Object (...)) + ✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #1 + ✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #2 + ✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), false) + ✓ it creates unique test case names with ('Name 2', Pest\Plugin Object (), false) + ✓ it creates unique test case names with ('Name 2', Pest\Plugin Object (), true) ✓ it creates unique test case names - count PASS Tests\Features\Exceptions @@ -148,5 +150,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 83 passed - Time: 3.59s + Tests: 6 skipped, 85 passed + Time: 3.52s diff --git a/tests/Features/Datasets.php b/tests/Features/Datasets.php index 1294e5d0..a4dfebdb 100644 --- a/tests/Features/Datasets.php +++ b/tests/Features/Datasets.php @@ -117,8 +117,10 @@ it('creates unique test case names', function (string $name, Plugin $plugin, boo ['Name 1', new Plugin(), true], ['Name 1', new Plugin(), true], ['Name 1', new Plugin(), false], + ['Name 2', new Plugin(), false], + ['Name 2', new Plugin(), true], ]); it('creates unique test case names - count', function () use (&$counter) { - assertEquals(3, $counter); + assertEquals(5, $counter); });