gomockctx

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 6 Imported by: 0

README

gomockctx

Go package with gomock helpers for matching context.Context.

Go Reference GitHub tag (latest SemVer) Actions Status Coverage GitHub issues GitHub pull requests License Status

Import

import "github.com/romdo/gomockctx"

Usage

Match against a specific context or any of its child contexts:

// Create a context with a gomockctx ID value.
ctx := gomockctx.New(context.Background())

// Match against a context with a gomockctx ID.
someMock.EXPECT().
	Get(gomockctx.Eq(ctx), "foo").
	Return("bar", nil)

// Use context with gomockctx ID when calling function.
someMock.Get(ctx, "foo")

Match against a context containing a specific value:

someMock.EXPECT().
	Get(gomockctx.WithValue(myCtxKey, "hello"), "foo").
	Return("bar", nil)

Match against any context:

someMock.EXPECT().
	Get(gomockctx.Any(), "foo").
	Return("bar", nil)

Documentation

Please see the Go Reference.

License

MIT

Documentation

Overview

Package gomockctx contains gomock helpers for matching context.Context objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any() gomock.Matcher

Any returns a gomock.Matcher which matches any context.Context object.

func Eq

func Eq(ctx context.Context) gomock.Matcher

Eq accepts a context with a gomockctx ID value (as returned from New), and returns a gomock.Matcher which returns true for the given context, and any child contexts of it.

If ctx was not returned from New, the resulting matcher will ALWAYS return false.

func ID

func ID(ctx context.Context) string

ID returns the gomockctx ID value in the given context, or a empty string if the context does not have a gomockctx ID value.

func New

func New(parent context.Context) context.Context

New returns a context as a child of the given parent, which includes a randomized gomockctx ID value set, which makes it a gomockctx context. This can then be used with Eq to get a gomock Matcher which returns true for the context from New, or any child contexts of it.

If crypto/rand returns an error, this will panic trying to generate the gomockctx ID. In practice though, crypto/rand should never return a error.

func WithValue

func WithValue(key interface{}, value interface{}) gomock.Matcher

WithValue returns a gomock.Matcher which matches any context that has the specified key and value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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