9 "github.com/stretchr/testify/require"
12 type random_decode_test struct {
17 var random_decode_tests = []random_decode_test{
19 {"i-9223372036854775808e", int64(-9223372036854775808)},
21 {"29:unicode test проверка", "unicode test проверка"},
22 {"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
23 {"li5ei10ei15ei20e7:bencodee",
24 []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}},
25 {"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
26 {"le", []interface{}{}},
29 func TestRandomDecode(t *testing.T) {
30 for _, test := range random_decode_tests {
32 err := Unmarshal([]byte(test.data), &value)
34 t.Error(err, test.data)
37 if !reflect.DeepEqual(test.expected, value) {
38 t.Errorf("got: %v (%T), expected: %v (%T)\n",
39 value, value, test.expected, test.expected)
44 func TestLoneE(t *testing.T) {
46 err := Unmarshal([]byte("e"), &v)
47 se := err.(*SyntaxError)
48 require.EqualValues(t, 0, se.Offset)
51 func TestDecoderConsecutive(t *testing.T) {
52 d := NewDecoder(bytes.NewReader([]byte("i1ei2e")))
55 require.NoError(t, err)
56 require.EqualValues(t, 1, i)
58 require.NoError(t, err)
59 require.EqualValues(t, 2, i)
61 require.Equal(t, io.EOF, err)
64 func check_error(t *testing.T, err error) {
70 func assert_equal(t *testing.T, x, y interface{}) {
71 if !reflect.DeepEqual(x, y) {
72 t.Errorf("got: %v (%T), expected: %v (%T)\n", x, x, y, y)
76 type unmarshaler_int struct {
80 func (this *unmarshaler_int) UnmarshalBencode(data []byte) error {
81 return Unmarshal(data, &this.x)
84 type unmarshaler_string struct {
88 func (this *unmarshaler_string) UnmarshalBencode(data []byte) error {
93 func TestUnmarshalerBencode(t *testing.T) {
95 var ss []unmarshaler_string
96 check_error(t, Unmarshal([]byte("i71e"), &i))
97 assert_equal(t, i.x, 71)
98 check_error(t, Unmarshal([]byte("l5:hello5:fruit3:waye"), &ss))
99 assert_equal(t, ss[0].x, "5:hello")
100 assert_equal(t, ss[1].x, "5:fruit")
101 assert_equal(t, ss[2].x, "3:way")