say_matcher.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package gbytes
  2. import (
  3. "fmt"
  4. "regexp"
  5. "github.com/onsi/gomega/format"
  6. )
  7. //Objects satisfying the BufferProvider can be used with the Say matcher.
  8. type BufferProvider interface {
  9. Buffer() *Buffer
  10. }
  11. /*
  12. Say is a Gomega matcher that operates on gbytes.Buffers:
  13. Expect(buffer).Should(Say("something"))
  14. will succeed if the unread portion of the buffer matches the regular expression "something".
  15. When Say succeeds, it fast forwards the gbytes.Buffer's read cursor to just after the succesful match.
  16. Thus, subsequent calls to Say will only match against the unread portion of the buffer
  17. Say pairs very well with Eventually. To assert that a buffer eventually receives data matching "[123]-star" within 3 seconds you can:
  18. Eventually(buffer, 3).Should(Say("[123]-star"))
  19. Ditto with consistently. To assert that a buffer does not receive data matching "never-see-this" for 1 second you can:
  20. Consistently(buffer, 1).ShouldNot(Say("never-see-this"))
  21. In addition to bytes.Buffers, Say can operate on objects that implement the gbytes.BufferProvider interface.
  22. In such cases, Say simply operates on the *gbytes.Buffer returned by Buffer()
  23. If the buffer is closed, the Say matcher will tell Eventually to abort.
  24. */
  25. func Say(expected string, args ...interface{}) *sayMatcher {
  26. if len(args) > 0 {
  27. expected = fmt.Sprintf(expected, args...)
  28. }
  29. return &sayMatcher{
  30. re: regexp.MustCompile(expected),
  31. }
  32. }
  33. type sayMatcher struct {
  34. re *regexp.Regexp
  35. receivedSayings []byte
  36. }
  37. func (m *sayMatcher) buffer(actual interface{}) (*Buffer, bool) {
  38. var buffer *Buffer
  39. switch x := actual.(type) {
  40. case *Buffer:
  41. buffer = x
  42. case BufferProvider:
  43. buffer = x.Buffer()
  44. default:
  45. return nil, false
  46. }
  47. return buffer, true
  48. }
  49. func (m *sayMatcher) Match(actual interface{}) (success bool, err error) {
  50. buffer, ok := m.buffer(actual)
  51. if !ok {
  52. return false, fmt.Errorf("Say must be passed a *gbytes.Buffer or BufferProvider. Got:\n%s", format.Object(actual, 1))
  53. }
  54. didSay, sayings := buffer.didSay(m.re)
  55. m.receivedSayings = sayings
  56. return didSay, nil
  57. }
  58. func (m *sayMatcher) FailureMessage(actual interface{}) (message string) {
  59. return fmt.Sprintf(
  60. "Got stuck at:\n%s\nWaiting for:\n%s",
  61. format.IndentString(string(m.receivedSayings), 1),
  62. format.IndentString(m.re.String(), 1),
  63. )
  64. }
  65. func (m *sayMatcher) NegatedFailureMessage(actual interface{}) (message string) {
  66. return fmt.Sprintf(
  67. "Saw:\n%s\nWhich matches the unexpected:\n%s",
  68. format.IndentString(string(m.receivedSayings), 1),
  69. format.IndentString(m.re.String(), 1),
  70. )
  71. }
  72. func (m *sayMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
  73. switch x := actual.(type) {
  74. case *Buffer:
  75. return !x.Closed()
  76. case BufferProvider:
  77. return !x.Buffer().Closed()
  78. default:
  79. return true
  80. }
  81. }