✏ update utils

Co-Authored-By: RW <7063188+ReneWerner87@users.noreply.github.com>
pull/1025/head
Fenny 2020-11-17 08:24:28 +01:00
parent e44da45bff
commit 0cc60e3e39
9 changed files with 58 additions and 58 deletions

View File

@ -6,7 +6,7 @@ package utils
import "testing"
func Test_Utils_AssertEqual(t *testing.T) {
func Test_AssertEqual(t *testing.T) {
t.Parallel()
AssertEqual(nil, []string{}, []string{})
AssertEqual(t, []string{}, []string{})

View File

@ -68,17 +68,3 @@ func EqualFoldBytes(b, s []byte) (equals bool) {
}
return
}
// DEPRECATED, please use EqualFoldBytes
func EqualsFold(b, s []byte) (equals bool) {
n := len(b)
equals = n == len(s)
if equals {
for i := 0; i < n; i++ {
if equals = b[i]|0x20 == s[i]|0x20; !equals {
break
}
}
}
return
}

View File

@ -9,7 +9,7 @@ import (
"testing"
)
func Test_Utils_ToLowerBytes(t *testing.T) {
func Test_ToLowerBytes(t *testing.T) {
t.Parallel()
res := ToLowerBytes([]byte("/MY/NAME/IS/:PARAM/*"))
AssertEqual(t, true, bytes.Equal([]byte("/my/name/is/:param/*"), res))
@ -41,7 +41,7 @@ func Benchmark_ToLowerBytes(b *testing.B) {
})
}
func Test_Utils_ToUpperBytes(t *testing.T) {
func Test_ToUpperBytes(t *testing.T) {
t.Parallel()
res := ToUpperBytes([]byte("/my/name/is/:param/*"))
AssertEqual(t, true, bytes.Equal([]byte("/MY/NAME/IS/:PARAM/*"), res))
@ -73,7 +73,7 @@ func Benchmark_ToUpperBytes(b *testing.B) {
})
}
func Test_Utils_TrimRightBytes(t *testing.T) {
func Test_TrimRightBytes(t *testing.T) {
t.Parallel()
res := TrimRightBytes([]byte("/test//////"), '/')
AssertEqual(t, []byte("/test"), res)
@ -99,7 +99,7 @@ func Benchmark_TrimRightBytes(b *testing.B) {
})
}
func Test_Utils_TrimLeftBytes(t *testing.T) {
func Test_TrimLeftBytes(t *testing.T) {
t.Parallel()
res := TrimLeftBytes([]byte("////test/"), '/')
AssertEqual(t, []byte("test/"), res)
@ -123,7 +123,7 @@ func Benchmark_TrimLeftBytes(b *testing.B) {
AssertEqual(b, []byte("foobar"), res)
})
}
func Test_Utils_TrimBytes(t *testing.T) {
func Test_TrimBytes(t *testing.T) {
t.Parallel()
res := TrimBytes([]byte(" test "), ' ')
AssertEqual(t, []byte("test"), res)

View File

@ -10,24 +10,24 @@ import (
"testing"
)
func Test_Utils_FunctionName(t *testing.T) {
func Test_FunctionName(t *testing.T) {
t.Parallel()
AssertEqual(t, "github.com/gofiber/fiber/v2/utils.Test_Utils_UUID", FunctionName(Test_Utils_UUID))
AssertEqual(t, "github.com/gofiber/fiber/v2/utils.Test_UUID", FunctionName(Test_UUID))
AssertEqual(t, "github.com/gofiber/fiber/v2/utils.Test_Utils_FunctionName.func1", FunctionName(func() {}))
AssertEqual(t, "github.com/gofiber/fiber/v2/utils.Test_FunctionName.func1", FunctionName(func() {}))
var dummyint = 20
AssertEqual(t, "int", FunctionName(dummyint))
}
func Test_Utils_UUID(t *testing.T) {
func Test_UUID(t *testing.T) {
t.Parallel()
res := UUID()
AssertEqual(t, 36, len(res))
AssertEqual(t, true, res != "00000000-0000-0000-0000-000000000000")
}
func Test_Utils_UUID_Concurrency(t *testing.T) {
func Test_UUID_Concurrency(t *testing.T) {
t.Parallel()
iterations := 10000
var res string

View File

@ -28,13 +28,13 @@ func UnsafeBytes(s string) (bs []byte) {
return
}
// SafeString copies a string to make it immutable
func SafeString(s string) string {
// CopyString copies a string to make it immutable
func CopyString(s string) string {
return string(UnsafeBytes(s))
}
// SafeBytes copies a slice to make it immutable
func SafeBytes(b []byte) []byte {
// CopyBytes copies a slice to make it immutable
func CopyBytes(b []byte) []byte {
tmp := make([]byte, len(b))
copy(tmp, b)
return tmp
@ -83,22 +83,3 @@ func ByteSize(bytes uint64) string {
result = strings.TrimSuffix(result, ".0")
return result + unit
}
// Deprecated fn's
// #nosec G103
// GetString returns a string pointer without allocation
func GetString(b []byte) string {
return UnsafeString(b)
}
// #nosec G103
// GetBytes returns a byte pointer without allocation
func GetBytes(s string) []byte {
return UnsafeBytes(s)
}
// ImmutableString copies a string to make it immutable
func ImmutableString(s string) string {
return SafeString(s)
}

View File

@ -6,7 +6,7 @@ package utils
import "testing"
func Test_Utils_GetString(t *testing.T) {
func Test_GetString(t *testing.T) {
t.Parallel()
res := GetString([]byte("Hello, World!"))
AssertEqual(t, "Hello, World!", res)
@ -31,7 +31,7 @@ func Benchmark_GetString(b *testing.B) {
})
}
func Test_Utils_GetBytes(t *testing.T) {
func Test_GetBytes(t *testing.T) {
t.Parallel()
res := GetBytes("Hello, World!")
AssertEqual(t, []byte("Hello, World!"), res)
@ -56,7 +56,7 @@ func Benchmark_GetBytes(b *testing.B) {
})
}
func Test_Utils_ImmutableString(t *testing.T) {
func Test_ImmutableString(t *testing.T) {
t.Parallel()
res := ImmutableString("Hello, World!")
AssertEqual(t, "Hello, World!", res)

33
utils/deprecated.go Normal file
View File

@ -0,0 +1,33 @@
package utils
// #nosec G103
// DEPRECATED, Please use UnsafeString instead
func GetString(b []byte) string {
return UnsafeString(b)
}
// #nosec G103
// DEPRECATED, Please use UnsafeBytes instead
func GetBytes(s string) []byte {
return UnsafeBytes(s)
}
// DEPRECATED, Please use CopyString instead
func ImmutableString(s string) string {
return CopyString(s)
}
// DEPRECATED, please use EqualFoldBytes
func EqualsFold(b, s []byte) (equals bool) {
return EqualFoldBytes(b, s)
}
// DEPRECATED, Please use CopyString instead
func SafeString(s string) string {
return CopyString(s)
}
// DEPRECATED, Please use CopyBytes instead
func SafeBytes(b []byte) []byte {
return CopyBytes(b)
}

View File

@ -10,7 +10,7 @@ import (
"testing"
)
func Test_Utils_GetMIME(t *testing.T) {
func Test_GetMIME(t *testing.T) {
t.Parallel()
res := GetMIME(".json")
AssertEqual(t, "application/json", res)
@ -53,7 +53,7 @@ func Benchmark_GetMIME(b *testing.B) {
})
}
func Test_Utils_StatusMessage(t *testing.T) {
func Test_StatusMessage(t *testing.T) {
t.Parallel()
res := StatusMessage(204)
AssertEqual(t, "No Content", res)

View File

@ -9,7 +9,7 @@ import (
"testing"
)
func Test_Utils_ToUpper(t *testing.T) {
func Test_ToUpper(t *testing.T) {
t.Parallel()
res := ToUpper("/my/name/is/:param/*")
AssertEqual(t, "/MY/NAME/IS/:PARAM/*", res)
@ -33,7 +33,7 @@ func Benchmark_ToUpper(b *testing.B) {
})
}
func Test_Utils_ToLower(t *testing.T) {
func Test_ToLower(t *testing.T) {
t.Parallel()
res := ToLower("/MY/NAME/IS/:PARAM/*")
AssertEqual(t, "/my/name/is/:param/*", res)
@ -64,7 +64,7 @@ func Benchmark_ToLower(b *testing.B) {
})
}
func Test_Utils_TrimRight(t *testing.T) {
func Test_TrimRight(t *testing.T) {
t.Parallel()
res := TrimRight("/test//////", '/')
AssertEqual(t, "/test", res)
@ -89,7 +89,7 @@ func Benchmark_TrimRight(b *testing.B) {
})
}
func Test_Utils_TrimLeft(t *testing.T) {
func Test_TrimLeft(t *testing.T) {
t.Parallel()
res := TrimLeft("////test/", '/')
AssertEqual(t, "test/", res)
@ -113,7 +113,7 @@ func Benchmark_TrimLeft(b *testing.B) {
AssertEqual(b, "foobar", res)
})
}
func Test_Utils_Trim(t *testing.T) {
func Test_Trim(t *testing.T) {
t.Parallel()
res := Trim(" test ", ' ')
AssertEqual(t, "test", res)