Add bencode tests for arrays and maps

This commit is contained in:
Justin Li 2014-07-06 17:47:32 -04:00
parent dd5e8e460f
commit e1575d1fa3

View file

@ -9,24 +9,39 @@ import (
"time" "time"
) )
var scalarTests = map[interface{}]string{ var tests = []struct {
int(42): "i42e", input interface{}
int(-42): "i-42e", expected string
uint(42): "i42e", }{
int64(42): "i42e", {int(42), "i42e"},
uint64(42): "i42e", {int(-42), "i-42e"},
{uint(43), "i43e"},
{int64(44), "i44e"},
{uint64(45), "i45e"},
"example": "7:example", {"example", "7:example"},
30 * time.Minute: "i1800e", {30 * time.Minute, "i1800e"},
{[]string{"one", "two"}, "l3:one3:twoe"},
{[]string{}, "le"},
{
map[string]interface{}{
"one": "aa",
"two": "bb",
},
"d3:one2:aa3:two2:bbe",
},
{map[string]interface{}{}, "de"},
} }
func TestScalar(t *testing.T) { func TestMarshal(t *testing.T) {
for val, expected := range scalarTests { for _, test := range tests {
got, err := Marshal(val) got, err := Marshal(test.input)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else if string(got) != expected { } else if string(got) != test.expected {
t.Errorf("\ngot: %s\nexpected: %s", got, expected) t.Errorf("\ngot: %s\nexpected: %s", got, test.expected)
} }
} }
} }