-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
66 lines (60 loc) · 1.57 KB
/
decode_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
package bencode
import (
"reflect"
"testing"
)
func TestDecodeNumeric(t *testing.T) {
input := "i-42e"
expected := -42
decoded, _ := Decode([]byte(input))
value, _ := decoded.(int)
if value != expected {
t.Fatalf("%d != %d", value, expected)
}
}
func TestDecodeFailed(t *testing.T) {
input := "-42e"
_, err := Decode([]byte(input))
if err.Error() != "Invalid data: Missing delimiter ':'" {
t.Fatalf("%s != %s", err.Error(), "Invalid data: Missing delimiter ':'")
}
}
func TestDecodeStrin(t *testing.T) {
input := "4:spam"
expected := "spam"
decoded, _ := Decode([]byte(input))
value, _ := decoded.([]byte)
if string(value) != expected {
t.Fatalf("%s != %s", string(value), expected)
}
}
func TestDecodeList(t *testing.T) {
input := "l4:spami42ee"
expected1 := "spam"
var expected2 int64 = 42
decoded, _ := Decode([]byte(input))
value, _ := decoded.([]interface{})
n1 := string(reflect.ValueOf(value[0]).Bytes())
n2 := reflect.ValueOf(value[1]).Int()
if n1 != expected1 {
t.Fatalf("%s != %s", n1, expected1)
}
if n2 != expected2 {
t.Fatalf("%d != %d", n2, expected2)
}
}
func TestDecodeDirectory(t *testing.T) {
input := "d3:bar4:spam3:fooi42ee"
decoded, _ := Decode([]byte(input))
value, _ := decoded.(map[string]interface{})
expected1 := "spam"
var expected2 int64 = 42
n1 := string(reflect.ValueOf(value["bar"]).Bytes())
n2 := reflect.ValueOf(value["foo"]).Int()
if n1 != expected1 {
t.Fatalf("%s != %s", n1, expected1)
}
if n2 != expected2 {
t.Fatalf("%d != %d", n2, expected2)
}
}