testutils

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InspectableLogger

type InspectableLogger struct {

	// Each logger needs to store its own list of fields, so Fields is not a pointer
	Fields fields
	// contains filtered or unexported fields
}

InspectableLogger is designed as a drop in replacement for a real logger that stores information internally and can be inspected during testing to assert on logs that have been made.

When the logger is passed between functions the `AsMockArg` function should be added to the expectation so that internal details do not break the matching of arguments.

Example
package main

import (
	"fmt"

	"github.com/matlab/matlab-mcp-core-server/internal/entities"
	"github.com/matlab/matlab-mcp-core-server/internal/testutils"
)

func main() {
	// Arrange your test
	testLogger := testutils.NewInspectableLogger()

	const (
		importantLogToCheck  = "important log"
		keyForImportantLog   = "importantKey"
		valueForImportantLog = "importantValue"

		importantKeyToPersist  = "keyToPersist"
		importantKeyToPersist2 = "keyToPersist2"
		firstLogWithKey        = "log with key"
		secondLogWithKey       = "another log with key"
	)

	// Example function
	act := func(logger entities.Logger) {
		logger.Info("Info log")
		logger.Error("Error log")
		logger.Warn("Warning log")

		logger.With(keyForImportantLog, valueForImportantLog).Debug(importantLogToCheck)

		newLogger := logger.With(importantKeyToPersist, "importantValue")
		newLogger.Debug(firstLogWithKey)
		newLogger.Debug(secondLogWithKey)
	}

	// Act
	act(testLogger)

	// Assert the logs were as you expected
	// Normally this would be with the assert package, but here we use fmt and the example output syntax
	infoLogs := testLogger.InfoLogs()
	fmt.Println("Number of info logs:", len(infoLogs))

	errorLogs := testLogger.ErrorLogs()
	fmt.Println("Number of error logs:", len(errorLogs))

	warnLogs := testLogger.WarnLogs()
	fmt.Println("Number of warn logs:", len(warnLogs))

	debugLogs := testLogger.DebugLogs()

	// We can also check the details of important logs
	fields, ok := debugLogs[importantLogToCheck]
	fmt.Println("Important log found:", ok)
	fmt.Println("Number of fields on the important log:", len(fields))
	fmt.Println("Field found:", fields)

	// The inspectable logger will also correctly handle creating new loggers using `With`
	firstLogFields := debugLogs[firstLogWithKey]
	_, firstKeyFound := firstLogFields[importantKeyToPersist]
	fmt.Println("First log with key found:", firstKeyFound)

	secondLogFields := debugLogs[secondLogWithKey]
	_, secondKeyFound := secondLogFields[importantKeyToPersist]
	fmt.Println("Second log with key found:", secondKeyFound)

}
Output:

Number of info logs: 1
Number of error logs: 1
Number of warn logs: 1
Important log found: true
Number of fields on the important log: 1
Field found: map[importantKey:importantValue]
First log with key found: true
Second log with key found: true

func NewInspectableLogger

func NewInspectableLogger() *InspectableLogger

NewInspectableLogger creates a new InspectableLogger with empty log and field lists

func (*InspectableLogger) AsMockArg

func (il *InspectableLogger) AsMockArg() any

func (*InspectableLogger) Debug

func (il *InspectableLogger) Debug(msg string)

Debug stores a debug log in the InspectableLogger

func (*InspectableLogger) DebugLogs

func (il *InspectableLogger) DebugLogs() InspectableLogs

DebugLogs returns the debug logs stored by the InspectableLogger

func (*InspectableLogger) Error

func (il *InspectableLogger) Error(msg string)

Error stores a error log in the InspectableLogger

func (*InspectableLogger) ErrorLogs

func (il *InspectableLogger) ErrorLogs() InspectableLogs

ErrorLogs returns the error logs stored by the InspectableLogger

func (*InspectableLogger) Info

func (il *InspectableLogger) Info(msg string)

Info stores a info log in the InspectableLogger

func (*InspectableLogger) InfoLogs

func (il *InspectableLogger) InfoLogs() InspectableLogs

InfoLogs returns the info logs stored by the InspectableLogger

func (*InspectableLogger) Warn

func (il *InspectableLogger) Warn(msg string)

Warn stores a warn log in the InspectableLogger

func (*InspectableLogger) WarnLogs

func (il *InspectableLogger) WarnLogs() InspectableLogs

WarnLogs returns the warn logs stored by the InspectableLogger

func (*InspectableLogger) With

func (il *InspectableLogger) With(key string, value interface{}) entities.Logger

With returns a new InspectableLogger with references to the previous logger's log lists. The fields from the previous logger are copied over and the new field added. This way the new logger stores it's own fields, but the lists of logs are shared between all inspectable loggers that have been spawned from the original instance.

func (*InspectableLogger) WithError

func (il *InspectableLogger) WithError(err error) entities.Logger

WithError calls With but with the key already defined

type InspectableLogs

type InspectableLogs map[string]fields

Jump to

Keyboard shortcuts

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