Permalink
Browse files

Other: Coverage for util.isset and service as a namespace

  • Loading branch information...
dcodeIO committed Mar 25, 2017
1 parent ef71e77 commit fff1eb297a728ed6d334c591e7d796636859aa9a
Showing with 82 additions and 16 deletions.
  1. +1 −1 src/decoder.js
  2. +7 −12 src/parse.js
  3. +1 −0 src/util/minimal.js
  4. +6 −1 tests/api_service.js
  5. +41 −0 tests/api_util.js
  6. +21 −2 tests/comp_parse-uncommon.js
  7. +5 −0 tests/data/uncommon.proto
@@ -42,7 +42,7 @@ function decoder(mtype) {
("%s={}", ref)
("k=r.%s()", field.keyType)
("r.pos++"); // assumes id 2 + value wireType
if (field.long) {
if (types.long[field.keyType] !== undefined) {
if (types.basic[type] === undefined) gen
("%s[typeof k===\"object\"?util.longToHash(k):k]=types[%d].decode(r,r.uint32())", ref, i); // can't be groups
else gen
@@ -590,19 +590,14 @@ function parse(source, root, options) {
var service = new Service(token);
ifBlock(service, function parseService_block(token) {
switch (token) {
case "option":
parseOption(service, token);
skip(";");
break;
case "rpc":
parseMethod(service, token);
break;
if (parseCommon(service, token))
return;
/* istanbul ignore next */
default:
throw illegal(token);
}
/* istanbul ignore else */
if (token === "rpc")
parseMethod(service, token);
else
throw illegal(token);
});
parent.add(service);
}
@@ -339,6 +339,7 @@ util.oneOfSetter = function setOneOf(fieldNames) {
};
};
/* istanbul ignore next */
/**
* Lazily resolves fully qualified type names against the specified root.
* @param {Root} root Root instanceof
@@ -4,7 +4,12 @@ var protobuf = require("..");
var def = {
methods: {},
nested: undefined,
nested: {
SomeEnum: {
options: undefined,
values: {}
}
},
options: undefined
};
@@ -29,5 +29,46 @@ tape.test("util", function(test) {
test.end();
});
test.test(test.name + " - isset", function(test) {
// note that encoders don't check for default values either
var neverPresent = [
[],
{},
undefined,
null
];
neverPresent.forEach(function(value) {
var proto = {};
var instance = Object.create(proto);
proto.p = value;
instance.i = value;
test.notOk(util.isset(proto, "p"), "should return that " + JSON.stringify(value) + " on the prototype is not present");
test.notOk(util.isset(instance, "i"), "should return that " + JSON.stringify(value) + " on the instance is not present");
});
var cases = {
"arrays": [ [], [0] ],
"objects": [ {}, {a:1} ],
"strings": [ undefined, "" ],
"numbers": [ undefined, 0 ],
"booleans": [ undefined, false ]
};
Object.keys(cases).forEach(function(name) {
var empty = cases[name][0],
value = cases[name][1];
var proto = {};
var instance = Object.create(proto);
proto.pe = instance.ie = empty;
proto.p = instance.i = value;
if (empty !== undefined) { // not present anyway
test.notOk(util.isset(instance, "pe"), "should return that empty " + name + " on the prototype are not present");
test.notOk(util.isset(instance, "ie"), "should return that empty " + name + " on the instance are not present");
}
test.notOk(util.isset(instance, "p"), "should return that " + name + " on the prototype are not present");
test.ok(util.isset(instance, "i"), "should return that " + name + " on the instance ARE present");
});
test.end();
});
test.end();
});
@@ -3,15 +3,34 @@ var tape = require("tape");
var protobuf = require("..");
tape.test("uncommon statements", function(test) {
test.plan(1);
test.plan(3);
protobuf.load("tests/data/uncommon.proto", function(err, root) {
if (err || !root)
test.fail(err && err.message || "should parse without errors");
new protobuf.Root().load("tests/data/uncommon.proto", { keepCase: true }, function(err, root) {
if (err || !root)
if (err || !root) {
test.fail(err && err.message || "should parse without errors");
return;
}
test.pass("should parse without errors");
test.doesNotThrow(function() {
root.resolveAll();
}, "should resolve without errors");
test.doesNotThrow(function() {
traverseTypes(root, function(type) {
type.setup();
});
}, "should setup all types without errors");
test.end();
});
});
});
function traverseTypes(current, fn) {
if (current instanceof protobuf.Type) // and/or protobuf.Enum, protobuf.Service etc.
fn(current);
if (current.nestedArray)
current.nestedArray.forEach(function(nested) {
traverseTypes(nested, fn);
});
}
@@ -29,6 +29,8 @@ message Test2 {
option (custom) = "";
};
map<uint64,Test> longmap = 10;
/** pre */
oneof kind;
oneof kind2; /// post
@@ -63,6 +65,9 @@ enum Test4_1{
service Test5;
service Test6 { option (custom).bar = "";
message DoSomethingRequest;
message DoSomethingResponse;
rpc DoSomething(stream DoSomethingRequest) returns (stream DoSomethingResponse){ option (custom).foo2 = ""; };
/** pre */
rpc DoSomethingElse( stream DoSomethingRequest ) returns (DoSomethingResponse); /// post

0 comments on commit fff1eb2

Please sign in to comment.