-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmarshal_test.go
150 lines (122 loc) · 2.83 KB
/
unmarshal_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
package bencoding
import (
"testing"
)
func Test_UnMarshal_Nil_Src(t *testing.T) {
if err := UnMarshal(nil, nil); err == nil {
t.Fatal("cannot unmarshal nil src")
}
}
func Test_Unmarshal_Invalid_Src(t *testing.T) {
if err := UnMarshal("string", nil); err == nil {
t.Fatal("cannot unmarshal invalid src")
}
}
func Test_UnMarshal_Nil_Dst(t *testing.T) {
if err := UnMarshal([]byte("asds"), nil); err == nil {
t.Fatal("cannot unmarshal nil dst")
}
}
func Test_UnMarshal_Invalid_Dst(t *testing.T) {
type test struct {
input interface{}
}
strdst := ""
if err := UnMarshal([]byte("3:cat"), strdst); err == nil {
t.Fatal("cannot unmarshal into string")
}
}
func Test_UnMarshal_String_Dst(t *testing.T) {
strdst := ""
if err := UnMarshal([]byte("3:cat"), &strdst); err != nil {
t.Fatal(err.Error())
}
if strdst != "cat" {
t.Fatalf("wanted cat got %s", strdst)
}
}
func Test_UnMarshal_Int_Dst(t *testing.T) {
intdst := 0
if err := UnMarshal([]byte("i12222222e"), &intdst); err != nil {
t.Fatal(err.Error())
}
if intdst != 12222222 {
t.Fatalf("wanted 12222222 got %d", intdst)
}
}
func Test_UnMarshal_List_Dst(t *testing.T) {
listdst := []interface{}{}
if err := UnMarshal([]byte("l4:coole"), &listdst); err != nil {
t.Fatal(err.Error())
}
if listdst[0] != "cool" {
t.Fatal("invalid marshalled list")
}
}
func Test_UnMarshal_Dict_Dst(t *testing.T) {
dict := map[string]interface{}{
"info": []interface{}{
map[string]interface{}{
"id": 1,
"ip": "192.168.0.1",
"peers": []interface{}{"a", "b", "c"},
},
map[string]interface{}{
"id": 2,
"ip": "192.168.0.2",
"peers": []interface{}{"d", "e", "f"},
},
map[string]interface{}{
"id": 3,
"ip": "192.168.0.3",
"peers": []interface{}{"g", "h", "i"},
},
map[string]interface{}{
"id": 4,
"ip": "192.168.0.4",
"peers": []interface{}{"j", "k", "l"},
},
map[string]interface{}{
"id": 5,
"ip": "192.168.0.5",
"peers": []interface{}{"m", "n", "o"},
},
},
}
bs, err := Marshal(dict)
if err != nil {
t.Fatal(err.Error())
}
dst := make(map[string]interface{})
if err := UnMarshal(bs, dst); err != nil {
t.Fatal(err.Error())
}
var list []interface{}
switch r := dst["info"].(type) {
case []interface{}:
list = r
case nil:
t.Fatal("dst was nil")
default:
t.Fatal("invalid type for dst")
}
if len(list) != 5 {
t.Fatal("invalid length of list")
}
first := list[0].(map[string]interface{})
last := list[4].(map[string]interface{})
if first["id"] != 1 {
t.Fatal("invalid result dict")
}
if last["ip"] != "192.168.0.5" {
t.Fatal("invalid result dict")
}
lastIps := last["peers"].([]interface{})
if len(lastIps) != 3 {
t.Fatal("invalid result dict")
}
peer := lastIps[0].(string)
if peer != "m" {
t.Fatal("invalid result dict")
}
}