-
Notifications
You must be signed in to change notification settings - Fork 3
/
assert_wrapper.go
85 lines (73 loc) · 2.55 KB
/
assert_wrapper.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
package mspec
import (
"fmt"
asserts "github.com/eduncan911/go-mspec/assert"
"strings"
)
/*
This is the glue used to bind Testify's assertions to the
gomspec package as the default asserting package.
mspecTestingT implements the asserts.TestingT interface
with Errorf(...) func.
It is used to print the specification portion to the output
when an error occurs. Also, it sets a flag that is used by
the bdd framework to know that an error has been printed and
therefore do not print a normal specification title.
Errors are only handled this way under one condition: that
is that Errorf() be executed by the Assertion package. Else,
we do not get the flag to know.
The current Testify assert package fires Errorf() on every
Fail(), which all asserts will fire when an error occurs. So,
we just wrap that below.
*/
type mspectTestingT struct {
spec *Specification
}
// Efforf is called by the Testify's assertions to signal a fail condition.
// This specific method sets an internal mspec flag so that the framework
// is aware that an error occurred.
func (m *mspectTestingT) Errorf(format string, args ...interface{}) {
// because we control the output of specification, we
// need to store these details in a state for later use in
// the bdd framework. to do that, we use the
// m.spec.AssertionFailed boolean.
m.spec.AssertionFailed = true
// parse out Testify's location info by removing the first
// line and reformat their Error message to our liking
// using string foo
err := fmt.Sprintf(format, args...)
err = strings.Replace(err, "\r", "", -1)
err = strings.Replace(err, " ", "\t\t\t", -1) // some errors are two-liners
lines := strings.Split(err, "\n")
out := ""
for i := range lines {
if strings.Contains(lines[i], "Location:") {
continue
}
if lines[i] == "" {
continue
}
if out == "" {
out = lines[i]
} else {
out = strings.Join([]string{out, "\n", lines[i]}, "")
}
}
m.spec.PrintSpecWithError()
// to propertly set the caller used, we currently need to call
// m.spec.PrinterError here to capture the proper line number.
// and since PrintError() comes after PrintTitleWithError(),
// we have the line above.
//
// TODO refactor to pass the caller information down along with
// the custom error message parsing. that way we can control the
// printing internally and seal up these Print*() messages.
m.spec.PrintError(out)
}
// newAssertions constructs a wrapper around Testify's asserts.
func newAssertions(s *Specification) *asserts.Assertions {
return asserts.New(
&mspectTestingT{
spec: s,
})
}