protobuf.js/tests/oneof.js

47 lines
1.6 KiB
JavaScript

var tape = require("tape");
var protobuf = require("..");
tape.test("oneofs", function(test) {
protobuf.load("tests/data/oneof.proto", function(err, root) {
if (err)
return test.fail(err.message);
var Message = root.lookup("Message");
var message = Message.create({
str: "a",
num: 1
});
test.equal(message.num, 1, "should initialize the last value");
test.equal(message.getKind(), "num", "should reference the last value");
message.setKind('num');
test.notOk(message.hasOwnProperty('str'), "should delete other values");
message.str = "a";
message.setKind('str'); // message.kind = 'str' if IE8 support isn't required
test.notOk(message.hasOwnProperty('num'), "should delete the previous value");
test.equal(message.str, "a", "should set the new value");
test.equal(message.kind, "str", "should reference the new value");
message.num = 0; // default
message.setKind('num');
test.notOk(message.hasOwnProperty('str'), "should delete the previous value");
test.equal(message.num, 0, "should set the new value");
test.equal(message.kind, "num", "should reference the new value");
test.equal(message.hasOwnProperty("num"), true, "should have the new value on the instance, not just the prototype");
var buf = Message.encode(message).finish();
test.equal(buf.length, 2, "should write a total of 2 bytes");
test.equal(buf[0], 16, "should write id 1, wireType 0");
test.equal(buf[1], 0, "should write a value of 0");
test.end();
});
});