Skip to content

Commit e27be10

Browse files
authored
fix: cannot find the correct parent test cases (#56)
1 parent 9df7a66 commit e27be10

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

.vscode/extensions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"recommendations": [
3-
"linuxsuren.api-testing"
3+
"linuxsuren.api-testing",
4+
"redhat.vscode-yaml"
45
]
56
}

pkg/render/template_test.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,24 @@ func TestRender(t *testing.T) {
2929
verify: func(t *testing.T, s string) {
3030
assert.Equal(t, 8, len(s))
3131
},
32+
}, {
33+
name: "complex",
34+
text: `{{(index .items 0).name}}?a=a&key={{randomKubernetesName}}`,
35+
ctx: map[string]interface{}{
36+
"items": []interface{}{map[string]string{
37+
"name": "one",
38+
}, map[string]string{
39+
"name": "two",
40+
}},
41+
},
42+
verify: func(t *testing.T, s string) {
43+
assert.Equal(t, 20, len(s), s)
44+
},
3245
}}
3346
for _, tt := range tests {
3447
t.Run(tt.name, func(t *testing.T) {
3548
result, err := Render(tt.name, tt.text, tt.ctx)
36-
assert.Nil(t, err)
49+
assert.Nil(t, err, err)
3750
if tt.expect != "" {
3851
assert.Equal(t, tt.expect, result)
3952
}

pkg/server/remote_server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (s *server) GetVersion(ctx context.Context, in *Empty) (reply *HelloReply,
141141
}
142142

143143
func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (testcases []testing.TestCase) {
144-
reg, matchErr := regexp.Compile(`.*\{\{.*\.\w*.*}\}.*`)
144+
reg, matchErr := regexp.Compile(`(.*?\{\{.*\.\w*.*?\}\})`)
145145
targetReg, targetErr := regexp.Compile(`\.\w*`)
146146

147147
expectNames := new(UniqueSlice[string])
@@ -155,10 +155,10 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
155155
}
156156
}
157157

158-
if mached := reg.MatchString(testcase.Request.API); mached {
158+
for _, sub := range reg.FindStringSubmatch(testcase.Request.API) {
159159
// remove {{ and }}
160160
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
161-
api := left.ReplaceAllString(testcase.Request.API, "")
161+
api := left.ReplaceAllString(sub, "")
162162

163163
expectName = targetReg.FindString(api)
164164
expectName = strings.TrimPrefix(expectName, ".")

pkg/server/remote_server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestFindParentTestCases(t *testing.T) {
112112
testcase: &atesting.TestCase{
113113
Name: "user",
114114
Request: atesting.Request{
115-
API: "/users/{{(index .users 0).name}}",
115+
API: "/users/{{(index .users 0).name}}{{randomKubernetesName}}",
116116
Header: map[string]string{
117117
"Authorization": "Bearer {{.login.data.access_token}}",
118118
},

sample/kubernetes.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!api-testing
2+
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
23
name: Kubernetes
34
api: |
45
{{default "https://172.11.0.18:6443" (env "SERVER")}}

sample/testsuite-gitee.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!api-testing
2+
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
23
name: Gitee
34
api: https://gitee.com/api/v5
45
items:

0 commit comments

Comments
 (0)