-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
48 lines (38 loc) · 1008 Bytes
/
utils_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
// Copyright (c) Omlox Client Go Contributors
// SPDX-License-Identifier: MIT
package omlox
import (
"encoding/json"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/nsf/jsondiff"
)
// JSONMarshalOK utility function to check that structs are marshalled correctly to json.
func JSONMarshalOK[T any](t *testing.T, input T, expected []byte) {
t.Helper()
o, err := json.Marshal(input)
if err != nil {
t.Fatal(err)
}
opts := jsondiff.DefaultConsoleOptions()
if r, diff := jsondiff.Compare(expected, o, &opts); r != jsondiff.FullMatch {
t.Fatalf("%s", diff)
}
}
func JSONUnmarshalOK[T any](t *testing.T, input []byte, expected T) {
var (
o T
err error
)
tp := reflect.ValueOf(expected)
if tp.Kind() == reflect.Ptr {
t.Fatal("unsupported pointer value. use a value instead.")
}
if err = json.Unmarshal(input, &o); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, o); diff != "" {
t.Errorf("%s mismatch (-want +got):\n%s", tp.String(), diff)
}
}