...

Source file src/github.com/onsi/gomega/gbytes/say_matcher.go

Documentation: github.com/onsi/gomega/gbytes

     1  // untested sections: 1
     2  
     3  package gbytes
     4  
     5  import (
     6  	"fmt"
     7  	"regexp"
     8  
     9  	"github.com/onsi/gomega/format"
    10  )
    11  
    12  //Objects satisfying the BufferProvider can be used with the Say matcher.
    13  type BufferProvider interface {
    14  	Buffer() *Buffer
    15  }
    16  
    17  /*
    18  Say is a Gomega matcher that operates on gbytes.Buffers:
    19  
    20  	Expect(buffer).Should(Say("something"))
    21  
    22  will succeed if the unread portion of the buffer matches the regular expression "something".
    23  
    24  When Say succeeds, it fast forwards the gbytes.Buffer's read cursor to just after the successful match.
    25  Thus, subsequent calls to Say will only match against the unread portion of the buffer
    26  
    27  Say pairs very well with Eventually.  To assert that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
    28  
    29  	Eventually(buffer, 3).Should(Say("[123]-star"))
    30  
    31  Ditto with consistently.  To assert that a buffer does not receive data matching "never-see-this" for 1 second you can:
    32  
    33  	Consistently(buffer, 1).ShouldNot(Say("never-see-this"))
    34  
    35  In addition to bytes.Buffers, Say can operate on objects that implement the gbytes.BufferProvider interface.
    36  In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
    37  
    38  If the buffer is closed, the Say matcher will tell Eventually to abort.
    39  */
    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