-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add helper function to generate unique case name
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2024 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package testing | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
) | ||
|
||
var caseNameReg = regexp.MustCompile(`#CASE_(\w+)#`) | ||
var unitTestCaseNameReg = regexp.MustCompile(`^Test(\w+)$`) | ||
|
||
// Case the unique case name label | ||
func Case(caseName string) Labels { | ||
if caseName == "" { | ||
return Labels{} | ||
} | ||
return Labels{fmt.Sprintf("#CASE_%s#", caseName)} | ||
} | ||
|
||
// GetCaseNames Resolve the case identifier from the testcase name in the junit report | ||
// For go test junit report, the case name may be started with `Test`, e.g: TestGetProject | ||
// For ginkgo test junit report, the case name may contain the #CASE_%s# string, e.g: [It] when xxxx [#CASE_GetProject#] | ||
func GetCaseNames(name string) []string { | ||
parts := caseNameReg.FindAllStringSubmatch(name, -1) | ||
|
||
if len(parts) == 0 { | ||
parts = unitTestCaseNameReg.FindAllStringSubmatch(name, -1) | ||
} | ||
|
||
matchedNames := make([]string, 0, len(parts)) | ||
for _, item := range parts { | ||
matchedNames = append(matchedNames, item[1]) | ||
} | ||
|
||
return matchedNames | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright 2024 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package testing | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestGetCaseName(t *testing.T) { | ||
g := NewWithT(t) | ||
tests := []struct { | ||
name string | ||
s string | ||
want []string | ||
}{ | ||
{ | ||
name: "without case name", | ||
s: "test", | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "with case name", | ||
s: "#CASE_aaaa#", | ||
want: []string{"aaaa"}, | ||
}, | ||
{ | ||
name: "case name contains underscore", | ||
s: "#CASE_aaaa_bbbb#", | ||
want: []string{"aaaa_bbbb"}, | ||
}, | ||
{ | ||
name: "case name contains special characters", | ||
s: "#CASE_aaaa@bbbb#", | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "empty string", | ||
s: "", | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "Multiple case name", | ||
s: "#CASE_aaaa# #CASE_bbbb#", | ||
want: []string{"aaaa", "bbbb"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g.Expect(GetCaseNames(tt.s)).To(Equal(tt.want)) | ||
}) | ||
} | ||
} | ||
|
||
func TestCase(t *testing.T) { | ||
g := NewWithT(t) | ||
tests := []struct { | ||
name string | ||
caseName string | ||
want ginkgo.Labels | ||
}{ | ||
{ | ||
name: "empty case name", | ||
caseName: "", | ||
want: ginkgo.Labels{}, | ||
}, | ||
{ | ||
name: "case name", | ||
caseName: "abc", | ||
want: ginkgo.Labels{"#CASE_abc#"}, | ||
}, | ||
{ | ||
name: "case name contains underscore", | ||
caseName: "abc_eee", | ||
want: ginkgo.Labels{"#CASE_abc_eee#"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g.Expect(Case(tt.caseName)).To(Equal(tt.want)) | ||
}) | ||
} | ||
} |