@@ -2,28 +2,96 @@ var tape = require("tape");
2
2
3
3
var protobuf = require ( ".." ) ;
4
4
5
- var proto1 = "message Test {\
5
+ var packedOption = "message Test {\
6
6
repeated uint32 a = 1 [packed = true];\
7
7
}" ;
8
8
9
- var proto2 = "message Test {\
9
+ var unpackedOption = "message Test {\
10
10
repeated uint32 a = 1 [packed = false];\
11
11
}" ;
12
12
13
+ var packedFeature = "edition = \"2023\";\
14
+ message Test {\
15
+ repeated uint32 a = 1 [features.repeated_field_encoding = PACKED];\
16
+ }" ;
17
+
18
+ var unpackedFeature = "edition = \"2023\";\
19
+ message Test {\
20
+ repeated uint32 a = 1 [features.repeated_field_encoding = EXPANDED];\
21
+ }" ;
22
+
23
+ var regular = "message Test {\
24
+ repeated uint32 a = 1;\
25
+ }" ;
26
+
13
27
var msg = {
14
28
a : [ 1 , 2 , 3 ]
15
29
} ;
16
30
17
- tape . test ( "packed repeated values" , function ( test ) {
18
- var root1 = protobuf . parse ( proto1 ) . root ,
19
- root2 = protobuf . parse ( proto2 ) . root ;
31
+ tape . test ( "packed repeated values roundtrip " , function ( test ) {
32
+ var root1 = protobuf . parse ( packedOption ) . root . resolveAll ( ) ,
33
+ root2 = protobuf . parse ( unpackedOption ) . root . resolveAll ( ) ;
20
34
var Test1 = root1 . lookup ( "Test" ) ,
21
35
Test2 = root2 . lookup ( "Test" ) ;
22
-
36
+
23
37
var dec1 = Test2 . decode ( Test1 . encode ( msg ) . finish ( ) ) ;
24
38
test . same ( dec1 , msg , "should decode packed even if defined non-packed" ) ;
25
39
var dec2 = Test1 . decode ( Test2 . encode ( msg ) . finish ( ) ) ;
26
40
test . same ( dec2 , msg , "should decode non-packed even if defined packed" ) ;
27
41
28
42
test . end ( ) ;
29
43
} ) ;
44
+
45
+ tape . test ( "packed repeated values encode" , function ( top ) {
46
+ [
47
+ packedOption ,
48
+ `syntax = "proto3";\n` + regular ,
49
+ `syntax = "proto3";\n` + packedOption ,
50
+ `edition = "2023";\n` + regular ,
51
+ packedFeature ,
52
+ ] . forEach ( function ( proto , index ) {
53
+ tape . test ( "proto " + index , function ( test ) {
54
+ var root = protobuf . parse ( proto ) . root . resolveAll ( ) ;
55
+ var Test = root . lookup ( "Test" ) ;
56
+
57
+ var buf = Test . encode ( msg ) . finish ( ) ;
58
+ test . equal ( buf . length , 5 , "a total of 4 bytes" ) ;
59
+ test . equal ( buf [ 0 ] , 1 << 3 | 2 , "id 1, wireType 2" ) ;
60
+ test . equal ( buf [ 1 ] , 3 , "length 3" ) ;
61
+ test . equal ( buf [ 2 ] , 1 , "element 1" ) ;
62
+ test . equal ( buf [ 3 ] , 2 , "element 2" ) ;
63
+ test . equal ( buf [ 4 ] , 3 , "element 3" ) ;
64
+
65
+ test . end ( ) ;
66
+ } ) ;
67
+ } ) ;
68
+
69
+ top . end ( )
70
+ } ) ;
71
+
72
+ tape . test ( "packed repeated values encode" , function ( top ) {
73
+ [
74
+ unpackedOption ,
75
+ regular ,
76
+ `syntax = "proto3";\n` + unpackedOption ,
77
+ unpackedFeature ,
78
+ ] . forEach ( function ( proto , index ) {
79
+ tape . test ( "proto " + index , function ( test ) {
80
+ var root = protobuf . parse ( proto ) . root . resolveAll ( ) ;
81
+ var Test = root . lookup ( "Test" ) ;
82
+
83
+ var buf = Test . encode ( msg ) . finish ( ) ;
84
+ test . equal ( buf . length , 6 , "a total of 6 bytes" ) ;
85
+ test . equal ( buf [ 0 ] , 1 << 3 | 0 , "id 1, wireType 0" ) ;
86
+ test . equal ( buf [ 1 ] , 1 , "element 1" ) ;
87
+ test . equal ( buf [ 2 ] , 1 << 3 | 0 , "id 1, wireType 0" ) ;
88
+ test . equal ( buf [ 3 ] , 2 , "element 2" ) ;
89
+ test . equal ( buf [ 4 ] , 1 << 3 | 0 , "id 1, wireType 0" ) ;
90
+ test . equal ( buf [ 5 ] , 3 , "element 3" ) ;
91
+
92
+ test . end ( )
93
+ } ) ;
94
+ } ) ;
95
+
96
+ top . end ( )
97
+ } ) ;
0 commit comments