Skip to content

Instantly share code, notes, and snippets.

@krak3n
Created September 12, 2017 10:38
Show Gist options
  • Save krak3n/4210e73d46b176f51311d1145fe0a89a to your computer and use it in GitHub Desktop.
Save krak3n/4210e73d46b176f51311d1145fe0a89a to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"flag"
"io/ioutil"
"path/filepath"
"testing"
)
var update = flag.Bool("update", false, "update .golden files")
func TestToJSON(t *testing.T) {
var b bytes.Buffer
w := bufio.NewWriter(&b)
err := ToJSON(w)
if err != nil {
t.Fatalf("failed writing json: %s", err)
}
w.Flush()
gp := filepath.Join("testdata", t.Name()+".golden")
if *update {
t.Log("update golden file")
if err := ioutil.WriteFile(gp, b.Bytes(), 0644); err != nil {
t.Fatalf("failed to update golden file: %s", err)
}
}
g, err := ioutil.ReadFile(gp)
if err != nil {
t.Fatalf("failed reading .golden: %s", err)
}
t.Log(string(b.Bytes()))
if !bytes.Equal(b.Bytes(), g) {
t.Errorf("writtein json does not match .golden file")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment