Skip to content

Commit fff1eb2

Browse files
committed
Other: Coverage for util.isset and service as a namespace
1 parent ef71e77 commit fff1eb2

7 files changed

Lines changed: 82 additions & 16 deletions

File tree

src/decoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function decoder(mtype) {
4242
("%s={}", ref)
4343
("k=r.%s()", field.keyType)
4444
("r.pos++"); // assumes id 2 + value wireType
45-
if (field.long) {
45+
if (types.long[field.keyType] !== undefined) {
4646
if (types.basic[type] === undefined) gen
4747
("%s[typeof k===\"object\"?util.longToHash(k):k]=types[%d].decode(r,r.uint32())", ref, i); // can't be groups
4848
else gen

src/parse.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,14 @@ function parse(source, root, options) {
590590

591591
var service = new Service(token);
592592
ifBlock(service, function parseService_block(token) {
593-
switch (token) {
594-
case "option":
595-
parseOption(service, token);
596-
skip(";");
597-
break;
598-
case "rpc":
599-
parseMethod(service, token);
600-
break;
593+
if (parseCommon(service, token))
594+
return;
601595

602-
/* istanbul ignore next */
603-
default:
604-
throw illegal(token);
605-
}
596+
/* istanbul ignore else */
597+
if (token === "rpc")
598+
parseMethod(service, token);
599+
else
600+
throw illegal(token);
606601
});
607602
parent.add(service);
608603
}

src/util/minimal.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ util.oneOfSetter = function setOneOf(fieldNames) {
339339
};
340340
};
341341

342+
/* istanbul ignore next */
342343
/**
343344
* Lazily resolves fully qualified type names against the specified root.
344345
* @param {Root} root Root instanceof

tests/api_service.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ var protobuf = require("..");
44

55
var def = {
66
methods: {},
7-
nested: undefined,
7+
nested: {
8+
SomeEnum: {
9+
options: undefined,
10+
values: {}
11+
}
12+
},
813
options: undefined
914
};
1015

tests/api_util.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,46 @@ tape.test("util", function(test) {
2929
test.end();
3030
});
3131

32+
test.test(test.name + " - isset", function(test) {
33+
// note that encoders don't check for default values either
34+
var neverPresent = [
35+
[],
36+
{},
37+
undefined,
38+
null
39+
];
40+
neverPresent.forEach(function(value) {
41+
var proto = {};
42+
var instance = Object.create(proto);
43+
proto.p = value;
44+
instance.i = value;
45+
test.notOk(util.isset(proto, "p"), "should return that " + JSON.stringify(value) + " on the prototype is not present");
46+
test.notOk(util.isset(instance, "i"), "should return that " + JSON.stringify(value) + " on the instance is not present");
47+
});
48+
var cases = {
49+
"arrays": [ [], [0] ],
50+
"objects": [ {}, {a:1} ],
51+
"strings": [ undefined, "" ],
52+
"numbers": [ undefined, 0 ],
53+
"booleans": [ undefined, false ]
54+
};
55+
Object.keys(cases).forEach(function(name) {
56+
var empty = cases[name][0],
57+
value = cases[name][1];
58+
var proto = {};
59+
var instance = Object.create(proto);
60+
proto.pe = instance.ie = empty;
61+
proto.p = instance.i = value;
62+
if (empty !== undefined) { // not present anyway
63+
test.notOk(util.isset(instance, "pe"), "should return that empty " + name + " on the prototype are not present");
64+
test.notOk(util.isset(instance, "ie"), "should return that empty " + name + " on the instance are not present");
65+
}
66+
test.notOk(util.isset(instance, "p"), "should return that " + name + " on the prototype are not present");
67+
test.ok(util.isset(instance, "i"), "should return that " + name + " on the instance ARE present");
68+
});
69+
70+
test.end();
71+
});
72+
3273
test.end();
3374
});

tests/comp_parse-uncommon.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,34 @@ var tape = require("tape");
33
var protobuf = require("..");
44

55
tape.test("uncommon statements", function(test) {
6-
test.plan(1);
6+
test.plan(3);
77
protobuf.load("tests/data/uncommon.proto", function(err, root) {
88
if (err || !root)
99
test.fail(err && err.message || "should parse without errors");
1010
new protobuf.Root().load("tests/data/uncommon.proto", { keepCase: true }, function(err, root) {
11-
if (err || !root)
11+
if (err || !root) {
1212
test.fail(err && err.message || "should parse without errors");
13+
return;
14+
}
1315
test.pass("should parse without errors");
16+
test.doesNotThrow(function() {
17+
root.resolveAll();
18+
}, "should resolve without errors");
19+
test.doesNotThrow(function() {
20+
traverseTypes(root, function(type) {
21+
type.setup();
22+
});
23+
}, "should setup all types without errors");
1424
test.end();
1525
});
1626
});
1727
});
28+
29+
function traverseTypes(current, fn) {
30+
if (current instanceof protobuf.Type) // and/or protobuf.Enum, protobuf.Service etc.
31+
fn(current);
32+
if (current.nestedArray)
33+
current.nestedArray.forEach(function(nested) {
34+
traverseTypes(nested, fn);
35+
});
36+
}

tests/data/uncommon.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ message Test2 {
2929
option (custom) = "";
3030
};
3131

32+
map<uint64,Test> longmap = 10;
33+
3234
/** pre */
3335
oneof kind;
3436
oneof kind2; /// post
@@ -63,6 +65,9 @@ enum Test4_1{
6365
service Test5;
6466

6567
service Test6 { option (custom).bar = "";
68+
message DoSomethingRequest;
69+
message DoSomethingResponse;
70+
6671
rpc DoSomething(stream DoSomethingRequest) returns (stream DoSomethingResponse){ option (custom).foo2 = ""; };
6772
/** pre */
6873
rpc DoSomethingElse( stream DoSomethingRequest ) returns (DoSomethingResponse); /// post

0 commit comments

Comments
 (0)