forked from getgauge-contrib/gauge-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaugecontext.go
85 lines (76 loc) · 1.65 KB
/
gaugecontext.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
package testsuit
import (
"errors"
"fmt"
)
type GaugeContext struct {
Steps []Step
Hooks []Hook
ScenarioStore map[string]interface{}
SpecStore map[string]interface{}
SuiteStore map[string]interface{}
CustomMessageRegistry []string
}
func (c *GaugeContext) GetStepByDesc(desc string) (*Step, error) {
for _, step := range c.Steps {
if step.Description == desc {
return &step, nil
}
}
return nil, errors.New(fmt.Sprintf("No implementation found for : %s", desc))
}
func (c *GaugeContext) GetHooks(hookType HookType, tags []string) []Hook {
filteredByType := filterByType(c.Hooks, hookType)
h := make([]Hook, 0)
//TODO complexity is O(n^3) optimize it
for _, hook := range filteredByType {
switch hook.Operator {
case OR:
if containsAny(tags, hook.Tags) {
h = append(h, hook)
}
case AND:
if containsAll(tags, hook.Tags) {
h = append(h, hook)
}
}
}
return h
}
// ClearCustomMessages clears the CustomMessageRegistry
func (c *GaugeContext) ClearCustomMessages() {
c.CustomMessageRegistry = make([]string, 0)
}
func filterByType(hooks []Hook, t HookType) []Hook {
h := make([]Hook, 0)
for _, hook := range hooks {
if hook.Type == t {
h = append(h, hook)
}
}
return h
}
func containsAny(s []string, k []string) bool {
for _, tag := range k {
if contains(s, tag) {
return true
}
}
return false
}
func containsAll(s []string, k []string) bool {
for _, tag := range k {
if !contains(s, tag) {
return false
}
}
return true
}
func contains(s []string, k string) bool {
for _, a := range s {
if a == k {
return true
}
}
return false
}