tlog.app/go/tlog@v0.23.1/tlwire/encoder_value_test.go (about) 1 package tlwire 2 3 import ( 4 "testing" 5 6 "github.com/nikandfor/assert" 7 "github.com/nikandfor/hacked/low" 8 ) 9 10 type testEncoder struct { 11 N int 12 } 13 14 func (t testEncoder) TlogAppend(b []byte) []byte { 15 return (Encoder{}).AppendInt(b, t.N) 16 } 17 18 func TestEncoderValueCustomEncoders(t *testing.T) { 19 var b low.Buf 20 var e Encoder 21 22 b = e.AppendValue(b[:0], testEncoder{N: 1}) 23 assert.Equal(t, low.Buf{Int | 1}, b) 24 25 SetEncoder(testEncoder{}, func(e *Encoder, b []byte, val interface{}) []byte { 26 return e.AppendInt(b, val.(testEncoder).N+1) 27 }) 28 29 b = e.AppendValue(b[:0], testEncoder{N: 1}) 30 assert.Equal(t, low.Buf{Int | 2}, b) 31 32 e.SetEncoder(testEncoder{}, func(e *Encoder, b []byte, val interface{}) []byte { 33 return e.AppendInt(b, val.(testEncoder).N+2) 34 }) 35 36 b = e.AppendValue(b[:0], testEncoder{N: 1}) 37 assert.Equal(t, low.Buf{Int | 3}, b) 38 39 b = (&Encoder{}).AppendValue(b[:0], testEncoder{N: 1}) 40 assert.Equal(t, low.Buf{Int | 2}, b) 41 42 SetEncoder(testEncoder{}, nil) 43 44 b = (&Encoder{}).AppendValue(b[:0], testEncoder{N: 1}) 45 assert.Equal(t, low.Buf{Int | 1}, b) 46 }