@@ -24,7 +24,10 @@ import {
24
24
import * as edition2023_ts from "./gen/ts/extra/edition2023_pb.js" ;
25
25
import * as edition2023_proto2_ts from "./gen/ts/extra/edition2023-proto2_pb.js" ;
26
26
import * as edition2023_proto3_ts from "./gen/ts/extra/edition2023-proto3_pb.js" ;
27
- import { Edition2023MapEncodingMessageDesc } from "./gen/ts/extra/edition2023-map-encoding_pb.js" ;
27
+ import {
28
+ Edition2023MapEncodingMessage_ChildDesc ,
29
+ Edition2023MapEncodingMessageDesc ,
30
+ } from "./gen/ts/extra/edition2023-map-encoding_pb.js" ;
28
31
import { BinaryReader , BinaryWriter , WireType } from "@bufbuild/protobuf/wire" ;
29
32
30
33
describe ( "edition2023 serialization" , ( ) => {
@@ -213,26 +216,40 @@ describe("edition2023 serialization", () => {
213
216
describe ( "message_encoding DELIMITED with maps" , ( ) => {
214
217
test ( "should round-trip" , ( ) => {
215
218
const a = create ( Edition2023MapEncodingMessageDesc ) ;
216
- a . mapField [ 123 ] = true ;
219
+ a . stringMap [ 123 ] = "abc" ;
217
220
const bytes = toBinary ( Edition2023MapEncodingMessageDesc , a ) ;
218
221
const b = fromBinary ( Edition2023MapEncodingMessageDesc , bytes ) ;
219
222
expect ( b ) . toStrictEqual ( a ) ;
220
223
} ) ;
221
- test ( "should expect LENGTH_PREFIXED" , ( ) => {
224
+ test ( "should expect LENGTH_PREFIXED map entry " , ( ) => {
222
225
const w = new BinaryWriter ( ) ;
223
226
w . tag ( 77 , WireType . LengthDelimited ) ;
224
- w . uint32 ( 4 ) ;
227
+ w . fork ( ) ;
228
+ w . tag ( 1 , WireType . Varint ) . int32 ( 123 ) ;
229
+ w . tag ( 2 , WireType . Varint ) . string ( "abc" ) ;
230
+ w . join ( ) ;
231
+ const bytes = w . finish ( ) ;
232
+ const msg = fromBinary ( Edition2023MapEncodingMessageDesc , bytes ) ;
233
+ expect ( msg . stringMap ) . toStrictEqual ( {
234
+ 123 : "abc" ,
235
+ } ) ;
236
+ } ) ;
237
+ test ( "should expect LENGTH_PREFIXED map value message" , ( ) => {
238
+ const w = new BinaryWriter ( ) ;
239
+ w . tag ( 88 , WireType . LengthDelimited ) ;
240
+ w . fork ( ) ;
225
241
w . tag ( 1 , WireType . Varint ) . int32 ( 123 ) ;
226
- w . tag ( 2 , WireType . Varint ) . bool ( true ) ;
242
+ w . tag ( 2 , WireType . LengthDelimited ) . fork ( ) . join ( ) ;
243
+ w . join ( ) ;
227
244
const bytes = w . finish ( ) ;
228
245
const msg = fromBinary ( Edition2023MapEncodingMessageDesc , bytes ) ;
229
- expect ( msg . mapField ) . toStrictEqual ( {
230
- 123 : true ,
246
+ expect ( msg . messageMap ) . toStrictEqual ( {
247
+ 123 : create ( Edition2023MapEncodingMessage_ChildDesc ) ,
231
248
} ) ;
232
249
} ) ;
233
- test ( "should serialize LENGTH_PREFIXED" , ( ) => {
250
+ test ( "should serialize map entry LENGTH_PREFIXED" , ( ) => {
234
251
const msg = create ( Edition2023MapEncodingMessageDesc ) ;
235
- msg . mapField [ 123 ] = true ;
252
+ msg . stringMap [ 123 ] = "abc" ;
236
253
const bytes = toBinary ( Edition2023MapEncodingMessageDesc , msg ) ;
237
254
const r = new BinaryReader ( bytes ) ;
238
255
{
@@ -242,17 +259,43 @@ describe("edition2023 serialization", () => {
242
259
const length = r . uint32 ( ) ;
243
260
expect ( length ) . toBe ( r . len - r . pos ) ;
244
261
}
262
+ {
263
+ const [ number ] = r . tag ( ) ;
264
+ expect ( number ) . toBe ( 1 ) ;
265
+ expect ( r . int32 ( ) ) . toBe ( 123 ) ;
266
+ }
267
+ {
268
+ const [ number ] = r . tag ( ) ;
269
+ expect ( number ) . toBe ( 2 ) ;
270
+ expect ( r . string ( ) ) . toBe ( "abc" ) ;
271
+ }
272
+ {
273
+ expect ( r . pos ) . toBe ( r . len ) ;
274
+ }
275
+ } ) ;
276
+ test ( "should serialize map value message LENGTH_PREFIXED" , ( ) => {
277
+ const msg = create ( Edition2023MapEncodingMessageDesc ) ;
278
+ msg . messageMap [ 123 ] = create ( Edition2023MapEncodingMessage_ChildDesc ) ;
279
+ const bytes = toBinary ( Edition2023MapEncodingMessageDesc , msg ) ;
280
+ const r = new BinaryReader ( bytes ) ;
245
281
{
246
282
const [ number , wireType ] = r . tag ( ) ;
283
+ expect ( number ) . toBe ( 88 ) ;
284
+ expect ( wireType ) . toBe ( WireType . LengthDelimited ) ;
285
+ const length = r . uint32 ( ) ;
286
+ expect ( length ) . toBe ( r . len - r . pos ) ;
287
+ }
288
+ {
289
+ const [ number ] = r . tag ( ) ;
247
290
expect ( number ) . toBe ( 1 ) ;
248
- expect ( wireType ) . toBe ( WireType . Varint ) ;
249
291
expect ( r . int32 ( ) ) . toBe ( 123 ) ;
250
292
}
251
293
{
252
294
const [ number , wireType ] = r . tag ( ) ;
253
295
expect ( number ) . toBe ( 2 ) ;
254
- expect ( wireType ) . toBe ( WireType . Varint ) ;
255
- expect ( r . bool ( ) ) . toBe ( true ) ;
296
+ expect ( wireType ) . toBe ( WireType . LengthDelimited ) ;
297
+ const length = r . uint32 ( ) ;
298
+ expect ( length ) . toBe ( 0 ) ;
256
299
}
257
300
{
258
301
expect ( r . pos ) . toBe ( r . len ) ;
0 commit comments