golden

package module
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 16 Imported by: 0

README

golden

pkg.go.dev

golden provides utilities for golden file tests.

package a_test

import (
	"flag"
	"os"
	"path/filepath"
	"testing"

	"github.com/tenntenn/golden"
)

var (
	flagUpdate bool
)

func init() {
	flag.BoolVar(&flagUpdate, "update", false, "update golden files")
}

func testTarget(dir string) error {
	if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("hello"), 0700); err != nil {
		return err
	}

	if err := os.WriteFile(filepath.Join(dir, "b.txt"), []byte("world"), 0700); err != nil {
		return err
	}

	return nil
}

func Test(t *testing.T) {
	dir := t.TempDir()
	if err := testTarget(dir); err != nil {
		t.Fatal("unexpected error:", err)
	}

	got := golden.Txtar(t, dir)
	if diff := golden.Check(t, flagUpdate, "testdata", "mytest", got); diff != "" {
		t.Error(diff)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check added in v0.3.0

func Check(t *testing.T, update bool, testdata, name string, data any, opts ...cmp.Option) string

Check updates a golden file when update is true otherwise compares data with the exsiting golden file by DiffWithOpts. If update is true Check does not compare and just return "".

var flagUpdate bool

func init() {
	flag.BoolVar(&flagUpdate, "update", false, "update golden files")
}

func Test(t *testing.T) {
	got := doSomething()
	if diff := golden.Check(t, flagUpdate, "testdata", t.Name(), got); diff != "" {
		t.Error(diff)
	}
}

func Diff

func Diff(t *testing.T, testdata, name string, data any) string

Diff compares between the given data and a golden file which is stored in testdata as name+".golden". Diff returns difference of them. Diff uses go-cmp to compare.

func DiffWithOpts added in v0.3.0

func DiffWithOpts(t *testing.T, testdata, name string, data any, opts ...cmp.Option) string

DiffWithOpts compares between the given data and a golden file which is stored in testdata as name+".golden". DiffWithOpts returns difference of them. DiffWithOpts uses go-cmp to compare.

func DirInit added in v0.2.0

func DirInit(t *testing.T, root, txtarStr string)

DirInit creates directory by txtar format.

func RemoveAll

func RemoveAll(t *testing.T, testdata string)

RemoveAll removes all golden files which has .golden extention and is under testdata.

func Txtar

func Txtar(t *testing.T, dir string) string

Txtar converts a directory as a txtar format.

func TxtarJoin added in v0.4.0

func TxtarJoin(t TestingT, txtars ...string) string

TxtarJoin appends a txtar format values. Its file list are sorted by the file path.

func TxtarWith added in v0.2.0

func TxtarWith(t *testing.T, nameAndData ...string) string

TxtarWith creates a txtar format value with given a file name and its data pairs.

func Update

func Update(t *testing.T, testdata, name string, data any)

Update updates a golden file with the given data. The golden file saved as name+".golden" in testdata.

Types

type Checker added in v0.3.0

type Checker struct {
	JSONIdent bool
	// contains filtered or unexported fields
}

Checker can do golden file testing for multiple data. Checker holds *testing.T, update flag, testdata directory, test name and options for go-cmp.

func New added in v0.3.0

func New(t TestingT, update bool, testdata, name string, opts ...cmp.Option) *Checker

New creates a Checker.

func (*Checker) Check added in v0.3.0

func (c *Checker) Check(suffix string, data any) (diff string)

Check do a golden file test for a single data. Check calls Check function with test name which combiend with suffix.

var flagUpdate bool

func init() {
	flag.BoolVar(&flagUpdate, "update", false, "update golden files")
}

func Test(t *testing.T) {
	got := doSomething()
	c := golden.New(t, flagUpdate, "testdata", t.Name())
	if diff := c.Check("_someting", got); diff != "" {
		t.Error(diff)
	}
}

type TestingT added in v0.3.0

type TestingT interface {
	Helper()
	Fatal(args ...any)
}

TestingT is interface for *testing.T.

Jump to

Keyboard shortcuts

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