Skip to content

Commit f2a69a5

Browse files
Add tests for handling optional fields in both proto2 and proto3 syntax
1 parent 4793755 commit f2a69a5

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

tests/cli.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,72 @@ tape.test("with null-defaults, absent optional fields have null values", functio
165165
});
166166

167167

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+
168234
tape.test("pbjs generates static code with message filter", function (test) {
169235
cliTest(test, function () {
170236
var root = protobuf.loadSync("tests/data/cli/test-filter.proto");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
3+
message OptionalFields {
4+
message SubMessage {
5+
string a = 1;
6+
}
7+
8+
optional SubMessage a = 1;
9+
optional string b = 2;
10+
optional uint32 c = 3;
11+
uint32 d = 4;
12+
}

tests/data/cli/null-defaults.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ message OptionalFields {
88
optional SubMessage a = 1;
99
optional string b = 2;
1010
optional uint32 c = 3;
11+
required uint32 d = 4;
1112
}

0 commit comments

Comments
 (0)