...

Package fctx

import "edge-infra.dev/test/f2/fctx"
Overview
Index

Overview ▾

Package fctx provides utilities for working with f2 test contexts.

This package is implemented based on the idea that nil references and panic() should not happen in test code being exercised by the framework. It includes helpers for acessing context values in type-safe ways without extra boilerplate per access.

Variables

ErrNotFound occurs when the expected value does not exist in the context.

var ErrNotFound = errors.New("not found in context")

func ValueFrom

func ValueFrom[V any](ctx context.Context) *V

ValueFrom allows retrieving a type-safe value that was stored using ValueInto[T] Only one instance of each type can be stored. Typically used for storing framework extensions in context, since there should only be one of those per instance of the framework.

func ValueFromT

func ValueFromT[V any](ctx context.Context, t *testing.T) *V

ValueFromT is a testing variant of ValueFrom that will automatically fail the test if the value being retrieved doesn't exist in ctx.

type Context

Context is the f2.Framework test context. It carries test configuration and state throughout the various stages of test execution. Individual tests and framework functions can pass state in a loosely coupled way by using WithValue, like a standard context.Context.

type Context struct {
    RunID string

    context.Context
}

func ValueInto

func ValueInto[V any](parent Context, v *V) Context

ValueInto is a type-safe helper for storing a value in a context by its type. Only one instance of each type can be stored. Typically used for storing framework extensions in context, since there should only be one of those per instance of the framework.

func WithCancel

func WithCancel(c Context) (Context, context.CancelFunc)

WithCancel is equivalent to context.WithCancel while preserving the generic Context implementation. It should be used when mutating f2.Context.

func WithDeadline

func WithDeadline(parent Context, d time.Time) (Context, context.CancelFunc)

WithDeadline is equivalent to context.WithDeadline while preserving the generic Context implementation. It should be used when mutating f2.Context.

func WithTimeout

func WithTimeout(parent Context, timeout time.Duration) (Context, context.CancelFunc)

WithTimeout is equivalent to context.WithTimeout while preserving the generic Context implementation. It should be used when mutating f2.Context.

func WithValue

func WithValue(parent Context, k, v any) Context

WithValue is equivalent to context.WithValue while preserving the generic Context implementation. It should be used when mutating f2.Context.

func (Context) InnerContext

func (c Context) InnerContext() context.Context

InnerContext returns the wrapped context.Context

func (Context) WithInnerContext

func (c Context) WithInnerContext(ctx context.Context) Context

WithInnerContext updates the wrapped context.Context