-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal_test.go
134 lines (108 loc) · 2.32 KB
/
marshal_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
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
package rfc8288
import (
"encoding/json"
"fmt"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func parseURL(u string, t *testing.T) url.URL {
require := require.New(t)
uri, err := url.Parse(u)
require.NoError(err)
return *uri
}
type marshalTestEntry struct {
name string
link Link
m map[string]interface{}
}
func marshalEntry(name string, link Link, m map[string]interface{}) marshalTestEntry {
return marshalTestEntry{
name: name,
link: link,
m: m,
}
}
func TestLinkMarshal(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
entries := []marshalTestEntry{
marshalEntry(
"should marshal all fields and extensions",
func() Link {
l := Link{
HREF: parseURL("https://www.google.com", t),
Rel: "rel",
Rev: "rev",
Anchor: "anchor",
HREFLang: "hreflang",
Media: "media",
Title: "title",
TitleStar: "title*",
Type: "type",
}
l.Extend("extension", "value")
return l
}(),
map[string]interface{}{
"href": "https://www.google.com",
"rel": "rel",
"rev": "rev",
"anchor": "anchor",
"hreflang": "hreflang",
"media": "media",
"title": "title",
"title*": "title*",
"type": "type",
"extension": "value",
},
),
}
for _, entry := range entries {
in := entry.link
out := entry.m
// given
jsonBytes, err := json.Marshal(entry.link)
result := make(map[string]interface{})
require.NoError(err)
// when
json.Unmarshal(jsonBytes, &result)
check := func(prop string, entry marshalTestEntry) {
assert.Equal(out[prop], result[prop], fmt.Sprintf("Comparing %s property for case %s", prop, entry.name))
}
// then
var zero url.URL
if in.HREF != zero {
check("href", entry)
}
if in.Rel != "" {
check("rel", entry)
}
if in.Rev != "" {
check("rev", entry)
}
if in.Anchor != "" {
check("anchor", entry)
}
if in.HREFLang != "" {
check("hreflang", entry)
}
if in.Media != "" {
check("media", entry)
}
if in.Title != "" {
check("title", entry)
}
if in.TitleStar != "" {
check("title*", entry)
}
if in.Type != "" {
check("type", entry)
}
for _, key := range in.ExtensionKeys() {
check(key, entry)
}
}
}