-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.go
97 lines (76 loc) · 1.68 KB
/
test.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
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
package barrier
import (
"fmt"
"strings"
)
// A Test represents an actual test.
type Test struct {
id string
Name string
Description string
Author string
Tags []string
Setup SetupFunction
Function TestFunction
SuiteName string
}
// matchTags matches all tags if --match-all is set otherwise matches any tag
func (t Test) matchTags(tags []string, matchAll bool) bool {
m := make([]string, len(tags))
copy(m, tags)
if !matchAll {
return t.matchAnyTags(m)
}
return t.matchAllTags(m)
}
// matchAllTags returns true if all incoming tags are matching minus exclusions
func (t Test) matchAllTags(tags []string) bool {
if len(tags) == 0 {
return true
}
for _, incoming := range tags {
if strings.HasPrefix(incoming, "~") {
if t.hasTag(strings.TrimPrefix(incoming, "~")) {
return false
}
continue
}
if !t.hasTag(incoming) {
return false
}
}
return true
}
// matchAnyTags returns true if any incoming tags are matching
func (t Test) matchAnyTags(tags []string) bool {
if len(tags) == 0 {
return true
}
for _, incoming := range tags {
if t.hasTag(incoming) {
return true
}
}
return false
}
// hasTag returns true if the slice has the tag
func (t Test) hasTag(tag string) bool {
tags := t.Tags
if t.SuiteName != "" {
tags = append(tags, fmt.Sprintf("suite:%s", strings.ToLower(t.SuiteName)))
}
for _, testTag := range tags {
if tag == testTag {
return true
}
}
return false
}
func (t Test) String() string {
return fmt.Sprintf(` id : %s
name : %s
desc : %s
author : %s
categories : %s
`, t.id, t.Name, t.Description, t.Author, strings.Join(t.Tags, ", "))
}