github.com/ipfans/trojan-go@v0.11.0/log/golog/buffer/buffer_test.go (about) 1 // Buffer-like byte slice 2 // Copyright (c) 2017 Fadhli Dzil Ikram 3 // 4 // Test file for buffer 5 6 package buffer 7 8 import ( 9 "testing" 10 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 func TestBufferAllocation(t *testing.T) { 15 Convey("Given new unallocated buffer", t, func() { 16 var buf Buffer 17 18 Convey("When appended with data", func() { 19 data := []byte("Hello") 20 buf.Append(data) 21 22 Convey("It should have same content as the original data", func() { 23 So(buf.Bytes(), ShouldResemble, data) 24 }) 25 }) 26 27 Convey("When appended with single byte", func() { 28 data := byte('H') 29 buf.AppendByte(data) 30 31 Convey("It should have 1 byte length", func() { 32 So(len(buf), ShouldEqual, 1) 33 }) 34 35 Convey("It should have same content", func() { 36 So(buf.Bytes()[0], ShouldEqual, data) 37 }) 38 }) 39 40 Convey("When appended with integer", func() { 41 data := 12345 42 repr := []byte("012345") 43 buf.AppendInt(data, len(repr)) 44 45 Convey("Should have same content with the integer representation", func() { 46 So(buf.Bytes(), ShouldResemble, repr) 47 }) 48 }) 49 }) 50 } 51 52 func TestBufferReset(t *testing.T) { 53 Convey("Given allocated buffer", t, func() { 54 var buf Buffer 55 data := []byte("Hello") 56 replace := []byte("World") 57 buf.Append(data) 58 59 Convey("When buffer reset", func() { 60 buf.Reset() 61 62 Convey("It should have zero length", func() { 63 So(len(buf), ShouldEqual, 0) 64 }) 65 }) 66 67 Convey("When buffer reset and replaced with another append", func() { 68 buf.Reset() 69 buf.Append(replace) 70 71 Convey("It should have same content with the replaced data", func() { 72 So(buf.Bytes(), ShouldResemble, replace) 73 }) 74 }) 75 }) 76 }