Skip to the content.

gomock-extra-matcher

Go Doc CircleCI Go Report Card

Coverage Status GitHub license GitHub release (latest by date)


Why ?

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 :) .

How to use ?

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]+\]$`))

Matchers

IntRangeMatcher

Explanation

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.

Example

Here is an example of usage:

mock.EXPECT().DoSomething(extra.IntRangeMatcher(lowerBound, upperBound))

StringRegexpMatcher

Explanation

This matcher will allow to test that a string is validating a Regexp.

Example

Here is an example of usage:

mock.EXPECT().DoSomething(extra.StringRegexpMatcher(`^[a-z]+\[[0-9]+\]$`))

MapMatcher

Explanation

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.

Example

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")))

StructMatcher

Explanation

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.

Example

// 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"})))

OrMatcher

Explanation

This matcher will allow to test multiple matchers with a logical “OR” between them. This will stop at first match.

Example

mock.EXPECT().DoSomething(extra.OrMatcher(gomock.Eq(1), gomock.Eq(10), gomock.Eq(15)))

Thanks

Author

License

Apache 2.0 (See in LICENSE)