frankban / quicktest

Quick helpers for testing Go applications
MIT License
529 stars 27 forks source link

Allow Matches and ErrorMatches matches to use an already-compiled regexp.Regexp #137

Closed pajlada closed 1 year ago

pajlada commented 2 years ago

This allows users to have full control over the regular expression used for matching.

My use-case was matching a multiline string pattern, and the way MatchString was used in the match-function made that infeasible.

With this change, I was able to use the Matches matcher like this:

c.Assert("asd\nasd", qt.Matches, regexp.MustCompile(`asd.*`))

Happy to update naming of variables on request. I left naming of parameters/arguments on purpose to keep the diff minimal, but happy to change that too.

As an alternative, if this change is not acceptable, I know I could implement my own Checker handles regular expressions like this, so no worries if this change does not fit into the project!

pajlada commented 2 years ago

@frankban I've gone ahead and pushed missing tests for this functionality to maintain the 100% test coverage of checker.go, let me know if you prefer this to be rebased or something before being approved/merged in :slightly_smiling_face:

pajlada commented 2 years ago

I added the following tests: qt.ErrorMatches single-line match expectedNegateFailure qt.ErrorMatches multi-line match expectedNegateFailure qt.ErrorMatches multi-line match expectedCheckFailure qt.PanicMatches single-line match expectedNegateFailure qt.PanicMatches multi-line match expectedNegateFailure qt.PanicMatches multi-line match expectedCheckFailure

I added the following documentation: Added example for Matches checker in checker.go and in README.md Added example for ErrorMatches checker in checker.go and in README.md Added example for PanicMatches checker in checker.go and in README.md

frankban commented 1 year ago

Released as v1.14.5