...
1
2
3 package gbytes
4
5 import (
6 "fmt"
7 "regexp"
8
9 "github.com/onsi/gomega/format"
10 )
11
12
13 type BufferProvider interface {
14 Buffer() *Buffer
15 }
16
17
40 func Say(expected string, args ...interface{}) *sayMatcher {
41 if len(args) > 0 {
42 expected = fmt.Sprintf(expected, args...)
43 }
44 return &sayMatcher{
45 re: regexp.MustCompile(expected),
46 }
47 }
48
49 type sayMatcher struct {
50 re *regexp.Regexp
51 receivedSayings []byte
52 }
53
54 func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
55 var buffer *Buffer
56
57 switch x := actual.(type) {
58 case *Buffer:
59 buffer = x
60 case BufferProvider:
61 buffer = x.Buffer()
62 default:
63 return nil, false
64 }
65
66 return buffer, true
67 }
68
69 func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
70 buffer, ok := m.buffer(actual)
71 if !ok {
72 return false, fmt.Errorf("Say must be passed a *gbytes.Buffer or BufferProvider. Got:\n%s", format.Object(actual, 1))
73 }
74
75 didSay, sayings := buffer.didSay(m.re)
76 m.receivedSayings = sayings
77
78 return didSay, nil
79 }
80
81 func (m *sayMatcher) FailureMessage(actual interface{}) (message string) {
82 return fmt.Sprintf(
83 "Got stuck at:\n%s\nWaiting for:\n%s",
84 format.IndentString(string(m.receivedSayings), 1),
85 format.IndentString(m.re.String(), 1),
86 )
87 }
88
89 func (m *sayMatcher) NegatedFailureMessage(actual interface{}) (message string) {
90 return fmt.Sprintf(
91 "Saw:\n%s\nWhich matches the unexpected:\n%s",
92 format.IndentString(string(m.receivedSayings), 1),
93 format.IndentString(m.re.String(), 1),
94 )
95 }
96
97 func (m *sayMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
98 switch x := actual.(type) {
99 case *Buffer:
100 return !x.Closed()
101 case BufferProvider:
102 return !x.Buffer().Closed()
103 default:
104 return true
105 }
106 }
107
View as plain text