github.com/anacrolix/torrent@v1.61.0/bencode/bytes_test.go (about)

     1  package bencode
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/go-quicktest/qt"
     7  )
     8  
     9  func TestBytesMarshalNil(t *testing.T) {
    10  	var b Bytes
    11  	Marshal(b)
    12  }
    13  
    14  type structWithBytes struct {
    15  	A Bytes
    16  	B Bytes
    17  }
    18  
    19  func TestMarshalNilStructBytes(t *testing.T) {
    20  	_, err := Marshal(structWithBytes{B: Bytes("i42e")})
    21  	qt.Assert(t, qt.IsNotNil(err))
    22  }
    23  
    24  type structWithOmitEmptyBytes struct {
    25  	A Bytes `bencode:",omitempty"`
    26  	B Bytes `bencode:",omitempty"`
    27  }
    28  
    29  func TestMarshalNilStructBytesOmitEmpty(t *testing.T) {
    30  	b, err := Marshal(structWithOmitEmptyBytes{B: Bytes("i42e")})
    31  	qt.Assert(t, qt.IsNil(err))
    32  	t.Logf("%q", b)
    33  	var s structWithBytes
    34  	err = Unmarshal(b, &s)
    35  	qt.Assert(t, qt.IsNil(err))
    36  	qt.Check(t, qt.DeepEquals(s.B, Bytes("i42e")))
    37  }