I've created this library because I cannot find such a library like this one and I don't want to create specific matcher for each structure, each map, ... in all tests that I have and can write.
Moreover, creating specific matcher need maintenance code and this is time consumption.
I'm sure that I'm not the only one that what to avoid all of this :) .
Import the library in your tests files:
import "github.com/oxyno-zeta/gomock-extra-matcher"
Use it in your gomock instance:
mock.EXPECT().DoSomething(extra.StringRegexpMatcher(`^[a-z]+\[[0-9]+\]$`))
This matcher will allow to test that an int is inside a range. Here it is considered that input can be equal to the lower bound and the same for the upper bound.
Here is an example of usage:
mock.EXPECT().DoSomething(extra.IntRangeMatcher(lowerBound, upperBound))
This matcher will allow to test that a string is validating a Regexp.
Here is an example of usage:
mock.EXPECT().DoSomething(extra.StringRegexpMatcher(`^[a-z]+\[[0-9]+\]$`))
This matcher will allow to test map key and map value.
To this one, it is possible to give a gomock matcher to a key and also to the value.
The Key
function is chainable. It is also possible to more than one test per key.
Here are some example of usage:
// Here we consider a map[string]string as input
mock.EXPECT().DoSomething(extra.MapMatcher().Key("key1", "value1").Key(gomock.Any(), "value1").Key("key1", gomock.Not("value2")))
This matcher will allow to test public fields of a structure (Only public ones. Reflect can't manage private fields...).
To this matcher, it is possible to give either a gomock matcher or either a real value for validation.
The Field
function is chainable. It is also possible to more than one test per field.
// Here we consider a struct as this one
/*
type Fake struct {
Name string
Data map[string]string
}
*/
mock.EXPECT().DoSomething(extra.StructMatcher().Field("Name", "value1").Field("Data", gomock.Eq(map[string]string{"fake":"value"})))
This matcher will allow to test multiple matchers with a logical "OR" between them. This will stop at first match.
mock.EXPECT().DoSomething(extra.OrMatcher(gomock.Eq(1), gomock.Eq(10), gomock.Eq(15)))
- My wife BH to support me doing this
- Oxyno-zeta (Havrileck Alexandre)
Apache 2.0 (See in LICENSE)