ldlogtest

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Overview

Package ldlogtest contains test helpers for use with the ldlog package.

This package provides the MockLog type, which allows you to capture output that is sent to the ldlog.Loggers API. This can be useful in test code for verifying that some component produces the log output you expect it to. It is separate from the ldlog package because production code normally will not use this tool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockLog

type MockLog struct {
	// Loggers is the ldlog.Loggers instance to be used for tests.
	Loggers ldlog.Loggers
	// contains filtered or unexported fields
}

MockLog provides the ability to capture log output.

It contains a Loggers instance which can be used like any other Loggers, but all of the output is captured by the enclosing MockLog and can be retrieved with the MockLog methods.

mockLog := ldlogtest.NewMockLog()
mockLog.Loggers.Warn("message")
mockLog.Loggers.Warnf("also: %t", true)
warnMessages := mockLog.GetOutput(ldlog.Warn) // returns {"message", "also: true"}

func NewMockLog

func NewMockLog() *MockLog

NewMockLog creates a log-capturing object.

func (*MockLog) AssertMessageMatch

func (ml *MockLog) AssertMessageMatch(t *testing.T, shouldMatch bool, level ldlog.LogLevel, pattern string)

AssertMessageMatch asserts whether there is a log message of this level that matches this regex. This is equivalent to using assert.True or assert.False with HasMessageMatch, except that if the test fails, it includes the actual log messages in the failure message.

func (*MockLog) Dump

func (ml *MockLog) Dump(w io.Writer)

Dump is a shortcut for writing all captured log lines to a Writer.

func (*MockLog) DumpIfTestFailed

func (ml *MockLog) DumpIfTestFailed(t *testing.T)

DumpIfTestFailed is a shortcut for writing all captured log lines to standard output only if t.Failed() is true.

This is useful in tests where you normally don't want to see the log output, but you do want to see it if there was a failure. The simplest way to do this is to use defer:

func TestSomething(t *testing.T) {
    ml := ldlogtest.NewMockLog()
    defer ml.DumpIfTestFailed(t)
    // ... do some test things that may generate log output and/or cause a failure
}

func (*MockLog) GetAllOutput

func (ml *MockLog) GetAllOutput() []MockLogItem

GetAllOutput returns the captured output for all log levels.

func (*MockLog) GetOutput

func (ml *MockLog) GetOutput(level ldlog.LogLevel) []string

GetOutput returns the captured output for a specific log level.

func (*MockLog) HasMessageMatch

func (ml *MockLog) HasMessageMatch(level ldlog.LogLevel, pattern string) bool

HasMessageMatch tests whether there is a log message of this level that matches this regex.

type MockLogItem

type MockLogItem struct {
	Level   ldlog.LogLevel
	Message string
}

MockLogItem represents a log message captured by MockLog.

Jump to

Keyboard shortcuts

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