-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinks_test.go
185 lines (158 loc) · 4.77 KB
/
links_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package jsonapi_test
import (
"reflect"
"testing"
"github.com/smotes/jsonapi"
)
func TestLinks_AddString_WhenNil(t *testing.T) {
var ls jsonapi.Links = nil
defer catchPanic(t, "Links", "AddString")
ls.AddString("foo", "")
}
func TestLinks_AddString(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
expected = "bar"
)
ls.AddString(key, expected)
if v, ok := ls[key]; !ok {
t.Error("underlying map in Links should have value associated with key after Links.AddString")
if actual, ok := v.(string); !ok {
t.Error("unexpected value type after Links.AddString, expected string")
} else if actual != expected {
t.Errorf("unexpected value after Links.AddString, expected: %s, actual: %s", expected, actual)
}
}
}
func TestLinks_Add_WhenNil(t *testing.T) {
var ls jsonapi.Links = nil
defer catchPanic(t, "Links", "Add")
ls.Add("foo", &jsonapi.Link{})
}
func TestLinks_Add(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
expected = &jsonapi.Link{}
)
ls.Add(key, expected)
if v, ok := ls[key]; !ok {
t.Error("underlying map in Links should have value associated with key after Links.Add")
} else if actual, ok := v.(*jsonapi.Link); !ok {
t.Error("unexpected value type after Links.Add, expected *Link")
} else if actual != expected {
t.Errorf("unexpected value after Links.Add, expected: %s, actual: %s", expected, actual)
}
}
func TestLinks_GetString_WhenNil(t *testing.T) {
var ls jsonapi.Links = nil
defer catchPanic(t, "Links", "GetString")
ls.GetString("foo")
}
func TestLinks_GetString(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
expected = "bar"
)
ls = jsonapi.Links{}
if actual, ok := ls.GetString(key); len(actual) > 0 || ok {
t.Error("expected Links.GetString to return empty string/false before Links.AddString for the given key")
}
ls.AddString(key, expected)
if actual, ok := ls.GetString(key); !ok {
t.Error("expected Links.GetString to return true existence check after Links.AddString for the given key")
} else if actual != expected {
t.Errorf("unexpected value after Links.GetString, expected: %s, actual: %s",
expected, actual)
}
}
func TestLinks_GetString_WhenWrongType(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
)
ls.Add(key, &jsonapi.Link{})
_, ok := ls.GetString(key)
if ok {
t.Error("Links.GetString should return empty string/false when the value associated with the key is not of type string")
}
}
func TestLinks_Get_WhenNil(t *testing.T) {
var ls jsonapi.Links = nil
defer catchPanic(t, "Links", "Get")
ls.Get("foo")
}
func TestLinks_Get(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
expected = &jsonapi.Link{}
)
if actual, ok := ls.Get(key); actual != nil || ok {
t.Error("Links.Get should return nil/false when no value is associated with the key")
}
ls.Add(key, expected)
if actual, ok := ls.Get(key); !ok {
t.Error("expected Links.Get to return true existence check after Links.Add for the given key")
} else if actual != expected {
t.Errorf("unexpected value after Links.Get, expected: %s, actual: %s",
expected, actual)
}
}
func TestLinks_Get_WhenWrongType(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
)
ls.AddString(key, "bar")
v, ok := ls.Get(key)
if v != nil || ok {
t.Error("Links.Get should return nil/false when the value associated with the key is not of type *Link")
}
}
func TestLinks_Get_FromMap(t *testing.T) {
var (
ls = jsonapi.Links{}
key = "foo"
expected = &jsonapi.Link{
Href: "http://foo.com",
Meta: map[string]interface{}{
key: "bar",
},
}
)
// manually set map[string]interface{} value on underlying Links map to emulate result of JSON umarshaling
ls[key] = map[string]interface{}{
"href": expected.Href,
"meta": expected.Meta,
}
if actual, ok := ls.Get(key); actual == nil || !ok {
t.Error("expected Links.Get to return *Links/true after calling Links.Get on key with underlying map[string]interface{} value")
} else if !reflect.DeepEqual(actual, expected) {
t.Errorf("expected Links.Get to return correct *Links value, expected: %v, actual: %v", expected, actual)
}
}
func TestLinks_Delete_WhenNil(t *testing.T) {
var ls jsonapi.Links = nil
defer catchPanic(t, "Links", "Delete")
ls.Delete("foo")
}
func TestLinks_Delete(t *testing.T) {
var (
ls = jsonapi.Links{}
strKey = "foo"
objKey = "bar"
)
ls.AddString(strKey, "")
ls.Delete(strKey)
if actual, ok := ls.GetString(strKey); len(actual) > 0 || ok {
t.Error("Links.GetString should return empty string/false after calling Links.Delete for the given key")
}
ls.Add(objKey, &jsonapi.Link{})
ls.Delete(objKey)
if actual, ok := ls.Get(objKey); actual != nil || ok {
t.Error("Links.Get should return nil/false after calling Links.Delete for the given key")
}
}