base

package
v0.0.0-...-4c78a19 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareOutToGolden

func CompareOutToGolden(outfilepath, goldenfilepath string) error

CompareOutToGolden reads the outfile and the goldenfile and compares them. It returns an error if they are not the same.

func FatalIfError

func FatalIfError(t *testing.T, err error, label string)

FatalIfError calls testing.T.Fatal if there is an error. This is typically used to wrap calls to the various Runner steps, for example:

base.FatalIfError(t, r.Arrange(), "Arrange")

func RunOne

func RunOne(r MultiRunner) error

RunOne runs one test on the MultiRunner by executing Init, then running RunTest, then Close.

func RunTest

func RunTest(r Runner) error

RunTest runs the test on the Runner by executing the Arrange, Act, and Assert functions.

Types

type MultiRunner

type MultiRunner interface {
	Runner

	// Init does one-time initialization of this Runner before running tests.
	Init() error

	// Close cleans everything up. Nothing else can be called after Close.
	Close() error
}

MultiRunner defines the methods used when running multiple tests that include common init and close steps. Typical use for a MultiRunner is to call m.Init(), then do some test-specific setup work and call RunTest, repeat that for all tests, and finish by calling m.Close().

type Runner

type Runner interface {
	// Arrange sets up for one test, to be performed by Act().
	Arrange() error

	// Act runs the code under test.
	Act() error

	// Assert checks the output against the golden file.
	Assert() error
}

Runner defines the methods used when running one of our unit tests.

type Tester

type Tester struct {
	// Base name for all files in the test, if not overridden; defaults to "test".
	BaseName string
	// Base directory; if not set, uses "testdata".
	BaseDir string

	// Base name for the test output file; if not set, uses BaseName.
	OutBaseName string
	// Path to the test output file; if not set, uses OutBaseName.
	OutPath string

	// Base name for the golden file; if not set, uses BaseName.
	GoldenBaseName string
	// Path to the golden file; if not set, uses GoldenBaseName.
	GoldenPath string

	// Function to run the test.
	Test func(*Tester) error

	// The output file.
	OutF *os.File
	// A Writer that can be used to write to the output file.
	OutW *bufio.Writer
}

Tester allows for configuring and running the different steps of the test.

func NewTester

func NewTester(basename string) *Tester

NewTester creates a new Tester instance.

func (*Tester) Act

func (r *Tester) Act() error

Act calls the users Test function.

func (*Tester) Arrange

func (r *Tester) Arrange() error

Arrange creates the output files for the test to write to and sets OutF and OutW in the Tester.

func (*Tester) Assert

func (r *Tester) Assert() error

Assert closes the output and compares it to the golden file.

func (*Tester) Close

func (r *Tester) Close() error

Close is a no-op in this Tester.

func (*Tester) GetFilePath

func (r *Tester) GetFilePath(fpath, basename, extension string) string

GetFilePath calculates and returns the complete path to a file. If fpath is set, it returns it, else it uses basename, with default "test", and the Tester's BaseDir, with default "testdata", plus the given extension to generate the path to return.

func (*Tester) GoldenFilePath

func (r *Tester) GoldenFilePath() string

GoldenFilePath returns the complete path to the golden file.

func (*Tester) Init

func (r *Tester) Init() error

Init is a no-op for this Tester.

func (*Tester) OutFilePath

func (r *Tester) OutFilePath() string

OutFilePath returns the complete path to the output file.

Jump to

Keyboard shortcuts

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