Giter Club home page Giter Club logo

go-goldsert's Introduction

go-goldsert

A suite of Go test helpers which uses golden files to assert marshaling and unmarshaling of given objects.

Go Reference Actions Status Coverage GitHub issues GitHub pull requests License Status

Each test helper operates in two stages:

  1. Marshal the provided object to a byte slice and reads the corresponding golden file from disk, follow by verifying both byte slices are identical.
  2. Unmarshal the content from the golden file and verify that the result is identical to the original object.

Import

import "github.com/jimeh/go-goldsert"

Usage

Typical usage should look something like this in a tabular test:

type MyStruct struct {
    FooBar string `json:"foo_bar" yaml:"fooBar" xml:"Foo_Bar"`
}

func TestMyStructMarshaling(t *testing.T) {
    tests := []struct {
        name string
        obj  *MyStruct
    }{
        {name: "empty", obj: &MyStruct{}},
        {name: "full", obj: &MyStruct{FooBar: "Hello World!"}},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            goldsert.JSONMarshaling(t, tt.obj)
            goldsert.YAMLMarshaling(t, tt.obj)
            goldsert.XMLMarshaling(t, tt.obj)
        })
    }
}

The above example will read from the following golden files:

  • testdata/TestMyStructMarshaling/empty/goldsert_json.golden
  • testdata/TestMyStructMarshaling/empty/goldsert_yaml.golden
  • testdata/TestMyStructMarshaling/empty/goldsert_xml.golden
  • testdata/TestMyStructMarshaling/full/goldsert_json.golden
  • testdata/TestMyStructMarshaling/full/goldsert_yaml.golden
  • testdata/TestMyStructMarshaling/full/goldsert_xml.golden

If a corresponding golden file cannot be found on disk, the test will fail. To create/update golden files, simply set the GOLDEN_UPDATE environment variable to one of 1, y, t, yes, on, or true when running tests.

It is highly recommended that golden files are committed to source control, as it allow tests to fail when the marshal results for an object changes.

Documentation

Please see the Go Reference for documentation and examples.

License

MIT

go-goldsert's People

Contributors

jimeh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.