Skip to content

Commit

Permalink
Test case for #531
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 9, 2016
1 parent 55db92e commit 78952a5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ TypePrototype.encode = function encode(message, writer) {
* @returns {Writer} writer
*/
TypePrototype.encodeDelimited = function encodeDelimited(message, writer) {
return this.encode(message, writer && writer.fork() || writer).ldelim();
return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();
};

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/data/simple.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto2";

message A {
required uint32 a = 1;
}

message B {
required string b = 1;
}
53 changes: 53 additions & 0 deletions tests/reuse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var tape = require("tape");

var protobuf = require("..");

tape.test("reusing", function(test) {

var root = protobuf.loadSync("tests/data/simple.proto");

var A = root.lookup("A"),
B = root.lookup("B");

test.test("a writer should write", function(test) {

var writer = protobuf.Writer.create();

A.encodeDelimited({
a: 1
}, writer);

B.encodeDelimited({
b: 'a'
}, writer);

var buffer = writer.finish();

test.equal(buffer[0], 2, "length 2");
test.equal(buffer[1], 8, "id 1, wireType 0");
test.equal(buffer[2], 1, "number 1");
test.equal(buffer[3], 3, "length 3");
test.equal(buffer[4], 10, "id 1, wireType 2");
test.equal(buffer[5], 1, "length 1");
test.equal(buffer[6], 97, "string 'a'");

var reader = protobuf.Reader.create(buffer);

test.test("and a reader should", function(test) {

var a = A.decodeDelimited(reader);
test.deepEqual(a, { a: 1 }, "read back the first message");

var b = B.decodeDelimited(reader);
test.deepEqual(b, { b: 'a' }, "read back the second message");

test.equal(reader.pos, reader.len, "consume the reader");

test.end();
});

test.end();
});

test.end();
});

0 comments on commit 78952a5

Please sign in to comment.