-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathattachment_test.go
67 lines (63 loc) · 1.26 KB
/
attachment_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
package messenger
import (
"encoding/json"
"reflect"
"testing"
"github.com/maciekmm/messenger-platform-go-sdk/template"
)
func TestAttachmentUnmarshalling(t *testing.T) {
//Location
testMarshaller(Attachment{
Type: AttachmentTypeLocation,
Payload: &Location{
Coordinates: Coordinates{
Latitude: 22.2,
Longitude: 10.1,
},
},
}, t)
//Resource
testMarshaller(Attachment{
Type: AttachmentTypeAudio,
Payload: &Resource{
URL: "http://example.com/example.mp3",
},
}, t)
//Template
att := Attachment{
Type: AttachmentTypeTemplate,
Payload: &template.Payload{
Elements: []template.Template{
template.GenericTemplate{
Title: "abc",
},
},
},
}
bytes, err := json.Marshal(&att)
if err != nil {
t.Error(err)
}
dest := Attachment{}
err = json.Unmarshal(bytes, &dest)
if err != nil {
t.Error(err)
}
if _, ok := dest.Payload.(json.RawMessage); !ok {
t.Error("Invalid template attachment unmarshalling")
}
}
func testMarshaller(att Attachment, t *testing.T) {
bytes, err := json.Marshal(&att)
if err != nil {
t.Error(err)
}
dest := Attachment{}
err = json.Unmarshal(bytes, &dest)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(dest, att) {
t.Error("Source and destination do not match.", att, dest)
}
}