1 /**
2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 
10 This file contains the interface used for reporting test cases that are found.
11 */
12 module dextool.plugin.mutate.backend.test_mutant.interface_;
13 
14 import dextool.plugin.mutate.backend.type : TestCase;
15 
16 interface TestCaseReport {
17     /// A test case that failed.
18     void reportFailed(TestCase tc) @safe nothrow;
19 
20     /// A test case that is found
21     void reportFound(TestCase tc) @safe nothrow;
22 
23     /// One or more test cases are unstable.
24     void reportUnstable(TestCase tc) @safe nothrow;
25 }
26 
27 /// A simple class to gather reported test cases.
28 class GatherTestCase : TestCaseReport {
29     import std.algorithm : map;
30     import std.array : array;
31     import dextool.set;
32 
33     /// Test cases reported as failed.
34     long[TestCase] failed;
35 
36     /// The unstable test cases.
37     Set!TestCase unstable;
38 
39     /// Found test cases.
40     Set!TestCase found;
41 
42     void merge(GatherTestCase o) @safe nothrow {
43         foreach (kv; o.failed.byKeyValue) {
44             if (auto v = kv.key in failed)
45                 (*v) += kv.value;
46             else
47                 failed[kv.key] = kv.value;
48         }
49 
50         foreach (k; o.found.toRange) {
51             found.add(k);
52         }
53 
54         foreach (k; o.unstable.toRange) {
55             unstable.add(k);
56         }
57     }
58 
59     TestCase[] failedAsArray() @safe nothrow {
60         return failed.byKey.array;
61     }
62 
63     TestCase[] foundAsArray() @safe nothrow {
64         return found.toArray;
65     }
66 
67     TestCase[] unstableAsArray() @safe nothrow {
68         return unstable.toArray;
69     }
70 
71     override void reportFailed(TestCase tc) @safe nothrow {
72         found.add(tc);
73 
74         if (auto v = tc in failed) {
75             (*v)++;
76         } else {
77             failed[tc] = 1;
78         }
79     }
80 
81     /// A test case that is found
82     override void reportFound(TestCase tc) @safe nothrow {
83         found.add(tc);
84     }
85 
86     override void reportUnstable(TestCase tc) @safe nothrow {
87         found.add(tc);
88         unstable.add(tc);
89     }
90 }