From 8ecd43cb670b9d1d2eaca73c283a78ed7a9772ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gasterst=C3=A4dt?= Date: Sun, 22 Nov 2020 12:41:34 +0100 Subject: [PATCH 1/2] Parameterise "StatusCounter" tests (#973) --- .../comparators/StatusCounterTest.java | 197 +++++++++++------- 1 file changed, 126 insertions(+), 71 deletions(-) diff --git a/src/test/java/net/masterthought/cucumber/json/support/comparators/StatusCounterTest.java b/src/test/java/net/masterthought/cucumber/json/support/comparators/StatusCounterTest.java index 779e0541e..b6eb3315c 100644 --- a/src/test/java/net/masterthought/cucumber/json/support/comparators/StatusCounterTest.java +++ b/src/test/java/net/masterthought/cucumber/json/support/comparators/StatusCounterTest.java @@ -1,11 +1,19 @@ package net.masterthought.cucumber.json.support.comparators; +import static java.util.Arrays.asList; +import static java.util.Arrays.stream; +import static java.util.Collections.emptySet; +import static java.util.Collections.singleton; +import static java.util.stream.Collectors.toSet; import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Collections; - +import java.util.List; +import java.util.Set; +import java.util.stream.Stream; import org.junit.Test; - +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; import net.masterthought.cucumber.json.support.Resultsable; import net.masterthought.cucumber.json.support.ResultsableBuilder; import net.masterthought.cucumber.json.support.Status; @@ -13,139 +21,186 @@ /** * @author Damian Szczepanik (damianszczepanik@github) + * @author Stefan Gasterstädt */ +@RunWith(Parameterized.class) public class StatusCounterTest { - @Test - public void StatusCounter_OnFailingStatuses_IncrementsPassing() { + @Parameters(name = "{5} by {6}") + public static Iterable data() { + return asList(new Object[][] { + { 0, 0, 0, 0, 0, Status.PASSED, asList() }, + { 1, 0, 0, 0, 0, Status.PASSED, asList(Status.PASSED) }, + { 0, 1, 0, 0, 0, Status.FAILED, asList(Status.FAILED) }, + { 0, 0, 1, 0, 0, Status.FAILED, asList(Status.SKIPPED) }, + { 0, 0, 0, 1, 0, Status.FAILED, asList(Status.PENDING) }, + { 0, 0, 0, 0, 1, Status.FAILED, asList(Status.UNDEFINED) }, + { 2, 0, 0, 0, 0, Status.PASSED, asList(Status.PASSED, Status.PASSED) }, + { 1, 1, 0, 0, 0, Status.FAILED, asList(Status.PASSED, Status.FAILED) }, + { 1, 0, 1, 0, 0, Status.FAILED, asList(Status.PASSED, Status.SKIPPED) }, + { 1, 0, 0, 1, 0, Status.FAILED, asList(Status.PASSED, Status.PENDING) }, + { 1, 0, 0, 0, 1, Status.FAILED, asList(Status.PASSED, Status.UNDEFINED) }, + { 3, 0, 0, 0, 0, Status.PASSED, asList(Status.PASSED, Status.PASSED, Status.PASSED) }, + { 2, 1, 0, 0, 0, Status.FAILED, asList(Status.PASSED, Status.FAILED, Status.PASSED) }, + { 2, 0, 1, 0, 0, Status.FAILED, asList(Status.PASSED, Status.SKIPPED, Status.PASSED) }, + { 2, 0, 0, 1, 0, Status.FAILED, asList(Status.PASSED, Status.PENDING, Status.PASSED) }, + { 2, 0, 0, 0, 1, Status.FAILED, asList(Status.PASSED, Status.UNDEFINED, Status.PASSED) }, + { 1, 2, 0, 0, 0, Status.FAILED, asList(Status.PASSED, Status.FAILED, Status.FAILED) }, + { 1, 1, 1, 0, 0, Status.FAILED, asList(Status.PASSED, Status.FAILED, Status.SKIPPED) }, + { 1, 1, 0, 1, 0, Status.FAILED, asList(Status.PASSED, Status.FAILED, Status.PENDING) }, + { 1, 1, 0, 0, 1, Status.FAILED, asList(Status.PASSED, Status.FAILED, Status.UNDEFINED) }, + }); + } - // given - Resultsable[] resultsables = ResultsableBuilder.Resultsable(Status.PASSED, Status.FAILED, Status.SKIPPED); + @Parameter(0) + public int passedCounter; - // when - StatusCounter statusCounter = new StatusCounter(resultsables, Collections.singleton(Status.SKIPPED)); + @Parameter(1) + public int failedCounter; - // then - assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(2); - assertThat(statusCounter.getValueFor(Status.FAILED)).isOne(); - assertThat(statusCounter.getValueFor(Status.SKIPPED)).isZero(); - } + @Parameter(2) + public int skippedCounter; - @Test - public void StatusCounter_OnNullFailingStatuses_IncrementsPassing() { + @Parameter(3) + public int pendingCounter; - // given - Resultsable[] resultsables = ResultsableBuilder.Resultsable(Status.PASSED, Status.FAILED, Status.SKIPPED); + @Parameter(4) + public int undefinedCounter; - // when - StatusCounter statusCounter = new StatusCounter(resultsables, null); + @Parameter(5) + public Status finalStatus; - // then - assertThat(statusCounter.getValueFor(Status.PASSED)).isOne(); - assertThat(statusCounter.getValueFor(Status.FAILED)).isOne(); - assertThat(statusCounter.getValueFor(Status.SKIPPED)).isOne(); - } + @Parameter(6) + public List statuses; @Test - public void StatusCounter_OnEmptyFailingStatuses_IncrementsPassing() { - + public void StatusCounter_OnNoneNotFailingStatuses() { // given - Resultsable[] resultsables = ResultsableBuilder.Resultsable(Status.PASSED, Status.FAILED, Status.SKIPPED); + Resultsable[] resultsables = ResultsableBuilder.Resultsable(this.statuses.toArray(new Status[0])); // when StatusCounter statusCounter = new StatusCounter(resultsables); // then - assertThat(statusCounter.getValueFor(Status.PASSED)).isOne(); - assertThat(statusCounter.getValueFor(Status.FAILED)).isOne(); - assertThat(statusCounter.getValueFor(Status.SKIPPED)).isOne(); + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isEqualTo(this.failedCounter); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isEqualTo(this.skippedCounter); + assertThat(statusCounter.getValueFor(Status.PENDING)).isEqualTo(this.pendingCounter); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isEqualTo(this.undefinedCounter); + assertThat(statusCounter.getFinalStatus()).isEqualTo(this.finalStatus); } @Test - public void getValueFor_ReturnsStatusCounter() { - + public void StatusCounter_OnNullNotFailingStatuses() { // given - StatusCounter statusCounter = new StatusCounter(); + Resultsable[] resultsables = ResultsableBuilder.Resultsable(this.statuses.toArray(new Status[0])); // when - statusCounter.incrementFor(Status.PASSED); - statusCounter.incrementFor(Status.PASSED); - statusCounter.incrementFor(Status.PASSED); + StatusCounter statusCounter = new StatusCounter(resultsables, null); // then - assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(3); - assertThat(statusCounter.getValueFor(Status.FAILED)).isZero(); - assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isZero(); - assertThat(statusCounter.getValueFor(Status.PENDING)).isZero(); - assertThat(statusCounter.getValueFor(Status.SKIPPED)).isZero(); + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isEqualTo(this.failedCounter); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isEqualTo(this.skippedCounter); + assertThat(statusCounter.getValueFor(Status.PENDING)).isEqualTo(this.pendingCounter); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isEqualTo(this.undefinedCounter); + assertThat(statusCounter.getFinalStatus()).isEqualTo(this.finalStatus); } @Test - public void size_ReturnsAllStatusCounter() { - + public void StatusCounter_OnEmptyNotFailingStatuses() { // given - StatusCounter statusCounter = new StatusCounter(); + Resultsable[] resultsables = ResultsableBuilder.Resultsable(this.statuses.toArray(new Status[0])); // when - statusCounter.incrementFor(Status.PASSED); - statusCounter.incrementFor(Status.FAILED); + StatusCounter statusCounter = new StatusCounter(resultsables, emptySet()); // then - assertThat(statusCounter.size()).isEqualTo(2); + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isEqualTo(this.failedCounter); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isEqualTo(this.skippedCounter); + assertThat(statusCounter.getValueFor(Status.PENDING)).isEqualTo(this.pendingCounter); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isEqualTo(this.undefinedCounter); + assertThat(statusCounter.getFinalStatus()).isEqualTo(this.finalStatus); } @Test - public void getFinalStatus_WithNoStatuses_ReturnsPass() { - + public void StatusCounter_OnSkippedNotFailingStatuses() { // given - StatusCounter statusCounter = new StatusCounter(); + Resultsable[] resultsables = ResultsableBuilder.Resultsable(this.statuses.toArray(new Status[0])); + Set notFailingStatuses = singleton(Status.SKIPPED); // when - Status status = statusCounter.getFinalStatus(); + StatusCounter statusCounter = new StatusCounter(resultsables, notFailingStatuses); // then - assertThat(status).isEqualTo(Status.PASSED); + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter + this.skippedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isEqualTo(this.failedCounter); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isZero(); + assertThat(statusCounter.getValueFor(Status.PENDING)).isEqualTo(this.pendingCounter); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isEqualTo(this.undefinedCounter); } @Test - public void getFinalStatus_OnSameStatuses_ReturnsThatStatus() { - + public void StatusCounter_OnNonPassedNotFailingStatuses() { // given - StatusCounter statusCounter = new StatusCounter(); + Resultsable[] resultsables = ResultsableBuilder.Resultsable(this.statuses.toArray(new Status[0])); + Set notFailingStatuses = Stream.of(Status.FAILED, Status.SKIPPED, Status.PENDING, Status.UNDEFINED).collect(toSet()); // when - statusCounter.incrementFor(Status.PASSED); - statusCounter.incrementFor(Status.PASSED); + StatusCounter statusCounter = new StatusCounter(resultsables, notFailingStatuses); // then + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isZero(); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isZero(); + assertThat(statusCounter.getValueFor(Status.PENDING)).isZero(); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isZero(); assertThat(statusCounter.getFinalStatus()).isEqualTo(Status.PASSED); } @Test - public void getFinalStatus_OnDifferentStatuses_ReturnsFailedStatus() { - + public void StatusCounter_OnAllNotFailingStatuses() { // given - StatusCounter statusCounter = new StatusCounter(); + Resultsable[] resultsables = ResultsableBuilder.Resultsable(this.statuses.toArray(new Status[0])); + Set notFailingStatuses = stream(Status.values()).collect(toSet()); // when - statusCounter.incrementFor(Status.PASSED); - statusCounter.incrementFor(Status.UNDEFINED); + StatusCounter statusCounter = new StatusCounter(resultsables, notFailingStatuses); // then - assertThat(statusCounter.getFinalStatus()).isEqualTo(Status.FAILED); + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isZero(); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isZero(); + assertThat(statusCounter.getValueFor(Status.PENDING)).isZero(); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isZero(); + assertThat(statusCounter.getFinalStatus()).isEqualTo(Status.PASSED); } @Test - public void getFinalStatus_OnFailedStatus_ReturnsFailedStatus() { - + public void StatusCounter_OnIncrementFor() { // given StatusCounter statusCounter = new StatusCounter(); // when - statusCounter.incrementFor(Status.PASSED); - statusCounter.incrementFor(Status.FAILED); + for (Status status : this.statuses) { + statusCounter.incrementFor(status); + } // then - assertThat(statusCounter.getFinalStatus()).isEqualTo(Status.FAILED); - } + assertThat(statusCounter.size()).isEqualTo(this.passedCounter + this.failedCounter + this.skippedCounter + this.pendingCounter + this.undefinedCounter); + assertThat(statusCounter.getValueFor(Status.PASSED)).isEqualTo(this.passedCounter); + assertThat(statusCounter.getValueFor(Status.FAILED)).isEqualTo(this.failedCounter); + assertThat(statusCounter.getValueFor(Status.SKIPPED)).isEqualTo(this.skippedCounter); + assertThat(statusCounter.getValueFor(Status.PENDING)).isEqualTo(this.pendingCounter); + assertThat(statusCounter.getValueFor(Status.UNDEFINED)).isEqualTo(this.undefinedCounter); + assertThat(statusCounter.getFinalStatus()).isEqualTo(this.finalStatus); + } + } From 88975a454ced8417892a14165a258e4a9a675156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Gasterst=C3=A4dt?= Date: Sun, 22 Nov 2020 15:45:35 +0100 Subject: [PATCH 2/2] Remove redundant size counter (#973) --- .../masterthought/cucumber/json/support/StatusCounter.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/net/masterthought/cucumber/json/support/StatusCounter.java b/src/main/java/net/masterthought/cucumber/json/support/StatusCounter.java index a09801864..e00043be8 100644 --- a/src/main/java/net/masterthought/cucumber/json/support/StatusCounter.java +++ b/src/main/java/net/masterthought/cucumber/json/support/StatusCounter.java @@ -20,8 +20,6 @@ public class StatusCounter { */ private Status finalStatus = Status.PASSED; - private int size = 0; - public StatusCounter(Resultsable[] resultsables) { this(resultsables, Collections.emptySet()); } @@ -53,7 +51,6 @@ public StatusCounter() { public void incrementFor(Status status) { final int statusCounter = getValueFor(status) + 1; this.counter.put(status, statusCounter); - size++; if (finalStatus == Status.PASSED && status != Status.PASSED) { finalStatus = Status.FAILED; @@ -76,7 +73,7 @@ public int getValueFor(Status status) { * @return sum of all occurrences for all statuses */ public int size() { - return size; + return counter.values().stream().mapToInt(Integer::valueOf).sum(); } /**