From 38481fb37982cf7cacf11ce020a7196d43e72b53 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 15 Dec 2018 15:18:57 +0100 Subject: unmarshal arrays --- bencode/bdecode.go | 7 +++++++ bencode/bencode_test.go | 34 +++++++++++++++++++++++++++++----- bitfield/bitfield_test.go | 4 ++-- meta/torrent_test.go | 2 +- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/bencode/bdecode.go b/bencode/bdecode.go index 67f37b1..14ed78a 100644 --- a/bencode/bdecode.go +++ b/bencode/bdecode.go @@ -123,6 +123,13 @@ func (d *decodeState) unmarshalString(v reflect.Value) error { switch v.Kind() { case reflect.Slice: v.SetBytes([]byte(s)) + case reflect.Array: + if len(s) != v.Len() { + return ErrValue + } + for i := 0; i < v.Len(); i++ { + v.Index(i).Set(reflect.ValueOf(s[i])) + } default: v.SetString(s) } diff --git a/bencode/bencode_test.go b/bencode/bencode_test.go index a194fb6..2120654 100644 --- a/bencode/bencode_test.go +++ b/bencode/bencode_test.go @@ -32,7 +32,7 @@ func TestParseString(t *testing.T) { in := "4:testZZZ" s, l := parseString([]byte(in)) if s != "test" || l != 6 { - t.Error("expected test, got", s) + t.Error("want test, got", s) } } @@ -40,7 +40,7 @@ func TestParseInt(t *testing.T) { in := "i12345e999" i, l := parseInt([]byte(in)) if i != 12345 || l != 7 { - t.Error("expected 12345, got", i) + t.Error("want 12345, got", i) } } @@ -77,7 +77,7 @@ func TestUnmarshal(t *testing.T) { } h := hex.EncodeToString(tor.InfoHash) if h != tc.InfoHash { - t.Error("got", h, "expected", tc.InfoHash) + t.Error("got", h, "want", tc.InfoHash) } } } @@ -109,7 +109,7 @@ func TestUnmarshalBool(t *testing.T) { t.Error(err) } if b.A != true { - t.Error("expected true, got", b.A) + t.Error("want true, got", b.A) } } @@ -121,6 +121,30 @@ func TestUnmarshalInt64(t *testing.T) { t.Error(err) } if b.A != 42 { - t.Error("expected true, got", b.A) + t.Error("want true, got", b.A) + } +} + +func TestUnmarshalSlice(t *testing.T) { + var b []byte + in := `4:test` + _, err := Unmarshal([]byte(in), &b) + if err != nil { + t.Error(err) + } + if string(b) != "test" { + t.Error("want test, got", string(b)) + } +} + +func TestUnmarshalArray(t *testing.T) { + var b [4]byte + in := `4:test` + _, err := Unmarshal([]byte(in), &b) + if err != nil { + t.Error(err) + } + if string(b[:]) != "test" { + t.Error("want test, got", string(b[:])) } } diff --git a/bitfield/bitfield_test.go b/bitfield/bitfield_test.go index 4b7218a..685881c 100644 --- a/bitfield/bitfield_test.go +++ b/bitfield/bitfield_test.go @@ -6,11 +6,11 @@ func TestNew(t *testing.T) { bf := New(63) bf.Set(7) if !bf.At(7) { - t.Error("expected set") + t.Error("want set, got not") } bf.Clear(7) if bf.At(7) { - t.Error("expected cleared") + t.Error("want cleared, got not") } t.Log("len:", len(bf), bf, bf.At(7)) } diff --git a/meta/torrent_test.go b/meta/torrent_test.go index ac54b95..f03142f 100644 --- a/meta/torrent_test.go +++ b/meta/torrent_test.go @@ -52,7 +52,7 @@ func TestReadAt(t *testing.T) { t.Log(err) } if n != len(buf) { - t.Error("expected", len(buf), "got", n) + t.Error("want", len(buf), "got", n) } //t.Logf("% x\n", buf) } -- cgit v1.2.3