blob: 8f6494f99421d5565b910bacb2c6f16005b72de6 [file] [log] [blame]
Matteo Scandoloabf872d2020-12-14 08:22:06 -10001package format // import "gotest.tools/internal/format"
2
3import "fmt"
4
5// Message accepts a msgAndArgs varargs and formats it using fmt.Sprintf
6func Message(msgAndArgs ...interface{}) string {
7 switch len(msgAndArgs) {
8 case 0:
9 return ""
10 case 1:
11 return fmt.Sprintf("%v", msgAndArgs[0])
12 default:
13 return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
14 }
15}
16
17// WithCustomMessage accepts one or two messages and formats them appropriately
18func WithCustomMessage(source string, msgAndArgs ...interface{}) string {
19 custom := Message(msgAndArgs...)
20 switch {
21 case custom == "":
22 return source
23 case source == "":
24 return custom
25 }
26 return fmt.Sprintf("%s: %s", source, custom)
27}