mirror of
https://github.com/protobufjs/protobuf.js.git
synced 2025-12-08 20:58:55 +00:00
85 lines
2.8 KiB
JavaScript
85 lines
2.8 KiB
JavaScript
var tape = require("tape");
|
|
|
|
var protobuf = require(".."),
|
|
Prototype = protobuf.Prototype,
|
|
inherits = protobuf.inherits;
|
|
|
|
tape.test("google.protobuf.Any class", function(test) {
|
|
protobuf.load("tests/data/common.proto", function(err, root) {
|
|
if (err)
|
|
return test.fail(err.message);
|
|
|
|
function Any(properties) {
|
|
Prototype.call(this, properties);
|
|
}
|
|
inherits(Any, root.lookup("google.protobuf.Any"));
|
|
|
|
var valueBuffer = typeof Buffer !== "undefined" ? new Buffer(0) : new Uint8Array(0);
|
|
var any = new Any({
|
|
type_url: "some.type",
|
|
value: valueBuffer
|
|
});
|
|
|
|
test.test("instances", function(test) {
|
|
|
|
test.ok(any instanceof protobuf.Prototype, "should extend Prototype");
|
|
test.ok(any instanceof Any, "should extend the custom class");
|
|
test.deepEqual(any, {
|
|
type_url: "some.type",
|
|
value: valueBuffer
|
|
}, "should be populated with the contents we provided");
|
|
|
|
var buf;
|
|
|
|
test.test("should encode", function(test) {
|
|
|
|
buf = Any.encode(any);
|
|
|
|
test.equal(buf[0] , 1 << 3 | 2, "a tag with id 1, wire type 2");
|
|
test.equal(buf[1] , 9 , "a field length of 9");
|
|
test.equal(buf[11] , 2 << 3 | 2, "a tag with id 2, wire type 2");
|
|
test.equal(buf[12] , 0 , "a field length of 0");
|
|
test.equal(buf.length, 13 , "13 bytes in total");
|
|
|
|
test.end();
|
|
});
|
|
|
|
test.test("should decode", function(test) {
|
|
|
|
msg = Any.decode(buf);
|
|
test.ok(msg instanceof Any, "to an object that extends the custom class");
|
|
test.deepEqual(msg, any, "an equal message");
|
|
|
|
test.end();
|
|
});
|
|
|
|
test.test("should encodeDelimited", function(test) {
|
|
|
|
buf = Any.encodeDelimited(any);
|
|
|
|
test.equal(buf[0] , 13 , "a length of 13");
|
|
test.equal(buf[1] , 1 << 3 | 2, "a tag with id 1, wire type 2");
|
|
test.equal(buf[2] , 9 , "a field length of 9");
|
|
test.equal(buf[12] , 2 << 3 | 2, "a tag with id 2, wire type 2");
|
|
test.equal(buf[13] , 0 , "a field length of 0");
|
|
test.equal(buf.length, 14 , "14 bytes in total");
|
|
|
|
test.end();
|
|
});
|
|
|
|
test.test("should decodeDelimited", function(test) {
|
|
|
|
msg = Any.decodeDelimited(buf);
|
|
test.ok(msg instanceof Any, "to an object that extends the custom class");
|
|
test.deepEqual(msg, any, "an equal message");
|
|
|
|
test.end();
|
|
});
|
|
|
|
test.end();
|
|
});
|
|
|
|
test.end();
|
|
});
|
|
});
|