File tree 4 files changed +32
-14
lines changed 4 files changed +32
-14
lines changed Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ func (w *testLoggerWriterCloser) Reset() {
93
93
func PrintCurrentTest (t testing.TB , skip ... int ) func () {
94
94
t .Helper ()
95
95
start := time .Now ()
96
- actualSkip := util .DefArgZero (skip ) + 1
96
+ actualSkip := util .DefaultArg (skip ) + 1
97
97
_ , filename , line , _ := runtime .Caller (actualSkip )
98
98
99
99
if log .CanColorStdout {
Original file line number Diff line number Diff line change @@ -230,18 +230,23 @@ func IfZero[T comparable](v, def T) T {
230
230
return v
231
231
}
232
232
233
- // DefArg helps the "optional argument" in Golang: func(foo string, optionalArg ...int)
234
- // it returns the first non-zero value from the given optional argument,
235
- // or the default value if there is no optional argument.
236
- func DefArg [T any ](defArgs []T , def T ) (ret T ) {
237
- if len (defArgs ) == 1 {
238
- return defArgs [0 ]
233
+ // DefaultArg helps the "optional argument" in Golang:
234
+ //
235
+ // func foo(optionalArg ...int) { return DefaultArg(optionalArg) }
236
+ // calling `foo()` gets 0, calling `foo(100)` gets 100
237
+ // func bar(optionalArg ...int) { return DefaultArg(optionalArg, 42) }
238
+ // calling `bar()` gets 42, calling `bar(100)` gets 100
239
+ //
240
+ // Passing more than 1 item to `optionalArg` or `def` is undefined behavior.
241
+ // At the moment it only returns the first argument.
242
+ func DefaultArg [T any ](optionalArg []T , def ... T ) (ret T ) {
243
+ if len (optionalArg ) >= 1 {
244
+ return optionalArg [0 ]
239
245
}
240
- return def
241
- }
242
-
243
- func DefArgZero [T any ](defArgs []T ) (ret T ) {
244
- return DefArg (defArgs , ret )
246
+ if len (def ) >= 1 {
247
+ return def [0 ]
248
+ }
249
+ return ret
245
250
}
246
251
247
252
func ReserveLineBreakForTextarea (input string ) string {
Original file line number Diff line number Diff line change @@ -240,3 +240,16 @@ func TestReserveLineBreakForTextarea(t *testing.T) {
240
240
assert .Equal (t , ReserveLineBreakForTextarea ("test\r \n data" ), "test\n data" )
241
241
assert .Equal (t , ReserveLineBreakForTextarea ("test\r \n data\r \n " ), "test\n data\n " )
242
242
}
243
+
244
+ func TestDefaultArg (t * testing.T ) {
245
+ foo := func (other any , optionalArg ... int ) int {
246
+ return DefaultArg (optionalArg )
247
+ }
248
+ bar := func (other any , optionalArg ... int ) int {
249
+ return DefaultArg (optionalArg , 42 )
250
+ }
251
+ assert .Equal (t , 0 , foo (nil ))
252
+ assert .Equal (t , 100 , foo (nil , 100 ))
253
+ assert .Equal (t , 42 , bar (nil ))
254
+ assert .Equal (t , 100 , bar (nil , 100 ))
255
+ }
Original file line number Diff line number Diff line change @@ -262,7 +262,7 @@ func PrepareCleanPackageData(t testing.TB) {
262
262
263
263
func PrepareTestEnv (t testing.TB , skip ... int ) func () {
264
264
t .Helper ()
265
- deferFn := PrintCurrentTest (t , util .DefArgZero (skip )+ 1 )
265
+ deferFn := PrintCurrentTest (t , util .DefaultArg (skip )+ 1 )
266
266
267
267
// load database fixtures
268
268
assert .NoError (t , unittest .LoadFixtures ())
@@ -276,7 +276,7 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
276
276
277
277
func PrintCurrentTest (t testing.TB , skip ... int ) func () {
278
278
t .Helper ()
279
- return testlogger .PrintCurrentTest (t , util .DefArgZero (skip )+ 1 )
279
+ return testlogger .PrintCurrentTest (t , util .DefaultArg (skip )+ 1 )
280
280
}
281
281
282
282
// Printf takes a format and args and prints the string to os.Stdout
You can’t perform that action at this time.
0 commit comments