-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject.go
73 lines (66 loc) · 1.57 KB
/
subject.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
66
67
68
69
70
71
72
73
package unit
import (
"bytes"
"fmt"
"runtime"
"strings"
)
type Subject struct {
assert *Assertion
name string
}
func NewSubject(assert *Assertion) *Subject {
return &Subject{
assert: assert,
name: "",
}
}
// decorate prefixes the string with the file and line of the call site
// and inserts the final newline if needed and indentation tabs for formatting.
func decorate(s string) string {
_, file, line, ok := runtime.Caller(4) // decorate + log + public function.
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
line = 1
}
buf := new(bytes.Buffer)
// Every line is indented at least one tab.
buf.WriteString(" ")
fmt.Fprintf(buf, "%s:%d: ", file, line)
lines := strings.Split(s, "\n")
if l := len(lines); l > 1 && lines[l-1] == "" {
lines = lines[:l-1]
}
for i, line := range lines {
if i > 0 {
// Second and subsequent lines are indented an extra tab.
buf.WriteString("\n\t\t")
}
buf.WriteString(line)
}
buf.WriteByte('\n')
return buf.String()
}
func (subject *Subject) FailWithMessage(message string) {
fmt.Println(decorate(message))
subject.assert.t.Fail()
}
func (subject *Subject) Named(name string) {
subject.name = name
}
func (subject *Subject) DisplayString(value string) string {
if len(value) == 0 {
value = "unknown"
}
if len(subject.name) == 0 {
return "<" + value + ">"
}
return subject.name + "(<" + value + ">)"
}