Created
September 12, 2017 10:38
-
-
Save krak3n/4210e73d46b176f51311d1145fe0a89a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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