-
Notifications
You must be signed in to change notification settings - Fork 28
/
main_test.go
54 lines (47 loc) · 1.58 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2017 Felix Lange <[email protected]>.
// Use of this source code is governed by the MIT license,
// which can be found in the LICENSE file.
package main
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/kylelemons/godebug/diff"
)
// 'golden' tests. These tests compare the output code with the expected
// code in internal/tests/*/output.go. The expected can be updated using
//
// go generate ./internal/...
func TestGolden(t *testing.T) {
tests := []Config{
Config{Dir: "mapconv", Type: "X", FieldOverride: "Xo", Formats: AllFormats},
Config{Dir: "sliceconv", Type: "X", FieldOverride: "Xo", Formats: AllFormats},
Config{Dir: "arrayconv", Type: "X", FieldOverride: "Xo", Formats: []string{"json"}},
Config{Dir: "nameclash", Type: "Y", FieldOverride: "yo", Formats: AllFormats},
Config{Dir: "omitempty", Type: "X", FieldOverride: "Xo", Formats: AllFormats},
Config{Dir: "reqfield", Type: "X", Formats: []string{"json"}},
Config{Dir: "ftypes", Type: "X", Formats: []string{"json"}},
Config{Dir: "funcoverride", Type: "Z", FieldOverride: "Zo", Formats: AllFormats},
}
for _, test := range tests {
test := test
t.Run(test.Dir, func(t *testing.T) {
t.Parallel()
runGoldenTest(t, test)
})
}
}
func runGoldenTest(t *testing.T, cfg Config) {
cfg.Dir = filepath.Join("internal", "tests", cfg.Dir)
want, err := ioutil.ReadFile(filepath.Join(cfg.Dir, "output.go"))
if err != nil {
t.Fatal(err)
}
code, err := cfg.process()
if err != nil {
t.Fatal(err)
}
if d := diff.Diff(string(want), string(code)); d != "" {
t.Errorf("output mismatch\n\n%s", d)
}
}