golden

package module
v0.0.0-...-4e91178 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2016 License: MIT Imports: 13 Imported by: 0

README

golden

MIT license GitHub release GoDoc
Go Report Card Build Status Coverage Status

Package golden helps writing golden tests.

A golden test (a.k.a. gold master or approval test) consists of pairs of files: given test input and expected output (the gold master).

This package is mainly intended to be used with the standard Go testing library. You can still use it without. It is not intended to be a full testing framework.

Installation

go get github.com/nochso/golden

Documentation

See godoc for API docs and examples.

Notes

It's important that golden files are easy to read, edit and diff. Usually it's enough to add a String() string method or more detailed TestString, debugString, etc. to dump the data you want to compare in a human readable way.

Any methods that could fail and have access to T (as in testing.T) will call T.Error(err). If T is nil, log.Println(err) is called instead. This mainly concerns Case and File structs and any methods mentioning failing.

Changes / Versioning

All notable changes to this project will be documented in CHANGELOG.md

This project adheres to Semantic Versioning.

License

This package is released under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Extension that is added to the name of the input file to identify the
	// matching golden file.
	Extension = ".golden"
	// BasePath is put in front of paths passed to any of the Dir* functions.
	BasePath = "."
	// ChannelSize used by Dir() is arbitrary ┐( ̄ヘ ̄)┌
	ChannelSize = 32
)

Functions

func Dir

func Dir(t *testing.T, path string) <-chan Case

Dir returns a Case channel from a given directory.

See ChannelSize for the channel size to be used. Any errors while walking the file system will fail and are not ignored.

func TestDir

func TestDir(t *testing.T, path string, fn func(Case))

TestDir calls fn with each Case in path. Each Case i is bound to a sub test named after the input file.

Types

type Case

type Case struct {
	In  File
	Out File
	T   *testing.T
}

Case provides input and expected output for a single test case.

func DirSlice

func DirSlice(t *testing.T, path string) []Case

DirSlice returns a Case slice from a given directory.

Any errors while walking the file system will fail and are not ignored.

func NewCase

func NewCase(t *testing.T, path string) Case

NewCase returns a Case based on the given input file.

func (Case) Diff

func (c Case) Diff(actual string)

Diff the given actual string with the expected content of c.Out. Fails a test if contents are different.

func (Case) Test

func (c Case) Test(fn func(Case))

Test runs fn in a sub test named after the input file.

type File

type File struct {
	Case *Case // The case this file belongs to.
	Path string
}

File provides read/write access to test files.

func (File) Bytes

func (f File) Bytes() []byte

Bytes returns the content as a byte slice.

It will fail when the file could not be read.

func (File) Exists

func (f File) Exists() bool

func (File) Reader

func (f File) Reader() io.ReadCloser

Reader returns a ReadCloser.

This is basically os.File: remember to call Close(), especially if you have many files or read them multiple times.

func (File) Split

func (f File) Split(sep string) []string

Split the file into a string slice using separator sep.

func (File) String

func (f File) String() string

String returns content as a string.

It will fail when the file could not be read.

func (File) Update

func (f File) Update(b []byte)

Update the file by writing b to it.

Jump to

Keyboard shortcuts

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