-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestify_assert.go
More file actions
145 lines (110 loc) · 3.49 KB
/
testify_assert.go
File metadata and controls
145 lines (110 loc) · 3.49 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/joaopsramos/gotestpp/utils"
)
type TestifyAssert struct {
Error []string
Trace []string
Test string
Message []string
}
func IsTestifyAssert(line string) bool {
trimmed := strings.TrimSpace(line)
return strings.HasPrefix(trimmed, "Error Trace:")
}
func NewTestifyAssert(firstLine string, scanner *RewindScanner) TestifyAssert {
t := TestifyAssert{Trace: []string{strings.TrimSpace(firstLine)}}
isErrorTrace := true
isError := false
isMessage := false
firstLineIndent := utils.CountSpacesAndTabs(firstLine)
Loop:
for scanner.Scan() {
originalLine := scanner.Text()
line := strings.TrimSpace(originalLine)
indent := utils.CountSpacesAndTabs(originalLine)
sameIndent := indent == firstLineIndent
switch {
case sameIndent && strings.HasPrefix(line, "Error:"):
isErrorTrace = false
isError = true
t.Error = append(t.Error, originalLine)
case sameIndent && strings.HasPrefix(line, "Messages:"):
isError = false
isMessage = true
t.Message = append(t.Message, originalLine)
case isErrorTrace:
t.Trace = append(t.Trace, line)
case strings.HasPrefix(line, "Test:"):
isError = false
t.Test = line
case isError:
t.Error = append(t.Error, originalLine)
case isMessage && indent <= utils.CountSpacesAndTabs(firstLine):
// Message has ended, but we already read the next line, so we need to rewind the scanner.
scanner.Rewind()
break Loop
case isMessage:
t.Message = append(t.Message, originalLine)
case t.Test != "" && !isMessage:
// The testify output has ended with no message, but we already read the next line,
// so we need to rewind the scanner.
scanner.Rewind()
break Loop
}
}
return t
}
func (t TestifyAssert) String() string {
output := t.formatError()
output += t.formatMessages()
output += "\n" + t.formatTrace()
return output
}
func (t TestifyAssert) formatError() string {
output := make([]string, 0, len(t.Error))
// Replace to get the correct indentation count
firstLine := strings.Replace(t.Error[0], "Error:", " ", 1)
baseIndent := utils.CountSpacesAndTabs(firstLine)
output = append(output, color.RedString(strings.TrimSpace(firstLine)))
for _, line := range t.Error[1:] {
trimmed := strings.TrimSpace(line)
if trimmed == "" {
output = append(output, "")
continue
}
indent := utils.CountSpacesAndTabs(line)
sameIndent := indent == baseIndent
line = utils.StripExtraSpacesAndTabs(line)
if indent == baseIndent+1 {
// We removed a space that was part of the Diff context (without -/+ signs)
line = " " + line
}
if sameIndent && strings.HasPrefix(trimmed, "-") {
line = color.RedString(line)
} else if sameIndent && strings.HasPrefix(trimmed, "+") {
line = color.GreenString(line)
}
output = append(output, line)
}
return fmt.Sprintf("\t%s\n\t\t%s", "Error:", strings.Join(output, "\n\t\t"))
}
func (t TestifyAssert) formatMessages() string {
if len(t.Message) == 0 {
return ""
}
output := make([]string, 0, len(t.Message))
t.Message[0] = strings.Replace(t.Message[0], "Messages:", " ", 1)
for _, line := range t.Message {
line = utils.StripExtraSpacesAndTabs(line)
output = append(output, line)
}
return fmt.Sprintf("\n\tMessages:\n\t\t%s", strings.Join(output, "\n\t\t"))
}
func (t TestifyAssert) formatTrace() string {
t.Trace[0] = strings.Replace(t.Trace[0], "Error Trace:", "", 1)
return fmt.Sprintf("\t%s\n\t%s", "Error Trace:", strings.Join(t.Trace, "\n\t\t"))
}