aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/test/CompoundTest.java
blob: 2c851b252c22a43818b7fe705093ba1a8c2bc254 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cz.crcs.ectester.common.test;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * A compound test that runs many Tests and has a Result dependent on all/some of their Results.
 *
 * @author Jan Jancar johny@neuromancer.sk
 */
public class CompoundTest extends Test {
    private Function<Test[], Result> resultCallback;
    private Consumer<Test[]> runCallback;
    private Test[] tests;
    private String description = "";

    private final static Consumer<Test[]> RUN_ALL = tests -> {
        for (Test t : tests) {
            t.run();
        }
    };

    private CompoundTest(Function<Test[], Result> resultCallback, Consumer<Test[]> runCallback, Test... tests) {
        this.resultCallback = resultCallback;
        this.runCallback = runCallback;
        this.tests = Arrays.stream(tests).filter(Objects::nonNull).toArray(Test[]::new);
    }

    private CompoundTest(Function<Test[], Result> callback, Consumer<Test[]> runCallback, String descripiton, Test... tests) {
        this(callback, runCallback, tests);
        this.description = descripiton;
    }

    public static CompoundTest function(Function<Test[], Result> callback, Test... tests) {
        return new CompoundTest(callback, RUN_ALL, tests);
    }

    public static CompoundTest function(Function<Test[], Result> callback, Consumer<Test[]> runCallback, Test... tests) {
        return new CompoundTest(callback, runCallback, tests);
    }

    public static CompoundTest function(Function<Test[], Result> callback, String description, Test... tests) {
        return new CompoundTest(callback, RUN_ALL, description, tests);
    }

    public static CompoundTest function(Function<Test[], Result> callback, Consumer<Test[]> runCallback, String description, Test... tests) {
        return new CompoundTest(callback, runCallback, description, tests);
    }

    public static CompoundTest all(Result.ExpectedValue what, Test... all) {
        return new CompoundTest((tests) -> {
            for (Test test : tests) {
                if (!Result.Value.fromExpected(what, test.ok()).ok()) {
                    return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
                }
            }
            return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
        }, RUN_ALL, all);
    }

    public static CompoundTest all(Result.ExpectedValue what, String description, Test... all) {
        CompoundTest result = CompoundTest.all(what, all);
        result.setDescription(description);
        return result;
    }

    public static CompoundTest greedyAll(Result.ExpectedValue what, Test... all) {
        return new CompoundTest((tests) -> {
            for (Test test : tests) {
                if (!Result.Value.fromExpected(what, test.ok()).ok()) {
                    return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
                }
            }
            return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
        }, (tests) -> {
            for (Test t : tests) {
                t.run();
                if (!t.ok()) {
                    break;
                }
            }
        }, all);
    }

    public static CompoundTest greedyAll(Result.ExpectedValue what, String description, Test... all) {
        CompoundTest result = CompoundTest.greedyAll(what, all);
        result.setDescription(description);
        return result;
    }

    public static CompoundTest greedyAllTry(Result.ExpectedValue what, Test... all) {
        return new CompoundTest((tests) -> {
            int run = 0;
            int ok = 0;
            for (Test test : tests) {
                if (test.hasRun()) {
                    run++;
                    if (Result.Value.fromExpected(what, test.ok()).ok()) {
                        ok++;
                    }
                }
            }
            if (run == tests.length) {
                if (ok == run) {
                    return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
                } else {
                    return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
                }
            } else {
                return new Result(Result.Value.SUCCESS, "All considered sub-tests had the expected result.");
            }
        }, (tests) -> {
            for (Test t : tests) {
                t.run();
                if (!t.ok()) {
                    break;
                }
            }
        }, all);
    }

    public static CompoundTest greedyAllTry(Result.ExpectedValue what, String description, Test... all) {
        CompoundTest result = CompoundTest.greedyAllTry(what, all);
        result.setDescription(description);
        return result;
    }

    public static CompoundTest greedyAny(Result.ExpectedValue what, Test... any) {
        return new CompoundTest((tests) -> {
            for (Test test : tests) {
                if (Result.Value.fromExpected(what, test.ok()).ok()) {
                    return new Result(Result.Value.SUCCESS, "Some sub-tests did have the expected result.");
                }
            }
            return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
        }, (tests) -> {
            for (Test t : tests) {
                t.run();
                if (t.ok()) {
                    break;
                }
            }
        }, any);
    }

    public static CompoundTest greedyAny(Result.ExpectedValue what, String description, Test... any) {
        CompoundTest result = CompoundTest.greedyAny(what, any);
        result.setDescription(description);
        return result;
    }

    public static CompoundTest any(Result.ExpectedValue what, Test... any) {
        return new CompoundTest((tests) -> {
            for (Test test : tests) {
                if (Result.Value.fromExpected(what, test.ok()).ok()) {
                    return new Result(Result.Value.SUCCESS, "Some sub-tests did have the expected result.");
                }
            }
            return new Result(Result.Value.FAILURE, "None of the sub-tests had the expected result.");
        }, RUN_ALL, any);
    }

    public static CompoundTest any(Result.ExpectedValue what, String description, Test... any) {
        CompoundTest result = CompoundTest.any(what, any);
        result.setDescription(description);
        return result;
    }

    public static CompoundTest mask(Result.ExpectedValue[] results, Test... masked) {
        return new CompoundTest((tests) -> {
            for (int i = 0; i < results.length; ++i) {
                if (!Result.Value.fromExpected(results[i], tests[i].ok()).ok()) {
                    return new Result(Result.Value.FAILURE, "Some sub-tests did not match the result mask.");
                }
            }
            return new Result(Result.Value.SUCCESS, "All sub-tests matched the expected mask.");
        }, RUN_ALL, masked);
    }

    public static CompoundTest mask(Result.ExpectedValue[] results, String description, Test... masked) {
        CompoundTest result = CompoundTest.mask(results, masked);
        result.setDescription(description);
        return result;
    }

    public Test[] getTests() {
        return tests.clone();
    }

    public Test[] getRunTests() {
        return Arrays.stream(tests).filter(Test::hasRun).toArray(Test[]::new);
    }

    public Test[] getStartedTests() {
        return Arrays.stream(tests).filter(Test::hasStarted).toArray(Test[]::new);
    }

    public Test[] getSkippedTests() {
        return Arrays.stream(tests).filter((test) -> !test.hasRun()).toArray(Test[]::new);
    }

    @Override
    protected void runSelf() {
        runCallback.accept(tests);
        result = resultCallback.apply(tests);
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String getDescription() {
        return description;
    }
}