-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_test.go
86 lines (78 loc) · 1.72 KB
/
profile_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
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
// Copyright 2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package psatoken
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/psatoken/encoding"
)
func Test_getJSONTag(t *testing.T) {
testCases := []struct {
Name string
Value interface{}
ExpectedTag string
ExpectedError string
}{
{
Name: "from cbor keyasint ok struct",
Value: struct {
Field string `cbor:"265,keyasint" json:"profile1"`
}{},
ExpectedTag: "profile1",
},
{
Name: "from cbor ok struct",
Value: struct {
Field string `cbor:"265" json:"profile2"`
}{},
ExpectedTag: "profile2",
},
{
Name: "from cbor ok pointer",
Value: &struct {
Field string `cbor:"265" json:"profile3"`
}{},
ExpectedTag: "profile3",
},
{
Name: "from name ok",
Value: struct {
Profile string `json:"profile4"`
}{},
ExpectedTag: "profile4",
},
{
Name: "prefer cbor over name",
Value: struct {
Profile string `cbor:"1" json:"profile5"`
Field string `cbor:"265" json:"profile6"`
}{Field: "test", Profile: "test"},
ExpectedTag: "profile6",
},
{
Name: "no json tag",
Value: struct {
Field string `cbor:"265"`
}{},
ExpectedError: `no "json" tag`,
},
{
Name: "no profile field",
Value: struct {
}{},
ExpectedError: `could not identify profile field`,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
tag, err := encoding.GetProfileJSONTag(tc.Value)
if tc.ExpectedTag != "" {
assert.Equal(t, tc.ExpectedTag, tag)
} else {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.ExpectedError)
}
})
}
}