format.go 695 B

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