Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | package format // import "gotest.tools/internal/format" |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // Message accepts a msgAndArgs varargs and formats it using fmt.Sprintf |
| 6 | func 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 |
| 18 | func 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 | } |