@@ -17,9 +17,9 @@ var protoRepeated = "message Test {\
17
17
}" ;
18
18
19
19
tape . test ( "legacy groups" , function ( test ) {
20
- var root = protobuf . parse ( protoRequired ) . root ;
20
+ var root = protobuf . parse ( protoRequired ) . root . resolveAll ( ) ;
21
21
22
- var Test = root . resolveAll ( ) . lookup ( "Test" ) ;
22
+ var Test = root . lookup ( "Test" ) ;
23
23
var MyGroupType = Test . get ( "MyGroup" ) ;
24
24
var MyGroupField = Test . get ( "myGroup" ) ;
25
25
var msg = {
@@ -30,6 +30,7 @@ tape.test("legacy groups", function(test) {
30
30
31
31
test . ok ( MyGroupType instanceof protobuf . Type && MyGroupField instanceof protobuf . Field , "should parse to a type and a field" ) ;
32
32
test . equal ( MyGroupType . group , true , "should have the group flag set on the type" ) ;
33
+ test . equal ( MyGroupField . delimited , true , "should have the delimited flag set on the field" ) ;
33
34
test . equal ( MyGroupField . resolvedType , MyGroupType , "should reference the type from the field" ) ;
34
35
var json = MyGroupType . toJSON ( ) ;
35
36
test . equal ( json . group , true , "should export group=true to JSON" ) ;
@@ -77,3 +78,77 @@ tape.test("legacy groups", function(test) {
77
78
78
79
test . end ( ) ;
79
80
} ) ;
81
+
82
+
83
+ tape . test ( "delimited encoding" , function ( test ) {
84
+ var root = protobuf . parse ( `
85
+ edition = "2023";
86
+ message Message {
87
+ uint32 a = 2;
88
+ };
89
+ message Test {
90
+ Message msg = 1 [features.message_encoding = DELIMITED];
91
+ }
92
+ ` ) . root . resolveAll ( ) ;
93
+
94
+ var Test = root . lookup ( "Test" ) ;
95
+ var Message = root . get ( "Message" ) ;
96
+ var Field = Test . get ( "msg" ) ;
97
+ var msg = {
98
+ msg : {
99
+ a : 111
100
+ }
101
+ } ;
102
+
103
+ test . ok ( Message instanceof protobuf . Type && Field instanceof protobuf . Field , "should parse to a type and a field" ) ;
104
+ test . notOk ( Message . group , "should not have the group flag set on the type" ) ;
105
+ test . ok ( Field . delimited , "should have the delimited flag set on the field" ) ;
106
+ test . equal ( Field . resolvedType , Message , "should reference the type from the field" ) ;
107
+
108
+ test . test ( test . name + " - should encode required" , ( function ( Test , msg ) { return function ( test ) {
109
+ var buf = Test . encode ( msg ) . finish ( ) ;
110
+ test . equal ( buf . length , 4 , "a total of 4 bytes" ) ;
111
+ test . equal ( buf [ 0 ] , 1 << 3 | 3 , "id 1, wireType 3" ) ;
112
+ test . equal ( buf [ 1 ] , 2 << 3 | 0 , "id 2, wireType 0" ) ;
113
+ test . equal ( buf [ 2 ] , 111 , "111" ) ;
114
+ test . equal ( buf [ 3 ] , 1 << 3 | 4 , "id 1, wireType 4" ) ;
115
+ test . same ( Test . decode ( buf ) , msg , "and decode back the original message" ) ;
116
+ test . end ( ) ;
117
+ } ; } ) ( Test , msg ) ) ;
118
+
119
+ // Same but repeated
120
+ root = protobuf . parse ( `
121
+ edition = "2023";
122
+ message Message {
123
+ uint32 a = 2;
124
+ };
125
+ message Test {
126
+ repeated Message msg = 1 [features.message_encoding = DELIMITED];
127
+ }
128
+ ` ) . root ;
129
+ Test = root . resolveAll ( ) . lookup ( "Test" ) ;
130
+ msg = {
131
+ msg : [ {
132
+ a : 111
133
+ } , {
134
+ a : 112
135
+ } ]
136
+ } ;
137
+
138
+ test . test ( test . name + " - should encode repeated" , ( function ( Test , msg ) { return function ( test ) {
139
+ var buf = Test . encode ( msg ) . finish ( ) ;
140
+ test . equal ( buf . length , 8 , "a total of 8 bytes" ) ;
141
+ test . equal ( buf [ 0 ] , 1 << 3 | 3 , "id 1, wireType 3" ) ;
142
+ test . equal ( buf [ 1 ] , 2 << 3 | 0 , "id 2, wireType 0" ) ;
143
+ test . equal ( buf [ 2 ] , 111 , "111" ) ;
144
+ test . equal ( buf [ 3 ] , 1 << 3 | 4 , "id 1, wireType 4" ) ;
145
+ test . equal ( buf [ 4 ] , 1 << 3 | 3 , "id 1, wireType 3" ) ;
146
+ test . equal ( buf [ 5 ] , 2 << 3 | 0 , "id 2, wireType 0" ) ;
147
+ test . equal ( buf [ 6 ] , 112 , "112" ) ;
148
+ test . equal ( buf [ 7 ] , 1 << 3 | 4 , "id 1, wireType 4" ) ;
149
+ test . same ( Test . decode ( buf ) , msg , "and decode back the original message" ) ;
150
+ test . end ( ) ;
151
+ } ; } ) ( Test , msg ) ) ;
152
+
153
+ test . end ( ) ;
154
+ } ) ;
0 commit comments