mock

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package mock provides a mock slog.Handler implementation and other helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Key   string
	Value any
}

Attr is a mockable representation of slog.Attr.

func AttrsUsingFunc

func AttrsUsingFunc(n int, fn func(func(slog.Attr))) []Attr

AttrsUsingFunc returns a new slice of Attr based on the given number and function to get next slog.Attr.

func NewAttr

func NewAttr(key string, value any) Attr

NewAttr returns a new Attr based on the given key and value.

func NewAttrs

func NewAttrs(attrs []slog.Attr) []Attr

NewAttrs returns a new slice of Attr based on the given slice of slog.Attr.

type CallList

type CallList []any

CallList is a list of recorded calls.

func (CallList) WithoutTime

func (l CallList) WithoutTime() CallList

WithoutTime returns a new CallList with all time fields set to zero.

type CallLog

type CallLog struct {
	// contains filtered or unexported fields
}

CallLog is a log of method of function calls that can be used to build assertions.

func NewCallLog

func NewCallLog() *CallLog

NewCallLog returns a new CallLog.

func (*CallLog) Calls

func (l *CallLog) Calls() CallList

Calls returns all recorded calls in the log.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is a mockable representation of slog.Handler.

func NewHandler

func NewHandler(log *CallLog) *Handler

NewHandler returns a new Handler with the given CallLog.

func (Handler) Enabled

func (h Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled records the call and returns true.

func (Handler) Handle

func (h Handler) Handle(_ context.Context, record slog.Record) error

Handle records the call.

func (Handler) WithAttrs

func (h Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs records the call and returns a new Handler with the given attributes.

func (Handler) WithGroup

func (h Handler) WithGroup(group string) slog.Handler

WithGroup records the call and returns a new Handler with the given group.

type HandlerEnabled

type HandlerEnabled struct {
	Instance string
	Level    slog.Level
}

HandlerEnabled is a representation of a call to slog.Handler.Enabled that can be used to compare against recorded calls in a CallLog.

type HandlerHandle

type HandlerHandle struct {
	Instance string
	Record   Record
}

HandlerHandle is a representation of a call to slog.Handler.Handle that can be used to compare against recorded calls in a CallLog.

type HandlerWithAttrs

type HandlerWithAttrs struct {
	Instance string
	Attrs    []Attr
}

HandlerWithAttrs is a representation of a call to slog.Handler.WithAttrs that can be used to compare against recorded calls in a CallLog.

type HandlerWithGroup

type HandlerWithGroup struct {
	Instance string
	Key      string
}

HandlerWithGroup is a representation of a call to slog.Handler.WithGroup that can be used to compare against recorded calls in a CallLog.

type Record

type Record struct {
	Time    time.Time
	Message string
	Level   slog.Level
	PC      uintptr
	Attrs   []Attr
}

Record is a mockable representation of slog.Record.

func NewRecord

func NewRecord(record slog.Record) Record

NewRecord returns a new Record based on the given slog.Record.

func (Record) WithoutTime

func (r Record) WithoutTime() Record

WithoutTime returns a copy of Record with [Record.Time] set to zero.

Jump to

Keyboard shortcuts

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