extra

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2023 License: Apache-2.0 Imports: 4 Imported by: 6

README

gomock-extra-matcher

Go Doc CircleCI Go Report Card

Coverage Status GitHub license GitHub release (latest by date)


Menu

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

  • My wife BH to support me doing this

Author

  • Oxyno-zeta (Havrileck Alexandre)

License

Apache 2.0 (See in LICENSE)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntRangeMatcher

func IntRangeMatcher(lowerBound, upperBound int) gomock.Matcher

IntRangeMatcher will return a new Int range matcher.

func OrMatcher added in v1.1.0

func OrMatcher(matchers ...gomock.Matcher) gomock.Matcher

OrMatcher will return a new Or matcher.

func StringRegexpMatcher

func StringRegexpMatcher(regexSt string) gomock.Matcher

Will return a new string regexp matcher.

Types

type MMatcher

type MMatcher interface {
	gomock.Matcher
	// Add a matcher for a specific key
	Key(key interface{}, match interface{}) MMatcher
}

MMatcher interface improved to add functions.

func MapMatcher

func MapMatcher() MMatcher

MapMatcher will return a new map matcher.

type StMatcher

type StMatcher interface {
	gomock.Matcher
	// Add a matcher for a specific field
	Field(fName string, match interface{}) StMatcher
}

StMatcher interface improved to add functions.

func StructMatcher

func StructMatcher() StMatcher

StructMatcher will return a new struct matcher.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL