Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Merged
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 @@ -18,7 +18,6 @@
import org.apache.commons.lang3.math.Fraction;

import edu.hm.hafner.coverage.Metric;
import edu.hm.hafner.coverage.Metric.MetricTendency;
import edu.hm.hafner.coverage.Node;
import edu.hm.hafner.coverage.Value;
import edu.hm.hafner.echarts.ChartModelConfiguration;
Expand Down Expand Up @@ -544,18 +543,23 @@ public String formatDelta(final Baseline baseline, final Metric metric) {
* @param metric
* the metric to check
*
* @return {@code true} if the trend is positive, {@code false} otherwise
* @return a positive value if the trend is positive, a negative value if the trend is negative, or {@code 0} if there is no significant change in the trend
Copy link
Member

Choose a reason for hiding this comment

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

There is a not required import in line 21

*/
@SuppressWarnings("unused") // Called by jelly view
public boolean isPositiveTrend(final Baseline baseline, final Metric metric) {
public double getTrend(final Baseline baseline, final Metric metric) {
var delta = getDelta(baseline, metric);
if (delta.isPresent()) {
if (delta.get().compareTo(Fraction.ZERO) > 0) {
return metric.getTendency() == MetricTendency.LARGER_IS_BETTER;
double deltaValue = delta.get().doubleValue();
if (-0.001 < deltaValue && deltaValue < 0.001) {
// for var(--text-color)
return 0;
}
else {
// for var(--red or --green)
return deltaValue;
}
return metric.getTendency() == MetricTendency.SMALLER_IS_BETTER;
}
return true;
return 0; // default to zero
}

/**
Expand Down
7 changes: 5 additions & 2 deletions plugin/src/main/resources/coverage/coverage-summary.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@
<li>${formatter.formatValueWithMetric(value)}
<j:if test="${it.hasDelta(baseline, value.metric)}">
<j:choose>
<j:when test="${it.isPositiveTrend(baseline, value.metric)}">
<j:when test="${it.getTrend(baseline, value.metric) gt 0}">
<j:set var="color" value="var(--green)"/>
</j:when>
<j:otherwise>
<j:when test="${it.getTrend(baseline, value.metric) lt 0}">
<j:set var="color" value="var(--red)"/>
</j:when>
<j:otherwise>
<j:set var="color" value="var(--text-color)"/>
</j:otherwise>
</j:choose>
<span style="color: ${color}">(${it.formatDelta(baseline, value.metric)})</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.TreeMap;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.Fraction;
Expand Down Expand Up @@ -30,6 +31,24 @@
*/
@DefaultLocale("en")
class CoverageBuildActionTest {
private CoverageBuildAction createCoverageBuildActionWithDelta(final Metric metric, final Optional<Fraction> delta) {
Node module = new ModuleNode("module");

var coverageBuilder = new CoverageBuilder();
var percent = coverageBuilder.setMetric(metric).setCovered(1).setMissed(1).build();

module.addValue(percent);

var deltas = new TreeMap<Metric, Fraction>();
delta.ifPresent(d -> deltas.put(metric, d));

var coverages = List.of(percent);

return spy(new CoverageBuildAction(mock(FreeStyleBuild.class), CoverageRecorder.DEFAULT_ID,
StringUtils.EMPTY, StringUtils.EMPTY, module, new QualityGateResult(),
createLog(), "-", deltas, coverages, deltas, coverages, deltas, coverages, false));
}

@Test
void shouldNotLoadResultIfCoverageValuesArePersistedInAction() {
Node module = new ModuleNode("module");
Expand Down Expand Up @@ -88,4 +107,28 @@ void shouldCreateViewModel() {
assertThat(action.getTarget()).extracting(CoverageViewModel::getNode).isSameAs(root);
assertThat(action.getTarget()).extracting(CoverageViewModel::getOwner).isSameAs(action.getOwner());
}

@Test
void shouldReturnPositiveTrendForLineMetric() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.of(Fraction.getFraction(1, 1000)));
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isPositive();
}

@Test
void shouldReturnNegativeTrendForLineMetric() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.of(Fraction.getFraction(-1, 1000)));
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isNegative();
}

@Test
void shouldReturnZeroForDeltaWithinBoundaries() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.of(Fraction.getFraction(9, 10_000)));
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isZero();
}

@Test
void shouldReturnZeroWhenDeltaIsNotPresentForGivenMetric() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.empty());
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isZero();
}
Copy link
Member

Choose a reason for hiding this comment

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

Tests look good now!

Just a couple of analysis warnings remaining...

}