-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonical.go
145 lines (115 loc) · 2.73 KB
/
canonical.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
137
138
139
140
141
142
143
144
145
package canonical
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Canonize - set to any non-zero value to canonize tests.
var Canonize = ""
//goland:noinspection GoBoolExpressions
func canonizeEnabled() bool {
return Canonize != ""
}
var canonicalValues = map[string]interface{}{}
const canonicalJson = "canonical.json"
var readOnce sync.Once
func read(t *testing.T) {
if canonizeEnabled() {
return
}
readOnce.Do(func() {
file, err := os.Open(canonicalJson)
if err != nil {
t.Fatal("open canonical.json: ", err)
}
if err = json.NewDecoder(file).Decode(&canonicalValues); err != nil {
t.Fatal("decode canonical.json: ", err)
}
})
}
var writeMu sync.Mutex
func write(t *testing.T) {
buf, err := json.Marshal(canonicalValues)
if err != nil {
t.Fatal("marshal canonical.json: ", err)
}
var indented bytes.Buffer
if err = json.Indent(&indented, buf, "", "\t"); err != nil {
t.Fatal("indent canonical.json: ", err)
}
if err = ioutil.WriteFile(canonicalJson, indented.Bytes(), 0644); err != nil {
t.Fatal("write canonical.json: ", err)
}
}
func mustMarshal(t *testing.T, v interface{}) string {
result, err := json.Marshal(v)
if err != nil {
t.Fatal("canonical: marshal failed: ", err)
}
return string(result)
}
// Assert asserts that the provided value matches the canonical value.
func Assert(t *testing.T, values ...interface{}) bool {
if len(values) == 0 {
t.Fatal("canonical.Assert of no values")
}
var value interface{}
if len(values) == 1 {
value = values[0]
} else {
value = values
}
if canonizeEnabled() {
writeMu.Lock()
defer writeMu.Unlock()
canonicalValues[t.Name()] = value
write(t)
return true
} else {
read(t)
canonicalValue, ok := canonicalValues[t.Name()]
if !ok {
t.Log("did not find canonical value")
t.Fail()
return false
}
return assert.JSONEq(t, mustMarshal(t, canonicalValue), mustMarshal(t, value))
}
}
// Require requires that the provided value matches the canonical value.
func Require(t *testing.T, values ...interface{}) {
if len(values) == 0 {
t.Fatal("canonical.Require of no values")
}
var value interface{}
if len(values) == 1 {
value = values[0]
} else {
value = values
}
if canonizeEnabled() {
writeMu.Lock()
defer writeMu.Unlock()
canonicalValues[t.Name()] = value
write(t)
} else {
read(t)
canonicalValue, ok := canonicalValues[t.Name()]
if !ok {
t.Fatal("did not find canonical value")
}
require.JSONEq(t, mustMarshal(t, canonicalValue), mustMarshal(t, value))
}
}
// Error returns the canonical form of an error.
func Error(err error) interface{} {
if err == nil {
return nil
}
return err.Error()
}