58
58
import union_vector .Attacker
59
59
import union_vector .Character
60
60
import union_vector .Movie
61
+ import union_vector .Rapunzel
62
+ import union_vector .BookReader
61
63
62
64
63
65
def create_namespace_shortcut (is_onefile ):
@@ -308,27 +310,41 @@ def test_default_values_with_pack_and_unpack(self):
308
310
def test_union_vectors_with_pack_and_unpack (self ):
309
311
b = flatbuffers .Builder (0 )
310
312
313
+ # Types of the characters
314
+ types = [
315
+ union_vector .Character .Character .MuLan ,
316
+ union_vector .Character .Character .Rapunzel ,
317
+ union_vector .Character .Character .Belle ,
318
+ union_vector .Character .Character .BookFan ,
319
+ union_vector .Character .Character .Other ,
320
+ union_vector .Character .Character .Unused ,
321
+ ]
322
+ # Pack the attacker manually
311
323
union_vector .Attacker .Start (b )
312
324
union_vector .Attacker .AddSwordAttackDamage (b , 1 )
313
325
attacker_offset = union_vector .Attacker .End (b )
314
-
315
- union_vector .Attacker .Start (b )
316
- union_vector .Attacker .AddSwordAttackDamage (b , 2 )
317
- attacker_offset2 = union_vector .Attacker .End (b )
318
-
319
- characters = [attacker_offset , attacker_offset2 ]
320
- character_types = [union_vector .Character .Character .MuLan , union_vector .Character .Character .MuLan ]
321
-
322
- union_vector .Movie .StartCharactersTypeVector (b , len (character_types ))
323
- for character_type in reversed (character_types ):
326
+ # Prepare the rest of the characters
327
+ characters = [
328
+ attacker_offset ,
329
+ union_vector .Rapunzel .CreateRapunzel (b , 2 ),
330
+ union_vector .BookReader .CreateBookReader (b , 3 ),
331
+ union_vector .BookReader .CreateBookReader (b , 4 ),
332
+ b .CreateString ("Other" , "utf-8" ),
333
+ b .CreateString ("Unused" , "utf-8" )
334
+ ]
335
+
336
+ # Pack the types
337
+ union_vector .Movie .StartCharactersTypeVector (b , len (types ))
338
+ for character_type in reversed (types ):
324
339
b .PrependByte (character_type )
325
340
character_types_offset = b .EndVector ()
326
-
341
+ # Pack the characters
327
342
union_vector .Movie .StartCharactersVector (b , len (characters ))
328
343
for character in reversed (characters ):
329
344
b .PrependUOffsetTRelative (character )
330
345
characters_offset = b .EndVector ()
331
346
347
+ # Pack the movie object
332
348
union_vector .Movie .Start (b )
333
349
union_vector .Movie .AddMainCharacterType (b , 0 )
334
350
union_vector .Movie .AddMainCharacter (b , 0 )
@@ -337,18 +353,39 @@ def test_union_vectors_with_pack_and_unpack(self):
337
353
movie_offset = union_vector .Movie .End (b )
338
354
b .Finish (movie_offset )
339
355
356
+ # Unpack the movie object
340
357
buf = b .Output ()
341
358
movie = union_vector .Movie .Movie .GetRootAsMovie (buf , 0 )
342
359
343
- self .assertEqual (movie .CharactersTypeLength (), len (character_types ))
344
- self .assertEqual (movie .CharactersLength (), len (characters ))
345
- self .assertEqual (movie .CharactersType (0 ), character_types [0 ])
346
-
347
- character = union_vector .Attacker .Attacker ()
348
- character .Init (movie .Characters (0 ).Bytes , movie .Characters (0 ).Pos )
349
- self .assertEqual (character .SwordAttackDamage (), 1 )
350
- character .Init (movie .Characters (1 ).Bytes , movie .Characters (1 ).Pos )
351
- self .assertEqual (character .SwordAttackDamage (), 2 )
360
+ self .assertEqual (movie .CharactersType (0 ), union_vector .Character .Character .MuLan )
361
+ self .assertEqual (movie .CharactersType (1 ), union_vector .Character .Character .Rapunzel )
362
+ self .assertEqual (movie .CharactersType (2 ), union_vector .Character .Character .Belle )
363
+ self .assertEqual (movie .CharactersType (3 ), union_vector .Character .Character .BookFan )
364
+ self .assertEqual (movie .CharactersType (4 ), union_vector .Character .Character .Other )
365
+ self .assertEqual (movie .CharactersType (5 ), union_vector .Character .Character .Unused )
366
+
367
+ attacker = union_vector .Attacker .Attacker ()
368
+ attacker .Init (movie .Characters (0 ).Bytes , movie .Characters (0 ).Pos )
369
+ self .assertEqual (attacker .SwordAttackDamage (), 1 )
370
+ rapunzel = union_vector .Rapunzel .Rapunzel ()
371
+ rapunzel .Init (movie .Characters (1 ).Bytes , movie .Characters (1 ).Pos )
372
+ self .assertEqual (rapunzel .HairLength (), 2 )
373
+ book_reader = union_vector .BookReader .BookReader ()
374
+ book_reader .Init (movie .Characters (2 ).Bytes , movie .Characters (2 ).Pos )
375
+ self .assertEqual (book_reader .BooksRead (), 3 )
376
+ book_reader .Init (movie .Characters (3 ).Bytes , movie .Characters (3 ).Pos )
377
+ self .assertEqual (book_reader .BooksRead (), 4 )
378
+
379
+ other = union_vector .Character .CharacterCreator (
380
+ union_vector .Character .Character .Other ,
381
+ movie .Characters (4 )
382
+ )
383
+ self .assertEqual (other .decode ("utf-8" ), "Other" )
384
+ unused = union_vector .Character .CharacterCreator (
385
+ union_vector .Character .Character .Unused ,
386
+ movie .Characters (5 )
387
+ )
388
+ self .assertEqual (unused .decode ("utf-8" ), "Unused" )
352
389
353
390
def test_optional_scalars_with_pack_and_unpack (self ):
354
391
""" Serializes and deserializes between a buffer with optional values (no
0 commit comments