-
Notifications
You must be signed in to change notification settings - Fork 12
/
link_test.go
83 lines (63 loc) · 1.97 KB
/
link_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
package jsh
import (
"encoding/json"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestLink(t *testing.T) {
Convey("Link Tests", t, func() {
Convey("->MarshalJSON()", func() {
Convey("should marshal as empty string", func() {
l := NewLink("")
jData, err := json.Marshal(l)
So(err, ShouldBeNil)
So(string(jData), ShouldEqual, `""`)
})
Convey("should marshal as string when no metadata is present", func() {
l := NewLink("/stringlink")
jData, err := json.Marshal(&l)
So(err, ShouldBeNil)
So(string(jData), ShouldEqual, `"/stringlink"`)
})
Convey("should marshal as an object when empty metadata is present", func() {
l := NewMetaLink("/metalink", make(map[string]interface{}))
jData, err := json.Marshal(&l)
So(err, ShouldBeNil)
So(string(jData), ShouldEqual, `{"href":"/metalink"}`)
})
Convey("should marshal as an object when metadata is present", func() {
meta := make(map[string]interface{})
meta["count"] = 10
l := NewMetaLink("/metalink", meta)
jData, err := json.Marshal(&l)
So(err, ShouldBeNil)
So(string(jData), ShouldEqual, `{"href":"/metalink","meta":{"count":10}}`)
})
})
Convey("->UnmarshalJSON()", func() {
Convey("should handle a string link", func() {
jStr := `"/stringlink"`
l := Link{}
err := l.UnmarshalJSON([]byte(jStr))
So(err, ShouldBeNil)
So(l.HREF, ShouldEqual, "/stringlink")
})
Convey("should handle a meta link", func() {
jMeta := `{"href": "/metalink"}`
l := Link{}
err := l.UnmarshalJSON([]byte(jMeta))
So(err, ShouldBeNil)
So(l.HREF, ShouldEqual, "/metalink")
})
Convey("should handle a meta link with metadata", func() {
jMeta := `{"href": "/metalink", "meta": {"count": 10}}`
l := Link{}
err := l.UnmarshalJSON([]byte(jMeta))
So(err, ShouldBeNil)
So(l.HREF, ShouldEqual, "/metalink")
So(l.Meta, ShouldNotBeEmpty)
So(l.Meta["count"], ShouldEqual, 10)
})
})
})
}