assertlogging

package
v0.0.0-...-2b5ea0c Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Empty

func Empty() fieldEmpty

Empty asserts that a log field value is empty.

func Equal

func Equal(expected interface{}) fieldEqual

Equal asserts that a log field value is equal to the given expected value.

func EqualError

func EqualError(expected string) fieldEqualError

EqualError asserts that a log field value is an error with a message equal to the given expected value.

func EqualErrorf

func EqualErrorf(expected string, args ...interface{}) fieldEqualError

EqualErrorf asserts that a log field value is an error with a message equal to the given expected value with arguments substituted in.

func Equalf

func Equalf(expected string, args ...interface{}) fieldEqual

Equalf asserts that a log field value is equal to the given expected string value with arguments substituted in.

func False

func False() fieldFalse

False asserts that a log field value is false.

func IsType

func IsType(expected interface{}) fieldIsType

IsType asserts that a log field value matched the type of the given expected value.

func Nil

func Nil() fieldNil

Nil asserts that a log field value is nil.

func NotEqual

func NotEqual(expected interface{}) fieldNotEqual

NotEqual asserts that a log field value is not equal to the given expected value.

func Regexp

func Regexp(expected *regexp.Regexp) fieldRegexp

Regexp asserts that a log field value is a string that matches the given regular expression.

func True

func True() fieldTrue

True asserts that a log field value is true.

Types

type ExpectedLog

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

ExpectedLog describes a single log statement that is expected to be generated by tested code.

func (*ExpectedLog) String

func (l *ExpectedLog) String() string

func (*ExpectedLog) WithError

func (l *ExpectedLog) WithError(value ...FieldValue) *ExpectedLog

WithField adds an expectation upon a log field added using WithError. There must be at least one expectation on each log field.

func (*ExpectedLog) WithField

func (l *ExpectedLog) WithField(key string, value ...FieldValue) *ExpectedLog

WithField adds an expectation upon a single log field. There must be at least one expectation on each log field.

func (*ExpectedLog) WithFields

func (l *ExpectedLog) WithFields(fields ...*Field) *ExpectedLog

WithFields adds expectations upon multiple log fields. There must be at least one expectation on each log field.

type Field

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

Field represents a log field with a key and number of expectations upon the log field value.

func NewField

func NewField(key string, value ...FieldValue) *Field

NewField creates a new Field instance.

type FieldValue

type FieldValue interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

FieldValue is used to assert that a log field value matches a given expectation.

There is currently no "Any" implementation to allow any value to be present in a log field, partly to encourage good behavior when asserting against log fields, but also to help identify new Value implementations to cover additional use cases.

type Fields

type Fields map[string][]FieldValue

Fields contains multiple log field expectations.

type Logger

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

Logger is a in-memory mock implementation of the logging.Logger interface.

func NewLogger

func NewLogger(t testing.TB, opts ...Option) *Logger

NewLogger creates a new Logger instance.

Any registered expectations are automatically verified when the test completes along with identification of unfulfilled expectations.

func (*Logger) Debug

func (l *Logger) Debug(msg string)

Debug creates a log statement at DEBUG level.

func (*Logger) Error

func (l *Logger) Error(msg string)

Error creates a log statement at ERROR level.

func (*Logger) ExpectDebug

func (l *Logger) ExpectDebug(msg string) *ExpectedLog

ExpectDebug adds an expectation for a log message at DEBUG level. If you wish to set expectations on DEBUG logs, you must also use the AssertDebugLogs option when calling NewLogger. Expectations on log fields can be added using the returned ExpectedLog.

func (*Logger) ExpectError

func (l *Logger) ExpectError(msg string) *ExpectedLog

ExpectError adds an expectation for a log message at ERROR level. Expectations on log fields can be added using the returned ExpectedLog.

func (*Logger) ExpectInfo

func (l *Logger) ExpectInfo(msg string) *ExpectedLog

ExpectInfo adds an expectation for a log message at INFO level. Expectations on log fields can be added using the returned ExpectedLog.

func (*Logger) ExpectWarn

func (l *Logger) ExpectWarn(msg string) *ExpectedLog

ExpectWarn adds an expectation for a log message at WARN level. Expectations on log fields can be added using the returned ExpectedLog.

func (*Logger) Flush

func (l *Logger) Flush() error

Flush flushes any pending log statements. This is a no-op as logs are stored in memory.

func (*Logger) Info

func (l *Logger) Info(msg string)

Info creates a log statement at INFO level.

func (*Logger) Warn

func (l *Logger) Warn(msg string)

Warn creates a log statement at WARN level.

func (*Logger) WithError

func (l *Logger) WithError(err error) logging.Logger

WithError adds a log field with a key of "error" to a log statement.

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) logging.Logger

WithField adds a single log field to a log statement.

func (*Logger) WithFields

func (l *Logger) WithFields(fields logging.Fields) logging.Logger

WithFields adds multiple log fields to a log statement.

type Option

type Option = func(*loggerOptions)

Option implementations can be used to configure a new Logger instance.

func AssertDebugLogs

func AssertDebugLogs() Option

AssertDebugLogs enables assertions against DEBUG level logs. If this is not used, DEBUG level logs are ignored by Logger.AssertExpectations.

func AssertWithoutOrder

func AssertWithoutOrder() Option

AssertWithoutOrder enables assertions in any order for each level of logs. This allows assertions on logs pushed to the same underlying logger from multiple goroutines (as the order the logs are generated in may be nondeterministic).

Each generated log will be compared with each expectation until one matches, at which point that expectation is removed from the available list, as such the expectations must be specific enough to match only the single log they are intended to match.

func SkipAssert

func SkipAssert() Option

SkipAssert disables all assertions such that any or no logs can be produced without being checked.

This is generally inappropriate for unit testing, but may be useful for contract testing where only the API inputs and outputs are of interest.

Jump to

Keyboard shortcuts

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