-
Notifications
You must be signed in to change notification settings - Fork 3
/
messages.go
94 lines (79 loc) · 3.26 KB
/
messages.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Package assert utility for formating test failure messages
package assert
import (
"fmt"
"github.com/shopspring/decimal"
"time"
)
const (
failedEqualityFormat = "\nEquality assertion failed:\n\t want: %s \n\t got: %s"
expectationsOnNil = "\nEquality assertion failed:\n\t reason: expectations on nil is not permitted"
failedInclusionFormat = "\nInclusion assertion failed:\n\t substring: %s \n\t to be present in: %s"
failedInclusionNoErrorFormat = "\nInclusion assertion failed:\n\t substring: %s \n\t to be present, but no error was received"
expectationsOnEmptyPhrase = "\nInclusion assertion failed:\n\t reason: expectations on empty phrase is not permitted"
)
type failedNoErrorCompMsg struct{ got error }
type failedPanicMsg struct{ want, got string }
type failedBoolCompMsg struct{ want, got bool }
type failedErrorCompMsg struct{ want, got error }
type failedIntCompMsg struct{ want, got int }
type failedFloatCompMsg struct{ want, got float64 }
type failedStrCompMsg struct{ want, got string }
type failedTimeCompMsg struct{ want, got time.Time }
type failedDecimalCompMsg struct{ want, got decimal.Decimal }
type failedStrIncludeMsg struct{ expectedPhrase, got string }
type failedErrorIncludeMsg struct {
expectedPhrase string
got error
}
type failedIsNilCompMsg struct{ got interface{} }
func (msg *failedPanicMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatPanic(msg.want), formatPanic(msg.got))
}
func (msg *failedBoolCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatBool(msg.want), formatBool(msg.got))
}
func (msg *failedNoErrorCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatErr(nil), formatErr(msg.got))
}
// Equality messages
func (msg *failedErrorCompMsg) String() string {
if msg.want == nil {
return expectationsOnNil
}
return fmt.Sprintf(failedEqualityFormat, formatErr(msg.want), formatErr(msg.got))
}
func (msg *failedIntCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatInt(msg.want), formatInt(msg.got))
}
func (msg *failedFloatCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatFloat(msg.want), formatFloat(msg.got))
}
func (msg *failedStrCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatStr(msg.want), formatStr(msg.got))
}
func (msg *failedTimeCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatTime(msg.want), formatTime(msg.got))
}
func (msg *failedIsNilCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, "Nil", formatInterface(msg.got))
}
func (msg *failedDecimalCompMsg) String() string {
return fmt.Sprintf(failedEqualityFormat, formatDecimal(msg.want), formatDecimal(msg.got))
}
// Inclusion
func (msg *failedStrIncludeMsg) String() string {
if msg.expectedPhrase == "" {
return expectationsOnEmptyPhrase
}
return fmt.Sprintf(failedInclusionFormat, formatStr(msg.expectedPhrase), formatStr(msg.got))
}
func (msg *failedErrorIncludeMsg) String() string {
if msg.expectedPhrase == "" {
return expectationsOnEmptyPhrase
}
if msg.got == nil {
return fmt.Sprintf(failedInclusionNoErrorFormat, formatStr(msg.expectedPhrase))
}
return fmt.Sprintf(failedInclusionFormat, formatStr(msg.expectedPhrase), formatErr(msg.got))
}