Skip to content

Commit

Permalink
README on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 9, 2016
1 parent 78952a5 commit 1a8c720
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 65 deletions.
3 changes: 2 additions & 1 deletion 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.

6 changes: 3 additions & 3 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.

1 change: 1 addition & 0 deletions src/codegen/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function genVerifyValue(gen, field, fieldIndex, ref) {
for (var j = 0; j < values.length; ++j) gen
("case %d:", values[j]);
gen
("break")
("}");
} else if (field.resolvedType instanceof Type) { gen
("var r;")
Expand Down
7 changes: 7 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This folder contains all the tests, one per file.

It is essential that tests only use the cross-platform API that is also available in browsers:

* Use `load`, not `loadSync`
* Use `Reader.create`, not `BufferReader`
* Use `Writer.create`, not `BufferWriter`
5 changes: 3 additions & 2 deletions tests/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ tape.test("bench.proto and bench.json", function(test) {
return test.fail(err.message);

var Test = root.lookup("Test");

var data = require("../bench/bench.json");

test.equal(Test.verify(data), null, "should verify our test data");
var writer = Test.encode(data);
var decoded = Test.decode(writer.finish());

var decoded = Test.decode(Test.encode(data).finish());
test.deepEqual(decoded, data, "should reproduce the original data when encoded and decoded again");

test.deepEqual(decoded.asJSON(), data, "should reproduce the original data asJSON");
Expand Down
3 changes: 3 additions & 0 deletions tests/empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ tape.test("empty messages", function(test) {
});

var Test = root.lookup("Test");

var buf = Test.encodeDelimited({}).finish();

test.equal(buf.length, 1, "should encodeDelimited to a buffer of length 1");
test.equal(buf[0], 0, "should encodeDelimited a length of 0");

test.end();
});
3 changes: 3 additions & 0 deletions tests/oneof.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ tape.test("oneofs", function(test) {
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");

Expand Down
9 changes: 4 additions & 5 deletions tests/package-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ tape.test("package.json (static)", function(test) {

var root = require("./data/package.js");

var Package = root.Package;
var Repository = root.Package.Repository;
var Package = root.Package,
Repository = root.Package.Repository;

var myPackage = new Package(pkg);

test.test("runtime message", function(test) {
Expand All @@ -21,9 +22,7 @@ tape.test("package.json (static)", function(test) {

test.test("decoded message", function(test) {

var writer = Package.encode(myPackage);
var buf = writer.finish();
var decoded = Package.decode(buf);
var decoded = Package.decode(Package.encode(myPackage).finish());

test.ok(decoded instanceof Package, "should extend Package");
test.ok(decoded.repository instanceof Repository, "submessage should extend Repository");
Expand Down
44 changes: 20 additions & 24 deletions tests/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,35 @@ tape.test("package.json", function(test) {
if (err)
return test.fail(err.message);

try {
var Package = root.lookup("Package"),
Repository = root.lookup("Package.Repository");

var Package = root.lookup("Package");
var Repository = root.lookup("Package.Repository");
var myPackage = Package.create(pkg);
var myPackage = Package.create(pkg);

test.test("runtime message", function(test) {
test.test("runtime message", function(test) {

test.ok(myPackage instanceof protobuf.Message, "should extend Message");
test.equal(myPackage.$type, Package, "should reference Package as its reflected type");
test.deepEqual(myPackage, pkg, "should have equal contents");
test.ok(myPackage instanceof protobuf.Message, "should extend Message");
test.equal(myPackage.$type, Package, "should reference Package as its reflected type");
test.deepEqual(myPackage, pkg, "should have equal contents");

test.end();
});
test.end();
});

test.test("decoded message", function(test) {
test.test("decoded message", function(test) {

var writer = Package.encode(myPackage);
var buf = writer.finish();
var decoded = Package.decode(buf);
var writer = Package.encode(myPackage);
var buf = writer.finish();
var decoded = Package.decode(buf);

test.ok(decoded instanceof protobuf.Message, "should extend Message");
test.equal(decoded.$type, Package, "should reference Package as its reflected type");
test.ok(decoded.repository instanceof protobuf.Message, "submessages should also extend Message");
test.equal(decoded.repository.$type, Repository, "repository submessage should reference Repository as its reflected type");
test.deepEqual(decoded, pkg, "should have equal contents");
test.ok(decoded instanceof protobuf.Message, "should extend Message");
test.equal(decoded.$type, Package, "should reference Package as its reflected type");
test.ok(decoded.repository instanceof protobuf.Message, "submessages should also extend Message");
test.equal(decoded.repository.$type, Repository, "repository submessage should reference Repository as its reflected type");
test.deepEqual(decoded, pkg, "should have equal contents");

test.end();
});
test.end();
});

} catch (e) {
test.threw(e);
}
test.end();
});

Expand Down
60 changes: 32 additions & 28 deletions tests/reuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,54 @@ var protobuf = require("..");

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

var root = protobuf.loadSync("tests/data/simple.proto");
protobuf.load("tests/data/simple.proto", function(err, root) {
if (err)
return test.fail(err.message);

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

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

var writer = protobuf.Writer.create();
var writer = protobuf.Writer.create();

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

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

var buffer = writer.finish();
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'");
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);
var reader = protobuf.Reader.create(buffer);

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

var a = A.decodeDelimited(reader);
test.deepEqual(a, { a: 1 }, "read back the first message");
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");
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.equal(reader.pos, reader.len, "consume the reader");

test.end();
});

test.end();
});

test.end();
});

test.end();
});
});

0 comments on commit 1a8c720

Please sign in to comment.