/
filter.go
72 lines (57 loc) · 1.83 KB
/
filter.go
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
package gen
import (
"encoding/json"
"fmt"
)
// getAllTestCasesFiltered recursively collects all test cases in a flattened list except the ones excluded by excludedTests.
func getAllTestCasesFiltered(jSrc []byte, excludedTests map[string]struct{}) (*[]testCase, error) {
var result = &[]testCase{}
// put the json object in an array to match the recursive structure
jSrc = append([]byte{'['}, append(jSrc, ']')...)
// recursively get all test cases except the excluded ones
err := recursiveFilterCases(jSrc, result, excludedTests)
if err != nil {
return nil, fmt.Errorf("failed to get/filter all test cases: %w", err)
}
return result, nil
}
func recursiveFilterCases(cases json.RawMessage, result *[]testCase, excludedTests map[string]struct{}) error {
var js []map[string]json.RawMessage
// 'cases' is always an array, where every item is either a single test or an object containing nested cases
err := json.Unmarshal(cases, &js)
if err != nil {
return err
}
// iterating over every item in 'cases'
for _, j := range js {
// If uuid key is present, this item is a single test-case
if uuid, ok := j["uuid"]; ok {
var uuidStr string
err := json.Unmarshal(uuid, &uuidStr)
if err != nil {
return fmt.Errorf("failed to unmarshal uuid %v: %w", uuid, err)
}
// ignore test-cases with include=false in tests.toml
if _, isExcluded := excludedTests[uuidStr]; isExcluded {
continue
}
jTestCase, err := json.Marshal(j)
if err != nil {
return err
}
var tc = testCase{}
err = json.Unmarshal(jTestCase, &tc)
if err != nil {
return err
}
*result = append(*result, tc)
continue
}
// If uuid key is not present, this item is an object containing nested cases.
err := recursiveFilterCases(j["cases"], result, excludedTests)
if err != nil {
return err
}
}
return nil
}