Skip to content

Commit

Permalink
Do not convert complexity density into a percentage.
Browse files Browse the repository at this point in the history
The density should be reported as a float value.
  • Loading branch information
uhafner committed Jun 30, 2024
1 parent 7f603f3 commit 276f385
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public String getTooltip(final Value value) {
if (value instanceof IntegerValue) {
return String.format("%s: %d", getLabel(value.getMetric()), ((IntegerValue) value).getValue());
}
return formatValueWithMetric(value) + " (" + formatAdditionalInformation(value) + ")";
var additional = formatAdditionalInformation(value);
if (additional.isBlank()) {
return formatValueWithMetric(value);
}
return formatValueWithMetric(value) + " (" + additional + ")";
}

/**
Expand Down Expand Up @@ -88,6 +92,9 @@ public String format(final Value value, final Locale locale) {
return String.valueOf(((IntegerValue) value).getValue());
}
if (value instanceof FractionValue) {
if (value.getMetric() == Metric.COMPLEXITY_DENSITY) {
return String.format(locale, "%.2f", ((FractionValue) value).getFraction().doubleValue());
}
return formatDelta(((FractionValue) value).getFraction(), value.getMetric(), locale);
}
return value.toString();
Expand Down Expand Up @@ -119,7 +126,7 @@ public String formatDetails(final Value value) {
*
* @return the formatted value as plain text
*/
public String formatDetails(final Value value, final Locale locale) {
private String formatDetails(final Value value, final Locale locale) {
if (value instanceof Coverage) {
var coverage = (Coverage) value;
return formatPercentage(coverage, locale)
Expand All @@ -129,6 +136,9 @@ public String formatDetails(final Value value, final Locale locale) {
return String.valueOf(((IntegerValue) value).getValue());
}
if (value instanceof FractionValue) {
if (value.getMetric() == Metric.COMPLEXITY_DENSITY) {

Check warning on line 139 in plugin/src/main/java/io/jenkins/plugins/coverage/metrics/model/ElementFormatter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 139 is only partially covered, one branch is missing
return String.format(locale, "%.2f", ((FractionValue) value).getFraction().doubleValue());
}
return String.format(locale, "%.2f%%", ((FractionValue) value).getFraction().doubleValue());
}
return value.toString();
Expand Down Expand Up @@ -210,7 +220,6 @@ else if (value instanceof FractionValue) {
*
* @return the value formatted as a string
*/
@SuppressWarnings("unused") // Called by jelly view
public String formatValue(final Value value) {
return formatDetails(value, Functions.getCurrentLocale());
}
Expand Down Expand Up @@ -353,6 +362,9 @@ public String formatDelta(final Fraction fraction, final Metric metric, final Lo
|| metric.equals(Metric.TESTS)) {
return String.format(locale, "%+d", fraction.intValue());
}
if (metric == Metric.COMPLEXITY_DENSITY) {
return String.format(locale, "%+.2f", fraction.doubleValue());
}
return String.format(locale, "%+.2f%%", new SafeFraction(fraction).multiplyBy(HUNDRED).doubleValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public String formatMetric(final Value value) {
*/
@SuppressWarnings("unused") // Called by jelly view
public String formatValue(final Value value) {
return FORMATTER.formatDetails(value, Functions.getCurrentLocale());
return FORMATTER.formatValue(value);

Check warning on line 135 in plugin/src/main/java/io/jenkins/plugins/coverage/metrics/steps/CoverageMetricColumn.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 135 is not covered by tests
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Locale;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.Fraction;

import edu.hm.hafner.coverage.Coverage;
Expand Down Expand Up @@ -35,7 +36,6 @@
import io.jenkins.plugins.datatables.TableConfiguration;
import io.jenkins.plugins.datatables.TableConfiguration.SelectStyle;
import io.jenkins.plugins.datatables.TableModel;
import org.apache.commons.lang3.StringUtils;

import static j2html.TagCreator.*;

Expand Down Expand Up @@ -119,11 +119,11 @@ public List<TableColumn> getColumns() {
Messages.Column_DeltaLineCoverage("Δ"), columns);
configureValueColumn("branchCoverage", Metric.BRANCH, Messages.Column_BranchCoverage(),
Messages.Column_DeltaBranchCoverage("Δ"), columns);

/* VectorCAST metrics */
configureValueColumn("mcdcPairCoverage", Metric.MCDC_PAIR, Messages.Column_MCDCPairs(),
configureValueColumn("mcdcPairCoverage", Metric.MCDC_PAIR, Messages.Column_MCDCPairs(),
"", columns);
configureValueColumn("functionCallCoverage", Metric.FUNCTION_CALL, Messages.Column_FunctionCall(),
configureValueColumn("functionCallCoverage", Metric.FUNCTION_CALL, Messages.Column_FunctionCall(),
"", columns);

configureValueColumn("mutationCoverage", Metric.MUTATION, Messages.Column_MutationCoverage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,63 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junitpioneer.jupiter.DefaultLocale;

import edu.hm.hafner.coverage.Coverage;
import edu.hm.hafner.coverage.FractionValue;
import edu.hm.hafner.coverage.LinesOfCode;
import edu.hm.hafner.coverage.Metric;

import static org.assertj.core.api.Assertions.*;

@DefaultLocale("en")
class ElementFormatterTest {
@Test
void shouldFormatTooltip() {
var formatter = new ElementFormatter();

var density = new FractionValue(Metric.COMPLEXITY_DENSITY, 33, 100);
assertThat(formatter.getTooltip(density)).isEqualTo("Complexity Density: 0.33");
assertThat(formatter.format(density)).isEqualTo("0.33");
assertThat(formatter.formatDetails(density)).isEqualTo("0.33");
assertThat(formatter.formatValue(density)).isEqualTo("0.33");
assertThat(formatter.formatAdditionalInformation(density)).isEmpty();

var loc = new LinesOfCode(123);
assertThat(formatter.getTooltip(loc)).isEqualTo("LOC: 123");
assertThat(formatter.format(loc)).isEqualTo("123");
assertThat(formatter.formatDetails(loc)).isEqualTo("123");
assertThat(formatter.formatValue(loc)).isEqualTo("123");
assertThat(formatter.formatAdditionalInformation(loc)).isEmpty();

var empty = Coverage.nullObject(Metric.BRANCH);
assertThat(formatter.getTooltip(empty)).isEqualTo("Branch Coverage: -");
assertThat(formatter.format(empty)).isEqualTo("-");
assertThat(formatter.formatDetails(empty)).isEqualTo("-");
assertThat(formatter.formatValue(empty)).isEqualTo("-");
assertThat(formatter.formatAdditionalInformation(empty)).isEmpty();

var line = Coverage.valueOf("LINE: 3/4");
assertThat(formatter.getTooltip(line)).isEqualTo("Line Coverage: 75.00% (Covered: 3 - Missed: 1)");
assertThat(formatter.format(line)).isEqualTo("75.00%");
assertThat(formatter.formatDetails(line)).isEqualTo("75.00% (3/4)");
assertThat(formatter.formatValue(line)).isEqualTo("75.00% (3/4)");
assertThat(formatter.formatAdditionalInformation(line)).isEqualTo("Covered: 3 - Missed: 1");

var mutation = Coverage.valueOf("MUTATION: 3/4");
assertThat(formatter.getTooltip(mutation)).isEqualTo("Mutation Coverage: 75.00% (Killed: 3 - Survived: 1)");
assertThat(formatter.format(mutation)).isEqualTo("75.00%");
assertThat(formatter.formatDetails(mutation)).isEqualTo("75.00% (3/4)");
assertThat(formatter.formatValue(mutation)).isEqualTo("75.00% (3/4)");
assertThat(formatter.formatAdditionalInformation(mutation)).isEqualTo("Killed: 3 - Survived: 1");

var delta = new FractionValue(Metric.FILE, 1, 5);
assertThat(formatter.getTooltip(delta)).isEqualTo("File Coverage: +20.00%");
assertThat(formatter.format(delta)).isEqualTo("+20.00%");
// assertThat(formatter.formatDetails(delta)).isEqualTo("+20.00%");
assertThat(formatter.formatAdditionalInformation(delta)).isEmpty();
}

@Test
void shouldHandleOverflowGracefully() {
var formatter = new ElementFormatter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void shouldProvideRemoteApi() {
.node("projectStatistics").isEqualTo("{\n"
+ " \"branch\": \"88.28%\",\n"
+ " \"complexity\": \"2558\",\n"
+ " \"complexity-density\": \"+44.12%\",\n"
+ " \"complexity-density\": \"0.44\",\n"
+ " \"complexity-maximum\": \"21\",\n"
+ " \"file\": \"99.67%\",\n"
+ " \"instruction\": \"96.11%\",\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
No changes detected, that affect the code coverage.
* Line Coverage: 91.02% (294/323)
* Branch Coverage: 93.97% (109/116)
* Complexity Density: 0.50%
* Complexity Density: 0.50
* Lines of Code: 323
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* **[Overall project (difference to reference job)](http://127.0.0.1:8080/job/pipeline-coding-style/job/5/coverage#overview)**
* Line Coverage: 91.02% (294/323) - Delta: +50.00%
* Branch Coverage: 93.97% (109/116)
* Complexity Density: 0.50%
* Complexity Density: 0.50
* Lines of Code: 323
* **[Modified files (difference to reference job)](http://127.0.0.1:8080/job/pipeline-coding-style/job/5/coverage#modifiedFilesCoverage)**
* Line Coverage: 50.00% (1/2) - Delta: +50.00%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ No changes detected, that affect the code coverage.
* Branch Coverage: 66.18% (180/272)
* MC/DC Pair Coverage: 40.68% (24/59)
* Function Call Coverage: 78.48% (62/79)
* Complexity Density: 0.34%
* Complexity Density: 0.34
* Lines of Code: 294

#### Quality Gates Summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
No changes detected, that affect the code coverage.
* Line Coverage: 65.00% (52/80)
* Branch Coverage: 67.65% (23/34)
* Complexity Density: 0.31%
* Complexity Density: 0.31
* Lines of Code: 80

#### Quality Gates Summary

No active quality gates.
No active quality gates.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ No changes detected, that affect the code coverage.
* Branch Coverage: 66.18% (180/272)
* MC/DC Pair Coverage: 40.68% (24/59)
* Function Call Coverage: 78.48% (62/79)
* Complexity Density: 0.34%
* Complexity Density: 0.34
* Lines of Code: 294

#### Quality Gates Summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ No changes detected, that affect the code coverage.
* Line Coverage: 65.22% (45/69)
* Branch Coverage: 71.43% (20/28)
* MC/DC Pair Coverage: 66.67% (8/12)
* Complexity Density: 0.36%
* Complexity Density: 0.36
* Lines of Code: 69

#### Quality Gates Summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* **[Overall project (difference to reference job)](http://127.0.0.1:8080/job/pipeline-coding-style/job/5/coverage#overview)**
* Line Coverage: 91.02% (294/323) - Delta: +50.00%
* Branch Coverage: 93.97% (109/116)
* Complexity Density: 0.50%
* Complexity Density: 0.50
* Lines of Code: 323
* **[Modified files (difference to reference job)](http://127.0.0.1:8080/job/pipeline-coding-style/job/5/coverage#modifiedFilesCoverage)**
* Line Coverage: 50.00% (1/2) - Delta: +50.00%
Expand Down

0 comments on commit 276f385

Please sign in to comment.