File: specs.go

package info (click to toggle)
golang-ginkgo 1.2.0%2Bgit20161006.acfa16a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,324 kB
  • ctags: 1,210
  • sloc: makefile: 12
file content (138 lines) | stat: -rw-r--r-- 2,947 bytes parent folder | download | duplicates (2)
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
package spec

import (
	"math/rand"
	"regexp"
	"sort"
)

type Specs struct {
	specs                 []*Spec
	numberOfOriginalSpecs int
	hasProgrammaticFocus  bool
	RegexScansFilePath    bool
}

func NewSpecs(specs []*Spec) *Specs {
	return &Specs{
		specs: specs,
		numberOfOriginalSpecs: len(specs),
	}
}

func (e *Specs) Specs() []*Spec {
	return e.specs
}

func (e *Specs) NumberOfOriginalSpecs() int {
	return e.numberOfOriginalSpecs
}

func (e *Specs) HasProgrammaticFocus() bool {
	return e.hasProgrammaticFocus
}

func (e *Specs) Shuffle(r *rand.Rand) {
	sort.Sort(e)
	permutation := r.Perm(len(e.specs))
	shuffledSpecs := make([]*Spec, len(e.specs))
	for i, j := range permutation {
		shuffledSpecs[i] = e.specs[j]
	}
	e.specs = shuffledSpecs
}

func (e *Specs) ApplyFocus(description string, focusString string, skipString string) {
	if focusString == "" && skipString == "" {
		e.applyProgrammaticFocus()
	} else {
		e.applyRegExpFocusAndSkip(description, focusString, skipString)
	}
}

func (e *Specs) applyProgrammaticFocus() {
	e.hasProgrammaticFocus = false
	for _, spec := range e.specs {
		if spec.Focused() && !spec.Pending() {
			e.hasProgrammaticFocus = true
			break
		}
	}

	if e.hasProgrammaticFocus {
		for _, spec := range e.specs {
			if !spec.Focused() {
				spec.Skip()
			}
		}
	}
}

// toMatch returns a byte[] to be used by regex matchers.  When adding new behaviours to the matching function,
// this is the place which we append to.
func (e *Specs) toMatch(description string, spec *Spec) []byte {
	if e.RegexScansFilePath {
		return []byte(
			description + " " +
				spec.ConcatenatedString() + " " +
				spec.subject.CodeLocation().FileName)
	} else {
		return []byte(
			description + " " +
				spec.ConcatenatedString())
	}
}

func (e *Specs) applyRegExpFocusAndSkip(description string, focusString string, skipString string) {
	for _, spec := range e.specs {
		matchesFocus := true
		matchesSkip := false

		toMatch := e.toMatch(description, spec)

		if focusString != "" {
			focusFilter := regexp.MustCompile(focusString)
			matchesFocus = focusFilter.Match([]byte(toMatch))
		}

		if skipString != "" {
			skipFilter := regexp.MustCompile(skipString)
			matchesSkip = skipFilter.Match([]byte(toMatch))
		}

		if !matchesFocus || matchesSkip {
			spec.Skip()
		}
	}
}

func (e *Specs) SkipMeasurements() {
	for _, spec := range e.specs {
		if spec.IsMeasurement() {
			spec.Skip()
		}
	}
}

func (e *Specs) TrimForParallelization(total int, node int) {
	startIndex, count := ParallelizedIndexRange(len(e.specs), total, node)
	if count == 0 {
		e.specs = make([]*Spec, 0)
	} else {
		e.specs = e.specs[startIndex : startIndex+count]
	}
}

//sort.Interface

func (e *Specs) Len() int {
	return len(e.specs)
}

func (e *Specs) Less(i, j int) bool {
	return e.specs[i].ConcatenatedString() < e.specs[j].ConcatenatedString()
}

func (e *Specs) Swap(i, j int) {
	e.specs[i], e.specs[j] = e.specs[j], e.specs[i]
}