@@ -9,6 +9,64 @@ import (
99type testStringEnum string
1010type testIntEnum int
1111
12+ func TestProtocolBufferFromValue (t * testing.T ) {
13+ type unsupported struct {}
14+ intVal := 42
15+ strVal := "hello"
16+
17+ tests := []struct {
18+ name string
19+ value interface {}
20+ wantLen int
21+ wantType byte
22+ wantError bool
23+ }{
24+ {"nil" , nil , 1 , CMD_NULL , false },
25+ {"string" , "hello" , 1 , CMD_ZEROSTRING , false },
26+ {"int" , int (42 ), 1 , CMD_INT , false },
27+ {"int8" , int8 (8 ), 1 , CMD_INT , false },
28+ {"int16" , int16 (16 ), 1 , CMD_INT , false },
29+ {"int32" , int32 (32 ), 1 , CMD_INT , false },
30+ {"int64" , int64 (64 ), 1 , CMD_INT , false },
31+ {"uint" , uint (1 ), 1 , CMD_INT , false },
32+ {"uint8" , uint8 (1 ), 1 , CMD_INT , false },
33+ {"uint16" , uint16 (1 ), 1 , CMD_INT , false },
34+ {"uint32" , uint32 (1 ), 1 , CMD_INT , false },
35+ {"uint64" , uint64 (1 ), 1 , CMD_INT , false },
36+ {"float32" , float32 (3.14 ), 1 , CMD_FLOAT , false },
37+ {"float64" , float64 (2.71 ), 1 , CMD_FLOAT , false },
38+ {"[]byte" , []byte ("blob" ), 2 , CMD_BLOB , false },
39+ {"bool true" , true , 1 , CMD_INT , false },
40+ {"bool false" , false , 1 , CMD_INT , false },
41+ {"*int" , & intVal , 1 , CMD_INT , false },
42+ {"*string" , & strVal , 1 , CMD_ZEROSTRING , false },
43+ {"*int nil" , (* int )(nil ), 1 , CMD_NULL , false },
44+ {"*string nil" , (* string )(nil ), 1 , CMD_NULL , false },
45+ {"unsupported" , unsupported {}, 0 , 0 , true },
46+ }
47+
48+ for _ , tt := range tests {
49+ t .Run (tt .name , func (t * testing.T ) {
50+ buffers , err := protocolBufferFromValue (tt .value )
51+ if tt .wantError {
52+ if err == nil {
53+ t .Fatalf ("expected error, got nil" )
54+ }
55+ return
56+ }
57+ if err != nil {
58+ t .Fatalf ("unexpected error: %v" , err )
59+ }
60+ if len (buffers ) != tt .wantLen {
61+ t .Fatalf ("got %d buffers, want %d" , len (buffers ), tt .wantLen )
62+ }
63+ if tt .wantLen > 0 && buffers [0 ][0 ] != tt .wantType {
64+ t .Fatalf ("got first buffer type %q, want %q" , buffers [0 ][0 ], tt .wantType )
65+ }
66+ })
67+ }
68+ }
69+
1270func TestProtocolBufferFromValueSupportsStringAlias (t * testing.T ) {
1371 val := testStringEnum ("active" )
1472 buffers , err := protocolBufferFromValue (val )
@@ -68,3 +126,33 @@ func TestProtocolBufferFromValueUnsupportedTypeReturnsError(t *testing.T) {
68126 t .Fatalf ("expected error for unsupported type" )
69127 }
70128}
129+
130+ func TestProtocolBufferFromValueMixedArrayNoSilentDrops (t * testing.T ) {
131+ pInt := 99
132+ values := []interface {}{
133+ "hello" ,
134+ int (42 ),
135+ nil ,
136+ & pInt ,
137+ float64 (3 ),
138+ uint (7 ),
139+ []byte ("x" ),
140+ true ,
141+ }
142+
143+ buffers := [][]byte {}
144+ for i , v := range values {
145+ valueBuffers , err := protocolBufferFromValue (v )
146+ if err != nil {
147+ t .Fatalf ("unexpected error at index %d (%T): %v" , i , v , err )
148+ }
149+ if len (valueBuffers ) == 0 {
150+ t .Fatalf ("value at index %d produced zero buffers" , i )
151+ }
152+ buffers = append (buffers , valueBuffers ... )
153+ }
154+
155+ if len (buffers ) < len (values ) {
156+ t .Fatalf ("got %d total buffers, expected at least %d" , len (buffers ), len (values ))
157+ }
158+ }
0 commit comments