mirror of https://github.com/knative/pkg.git
Move common helper functions to pkg test (#424)
* move common functions to pkg test * add unit test cases * fix CR issues
This commit is contained in:
parent
523d91657b
commit
d66945c363
|
|
@ -20,12 +20,13 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
letterBytes = "abcdefghijklmnopqrstuvwxyz"
|
letterBytes = "abcdefghijklmnopqrstuvwxyz"
|
||||||
randSuffixLen = 8
|
randSuffixLen = 8
|
||||||
sep = "-"
|
sep = '-'
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -45,5 +46,32 @@ func AppendRandomString(prefix string) string {
|
||||||
suffix[i] = letterBytes[rand.Intn(len(letterBytes))]
|
suffix[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join([]string{prefix, string(suffix)}, sep)
|
return strings.Join([]string{prefix, string(suffix)}, string(sep))
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeK8sNamePrefix converts each chunk of non-alphanumeric character into a single dash
|
||||||
|
// and also convert camelcase tokens into dash-delimited lowercase tokens.
|
||||||
|
func MakeK8sNamePrefix(s string) string {
|
||||||
|
var sb strings.Builder
|
||||||
|
newToken := false
|
||||||
|
for _, c := range s {
|
||||||
|
if !(unicode.IsLetter(c) || unicode.IsNumber(c)) {
|
||||||
|
newToken = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if sb.Len() > 0 && (newToken || unicode.IsUpper(c)) {
|
||||||
|
sb.WriteRune(sep)
|
||||||
|
}
|
||||||
|
sb.WriteRune(unicode.ToLower(c))
|
||||||
|
newToken = false
|
||||||
|
}
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBaseFuncName returns the baseFuncName parsed from the fullFuncName.
|
||||||
|
// eg. test/e2e.TestMain will return TestMain.
|
||||||
|
func GetBaseFuncName(fullFuncName string) string {
|
||||||
|
baseFuncName := fullFuncName[strings.LastIndex(fullFuncName, "/")+1:]
|
||||||
|
baseFuncName = baseFuncName[strings.LastIndex(baseFuncName, ".")+1:]
|
||||||
|
return baseFuncName
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,16 +17,53 @@ limitations under the License.
|
||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
var matcher = regexp.MustCompile("abcd-[a-z]{8}")
|
var matcher = regexp.MustCompile("abcd-[a-z]{8}")
|
||||||
|
|
||||||
func ExampleAppendRandomString() {
|
func TestAppendRandomString(t *testing.T) {
|
||||||
const s = "abcd"
|
const s = "abcd"
|
||||||
t := AppendRandomString(s)
|
w := AppendRandomString(s)
|
||||||
o := AppendRandomString(s)
|
o := AppendRandomString(s)
|
||||||
fmt.Println(matcher.MatchString(t), matcher.MatchString(o), o != t)
|
if !matcher.MatchString(w) || !matcher.MatchString(o) || o == w {
|
||||||
// Output: true true true
|
t.Fatalf("Generated string(s) are incorrect: %q, %q", w, o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeK8sNamePrefix(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"abcd123", "abcd123"},
|
||||||
|
{"AbCdef", "ab-cdef"},
|
||||||
|
{"ABCD", "a-b-c-d"},
|
||||||
|
{"aBc*ef&d", "a-bc-ef-d"},
|
||||||
|
}
|
||||||
|
for _, v := range testCases {
|
||||||
|
actual := MakeK8sNamePrefix(v.input)
|
||||||
|
if v.expected != actual {
|
||||||
|
t.Fatalf("Expect %q but actual is %q", v.expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetBaseFuncName(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"test/e2e.TestMain", "TestMain"},
|
||||||
|
{"e2e.TestMain", "TestMain"},
|
||||||
|
{"test/TestMain", "TestMain"},
|
||||||
|
{"TestMain", "TestMain"},
|
||||||
|
}
|
||||||
|
for _, v := range testCases {
|
||||||
|
actual := GetBaseFuncName(v.input)
|
||||||
|
if v.expected != actual {
|
||||||
|
t.Fatalf("Expect %q but actual is %q", v.expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue