Skip to content

Commit

Permalink
make order features applied in consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed Mar 25, 2023
1 parent 1d39ac1 commit 1ceafb3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pitest/src/main/java/org/pitest/plugin/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Locale;
import java.util.Objects;

public final class Feature {
public final class Feature implements Comparable<Feature> {

private final boolean onByDefault;
private final boolean isInternal;
Expand All @@ -20,7 +20,6 @@ private Feature(boolean onByDefault, boolean isInternal, String name, String des
this.name = name;
this.description = description;
this.params = params;

}

public static Feature named(String name) {
Expand Down Expand Up @@ -83,4 +82,8 @@ public boolean equals(Object obj) {
}


@Override
public int compareTo(Feature o) {
return name.compareTo(o.name);
}
}
2 changes: 2 additions & 0 deletions pitest/src/main/java/org/pitest/plugin/FeatureSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -61,6 +62,7 @@ private List<T> selectFeatures(List<FeatureSetting> features, Collection<T> filt
}

return active.stream()
.sorted(Comparator.comparing(ProvidesFeature::provides))
.collect(Collectors.toList());
}

Expand Down
15 changes: 15 additions & 0 deletions pitest/src/test/java/org/pitest/plugin/FeatureSelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ public void doesNotDuplicateFeatures() {
assertThat(this.testee.getActiveFeatures()).hasSize(1);
}

@Test
public void ordersFeaturesConsistently() {
final FeatureSetting fooConfig = new FeatureSetting("bar", ToggleStatus.ACTIVATE, new HashMap<>());
this.testee = new FeatureSelector<>(Arrays.asList(fooConfig), features(this.onByDefault, this.offByDefault));

assertThat(this.testee.getActiveFeatures()).containsExactly(offByDefault, onByDefault);

// swap order
this.testee = new FeatureSelector<>(Arrays.asList(fooConfig), features(this.offByDefault, this.onByDefault));

assertThat(this.testee.getActiveFeatures()).containsExactly(offByDefault, onByDefault);

}


private List<FeatureSetting> noSettings() {
return Collections.emptyList();
}
Expand Down
14 changes: 14 additions & 0 deletions pitest/src/test/java/org/pitest/plugin/FeatureTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.pitest.plugin;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;

import java.util.List;

public class FeatureTest {

@Test
Expand All @@ -27,4 +30,15 @@ public void nameEqualityIsCaseInsensitive() {
.isEqualTo(Feature.named("FOO"));
}

@Test
public void ordersByName() {
Feature a = Feature.named("a");
Feature b = Feature.named("b");
Feature c = Feature.named("c");
Feature d = Feature.named("d");
List<Feature> features = asList(c, a, b, d);

assertThat(features.stream().sorted()).containsExactly(a, b, c, d);
}

}

0 comments on commit 1ceafb3

Please sign in to comment.