Skip to content

Commit

Permalink
improving test coverage, thanks to pitest
Browse files Browse the repository at this point in the history
  • Loading branch information
dfa1 committed Dec 23, 2023
1 parent 3b08034 commit dce2a06
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void interrupt() {
withThread.interrupt();
ExitStatus exitStatus = sut.run(List.of(), in, out, err);
assertThat(exitStatus).isSuccess();
then(in).shouldHaveNoMoreInteractions();
then(in).shouldHaveNoInteractions();
then(out).shouldHaveNoInteractions();
then(err).shouldHaveNoInteractions();
}
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@
<param>hosh.integration.*</param>
<param>hosh.runtime.PipelineCommandTest</param>
<param>hosh.runtime.PipelineChannelTest</param>
<param>hosh.runtime.InterpreterTest</param>
<param>hosh.runtime.SupervisorTest</param>
</excludedTestClasses>
<threads>2</threads>
</configuration>
Expand Down
24 changes: 9 additions & 15 deletions spi/src/main/java/hosh/spi/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public int compareTo(Value obj) {
if (obj instanceof TextValue that) {
return BY_TEXT_ALPHA_NUM.compare(this.value, that.value);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ public int compareTo(Value obj) {
if (obj instanceof SizeValue that) {
return Long.compare(this.bytes, that.bytes);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -282,7 +282,7 @@ public int compareTo(Value obj) {
if (obj instanceof NumericValue that) {
return Long.compare(this.number, that.number);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -334,7 +334,7 @@ public int compareTo(Value obj) {
if (obj instanceof None) {
return 0;
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -364,7 +364,7 @@ public int compareTo(Value obj) {
if (obj instanceof DurationValue that) {
return this.duration.compareTo(that.duration);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -409,7 +409,7 @@ public int compareTo(Value obj) {
if (obj instanceof InstantValue that) {
return this.instant.compareTo(that.instant);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -493,14 +493,13 @@ public int compareTo(Value obj) {
if (obj instanceof PathValue that) {
return BY_PATH_NATURAL_ORDER.compare(this.path, that.path);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}
}

static class BytesValue implements Value {


private static final HexFormat HEX_FORMAT = HexFormat.ofDelimiter(":").withLowerCase();

private final byte[] bytes;
Expand All @@ -523,7 +522,7 @@ public int compareTo(Value o) {
if (o instanceof BytesValue that) {
return Arrays.compare(this.bytes, that.bytes);
}
return cannotCompare(this, o);
throw new IllegalArgumentException("cannot compare " + this + " with " + o);
}

@Override
Expand Down Expand Up @@ -596,7 +595,7 @@ public int compareTo(Value obj) {
if (obj instanceof StyledValue that) {
return BY_VALUE_AND_STYLE.compare(this, that);
} else {
return cannotCompare(this, obj);
throw new IllegalArgumentException("cannot compare " + this + " with " + obj);
}
}

Expand Down Expand Up @@ -713,9 +712,4 @@ public int compare(Value a, Value b) {
}

}

// generic error for compareTo, when types are not compatible
private static int cannotCompare(Value a, Value b) {
throw new IllegalArgumentException("cannot compare " + a + " with " + b);
}
}
2 changes: 1 addition & 1 deletion spi/src/main/java/hosh/spi/VariableName.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public final class VariableName {

private static final int MAX_VARIABLE_LENGTH = 256;
static final int MAX_VARIABLE_LENGTH = 256;

private static final Logger LOGGER = LoggerFactory.forEnclosingClass();

Expand Down
86 changes: 60 additions & 26 deletions spi/src/test/java/hosh/spi/ValuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ void compareToAnotherValueType() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("cannot compare Duration[PT1H] with Text[2]");
}

@Test
void compareToSameValueType() {
Value a = Values.ofDuration(Duration.ofHours(1));
Value b = Values.ofDuration(Duration.ofHours(2));
Value c = Values.ofDuration(Duration.ofHours(1));
assertThat(a.compareTo(b)).isEqualTo(-1);
assertThat(b.compareTo(a)).isEqualTo(1);
assertThat(a.compareTo(c)).isEqualTo(0);
}
}

@Nested
Expand Down Expand Up @@ -270,30 +280,38 @@ class SizeValueTest {

@ParameterizedTest
@CsvSource({
" 0, 0, B",
" 1, 1, B",
" 2, 2, B",
" 10, 10, B",
" 1023, 1023, B",
" 1024, 1, KB",
" 2047, 2, KB",
" 2048, 2, KB",
" 4096, 4, KB",
" 8192, 8, KB",
" 16384, 16, KB",
" 1048576, 1, MB",
" 2097152, 2, MB",
" 11048576, 10.5, MB",
" 134217728, 128, MB",
" 199999999, 190.7, MB",
" 200000001, 190.7, MB",
" 1073741824, 1, GB",
" 17179869184, 16, GB",
" 274877906944, 256, GB",
"1099511627780, 1, TB"
" 0, 0B",
" 1, 1B",
" 2, 2B",
" 10, 10B",
" 1023, 1023B",
" 1024, 1KB",
" 1025, 1KB",
" 1026, 1KB",
" 1027, 1KB",
" 1028, 1KB",
" 2047, 2KB",
" 2048, 2KB",
" 4096, 4KB",
" 8192, 8KB",
" 16384, 16KB",
" 1048576, 1MB",
" 2097152, 2MB",
" 11048576, 10.5MB",
" 134217728, 128MB",
" 199999999, 190.7MB",
" 200000001, 190.7MB",
" 200001000, 190.7MB",
" 201000000, 191.7MB",
" 205000000, 195.5MB",
" 210000000, 200.3MB",
" 1073741824, 1GB",
" 17179869184, 16GB",
" 274877906944, 256GB",
"1099511627780, 1TB"
})
void approximateOnPrint(long bytes, String expectedValue, String expectedUnit) {
assertThat(Values.ofSize(bytes).show(Locale.US)).isEqualTo(expectedValue + expectedUnit);
void approximateOnPrint(long bytes, String expectedValue) {
assertThat(Values.ofSize(bytes).show(Locale.US)).isEqualTo(expectedValue);
}

@Test
Expand Down Expand Up @@ -338,6 +356,16 @@ void compareToAnotherValueType() {
.hasMessage("cannot compare Size[1000B] with Text[2]");
}

@Test
void compareToSameValueType() {
Value a = Values.ofSize(100);
Value b = Values.ofSize(10);
Value c = Values.ofSize(100);
assertThat(a.compareTo(b)).isEqualTo(1);
assertThat(b.compareTo(a)).isEqualTo(-1);
assertThat(a.compareTo(c)).isEqualTo(0);
}

@Test
void unwrap() {
Value value = Values.ofSize(10);
Expand Down Expand Up @@ -502,7 +530,7 @@ void asString() {

@Test
void unwrap() {
Value value = Values.ofPath(Paths.get("."));
Value value = Values.withStyle(Values.ofPath(Paths.get(".")), Ansi.Style.FG_RED);
assertThat(value.unwrap(Path.class)).isPresent();
assertThat(value.unwrap(Integer.class)).isEmpty();
}
Expand Down Expand Up @@ -700,11 +728,17 @@ void noneFirst() {
}

@SuppressWarnings("squid:S2245")
// creates random lists of values with at least 1 none (in an unspecified position)
// creates random lists of values with 3 None elements
private Gen<List<Value>> listOfNumericValuesContainingNone() {
return lists().of(integers().all().map(Values::ofNumeric))
return lists()
.of(integers().all().map(Values::ofNumeric))
.ofSizeBetween(1, 20)
.mutate((base, r) -> {
// add None as first element
base.add(0, Values.none());
// add None as last element
base.add(Values.none());
// add one at random position
int randomIndex = ThreadLocalRandom.current().nextInt(base.size() + 1);
base.add(randomIndex, Values.none());
return base;
Expand Down
12 changes: 9 additions & 3 deletions spi/src/test/java/hosh/spi/VariableNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ void asString() {
})
void validVariableNames(String userInput) {
Optional<VariableName> from = VariableName.from(userInput);
VariableName of = VariableName.constant(userInput);
assertThat(from).hasValue(of);
assertThat(from).map(VariableName::name).hasValue(userInput);
}

@ParameterizedTest
Expand All @@ -84,9 +83,16 @@ void invalidVariableNames(String userInput) {
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void longVariableName() {
String userInput = "a".repeat(VariableName.MAX_VARIABLE_LENGTH); // still valid as it is exactly the length
Optional<VariableName> from = VariableName.from(userInput);
assertThat(from).map(VariableName::name).hasValue(userInput);
}

@Test
void veryLongVariableName() {
String userInput = "a".repeat(257);
String userInput = "a".repeat(VariableName.MAX_VARIABLE_LENGTH + 1); // too long
assertThatThrownBy(() -> VariableName.constant(userInput))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("variable name too long");
Expand Down

0 comments on commit dce2a06

Please sign in to comment.