codegentest

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package codegentest provides utilities for testing generators.

Index

Constants

This section is empty.

Variables

Functions

func Golden

func Golden(t *testing.T, results []*Result, update bool)

Golden compares the results with golden files. Golden creates read a golden file which name is codegen.Generator.Name + ".golden". The golden file is stored in same directory of the package. If Golden cannot find a golden file or the result of Generator test is not same with the golden, Golden reports error via *testing.T. If update is true, golden files would be updated.

var flagUpdate bool

func TestMain(m *testing.M) {
	flag.BoolVar(&flagUpdate, "update", false, "update the golden files")
	flag.Parse()
	os.Exit(m.Run())
}

func TestGenerator(t *testing.T) {
	rs := codegentest.Run(t, codegentest.TestData(), example.Generator, "example")
	codegentest.Golden(t, rs, flagUpdate)
}

Types

type Result

type Result struct {
	Dir    string
	Pass   *codegen.Pass
	Facts  map[types.Object][]analysis.Fact
	Err    error
	Output *bytes.Buffer
}

A Result holds the result of applying a generator to a package.

func Run

func Run(t Testing, dir string, g *codegen.Generator, patterns ...string) []*Result

Run applies a generator to the packages denoted by the "go list" patterns.

It loads the packages from the specified GOPATH-style project directory using golang.org/x/tools/go/packages, runs the generator on them.

type Testing

type Testing = analysistest.Testing

Jump to

Keyboard shortcuts

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