protobuf.js/tests/comp_optional.js
Damien Elmes d9144dea61
fix: proto3 optional scalars should default to null in reflection API (#1693)
https://github.com/protobufjs/protobuf.js/pull/1584 made proto3 optional
scalars default to null when using static/static-module, but the old
behaviour remained when using reflection (eg json-module).
2022-07-07 17:13:48 -07:00

31 lines
813 B
JavaScript

var tape = require("tape");
var protobuf = require("..");
var proto = "syntax = \"proto3\";\
\
message Message {\
int32 regular_int32 = 1;\
optional int32 optional_int32 = 2;\
oneof _oneof_int32 {\
int32 oneof_int32 = 3;\
}\
}\
";
tape.test("proto3 optional", function(test) {
var root = protobuf.parse(proto).root;
var Message = root.lookup("Message");
test.equal(Message.fields.optionalInt32.optional, true);
test.equal(Message.fields.optionalInt32.options.proto3_optional, true);
test.equal(Message.oneofs._optionalInt32.name, '_optionalInt32');
test.deepEqual(Message.oneofs._optionalInt32.oneof, ['optionalInt32']);
var m = Message.create({});
test.strictEqual(m.regularInt32, 0);
test.strictEqual(m.optionalInt32, null);
test.end();
});