Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterise "StatusCounter" tests (#973) #979

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class StatusCounter {
*/
private Status finalStatus = Status.PASSED;

private int size = 0;

public StatusCounter(Resultsable[] resultsables) {
this(resultsables, Collections.emptySet());
}
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice but rather not very efficient if size() is invoked often and report is big

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,151 +1,206 @@
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;
import net.masterthought.cucumber.json.support.StatusCounter;

/**
* @author Damian Szczepanik (damianszczepanik@github)
* @author <a href="https://github.com/ghostcity">Stefan Gasterstädt</a>
*/
@RunWith(Parameterized.class)
public class StatusCounterTest {

@Test
public void StatusCounter_OnFailingStatuses_IncrementsPassing() {
@Parameters(name = "{5} by {6}")
public static Iterable<Object[]> 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<Status> 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<Status> 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<Status> 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<Status> 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);
}

}