/
msg.go
65 lines (54 loc) · 1.65 KB
/
msg.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package should
import (
"strings"
)
// tools for working with failure messages
const (
// ShortSeparator ends the failure message short portion (and begins the long
// portion)
ShortSeparator = "\n"
// ShortLength is an arbitrary length used to shorten failure messages that
// don't contain ShortSeparator
ShortLength = 80
// LongSeparator ends the failure message explanation section (and begins the
// details)
LongSeparator = "\n# DETAILS:"
// DetailsSeparator ends the failure debugging portion (and begins details for
// debugging asserts and test runner.)
DetailsSeparator = "\n# INTERNALS:"
// SectionSeparator separates the long, details, and internals sections.
SectionSeparator = "\n~~~~~~~~~~\n"
)
func trim(s string) string {
return strings.Trim(s, " \n\t\r")
}
func splitShortLong(s string) (short, long string) {
sl := strings.SplitN(trim(s), ShortSeparator, 2)
if len(sl) > 1 {
return trim(sl[0]), trim(sl[1])
}
if len(s) > ShortLength {
return trim(s[:ShortLength]), trim(s[ShortLength:])
}
return trim(s), ""
}
// ParseFailure divides a failure message into parts that may be muted depending on verbosity levels
func ParseFailure(msg string) (short, long, details, meta string) {
if msg == "" {
return
}
secs := strings.Split(msg, SectionSeparator)
short, long = splitShortLong(secs[0])
if len(secs) > 1 {
details = trim(secs[1])
}
if len(secs) > 2 {
meta = trim(secs[2])
}
return
}
// FormatFailure creates a failure message from its components
func FormatFailure(short, long, details, meta string) (result string) {
result = short + ShortSeparator + long + SectionSeparator + details + SectionSeparator + meta
return
}