@@ -3,16 +3,215 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
var path = require ( "path" ) ;
4
4
var tape = require ( "tape" ) ;
5
5
var protobuf = require ( "../index" ) ;
6
+
6
7
// to extend Root
7
- require ( "../ext/descriptor" ) ;
8
- tape . test ( "extensions" , function ( test ) {
8
+ var descriptor = require ( "../ext/descriptor" ) ;
9
+
10
+ tape . test ( "extensions - proto2 to proto3" , function ( test ) {
9
11
// load document with extended field imported multiple times
10
12
var root = protobuf . loadSync ( path . resolve ( __dirname , "data/test.proto" ) ) ;
11
- root . resolveAll ( ) ;
13
+
12
14
// convert to Descriptor Set
13
15
var decodedDescriptorSet = root . toDescriptor ( "proto3" ) ;
16
+
14
17
// load back from descriptor set
15
18
var root2 = protobuf . Root . fromDescriptor ( decodedDescriptorSet ) ;
19
+
20
+ var Simple1 = root2 . lookup ( "Simple1" ) ;
21
+ test . notOk ( Simple1 . fields . aString . required , "required fields don't exist in proto3" ) ;
22
+ test . notOk ( Simple1 . fields . aBoolean . hasPresence , "presence is not preserved" ) ;
23
+
16
24
test . pass ( "should parse and resolve without errors" ) ;
17
25
test . end ( ) ;
18
26
} ) ;
27
+
28
+ tape . test ( "extensions - proto2 roundtrip" , function ( test ) {
29
+ // load document with extended field imported multiple times
30
+ var root = protobuf . parse ( `syntax = "proto2";
31
+
32
+ message Message {
33
+ optional string explicit = 1;
34
+ required string required = 2;
35
+ repeated int32 packed = 3 [packed = true];
36
+ repeated int32 unpacked = 4;
37
+ optional int32 repeated_options = 5 [targets = TARGET_TYPE_SERVICE, targets = TARGET_TYPE_FILE];
38
+ }
39
+ ` ) . root . resolveAll ( ) ;
40
+
41
+ // convert to Descriptor Set
42
+ var decodedDescriptorSet = root . toDescriptor ( "proto2" ) ;
43
+
44
+ // load back from descriptor set
45
+ var root2 = protobuf . Root . fromDescriptor ( decodedDescriptorSet ) ;
46
+
47
+ test . same ( root . toJSON ( ) , root2 . toJSON ( ) , "JSON should roundtrip" ) ;
48
+
49
+ var Message = root2 . lookup ( "Message" ) ;
50
+ test . ok ( Message . fields . required . required , "required field preserved" ) ;
51
+ test . ok ( Message . fields . explicit . hasPresence , "presence is preserved" ) ;
52
+ test . ok ( Message . fields . packed . packed , "packed is preserved" ) ;
53
+ test . notOk ( Message . fields . unpacked . packed , "expanded is preserved" ) ;
54
+
55
+ test . end ( ) ;
56
+ } ) ;
57
+
58
+ tape . test ( "extensions - proto3 roundtrip" , function ( test ) {
59
+ var root = protobuf . parse ( `syntax = "proto3";
60
+
61
+ message Message {
62
+ optional string explicit = 1;
63
+ string implicit = 2;
64
+ repeated int32 packed = 3;
65
+ repeated int32 unpacked = 4 [packed = false];
66
+ }
67
+ ` ) . root . resolveAll ( ) ;
68
+
69
+ // convert to Descriptor Set
70
+ const decodedDescriptorSet = root . toDescriptor ( "proto3" ) ;
71
+
72
+ // load back from descriptor set
73
+ const root2 = protobuf . Root . fromDescriptor ( decodedDescriptorSet ) ;
74
+
75
+ var Message = root2 . lookup ( "Message" ) ;
76
+
77
+ test . same ( root2 . toJSON ( ) , root . toJSON ( ) , "JSON should roundtrip" ) ;
78
+
79
+ test . ok ( Message . fields . explicit . hasPresence , "should have explicit presence" ) ;
80
+ test . notOk ( Message . fields . implicit . hasPresence , "should have implicit presence" ) ;
81
+ test . ok ( Message . fields . packed . packed , "packed is preserved" ) ;
82
+ test . notOk ( Message . fields . unpacked . packed , "expanded is preserved" ) ;
83
+
84
+ test . end ( ) ;
85
+ } ) ;
86
+
87
+ tape . test ( "extensions - edition 2023 file roundtrip" , function ( test ) {
88
+ var json = {
89
+ nested : { Message : {
90
+ edition : "2023" ,
91
+ options : { "features" : { "field_presence" : "IMPLICIT" } } ,
92
+ fields : {
93
+ explicit : { type : "string" , id : 1 , options : { "features" : { "field_presence" : "EXPLICIT" } } } ,
94
+ implicit : { type : "string" , id : 2 } ,
95
+ required : { type : "string" , id : 3 , options : { "features" : { "field_presence" : "LEGACY_REQUIRED" } } } ,
96
+ } ,
97
+ nested : { Nested : { fields : {
98
+ explicit : { type : "string" , id : 1 , options : { "features" : { "field_presence" : "EXPLICIT" } } } ,
99
+ implicit : { type : "string" , id : 2 } ,
100
+ } } }
101
+ } }
102
+ } ;
103
+ var root = protobuf . Root . fromJSON ( json ) ;
104
+
105
+ // convert to Descriptor Set
106
+ const decodedDescriptorSet = root . toDescriptor ( "2023" ) ;
107
+
108
+ // load back from descriptor set
109
+ const root2 = protobuf . Root . fromDescriptor ( decodedDescriptorSet ) ;
110
+
111
+ var Type = root2 . lookup ( "Message" ) ;
112
+ var Nested = Type . nested . Nested ;
113
+
114
+ test . same ( root2 . toJSON ( ) , json , "JSON should roundtrip" ) ;
115
+
116
+ test . ok ( Type . fields . explicit . hasPresence , "should have explicit presence" ) ;
117
+ test . notOk ( Type . fields . implicit . hasPresence , "should have implicit presence" ) ;
118
+
119
+ test . ok ( Nested . fields . explicit . hasPresence , "nested should have explicit presence" ) ;
120
+ test . notOk ( Nested . fields . implicit . hasPresence , "nested should have implicit presence" ) ;
121
+
122
+ test . end ( ) ;
123
+ } ) ;
124
+
125
+
126
+ tape . test ( "extensions - proto2 root-less type" , function ( test ) {
127
+ var Message = protobuf . Type . fromJSON ( "Message" , {
128
+ "edition" : "proto2" ,
129
+ "fields" : {
130
+ "explicit" : {
131
+ "type" : "string" ,
132
+ "id" : 1
133
+ } ,
134
+ "required" : {
135
+ "rule" : "required" ,
136
+ "type" : "string" ,
137
+ "id" : 2
138
+ } ,
139
+ "packed" : {
140
+ "rule" : "repeated" ,
141
+ "type" : "int32" ,
142
+ "id" : 3 ,
143
+ "options" : {
144
+ "packed" : true
145
+ }
146
+ } ,
147
+ "unpacked" : {
148
+ "rule" : "repeated" ,
149
+ "type" : "int32" ,
150
+ "id" : 4
151
+ } ,
152
+ "nested" : {
153
+ "type" : "Nested" ,
154
+ "id" : 5
155
+ }
156
+ } ,
157
+ "nested" : {
158
+ "Nested" : {
159
+ "fields" : {
160
+ "a" : {
161
+ "type" : "int32" ,
162
+ "id" : 1
163
+ }
164
+ }
165
+ }
166
+ }
167
+ } ) . resolveAll ( ) ;
168
+
169
+ // convert to Descriptor Set
170
+ const decodedDescriptorSet = Message . toDescriptor ( "proto2" ) ;
171
+
172
+ // load back from descriptor set
173
+ const Message2 = protobuf . Type . fromDescriptor ( decodedDescriptorSet , "proto2" ) . resolveAll ( ) ;
174
+
175
+ test . same ( Message2 . toJSON ( ) , Message . toJSON ( ) , "JSON should roundtrip" ) ;
176
+
177
+ test . ok ( Message2 . fields . explicit . hasPresence , "should have explicit presence" ) ;
178
+ test . ok ( Message2 . fields . required . required , "should have required presence" ) ;
179
+ test . ok ( Message2 . fields . packed . packed , "should have packed encoding" ) ;
180
+ test . notOk ( Message2 . fields . unpacked . packed , "should have expanded encoding" ) ;
181
+ test . same ( Message2 . fields . nested . resolvedType , Message2 . nested . Nested , "should have cross linkage" ) ;
182
+
183
+ test . end ( ) ;
184
+ } ) ;
185
+
186
+
187
+ tape . test ( "extensions - unsupported edition" , function ( test ) {
188
+ var json = {
189
+ nested : { Message : {
190
+ edition : "2023" ,
191
+ options : { "features" : { "field_presence" : "IMPLICIT" } } ,
192
+ fields : {
193
+ explicit : { type : "string" , id : 1 , options : { "features" : { "field_presence" : "EXPLICIT" } } } ,
194
+ implicit : { type : "string" , id : 2 } ,
195
+ } ,
196
+ nested : { Nested : { fields : {
197
+ explicit : { type : "string" , id : 1 , options : { "features" : { "field_presence" : "EXPLICIT" } } } ,
198
+ implicit : { type : "string" , id : 2 } ,
199
+ } } }
200
+ } }
201
+ } ;
202
+ var root = protobuf . Root . fromJSON ( json ) ;
203
+
204
+ // convert to Descriptor Set
205
+ test . throws ( function ( ) {
206
+ root . toDescriptor ( "2030" )
207
+ } , / U n s u p p o r t e d e d i t i o n 2 0 3 0 / , "unsupported edition output throws" ) ;
208
+
209
+ const decodedDescriptorSet = root . toDescriptor ( "2023" ) ;
210
+ decodedDescriptorSet . file [ 0 ] . edition = descriptor . Edition . EDITION_99997_TEST_ONLY
211
+
212
+ test . throws ( function ( ) {
213
+ protobuf . Root . fromDescriptor ( decodedDescriptorSet )
214
+ } , / U n s u p p o r t e d e d i t i o n 9 9 9 9 7 / , "unsupported edition input throws" ) ;
215
+
216
+ test . end ( ) ;
217
+ } ) ;
0 commit comments