This repository has been archived by the owner on Nov 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshalling_test.go
64 lines (58 loc) · 1.52 KB
/
marshalling_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
package bencode
import (
"reflect"
"testing"
)
type mtestcase struct {
in interface{}
out string
}
func TestMarshal(t *testing.T) {
tests := []mtestcase{
mtestcase{2, "i2e"},
mtestcase{"fart", "4:fart"},
mtestcase{[]string{"fart", "butt"}, "l4:fart4:butte"},
mtestcase{struct{ A string }{A: "butt"}, "d1:A4:butte"},
}
for _, test := range tests {
out := string(Marshal(test.in))
if out != test.out {
t.Error("Expecting", test.out, "got", out)
}
}
}
func TestConvertSlice(t *testing.T) {
type twofield struct {
A int
B string
}
tests := []mtestcase{
mtestcase{[]int{2, 42, 666}, "li2ei42ei666ee"},
mtestcase{[]string{"fart", "butt"}, "l4:fart4:butte"},
mtestcase{[][]int{[]int{2}, []int{42}}, "lli2eeli42eee"},
mtestcase{
[]twofield{twofield{420, "blaze it"}, twofield{360, "no scope"}},
"ld1:Ai420e1:B8:blaze ited1:Ai360e1:B8:no scopeee",
},
}
for _, test := range tests {
out := string(convertSlice(reflect.ValueOf(test.in)))
if out != test.out {
t.Error("Expecting", test.out, "got", out)
}
}
}
func TestConvertDict(t *testing.T) {
tests := []mtestcase{
mtestcase{struct{ A string }{A: "butt"}, "d1:A4:butte"},
mtestcase{struct{ A []int }{A: []int{42, 666}}, "d1:Ali42ei666eee"},
mtestcase{struct{ A struct{ A string } }{A: struct{ A string }{A: "butt"}}, "d1:Ad1:A4:buttee"},
mtestcase{struct{ a int }{1}, "de"},
}
for _, test := range tests {
out := string(convertDict(reflect.ValueOf(test.in)))
if out != test.out {
t.Error("Expecting", test.out, "got", out)
}
}
}