mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor tests to prevent from unnecessary preparations (#32398)
This commit is contained in:
@@ -230,6 +230,25 @@ func IfZero[T comparable](v, def T) T {
|
||||
return v
|
||||
}
|
||||
|
||||
// OptionalArg helps the "optional argument" in Golang:
|
||||
//
|
||||
// func foo(optArg ...int) { return OptionalArg(optArg) }
|
||||
// calling `foo()` gets zero value 0, calling `foo(100)` gets 100
|
||||
// func bar(optArg ...int) { return OptionalArg(optArg, 42) }
|
||||
// calling `bar()` gets default value 42, calling `bar(100)` gets 100
|
||||
//
|
||||
// Passing more than 1 item to `optArg` or `defaultValue` is undefined behavior.
|
||||
// At the moment only the first item is used.
|
||||
func OptionalArg[T any](optArg []T, defaultValue ...T) (ret T) {
|
||||
if len(optArg) >= 1 {
|
||||
return optArg[0]
|
||||
}
|
||||
if len(defaultValue) >= 1 {
|
||||
return defaultValue[0]
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func ReserveLineBreakForTextarea(input string) string {
|
||||
// Since the content is from a form which is a textarea, the line endings are \r\n.
|
||||
// It's a standard behavior of HTML.
|
||||
|
@@ -240,3 +240,16 @@ func TestReserveLineBreakForTextarea(t *testing.T) {
|
||||
assert.Equal(t, ReserveLineBreakForTextarea("test\r\ndata"), "test\ndata")
|
||||
assert.Equal(t, ReserveLineBreakForTextarea("test\r\ndata\r\n"), "test\ndata\n")
|
||||
}
|
||||
|
||||
func TestOptionalArg(t *testing.T) {
|
||||
foo := func(other any, optArg ...int) int {
|
||||
return OptionalArg(optArg)
|
||||
}
|
||||
bar := func(other any, optArg ...int) int {
|
||||
return OptionalArg(optArg, 42)
|
||||
}
|
||||
assert.Equal(t, 0, foo(nil))
|
||||
assert.Equal(t, 100, foo(nil, 100))
|
||||
assert.Equal(t, 42, bar(nil))
|
||||
assert.Equal(t, 100, bar(nil, 100))
|
||||
}
|
||||
|
Reference in New Issue
Block a user