From 0738e113adc51c6b496d15ab8c32104a058b0375 Mon Sep 17 00:00:00 2001 From: "johannes.pichler" Date: Fri, 19 Jun 2020 12:29:20 +0200 Subject: [PATCH 1/3] Fix dataset name creation with objects fixes #98 --- src/Datasets.php | 4 +++- tests/.snapshots/success.txt | 36 ++++++++++++++++++++---------------- tests/Features/Datasets.php | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index e2009c86..309ca51a 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -75,11 +75,13 @@ final class Datasets } $namedData = []; + $number = 1; foreach ($data as $values) { $values = is_array($values) ? $values : [$values]; - $name = $description . self::getDataSetDescription($values); + $name = $description . sprintf(' #%d', $number) . self::getDataSetDescription($values); $namedData[$name] = $values; + $number++; } return $namedData; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 961a0180..c85f178e 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -22,25 +22,29 @@ ✓ it throws exception if dataset already exist ✓ it sets closures ✓ it sets arrays - ✓ 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) + ✓ 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) ✓ lazy datasets did the job right - ✓ eager datasets with (1) - ✓ eager datasets with (2) + ✓ eager datasets #1 with (1) + ✓ eager datasets #2 with (2) ✓ eager datasets did the job right - ✓ lazy registered datasets with (1) - ✓ lazy registered datasets with (2) + ✓ lazy registered datasets #1 with (1) + ✓ lazy registered datasets #2 with (2) ✓ lazy registered datasets did the job right - ✓ eager registered datasets with (1) - ✓ eager registered datasets with (2) + ✓ eager registered datasets #1 with (1) + ✓ eager registered datasets #2 with (2) ✓ eager registered datasets did the job right - ✓ eager wrapped registered datasets with (1) - ✓ eager wrapped registered datasets with (2) + ✓ eager wrapped registered datasets #1 with (1) + ✓ eager wrapped registered datasets #2 with (2) ✓ eager registered wrapped datasets did the job right - ✓ lazy named datasets with (Bar Object (...)) + ✓ 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) + ✓ it creates unique test case names - count PASS Tests\Features\Exceptions ✓ it gives access the the underlying expectException @@ -144,5 +148,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 79 passed - Time: 3.44s + Tests: 6 skipped, 83 passed + Time: 3.59s diff --git a/tests/Features/Datasets.php b/tests/Features/Datasets.php index 688616c0..1294e5d0 100644 --- a/tests/Features/Datasets.php +++ b/tests/Features/Datasets.php @@ -3,6 +3,7 @@ use Pest\Datasets; use Pest\Exceptions\DatasetAlreadyExist; use Pest\Exceptions\DatasetDoesNotExist; +use Pest\Plugin; it('throws exception if dataset does not exist', function () { $this->expectException(DatasetDoesNotExist::class); @@ -106,3 +107,18 @@ $namedDatasets = [ test('lazy named datasets', function ($text) use ($state, $datasets) { assertTrue(true); })->with($namedDatasets); + +$counter = 0; + +it('creates unique test case names', function (string $name, Plugin $plugin, bool $bool) use (&$counter) { + assertTrue(true); + $counter++; +})->with([ + ['Name 1', new Plugin(), true], + ['Name 1', new Plugin(), true], + ['Name 1', new Plugin(), false], +]); + +it('creates unique test case names - count', function () use (&$counter) { + assertEquals(3, $counter); +}); From d9ea378819cf33f32902b8aeba2a83628208129e Mon Sep 17 00:00:00 2001 From: "johannes.pichler" Date: Sat, 20 Jun 2020 09:47:40 +0200 Subject: [PATCH 2/3] 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); }); From 6dd3ca20e46877d786ac45f64184207828e69979 Mon Sep 17 00:00:00 2001 From: "johannes.pichler" Date: Sat, 20 Jun 2020 11:27:32 +0200 Subject: [PATCH 3/3] Also handle multiple descriptions within whole dataset --- src/Datasets.php | 25 ++++++++++++------------- tests/.snapshots/success.txt | 5 +++-- tests/Features/Datasets.php | 3 ++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Datasets.php b/src/Datasets.php index 203ce741..26dc9cbc 100644 --- a/src/Datasets.php +++ b/src/Datasets.php @@ -80,25 +80,24 @@ final class Datasets foreach ($data as $values) { $values = is_array($values) ? $values : [$values]; - $dataSetDescriptions[] = self::getDataSetDescription($values); + $dataSetDescriptions[] = $description . 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]; + foreach (array_count_values($dataSetDescriptions) as $descriptionToCheck => $count) { + if ($count > 1) { + $index = 1; + foreach ($dataSetDescriptions as $i => $dataSetDescription) { + if ($dataSetDescription === $descriptionToCheck) { + $dataSetDescriptions[$i] .= sprintf(' #%d', $index++); + } } } + } - $valueIndex += $count; + $namedData = []; + foreach ($dataSetDescriptions as $i => $dataSetDescription) { + $namedData[$dataSetDescription] = $dataSetValues[$i]; } return $namedData; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 5c74bcf3..1671b70b 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -46,6 +46,7 @@ ✓ 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 with ('Name 1', Pest\Plugin Object (), true) #3 ✓ it creates unique test case names - count PASS Tests\Features\Exceptions @@ -150,5 +151,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 85 passed - Time: 3.52s + Tests: 6 skipped, 86 passed + Time: 3.58s diff --git a/tests/Features/Datasets.php b/tests/Features/Datasets.php index a4dfebdb..35764433 100644 --- a/tests/Features/Datasets.php +++ b/tests/Features/Datasets.php @@ -119,8 +119,9 @@ it('creates unique test case names', function (string $name, Plugin $plugin, boo ['Name 1', new Plugin(), false], ['Name 2', new Plugin(), false], ['Name 2', new Plugin(), true], + ['Name 1', new Plugin(), true], ]); it('creates unique test case names - count', function () use (&$counter) { - assertEquals(5, $counter); + assertEquals(6, $counter); });