-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check the received value in producers (synch/asych) mocks agains a regexp #687
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ package mocks | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/Shopify/sarama" | ||
) | ||
|
@@ -25,6 +27,24 @@ type ErrorReporter interface { | |
Errorf(string, ...interface{}) | ||
} | ||
|
||
// ValueChecker is a function type to be set in each expectation of the producer mocks | ||
// to check the value passed. | ||
type ValueChecker func(val []byte) error | ||
|
||
// This function is used inside the mocks unit tests to generate ValueCheckers | ||
func generateRegexpChecker(re string) func([]byte) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not exported, so if you want to actually use it in your app you won't be able to... I think it might just be easier to implement this directly in your app instead of here anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put this there on purpuse just for the unit tests, thanks. |
||
return func(val []byte) error { | ||
matched, err := regexp.MatchString(re, string(val)) | ||
if err != nil { | ||
return errors.New("Error while trying to match the input message with the expected pattern: " + err.Error()) | ||
} | ||
if !matched { | ||
return fmt.Errorf("No match between input value \"%s\" and expected pattern \"%s\"", val, re) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
var ( | ||
errProduceSuccess error = nil | ||
errOutOfExpectations = errors.New("No more expectations set on mock") | ||
|
@@ -34,7 +54,8 @@ var ( | |
const AnyOffset int64 = -1000 | ||
|
||
type producerExpectation struct { | ||
Result error | ||
Result error | ||
CheckFunction ValueChecker | ||
} | ||
|
||
type consumerExpectation struct { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could use a (brief) godoc comment