Skip to content

Commit

Permalink
Other: Coverage for util.isset and service as a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 25, 2017
1 parent ef71e77 commit fff1eb2
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 7 additions & 12 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/util/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/api_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ var protobuf = require("..");

var def = {
methods: {},
nested: undefined,
nested: {
SomeEnum: {
options: undefined,
values: {}
}
},
options: undefined
};

Expand Down
41 changes: 41 additions & 0 deletions tests/api_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
23 changes: 21 additions & 2 deletions tests/comp_parse-uncommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
5 changes: 5 additions & 0 deletions tests/data/uncommon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ message Test2 {
option (custom) = "";
};

map<uint64,Test> longmap = 10;

/** pre */
oneof kind;
oneof kind2; /// post
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fff1eb2

Please sign in to comment.