@@ -165,6 +165,72 @@ tape.test("with null-defaults, absent optional fields have null values", functio
165
165
} ) ;
166
166
167
167
168
+ tape . test ( "with force-optional, optional fields are handled correctly in proto2" , function ( test ) {
169
+ cliTest ( test , function ( ) {
170
+ var root = protobuf . loadSync ( "tests/data/cli/null-defaults.proto" ) ;
171
+ root . resolveAll ( ) ;
172
+
173
+ var staticTarget = require ( "../cli/targets/static" ) ;
174
+
175
+ staticTarget ( root , {
176
+ create : true ,
177
+ decode : true ,
178
+ encode : true ,
179
+ convert : true ,
180
+ comments : true ,
181
+ "force-optional" : true ,
182
+ } , function ( err , jsCode ) {
183
+
184
+ test . error ( err , 'static code generation worked' ) ;
185
+
186
+ test . ok ( jsCode . includes ( "@property {number|null|undefined} [c] OptionalFields c" ) , "Property for c should be nullable" )
187
+ test . ok ( jsCode . includes ( "@member {number|null|undefined} c" ) , "Member for c should be nullable" )
188
+ test . ok ( jsCode . includes ( "OptionalFields.prototype.c = null;" ) , "Initializer for c should be null" )
189
+
190
+ test . ok ( jsCode . includes ( "@property {number} d OptionalFields d" ) , "Property for d should not be nullable" )
191
+ test . ok ( jsCode . includes ( "@member {number} d" ) , "Member for d should not be nullable" )
192
+ test . ok ( jsCode . includes ( "OptionalFields.prototype.d = 0;" ) , "Initializer for d should be zero" )
193
+
194
+ test . end ( ) ;
195
+ } ) ;
196
+ } ) ;
197
+ } ) ;
198
+
199
+
200
+ tape . test ( "with force-optional, optional fields are handled correctly in proto3" , function ( test ) {
201
+ cliTest ( test , function ( ) {
202
+ var root = protobuf . loadSync ( "tests/data/cli/null-defaults-proto3.proto" ) ;
203
+ root . resolveAll ( ) ;
204
+
205
+ var staticTarget = require ( "../cli/targets/static" ) ;
206
+
207
+ staticTarget ( root , {
208
+ create : true ,
209
+ decode : true ,
210
+ encode : true ,
211
+ convert : true ,
212
+ comments : true ,
213
+ "force-optional" : true ,
214
+ } , function ( err , jsCode ) {
215
+
216
+ console . log ( jsCode ) ;
217
+
218
+ test . error ( err , 'static code generation worked' ) ;
219
+
220
+ test . ok ( jsCode . includes ( "@property {number|null|undefined} [c] OptionalFields c" ) , "Property for c should be nullable" )
221
+ test . ok ( jsCode . includes ( "@member {number|null|undefined} c" ) , "Member for c should be nullable" )
222
+ test . ok ( jsCode . includes ( "OptionalFields.prototype.c = null;" ) , "Initializer for c should be null" )
223
+
224
+ test . ok ( jsCode . includes ( "@property {number} d OptionalFields d" ) , "Property for d should not be nullable" )
225
+ test . ok ( jsCode . includes ( "@member {number} d" ) , "Member for d should not be nullable" )
226
+ test . ok ( jsCode . includes ( "OptionalFields.prototype.d = 0;" ) , "Initializer for d should be zero" )
227
+
228
+ test . end ( ) ;
229
+ } ) ;
230
+ } ) ;
231
+ } ) ;
232
+
233
+
168
234
tape . test ( "pbjs generates static code with message filter" , function ( test ) {
169
235
cliTest ( test , function ( ) {
170
236
var root = protobuf . loadSync ( "tests/data/cli/test-filter.proto" ) ;
0 commit comments