Skip to content

Commit

Permalink
Add a methods to round the percentage values.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Jan 18, 2025
1 parent e27cf68 commit 7516bc5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/main/java/edu/hm/hafner/coverage/Coverage.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ public String asText() {
return String.format(Locale.ENGLISH, "%d/%d", getCovered(), getTotal());
}

@Override
public int asInteger() {
return getCoveredPercentage().toInt();
}

@Override
public double asDouble() {
return getCoveredPercentage().toDouble();
}

@Override
public double asRounded() {
return getCoveredPercentage().toRounded();
}

/**
* Builder to create cached {@link Coverage} instances.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/edu/hm/hafner/coverage/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Locale;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -278,6 +280,17 @@ public double asDouble() {
return fraction.doubleValue();
}

/**
* Returns this value as rounded double.
*
* @return this value as a double
*/
public double asRounded() {
var value = BigDecimal.valueOf(fraction.doubleValue());

return value.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

/**
* Returns whether this value has the same metric as the specified value.
*
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/edu/hm/hafner/coverage/CoverageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ class CoverageTest {
.withMissed(0)
.build();

@Test
void shouldHandlePercentageRounding() {
var builder = new CoverageBuilder().withMetric(Metric.LINE);

var oneThird = builder.withCovered(1).withMissed(2).build();

assertThat(oneThird.asInteger()).isEqualTo(33);
assertThat(oneThird.asDouble()).isEqualTo(100.0 / 3);
assertThat(oneThird.asRounded()).isEqualTo(33.33);

var twoThirds = builder.withCovered(2).withMissed(1).build();

assertThat(twoThirds.asInteger()).isEqualTo(67);
assertThat(twoThirds.asDouble()).isEqualTo(200.0 / 3);
assertThat(twoThirds.asRounded()).isEqualTo(66.67);
}

@Test
void shouldComputeDelta() {
var builder = new CoverageBuilder().withMetric(Metric.LINE);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/edu/hm/hafner/coverage/PercentageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void shouldRoundCorrectly() {
assertThat(oneThird.serializeToString()).isEqualTo("1/3");
assertThat(oneThird.formatPercentage(Locale.GERMAN)).isEqualTo("33,33%");
assertThat(oneThird.formatPercentage()).isEqualTo("33.33%");
assertThat(oneThird.toInt()).isEqualTo(33);
assertThat(oneThird.toRounded()).isEqualTo(33.33);

var twoThirds = Percentage.valueOf(2, 3);

Expand Down
32 changes: 24 additions & 8 deletions src/test/java/edu/hm/hafner/coverage/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,32 @@
import static edu.hm.hafner.coverage.assertions.Assertions.*;

class ValueTest {
@Test
void shouldHandlePercentageRounding() {
var oneThird = new Value(Metric.COHESION, 1, 3);

assertThat(oneThird.asInteger()).isZero();
assertThat(oneThird.asDouble()).isEqualTo(1.0 / 3);
assertThat(oneThird.asRounded()).isEqualTo(0.33);

var twoThirds = new Value(Metric.COHESION, 2, 3);

assertThat(twoThirds.asInteger()).isEqualTo(0);
assertThat(twoThirds.asDouble()).isEqualTo(2.0 / 3);
assertThat(twoThirds.asRounded()).isEqualTo(0.67);
}

@Test
void shouldReturnCorrectValueOfCoverage() {
var container = Value.valueOf("CONTAINER: 1/1");

assertThat(container)
.isInstanceOf(Coverage.class);
.isInstanceOfSatisfying(Coverage.class, coverage -> {
assertThat(coverage.getMetric()).isEqualTo(Metric.CONTAINER);
assertThat(coverage.getCovered()).isOne();
assertThat(coverage.getMissed()).isZero();
});

assertThat(Value.valueOf("MODULE: 1/1"))
.isInstanceOf(Coverage.class);
assertThat(Value.valueOf("PACKAGE: 1/1"))
Expand All @@ -35,10 +55,6 @@ void shouldReturnCorrectValueOfCoverage() {
.isInstanceOf(Coverage.class);
assertThat(Value.valueOf("MUTATION: 1/1"))
.isInstanceOf(Coverage.class);

assertThat((Coverage) container)
.hasCovered(1)
.hasMissed(0);
}

@Test
Expand Down Expand Up @@ -80,9 +96,9 @@ void shouldGetValue() {
var cyclomaticComplexity = new Value(Metric.CYCLOMATIC_COMPLEXITY, 20);
var ncss = new Value(Metric.NCSS, 30);
var npathComplexity = new Value(Metric.NPATH_COMPLEXITY, 40);
var coginitiveComplexity = new Value(Metric.COGNITIVE_COMPLEXITY, 50);
var cognitiveComplexity = new Value(Metric.COGNITIVE_COMPLEXITY, 50);

List<Value> values = List.of(linesOfCode, cyclomaticComplexity, ncss, npathComplexity, coginitiveComplexity);
List<Value> values = List.of(linesOfCode, cyclomaticComplexity, ncss, npathComplexity, cognitiveComplexity);

assertThat(Value.getValue(Metric.LOC, values))
.isEqualTo(linesOfCode);
Expand All @@ -93,7 +109,7 @@ void shouldGetValue() {
assertThat(Value.getValue(Metric.NPATH_COMPLEXITY, values))
.isEqualTo(npathComplexity);
assertThat(Value.getValue(Metric.COGNITIVE_COMPLEXITY, values))
.isEqualTo(coginitiveComplexity);
.isEqualTo(cognitiveComplexity);
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> Value.getValue(Metric.LINE, values))
.withMessageContaining("No value for metric");
Expand Down

0 comments on commit 7516bc5

Please sign in to comment.