fix

package module
v0.0.0-...-78c32d2 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 11 Imported by: 0

README

fix - Golden Files Testing library for Go

Go Reference

This library, like Ruby's vcr, serializes the results once executed and saves them as a file. You can easily write tests to verify that the code outputs the same results as in the past, even after modifying the code.

go get github.com/juntaki/fix

How to use

Append code like below to your tests. and Run test twice.

err := fix.Fix(&output) // output is dumped to a file.
if err != nil {
  t.Fatal(err)
}

First test will fail, because juntaki/fix writes serialized data to file. From the second time, the test will pass, if output is the same as first output.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// JSON is a Codec that uses the json package.
	JSON = Codec{jsonMarshal, jsonCompare}
	// PP is a Codec that uses the pp package.
	PP = Codec{ppMarshal, ppCompare}
)

Functions

func DefaultOutputPath

func DefaultOutputPath(funcName string, additional ...string) string

DefaultOutputPath is ./testdata/<caller_func_name>

func Fix

func Fix(target interface{}, additional ...string) error

Fix uses PP codec as default

func SetOutputPathFunc

func SetOutputPathFunc(in func(funcName string, additional ...string) string)

SetOutputPathFunc overwrite DefaultOutputPath

Types

type Codec

type Codec struct {
	Marshal func(interface{}) ([]byte, error)
	Compare func(old, new []byte) error
}

Codec is functions to store structure to file.

func (*Codec) Fix

func (c *Codec) Fix(target interface{}, additional ...string) error

Fix target pointer as codec encoded data file. if file doesn't exist, just write data to file and return error. if file exist, test if the target is the same as file's data.

Jump to

Keyboard shortcuts

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