Skip to content

Commit 52ce27d

Browse files
authored
feat: support with nested parent test cases (#35)
1 parent a27b13a commit 52ce27d

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

pkg/server/remote_server.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
4646
for key, val := range oldEnv {
4747
os.Setenv(key, val)
4848
}
49+
fmt.Println(reply, err)
4950
}()
5051

5152
switch task.Kind {
@@ -82,6 +83,7 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
8283

8384
if targetTestcase != nil {
8485
parentCases := findParentTestCases(targetTestcase, suite)
86+
fmt.Printf("find %d parent cases\n", len(parentCases))
8587
suite.Items = append(parentCases, *targetTestcase)
8688
} else {
8789
err = fmt.Errorf("cannot found testcase %s", task.CaseName)
@@ -101,6 +103,8 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
101103
suite.API = result
102104
suite.API = strings.TrimSuffix(suite.API, "/")
103105
} else {
106+
reply.Error = err.Error()
107+
err = nil
104108
return
105109
}
106110

@@ -138,28 +142,71 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
138142
reg, matchErr := regexp.Compile(`.*\{\{.*\.\w*.*}\}.*`)
139143
targetReg, targetErr := regexp.Compile(`\.\w*`)
140144

145+
expectNames := new(UniqueSlice[string])
141146
if matchErr == nil && targetErr == nil {
142-
expectName := ""
147+
var expectName string
143148
for _, val := range testcase.Request.Header {
144149
if matched := reg.MatchString(val); matched {
145150
expectName = targetReg.FindString(val)
146151
expectName = strings.TrimPrefix(expectName, ".")
147-
break
152+
expectNames.Push(expectName)
148153
}
149154
}
150155

151-
if expectName == "" {
152-
if mached := reg.MatchString(testcase.Request.API); mached {
153-
expectName = targetReg.FindString(testcase.Request.API)
156+
if mached := reg.MatchString(testcase.Request.API); mached {
157+
// remove {{ and }}
158+
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
159+
api := left.ReplaceAllString(testcase.Request.API, "")
160+
161+
expectName = targetReg.FindString(api)
154162
expectName = strings.TrimPrefix(expectName, ".")
163+
expectNames.Push(expectName)
155164
}
156165
}
157166

167+
fmt.Println("expect test case names", expectNames.GetAll())
158168
for _, item := range suite.Items {
159-
if item.Name == expectName {
169+
if expectNames.Exist(item.Name) {
160170
testcases = append(testcases, item)
161171
}
162172
}
163173
}
164174
return
165175
}
176+
177+
// UniqueSlice represents an unique slice
178+
type UniqueSlice[T comparable] struct {
179+
data []T
180+
}
181+
182+
// Push pushes an item if it's not exist
183+
func (s *UniqueSlice[T]) Push(item T) *UniqueSlice[T] {
184+
if s.data == nil {
185+
s.data = []T{item}
186+
} else {
187+
for _, it := range s.data {
188+
if it == item {
189+
return s
190+
}
191+
}
192+
s.data = append(s.data, item)
193+
}
194+
return s
195+
}
196+
197+
// Exist checks if the item exist, return true it exists
198+
func (s *UniqueSlice[T]) Exist(item T) bool {
199+
if s.data != nil {
200+
for _, it := range s.data {
201+
if it == item {
202+
return true
203+
}
204+
}
205+
}
206+
return false
207+
}
208+
209+
// GetAll returns all the items
210+
func (s *UniqueSlice[T]) GetAll() []T {
211+
return s.data
212+
}

pkg/server/remote_server_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,43 @@ func TestFindParentTestCases(t *testing.T) {
107107
expect: []atesting.TestCase{{
108108
Name: "login",
109109
}},
110+
}, {
111+
name: "nest dep",
112+
testcase: &atesting.TestCase{
113+
Name: "user",
114+
Request: atesting.Request{
115+
API: "/users/{{(index .users 0).name}}",
116+
Header: map[string]string{
117+
"Authorization": "Bearer {{.login.data.access_token}}",
118+
},
119+
},
120+
},
121+
suite: &atesting.TestSuite{
122+
Items: []atesting.TestCase{{
123+
Name: "login",
124+
}, {
125+
Name: "users",
126+
Request: atesting.Request{
127+
API: "/users",
128+
},
129+
}, {
130+
Name: "user",
131+
Request: atesting.Request{
132+
API: "/users/{{(index .users 0).name}}",
133+
Header: map[string]string{
134+
"Authorization": "Bearer {{.login.data.access_token}}",
135+
},
136+
},
137+
}},
138+
},
139+
expect: []atesting.TestCase{{
140+
Name: "login",
141+
}, {
142+
Name: "users",
143+
Request: atesting.Request{
144+
API: "/users",
145+
},
146+
}},
110147
}}
111148
for _, tt := range tests {
112149
t.Run(tt.name, func(t *testing.T) {
@@ -116,6 +153,12 @@ func TestFindParentTestCases(t *testing.T) {
116153
}
117154
}
118155

156+
func TestUniqueSlice(t *testing.T) {
157+
uniqueSlice := new(UniqueSlice[string])
158+
uniqueSlice.Push("a").Push("a").Push("b")
159+
assert.Equal(t, []string{"a", "b"}, uniqueSlice.GetAll())
160+
}
161+
119162
//go:embed testdata/simple.yaml
120163
var simpleSuite string
121164

0 commit comments

Comments
 (0)