Permalink
Browse files

README on tests

  • Loading branch information...
dcodeIO committed Dec 9, 2016
1 parent 78952a5 commit 1a8c720714bf867f1f0195b4690faefa4f65e66a

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +0 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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;")
@@ -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`
@@ -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");
@@ -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();
});
@@ -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");
@@ -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) {
@@ -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");
@@ -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();
});
@@ -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.