Debian Codegraph @

helpers/tdutil/name.go

raw
1 // Copyright (c) 2019, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package tdutil
8
9 import (
10 "fmt"
11 "io"
12 "strings"
13 )
14
15 // BuildTestName builds a string from given args.
16 //
17 // If optional first args is a string containing at least one %, args
18 // are passed as is to [fmt.Fprintf], else they are passed to [fmt.Fprint].
19 func BuildTestName(args ...any) string {
20 if len(args) == 0 {
21 return ""
22 }
23
24 var b strings.Builder
25 FbuildTestName(&b, args...)
26 return b.String()
27 }
28
29 // FbuildTestName builds a string from given args.
30 //
31 // If optional first args is a string containing at least one %, args
32 // are passed as is to [fmt.Fprintf], else they are passed to [fmt.Fprint].
33 func FbuildTestName(w io.Writer, args ...any) {
34 if len(args) == 0 {
35 return
36 }
37
38 str, ok := args[0].(string)
39 if ok && len(args) > 1 {
40 if pos := strings.IndexRune(str, '%'); pos >= 0 && pos < len(str)-1 {
41 fmt.Fprintf(w, str, args[1:]...) //nolint: errcheck
42 return
43 }
44 }
45
46 // create a new slice to fool govet and avoid "call has possible
47 // formatting directive" errors
48 fmt.Fprint(w, args[:]...) //nolint: errcheck,gocritic
49 }