File: name.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,238 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2019, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package tdutil

import (
	"fmt"
	"io"
	"strings"
)

// BuildTestName builds a string from given args.
//
// If optional first args is a string containing at least one %, args
// are passed as is to [fmt.Fprintf], else they are passed to [fmt.Fprint].
func BuildTestName(args ...any) string {
	if len(args) == 0 {
		return ""
	}

	var b strings.Builder
	FbuildTestName(&b, args...)
	return b.String()
}

// FbuildTestName builds a string from given args.
//
// If optional first args is a string containing at least one %, args
// are passed as is to [fmt.Fprintf], else they are passed to [fmt.Fprint].
func FbuildTestName(w io.Writer, args ...any) {
	if len(args) == 0 {
		return
	}

	str, ok := args[0].(string)
	if ok && len(args) > 1 {
		if pos := strings.IndexRune(str, '%'); pos >= 0 && pos < len(str)-1 {
			fmt.Fprintf(w, str, args[1:]...) //nolint: errcheck
			return
		}
	}

	// create a new slice to fool govet and avoid "call has possible
	// formatting directive" errors
	fmt.Fprint(w, args[:]...) //nolint: errcheck,gocritic
}