github.com/blend/go-sdk@v1.20220411.3/bufferutil/buffer_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package bufferutil 9 10 import ( 11 "encoding/json" 12 "io" 13 "testing" 14 "time" 15 16 "github.com/blend/go-sdk/assert" 17 ) 18 19 func justError(_ interface{}, err error) error { 20 return err 21 } 22 23 func TestOutputBuffer(t *testing.T) { 24 assert := assert.New(t) 25 26 lw := new(Buffer) 27 28 assert.Nil(justError(io.WriteString(lw, "this is a test\n"))) 29 assert.Nil(justError(io.WriteString(lw, "this is another test\n"))) 30 assert.Nil(justError(io.WriteString(lw, "this is a test\n"))) 31 assert.Nil(justError(io.WriteString(lw, "this is another test\n"))) 32 assert.Nil(justError(io.WriteString(lw, "this is another test\r\nand another\n"))) 33 assert.Len(lw.Chunks, 5) 34 } 35 36 func TestOutputBufferShadowed(t *testing.T) { 37 assert := assert.New(t) 38 39 ob := new(Buffer) 40 41 lines := [][]byte{ 42 []byte("this is a test"), 43 []byte("this is another test"), 44 } 45 46 for _, buf := range lines { 47 assert.Nil(justError(ob.Write(buf))) 48 } 49 assert.Len(ob.Chunks, 2) 50 assert.Equal("this is a test", string(ob.Chunks[0].Data)) 51 assert.Equal("this is another test", string(ob.Chunks[1].Data)) 52 } 53 54 func TestOutputBufferWritten(t *testing.T) { 55 assert := assert.New(t) 56 57 ob := new(Buffer) 58 59 written, err := ob.Write([]byte("this is just a test")) 60 assert.Nil(err) 61 assert.Equal(19, written) 62 assert.Len(ob.Chunks, 1) 63 64 written, err = ob.Write([]byte("this is just a test\n")) 65 assert.Nil(err) 66 assert.Equal(20, written) 67 assert.Len(ob.Chunks, 2) 68 assert.Equal("this is just a testthis is just a test\n", ob.String()) 69 } 70 71 func TestOutputChunkJSON(t *testing.T) { 72 assert := assert.New(t) 73 74 chunk := BufferChunk{ 75 Timestamp: time.Date(2019, 9, 21, 12, 11, 10, 9, time.UTC), 76 Data: []byte("this is just a test"), 77 } 78 79 jsonContents, err := json.Marshal(chunk) 80 assert.Nil(err) 81 assert.NotEmpty(jsonContents) 82 83 var verify BufferChunk 84 assert.Nil(json.Unmarshal(jsonContents, &verify)) 85 assert.Equal(chunk.Timestamp, verify.Timestamp) 86 assert.Equal(chunk.Data, verify.Data) 87 }