Explanation.java

1
package com.github.dakusui.pcond.validator;
2
3
import java.util.LinkedList;
4
import java.util.List;
5
import java.util.Objects;
6
import java.util.stream.IntStream;
7
8
import static com.github.dakusui.pcond.internals.InternalUtils.newLine;
9
import static com.github.dakusui.pcond.validator.ReportComposer.Utils.composeReport;
10
import static java.lang.String.format;
11
import static java.util.Objects.requireNonNull;
12
import static java.util.stream.Collectors.joining;
13
14
public class Explanation {
15
  private final String                message;
16
  private final ReportComposer.Report expected;
17
  private final ReportComposer.Report actual;
18
19
  public Explanation(String message) {
20
    this(message, composeReport("", null), composeReport("", null));
21
  }
22
23
  public Explanation(String message, ReportComposer.Report expected, ReportComposer.Report actual) {
24
    this.message = message;
25
    this.expected = requireNonNull(expected);
26
    this.actual = requireNonNull(actual);
27
  }
28
29
  public String message() {
30 1 1. message : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::message → KILLED
    return this.message;
31
  }
32
33
  public ReportComposer.Report expected() {
34 1 1. expected : replaced return value with null for com/github/dakusui/pcond/validator/Explanation::expected → KILLED
    return this.expected;
35
  }
36
37
  public ReportComposer.Report actual() {
38 1 1. actual : replaced return value with null for com/github/dakusui/pcond/validator/Explanation::actual → KILLED
    return this.actual;
39
  }
40
41
  public String toString() {
42 2 1. toString : negated conditional → KILLED
2. toString : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::toString → KILLED
    return actual != null ?
43
        format("%s%n%s", message, composeDiff(expected, actual)) :
44
        message;
45
  }
46
47
  private static String composeDiff(ReportComposer.Report expected, ReportComposer.Report actual) {
48
    String[] e = splitAndTrim(expected.summary());
49
    String[] a = splitAndTrim(actual.summary());
50
    List<String> b = new LinkedList<>();
51 3 1. composeDiff : changed conditional boundary → SURVIVED
2. composeDiff : negated conditional → TIMED_OUT
3. composeDiff : Changed increment from 1 to -1 → KILLED
    for (int i = 0; i < Math.max(a.length, e.length); i++) {
52 3 1. composeDiff : changed conditional boundary → SURVIVED
2. composeDiff : negated conditional → KILLED
3. composeDiff : negated conditional → KILLED
      if (i < Math.min(e.length, a.length) && Objects.equals(e[i], a[i])) {
53
        b.add(format("          %s", a[i]));
54
      } else {
55 2 1. composeDiff : changed conditional boundary → SURVIVED
2. composeDiff : negated conditional → KILLED
        b.add(format("Mismatch<:%s", i < e.length ? e[i] : ""));
56 2 1. composeDiff : changed conditional boundary → SURVIVED
2. composeDiff : negated conditional → KILLED
        b.add(format("Mismatch>:%s", i < a.length ? a[i] : ""));
57
      }
58
    }
59
    b.add(newLine());
60
    assert expected.details().size() == actual.details().size();
61 1 1. composeDiff : negated conditional → KILLED
    return !expected.details().isEmpty() ?
62
        b.stream().collect(joining(newLine()))
63
            + IntStream.range(0, expected.details().size())
64 1 1. lambda$composeDiff$0 : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::lambda$composeDiff$0 → KILLED
            .mapToObj(i -> formatDetailItemPair(i, expected.details().get(i), actual.details().get(i)))
65
            .collect(joining(newLine())) :
66
        "";
67
  }
68
69
  private static String formatDetailItemPair(int i, String detailItemForExpectation, String detailItemForActual) {
70 1 1. formatDetailItemPair : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::formatDetailItemPair → KILLED
    return format(".Detail of failure [%s] (expectation)%n", i)
71
        + format("----%n")
72
        + detailItemForExpectation
73
        + newLine()
74
        + format("----%n")
75
        + newLine()
76
        + format(".Detail of failure [%s] (actual value)%n", i)
77
        + format("----%n")
78
        + detailItemForActual
79
        + newLine()
80
        + "----";
81
  }
82
83
  public static String reportToString(ReportComposer.Report report) {
84
    String ret = report.summary();
85
    ret += newLine();
86
    ret += newLine();
87
    ret += IntStream.range(0, report.details().size())
88 1 1. lambda$reportToString$1 : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::lambda$reportToString$1 → KILLED
        .mapToObj(i -> formatDetailItem(i, report.details().get(i)))
89
        .collect(joining(newLine()));
90 1 1. reportToString : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::reportToString → KILLED
    return ret;
91
  }
92
93
  private static String formatDetailItem(int i, String detailItem) {
94 1 1. formatDetailItem : replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::formatDetailItem → KILLED
    return format(".Detail of failure [%s]%n", i)
95
        + format("----%n")
96
        + detailItem
97
        + newLine()
98
        + format("----%n");
99
  }
100
101
  public static Explanation fromMessage(String msg) {
102 1 1. fromMessage : replaced return value with null for com/github/dakusui/pcond/validator/Explanation::fromMessage → KILLED
    return new Explanation(msg);
103
  }
104
105
  private static String[] splitAndTrim(String expected) {
106
    String[] in = expected.split(newLine());
107
    List<String> out = new LinkedList<>();
108
    boolean nonEmptyFound = false;
109 4 1. splitAndTrim : changed conditional boundary → KILLED
2. splitAndTrim : Changed increment from -1 to 1 → KILLED
3. splitAndTrim : Replaced integer subtraction with addition → KILLED
4. splitAndTrim : negated conditional → KILLED
    for (int i = in.length - 1; i >= 0; i--) {
110 1 1. splitAndTrim : negated conditional → KILLED
      if (!"".equals(in[i]))
111
        nonEmptyFound = true;
112 1 1. splitAndTrim : negated conditional → KILLED
      if (nonEmptyFound)
113 1 1. splitAndTrim : removed call to java/util/List::add → KILLED
        out.add(0, in[i]);
114
    }
115 1 1. splitAndTrim : replaced return value with null for com/github/dakusui/pcond/validator/Explanation::splitAndTrim → KILLED
    return out.toArray(new String[0]);
116
  }
117
}

Mutations

30

1.1
Location : message
Killed by : com.github.dakusui.ut.thincrest.ut.styles.FluentStyleTestAssertionTest$ForTestAssertionsTest.string_assertThatTest_failed(com.github.dakusui.ut.thincrest.ut.styles.FluentStyleTestAssertionTest$ForTestAssertionsTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::message → KILLED

34

1.1
Location : expected
Killed by : com.github.dakusui.pcond.ut.valuechecker.Opentest4jTest.test_testFailedException(com.github.dakusui.pcond.ut.valuechecker.Opentest4jTest)
replaced return value with null for com/github/dakusui/pcond/validator/Explanation::expected → KILLED

38

1.1
Location : actual
Killed by : com.github.dakusui.pcond.ut.valuechecker.Opentest4jTest.test_testFailedException(com.github.dakusui.pcond.ut.valuechecker.Opentest4jTest)
replaced return value with null for com/github/dakusui/pcond/validator/Explanation::actual → KILLED

42

1.1
Location : toString
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

2.2
Location : toString
Killed by : com.github.dakusui.pcond.ut.InternalUtilsTest$FormatObjectTest.testCreateInstanceFromClassName$thenNotFound(com.github.dakusui.pcond.ut.InternalUtilsTest$FormatObjectTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::toString → KILLED

51

1.1
Location : composeDiff
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : composeDiff
Killed by : com.github.dakusui.ut.valid8j.compatibility.BooleanTest.testIsTrue(com.github.dakusui.ut.valid8j.compatibility.BooleanTest)
Changed increment from 1 to -1 → KILLED

3.3
Location : composeDiff
Killed by : none
negated conditional → TIMED_OUT

52

1.1
Location : composeDiff
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : composeDiff
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$mergedWhenNotMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

3.3
Location : composeDiff
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

55

1.1
Location : composeDiff
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : composeDiff
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

56

1.1
Location : composeDiff
Killed by : none
changed conditional boundary → SURVIVED

2.2
Location : composeDiff
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

61

1.1
Location : composeDiff
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

64

1.1
Location : lambda$composeDiff$0
Killed by : com.github.dakusui.pcond.CallTest.methodNotFound(com.github.dakusui.pcond.CallTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::lambda$composeDiff$0 → KILLED

70

1.1
Location : formatDetailItemPair
Killed by : com.github.dakusui.pcond.CallTest.methodNotFound(com.github.dakusui.pcond.CallTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::formatDetailItemPair → KILLED

88

1.1
Location : lambda$reportToString$1
Killed by : com.github.dakusui.ut.thincrest.ut.ReportDetailTest.givenLongString_whenCheckEqualnessUsingCustomPredicateWithSlightlyDifferentString_thenFailWithDetailsArePrinted(com.github.dakusui.ut.thincrest.ut.ReportDetailTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::lambda$reportToString$1 → KILLED

90

1.1
Location : reportToString
Killed by : com.github.dakusui.ut.thincrest.ut.ReportDetailTest.givenLongString_whenCheckEqualnessUsingCustomPredicateWithSlightlyDifferentString_thenFailWithDetailsArePrinted(com.github.dakusui.ut.thincrest.ut.ReportDetailTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::reportToString → KILLED

94

1.1
Location : formatDetailItem
Killed by : com.github.dakusui.ut.thincrest.ut.ReportDetailTest.givenLongString_whenCheckEqualnessUsingCustomPredicateWithSlightlyDifferentString_thenFailWithDetailsArePrinted(com.github.dakusui.ut.thincrest.ut.ReportDetailTest)
replaced return value with "" for com/github/dakusui/pcond/validator/Explanation::formatDetailItem → KILLED

102

1.1
Location : fromMessage
Killed by : com.github.dakusui.pcond.ut.valuechecker.Opentest4jTest.test_testSkippedException$String(com.github.dakusui.pcond.ut.valuechecker.Opentest4jTest)
replaced return value with null for com/github/dakusui/pcond/validator/Explanation::fromMessage → KILLED

109

1.1
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
changed conditional boundary → KILLED

2.2
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.ut.ExceptionsTest.testWrapIfNecessaryWithCheckedException(com.github.dakusui.pcond.ut.ExceptionsTest)
Changed increment from -1 to 1 → KILLED

3.3
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.ut.ExceptionsTest.testWrapIfNecessaryWithCheckedException(com.github.dakusui.pcond.ut.ExceptionsTest)
Replaced integer subtraction with addition → KILLED

4.4
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

110

1.1
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

112

1.1
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
negated conditional → KILLED

113

1.1
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.NegateTest.whenInvertedTrasformingPredicateFails_thenPrintDesignedMessage$notMergedWhenMismatch(com.github.dakusui.pcond.NegateTest)
removed call to java/util/List::add → KILLED

115

1.1
Location : splitAndTrim
Killed by : com.github.dakusui.pcond.ut.ExceptionsTest.testWrapIfNecessaryWithCheckedException(com.github.dakusui.pcond.ut.ExceptionsTest)
replaced return value with null for com/github/dakusui/pcond/validator/Explanation::splitAndTrim → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3