summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java
blob: 620b3d16085fb726f7093af57e8e4929158d00e7 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package au.id.zancanaro.javacheck.junit;

import au.id.zancanaro.javacheck.ShrinkResult;

import java.util.Iterator;
import java.util.List;

@SuppressWarnings("WeakerAccess")
public class PropertyError extends AssertionError {
    private static final long serialVersionUID = 1L;

    public PropertyError(String methodName, long seed, ShrinkResult shrunk) {
        super(shrunk.thrown.getMessage() == null ?
                String.format("%s(%s)%n\tSeed: %s%n\tVisited: %s; Depth: %s",
                        methodName, joinArgs(shrunk.args),
                        seed, shrunk.visited, shrunk.depth):
                String.format("%s(%s)%n\tSeed: %s%n\tVisited: %s; Depth: %s%n%s",
                        methodName, joinArgs(shrunk.args),
                        seed, shrunk.visited, shrunk.depth,
                        shrunk.thrown.getMessage()));
        initCause(shrunk.thrown);
    }

    private static String joinArgs(List<Object> params) {
        StringBuilder sb = new StringBuilder();
        Iterator<Object> iterator = params.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            sb.append(stringValueOf(next));
            if (iterator.hasNext()) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }

    private static String stringValueOf(Object next) {
        if (next instanceof String) {
            return '"' + ((String) next).replace("\"", "\\\"") + '"';
        } else {
            try {
                return String.valueOf(next);
            } catch (Throwable e) {
                return "[toString failed]";
            }
        }
    }
}