Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tslamic/golden
Excruciatingly simple golden file handling.
https://github.com/tslamic/golden
go golang golden golden-file golden-tests testing
Last synced: about 2 months ago
JSON representation
Excruciatingly simple golden file handling.
- Host: GitHub
- URL: https://github.com/tslamic/golden
- Owner: tslamic
- License: mit
- Created: 2019-12-07T17:50:05.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-28T13:19:05.000Z (over 2 years ago)
- Last Synced: 2024-06-20T05:21:48.688Z (6 months ago)
- Topics: go, golang, golden, golden-file, golden-tests, testing
- Language: Go
- Size: 22.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![CircleCI](https://circleci.com/gh/tslamic/golden.svg?style=svg)](https://circleci.com/gh/tslamic/golden)
[![Go Report Card](https://goreportcard.com/badge/github.com/tslamic/golden)](https://goreportcard.com/report/github.com/tslamic/golden)
[![Go Reference](https://pkg.go.dev/badge/github.com/tslamic/golden/v2.svg)](https://pkg.go.dev/github.com/tslamic/golden/v2)# :large_orange_diamond: Golden
Excruciatingly simple golden file handling.
If you're unsure what golden files are, check [this video](https://youtu.be/8hQG7QlcLBk?t=735).## How to use it?
```
go get github.com/tslamic/golden/v2
```If your files are JSON, XML or plain text, you're good to go:
```go
func TestJSON(t *testing.T) {
expected := newGreeter()
gf := golden.File("testdata/hello.json")
gf.Equals(t, expected)
}func TestXML(t *testing.T) {
expected := newCatalog()
gf := golden.File("testdata/catalog.xml")
gf.Equals(t, expected)
}func TestText(t *testing.T) {
expected := "Hello World!"
gf := golden.File("testdata/world.txt")
gf.Equals(t, expected)
}
```For custom marshalling or attributes, you can do the following:
```go
gf := File(name, func(attrs *Attrs) {
attrs.Marshaller = func(v interface{}) ([]byte, error) {
b := new(bytes.Buffer)
err := gob.NewEncoder(b).Encode(v)
return b.Bytes(), err
}
})
```and if you want to tweak the underlying `[]byte` slices that will be compared, apply some transformers:
```go
func TestJSONWhitespace(t *testing.T) {
expected := newGreeter()
gf := File("testdata/hello_whitespace.json").Apply(StripWhitespace)
gf.Equals(t, expected)
}
```Make sure to run the tests with the `-update` command line argument to populate the files,
then drop the flag for all subsequent tests.## License
The MIT License.