Skip to content

Commit fa0403c

Browse files
authored
Merge pull request #6 from stackrox/sb/add-find-func
Add find function for Test
2 parents c6fc838 + e2cdbca commit fa0403c

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

pkg/framework/test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,37 @@ func (t *Test) forEachScopeTopDown(doFn func(t *Test)) {
144144
}
145145
doFn(t)
146146
}
147+
148+
// findFirst tries to find the first test under a given path where the path's index represents the level of the test.
149+
func (t *Test) findFirst(path []string) *Test {
150+
r := t.find(path)
151+
if len(r) > 0 {
152+
return r[0]
153+
}
154+
155+
return nil
156+
}
157+
158+
// find tries to find all tests under a given path where the path's index represents the level of the test.
159+
func (t *Test) find(path []string) []*Test {
160+
var result []*Test
161+
if t == nil || len(path) == 0 {
162+
return nil
163+
}
164+
165+
if path[0] != t.Name {
166+
return nil
167+
}
168+
if len(path) == 1 {
169+
return append(result, t)
170+
}
171+
172+
for _, child := range t.Tests {
173+
found := child.find(path[1:])
174+
if found != nil {
175+
return append(result, found...)
176+
}
177+
}
178+
179+
return nil
180+
}

pkg/framework/test_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package framework
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/stretchr/testify/require"
6+
"testing"
7+
)
8+
9+
func TestFind(t *testing.T) {
10+
childTest := &Test{
11+
Name: "child test",
12+
Tests: []*Test{{
13+
Name: "child child test",
14+
}},
15+
}
16+
anotherChildTest := &Test{
17+
Name: "another child test",
18+
Tests: []*Test{{
19+
Name: "another child child test",
20+
}},
21+
}
22+
23+
suite := &Test{
24+
Name: "root test",
25+
Tests: []*Test{
26+
childTest,
27+
anotherChildTest,
28+
{Name: "same name"},
29+
{Name: "same name"},
30+
},
31+
}
32+
33+
testCases := map[string]struct {
34+
query []string
35+
expectNotFound bool
36+
}{
37+
"with only root node": {query: []string{"root test"}},
38+
"with child test": {query: []string{"root test", "child test"}},
39+
"with nested child": {query: []string{"root test", "child test", "child child test"}},
40+
"with not existing nested": {query: []string{"root test", "child test", "child child test", "does not exist"}, expectNotFound: true},
41+
"with not existing root": {query: []string{"root does not exist"}, expectNotFound: true},
42+
"with another child": {query: []string{"root test", "another child test"}},
43+
"with another nested child": {query: []string{"root test", "another child test", "another child child test"}},
44+
"with same name finds both": {query: []string{"root test", "same name"}},
45+
"with not existent root should not be found": {query: []string{"non-existent root test", "child test"}, expectNotFound: true},
46+
}
47+
48+
for _, tt := range testCases {
49+
results := suite.find(tt.query)
50+
if tt.expectNotFound {
51+
assert.Empty(t, results)
52+
} else {
53+
require.NotEmpty(t, results)
54+
for _, result := range results {
55+
assert.Equal(t, tt.query[len(tt.query)-1], result.Name)
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)