Skip to content

Commit

Permalink
fix: cannot find the correct parent test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Apr 25, 2023
1 parent 9df7a66 commit 134bee5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"recommendations": [
"linuxsuren.api-testing"
"linuxsuren.api-testing",
"redhat.vscode-yaml"
]
}
15 changes: 14 additions & 1 deletion pkg/render/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,24 @@ func TestRender(t *testing.T) {
verify: func(t *testing.T, s string) {
assert.Equal(t, 8, len(s))
},
}, {
name: "complex",
text: `{{(index .items 0).name}}?a=a&key={{randomKubernetesName}}`,
ctx: map[string]interface{}{
"items": []interface{}{map[string]string{
"name": "one",
}, map[string]string{
"name": "two",
}},
},
verify: func(t *testing.T, s string) {
assert.Equal(t, 20, len(s), s)
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Render(tt.name, tt.text, tt.ctx)
assert.Nil(t, err)
assert.Nil(t, err, err)
if tt.expect != "" {
assert.Equal(t, tt.expect, result)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *server) GetVersion(ctx context.Context, in *Empty) (reply *HelloReply,
}

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

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

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

expectName = targetReg.FindString(api)
expectName = strings.TrimPrefix(expectName, ".")
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/remote_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestFindParentTestCases(t *testing.T) {
testcase: &atesting.TestCase{
Name: "user",
Request: atesting.Request{
API: "/users/{{(index .users 0).name}}",
API: "/users/{{(index .users 0).name}}{{randomKubernetesName}}",
Header: map[string]string{
"Authorization": "Bearer {{.login.data.access_token}}",
},
Expand Down
1 change: 1 addition & 0 deletions sample/kubernetes.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!api-testing
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
name: Kubernetes
api: |
{{default "https://172.11.0.18:6443" (env "SERVER")}}
Expand Down
1 change: 1 addition & 0 deletions sample/testsuite-gitee.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!api-testing
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
name: Gitee
api: https://gitee.com/api/v5
items:
Expand Down

0 comments on commit 134bee5

Please sign in to comment.