From fff1eb297a728ed6d334c591e7d796636859aa9a Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sat, 25 Mar 2017 23:20:52 +0100 Subject: [PATCH] Other: Coverage for util.isset and service as a namespace --- src/decoder.js | 2 +- src/parse.js | 19 ++++++----------- src/util/minimal.js | 1 + tests/api_service.js | 7 +++++- tests/api_util.js | 41 ++++++++++++++++++++++++++++++++++++ tests/comp_parse-uncommon.js | 23 ++++++++++++++++++-- tests/data/uncommon.proto | 5 +++++ 7 files changed, 82 insertions(+), 16 deletions(-) diff --git a/src/decoder.js b/src/decoder.js index 955a5351f..bac7ee862 100644 --- a/src/decoder.js +++ b/src/decoder.js @@ -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 diff --git a/src/parse.js b/src/parse.js index 0e7226043..c22a38b77 100644 --- a/src/parse.js +++ b/src/parse.js @@ -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); } diff --git a/src/util/minimal.js b/src/util/minimal.js index 48f9ad42b..6f5bde6b7 100644 --- a/src/util/minimal.js +++ b/src/util/minimal.js @@ -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 diff --git a/tests/api_service.js b/tests/api_service.js index 1fc0ce4e6..74bfa51c2 100644 --- a/tests/api_service.js +++ b/tests/api_service.js @@ -4,7 +4,12 @@ var protobuf = require(".."); var def = { methods: {}, - nested: undefined, + nested: { + SomeEnum: { + options: undefined, + values: {} + } + }, options: undefined }; diff --git a/tests/api_util.js b/tests/api_util.js index f02c1697f..5a031add2 100644 --- a/tests/api_util.js +++ b/tests/api_util.js @@ -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(); }); \ No newline at end of file diff --git a/tests/comp_parse-uncommon.js b/tests/comp_parse-uncommon.js index 2aace4a97..07ce36339 100644 --- a/tests/comp_parse-uncommon.js +++ b/tests/comp_parse-uncommon.js @@ -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); + }); +} diff --git a/tests/data/uncommon.proto b/tests/data/uncommon.proto index 19dc269bd..d3d2cf266 100644 --- a/tests/data/uncommon.proto +++ b/tests/data/uncommon.proto @@ -29,6 +29,8 @@ message Test2 { option (custom) = ""; }; + map 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