encode_test.go (955B)
1 package gojay 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type TestWriterError string 11 12 func (t TestWriterError) Write(b []byte) (int, error) { 13 return 0, errors.New("Test Error") 14 } 15 16 func TestAppendBytes(t *testing.T) { 17 b := []byte(``) 18 enc := NewEncoder(nil) 19 enc.buf = b 20 enc.AppendBytes([]byte(`true`)) 21 assert.Equal(t, string(enc.buf), `true`, "string(enc.buf) should equal to true") 22 } 23 24 func TestAppendByte(t *testing.T) { 25 b := []byte(``) 26 enc := NewEncoder(nil) 27 enc.buf = b 28 enc.AppendByte(1) 29 assert.Equal(t, enc.buf[0], uint8(0x1), "b[0] should equal to 1") 30 } 31 32 func TestAppendString(t *testing.T) { 33 b := []byte(``) 34 enc := NewEncoder(nil) 35 enc.buf = b 36 enc.AppendString("true") 37 assert.Equal(t, string(enc.buf), `"true"`, "string(enc.buf) should equal to true") 38 } 39 40 func TestBuf(t *testing.T) { 41 b := []byte(`test`) 42 enc := NewEncoder(nil) 43 enc.buf = b 44 assert.Equal(t, b, enc.Buf(), "enc.Buf() should equal to b") 45 }