goldenfile

package module
v0.0.0-...-52d2910 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 8 Imported by: 0

README

goldenfile

Go test helper to verify output against golden files

Example

===
simple
===
var integer a;
---
(Root
  (VarDecl
    (Reference)
    (Declarators
      (Declarator
        (Name "a")))))

===
with assignment
===
var integer a := 5;
---
(Root
  (VarDecl
    (Reference)
    (Declarators
      (Declarator
        (Name "a")
        (Literal "5")))))

Testing

tests, err := goldenfile.Parse(fh)
if err != nil {
	t.Fatalf("goldenfile.Parse(%s): %s", file, err.Error())
}

if *goldenfile.Update {
	updateTests(t, tests)
}

for _, test := range tests {
	t.Run(test.Name, func (t *testing.T) {
		if myFunc(test.Text) != test.Want {
			log.Errorf("myFunc() failed")
		}
	})
}


Documentation

Overview

Package goldenfile implement simplistic goldenfile parsing in a format similar to TreeSitter tests.

Unlike TreeSitter we do not support any additional test-data (such as skipping tests, or multi-file input).

Each file contains one or more tests. The name musst be enclosed by at least three equal-signs (`===`). The input text and expected text musst be separated by at least three minuses (`---`):

``` === Test 1 === input of test 1 --- wanted output of test 1

=== Test 2 === --- ```

Example

tests, _ := goldenfile.Parse(os.Stdin)

for _, test := range tests {
	fmt.Printf("Test: %+n")
}

Updating files

The package add a new command line flag `-update` to go test. This flags can be checked by the user and let update the goldenfiles. See the example in the repository for details.

Index

Constants

This section is empty.

Variables

View Source
var (
	Update = flag.Bool("update", false, "update golden files")
)

Functions

func TestMain

func TestMain(m *testing.M)

Types

type Test

type Test struct {
	Name string // Testname
	Text string // Given input
	Want string // Expected output
}

Test is a single test

func Parse

func Parse(r io.Reader) ([]Test, error)

Parse content and return a slice of tests.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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