-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
136 lines (119 loc) · 3.5 KB
/
file.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2019 rameshvk. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.
// Package test implements simple test utilities.
package test
import (
"encoding/json"
"flag"
"io/ioutil"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/google/go-cmp/cmp"
)
// File implements testing against input/output files
//
// The input and output file names are relative to the testdata/
// folder of the caller.
//
// The provided function is one of these two forms:
//
// func (input someType) (output someOtherType)
// func (input someType) (output someOtherType, err error)
//
// The input file is read and the contents passed through this
// function. For input arguments of type string, []byte or []rune the
// contents of the files are passed as is. For other types, the
// contents are assumed to be JSON encoded. The output is similarly
// JSON encoded for such types.
//
// The discrepancies are reported using regular diff format via the
// error function (which sports the same signature as testing.T.Error
// or testing.T.Fatal)
//
// If the tests are run with -golden flag, the output is not compared
// but instead the output files are created to match the output
// provided by the test.
//
// Example Usage:
//
// test.File(t.Fatal, "input.txt", "output.txt",
// func(input string) string { .... },
// )
//
func File(errorf Errorf, inputFile string, outputFile string, fn interface{}) {
pc := []uintptr{0}
runtime.Callers(2, pc)
f, _ := runtime.CallersFrames(pc).Next()
inputFile = filepath.Join(filepath.Dir(f.File), "testdata/"+inputFile)
outputFile = filepath.Join(filepath.Dir(f.File), "testdata/"+outputFile)
bytes, err := ioutil.ReadFile(inputFile)
if err != nil {
errorf("error reading", inputFile, err)
return
}
output, err := invoke(fn, string(bytes))
if err != nil {
errorf(err)
return
}
if *goldenFlag {
if err := ioutil.WriteFile(outputFile, []byte(output), 0644); err != nil {
errorf("Could not save golden output", outputFile, err)
}
return
}
bytes, err = ioutil.ReadFile(outputFile)
if err != nil {
errorf("error reading", outputFile, err)
return
}
if output != string(bytes) {
errorf("unexpected output", linediff(string(bytes), output))
}
}
func linediff(s1, s2 string) string {
return cmp.Diff(strings.Split(s1, "\n"), strings.Split(s2, "\n"))
}
func invoke(fn interface{}, input string) (string, error) {
v := reflect.ValueOf(fn)
argType := v.Type().In(0)
var results []reflect.Value
switch reflect.Zero(argType).Interface().(type) {
case string:
results = v.Call([]reflect.Value{reflect.ValueOf(input)})
case []byte:
bytes := []byte(input)
results = v.Call([]reflect.Value{reflect.ValueOf(bytes)})
case []rune:
runes := []rune(input)
results = v.Call([]reflect.Value{reflect.ValueOf(runes)})
default:
ptr := reflect.New(argType)
if err := json.Unmarshal([]byte(input), ptr.Interface()); err != nil {
return "", err
}
results = v.Call([]reflect.Value{ptr.Elem()})
}
if len(results) > 1 {
if err, _ := results[1].Interface().(error); err != nil {
return "", err
}
}
switch r := results[0].Interface().(type) {
case string:
return r, nil
case []byte:
return string(r), nil
case []rune:
return string(r), nil
}
bytes, err := json.MarshalIndent(results[0].Interface(), "", "\t")
if err != nil {
return "", err
}
return string(bytes), nil
}
var goldenFlag = flag.Bool("golden", false, "build golden testdata files instead of verifying")