-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_utils.go
71 lines (55 loc) · 1.66 KB
/
test_utils.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
package test
import (
"encoding/json"
"os"
"runtime"
"strings"
"sync"
"testing"
"github.com/sebdah/goldie/v2"
)
var (
nameRegistry = map[string]struct{}{}
mux = &sync.Mutex{}
)
func AssertBytesAsJSON(t *testing.T, testCase string, bytes []byte) {
t.Helper()
caseName := createCanonicalTestCaseName(t, testCase)
g := goldie.New(t)
object := map[string]interface{}{}
if err := json.Unmarshal(bytes, &object); err != nil {
panic(err.Error())
}
g.AssertJson(t, caseName, object)
}
// createCanonicalTestCaseName Generate a canonical name for this test case
// and validates that it is not already in use
func createCanonicalTestCaseName(t *testing.T, testCase string) string {
mux.Lock()
defer mux.Unlock()
caseName := getCanonicalName(testCase)
if _, exists := nameRegistry[caseName]; exists {
t.Errorf("golden: test case name %s already in use", caseName)
}
nameRegistry[caseName] = struct{}{}
return caseName
}
// getCanonicalName Create a canonical test case name using the caller function name
func getCanonicalName(testCase string) string {
return getCaller() + string(os.PathSeparator) + testCase
}
func getCaller() string {
// Skip frames up to the caller test function
// nolint:gomnd,dogsled // runtime api & see upper comment
pc, _, _, _ := runtime.Caller(4)
f := runtime.FuncForPC(pc)
fullName := f.Name()
tokens := strings.Split(fullName, "/")
testFuncName := tokens[len(tokens)-1]
// Remove anonymous function names from test function name
tokens = strings.Split(testFuncName, ".")
if strings.HasPrefix(tokens[len(tokens)-1], "func") {
tokens = tokens[:len(tokens)-1]
}
return strings.TrimSpace(strings.Join(tokens, "."))
}