github.com/puellanivis/breton@v0.2.16/lib/mpeg/ts/pes/writer_test.go (about) 1 package pes 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 ) 8 9 func TestMarshal(t *testing.T) { 10 b := new(bytes.Buffer) 11 12 wr := &Writer{ 13 dst: b, 14 Stream: Stream{ 15 ID: 0x42, 16 }, 17 } 18 19 input := []byte("Hello World") 20 21 expected := []byte{ 22 0, 0, 1, 23 0x42, 24 0, byte(3 + len(input)), 25 0x80, 0x00, 0x00, 26 } 27 expected = append(expected, input...) 28 expectedWriteLength := len(input) 29 30 hlen, err := wr.HeaderLength() 31 if err != nil { 32 t.Fatalf("%+v", err) 33 } 34 35 if hlen != 9 { 36 t.Errorf("expected HeaderLength of 9 bytes, but got %d", hlen) 37 } 38 39 n, err := wr.Write(input) 40 if err != nil { 41 t.Fatalf("%+v", err) 42 } 43 44 if n != expectedWriteLength { 45 t.Errorf("expected to write %d bytes, but wrote %d", expectedWriteLength, n) 46 } 47 48 output := b.Bytes() 49 if !reflect.DeepEqual(output, expected) { 50 t.Errorf("expected to write %v, but wrote %v", expected, output) 51 } 52 53 // Here, we’re checking minimally that a second write does the same expected thing, but twice. 54 expected = append(expected, expected...) 55 56 n, err = wr.Write(input) 57 if err != nil { 58 t.Fatalf("%+v", err) 59 } 60 61 if n != expectedWriteLength { 62 t.Errorf("expected to write %d bytes, but wrote %d", expectedWriteLength, n) 63 } 64 65 output = b.Bytes() 66 if !reflect.DeepEqual(output, expected) { 67 t.Errorf("expected to write %v, but wrote %v", expected, output) 68 } 69 }