From b5daa272407cb31945fd38c34bbef7c9edd1db1c Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 16 Jan 2017 18:54:09 +0100 Subject: [PATCH] Other: Cleaned up test case data and removed unused files --- scripts/gentests.js | 1 - tests/ambiguous.js | 18 +- tests/data/ambiguous-names.js | 345 ---------------------------- tests/data/ambiguous-names.proto | 9 - tests/data/extend.proto | 16 -- tests/data/file1.proto | 1 - tests/data/file2.proto | 1 - tests/data/groups.proto | 6 - tests/data/oneof.proto | 8 - tests/data/options-textformat.proto | 15 -- tests/data/service.proto | 16 -- tests/data/simple.proto | 9 - tests/data/tsd-innerclass.js | 13 -- tests/extend.js | 31 ++- tests/groups.js | 70 +++--- tests/oneof.js | 66 +++--- tests/options-textformat.js | 28 ++- tests/reuse.js | 68 +++--- tests/service.js | 90 ++++---- 19 files changed, 207 insertions(+), 604 deletions(-) delete mode 100644 tests/data/ambiguous-names.js delete mode 100644 tests/data/ambiguous-names.proto delete mode 100644 tests/data/extend.proto delete mode 100644 tests/data/file1.proto delete mode 100644 tests/data/file2.proto delete mode 100644 tests/data/groups.proto delete mode 100644 tests/data/oneof.proto delete mode 100644 tests/data/options-textformat.proto delete mode 100644 tests/data/service.proto delete mode 100644 tests/data/simple.proto delete mode 100644 tests/data/tsd-innerclass.js diff --git a/scripts/gentests.js b/scripts/gentests.js index 6d69a3a94..84020c170 100644 --- a/scripts/gentests.js +++ b/scripts/gentests.js @@ -8,7 +8,6 @@ var fs = require("fs"), "tests/data/package.proto", "tests/data/rpc.proto", "tests/data/mapbox/vector_tile.proto", - "tests/data/ambiguous-names.proto", "tests/data/test.proto", "tests/data/convert.proto", "tests/data/comments.proto" diff --git a/tests/ambiguous.js b/tests/ambiguous.js index 270797492..3a86e1b84 100644 --- a/tests/ambiguous.js +++ b/tests/ambiguous.js @@ -2,12 +2,16 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("ambiguous names", function(test) { - protobuf.load("tests/data/ambiguous-names.proto", function(err, root) { - if (err) - return test.fail(err.message); +var proto = "syntax = \"proto3\";\ +message A {\ + string whatever = 1;\ +}\ +message B {\ + A A = 1;\ +}"; - test.pass("should parse without errors"); - test.end(); - }); +tape.test("ambiguous names", function(test) { + protobuf.parse(proto); + test.pass("should parse without errors"); + test.end(); }); diff --git a/tests/data/ambiguous-names.js b/tests/data/ambiguous-names.js deleted file mode 100644 index 387e30a7f..000000000 --- a/tests/data/ambiguous-names.js +++ /dev/null @@ -1,345 +0,0 @@ -/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/ -"use strict"; - -var $protobuf = require("../../runtime"); - -// Common aliases -var $Reader = $protobuf.Reader, - $Writer = $protobuf.Writer, - $util = $protobuf.util; - -// Lazily resolved type references -var $lazyTypes = []; - -// Exported root namespace -var $root = {}; - -$root.A = (function() { - - /** - * Constructs a new A. - * @exports A - * @constructor - * @param {Object} [properties] Properties to set - */ - function A(properties) { - if (properties) { - var keys = Object.keys(properties); - for (var i = 0; i < keys.length; ++i) - this[keys[i]] = properties[keys[i]]; - } - } - - /** - * A whatever. - * @type {string} - */ - A.prototype.whatever = ""; - - /** - * Creates a new A instance using the specified properties. - * @param {Object} [properties] Properties to set - * @returns {A} A instance - */ - A.create = function create(properties) { - return new A(properties); - }; - - /** - * Encodes the specified A message. - * @param {A|Object} message A message or plain object to encode - * @param {$protobuf.Writer} [writer] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - A.encode = function encode(message, writer) { - if (!writer) - writer = $Writer.create(); - if (message.hasOwnProperty("whatever") && message.whatever !== undefined) - writer.uint32(/* id 1, wireType 2 =*/10).string(message.whatever); - return writer; - }; - - /** - * Encodes the specified A message, length delimited. - * @param {A|Object} message A message or plain object to encode - * @param {$protobuf.Writer} [writer] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - A.encodeDelimited = function encodeDelimited(message, writer) { - return this.encode(message, writer).ldelim(); - }; - - /** - * Decodes a A message from the specified reader or buffer. - * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from - * @param {number} [length] Message length if known beforehand - * @returns {A} A - */ - A.decode = function decode(reader, length) { - if (!(reader instanceof $Reader)) - reader = $Reader.create(reader); - var end = length === undefined ? reader.len : reader.pos + length, message = new $root.A(); - while (reader.pos < end) { - var tag = reader.uint32(); - switch (tag >>> 3) { - case 1: - message.whatever = reader.string(); - break; - default: - reader.skipType(tag & 7); - break; - } - } - return message; - }; - - /** - * Decodes a A message from the specified reader or buffer, length delimited. - * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from - * @returns {A} A - */ - A.decodeDelimited = function decodeDelimited(reader) { - if (!(reader instanceof $Reader)) - reader = $Reader(reader); - return this.decode(reader, reader.uint32()); - }; - - /** - * Verifies a A message. - * @param {A|Object} message A message or plain object to verify - * @returns {?string} `null` if valid, otherwise the reason why it is not - */ - A.verify = function verify(message) { - if (message.whatever !== undefined) - if (!$util.isString(message.whatever)) - return "whatever: string expected"; - return null; - }; - - /** - * Creates a A message from a plain object. Also converts values to their respective internal types. - * @param {Object.} object Plain object - * @returns {A} A - */ - A.fromObject = function fromObject(object) { - var message = new $root.A(); - if (object.whatever !== undefined && object.whatever !== null) - message.whatever = String(object.whatever); - return message; - }; - - /** - * Creates a A message from a plain object. Also converts values to their respective internal types. - * This is an alias of {@link A.fromObject}. - * @param {Object.} object Plain object - * @returns {A} A - */ - A.from = A.fromObject; - - /** - * Creates a plain object from a A message. Also converts values to other types if specified. - * @param {A} message A - * @param {$protobuf.ConversionOptions} [options] Conversion options - * @returns {Object.} Plain object - */ - A.toObject = function toObject(message, options) { - if (!options) - options = {}; - var object = {}; - if (options.defaults) - object.whatever = ""; - if (message.hasOwnProperty("whatever") && message.whatever !== undefined && message.whatever !== null) - object.whatever = message.whatever; - return object; - }; - - /** - * Creates a plain object from this A message. Also converts values to other types if specified. - * @param {$protobuf.ConversionOptions} [options] Conversion options - * @returns {Object.} Plain object - */ - A.prototype.toObject = function toObject(options) { - return this.constructor.toObject(this, options); - }; - - /** - * Converts this A to JSON. - * @returns {Object.} JSON object - */ - A.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return A; -})(); - -$root.B = (function() { - - /** - * Constructs a new B. - * @exports B - * @constructor - * @param {Object} [properties] Properties to set - */ - function B(properties) { - if (properties) { - var keys = Object.keys(properties); - for (var i = 0; i < keys.length; ++i) - this[keys[i]] = properties[keys[i]]; - } - } - - /** - * B A. - * @type {A} - */ - B.prototype.A = null; - - // Lazily resolved type references - var $types = { - 0: "A" - }; $lazyTypes.push($types); - - /** - * Creates a new B instance using the specified properties. - * @param {Object} [properties] Properties to set - * @returns {B} B instance - */ - B.create = function create(properties) { - return new B(properties); - }; - - /** - * Encodes the specified B message. - * @param {B|Object} message B message or plain object to encode - * @param {$protobuf.Writer} [writer] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - B.encode = function encode(message, writer) { - if (!writer) - writer = $Writer.create(); - if (message.hasOwnProperty("A") && message.A !== undefined) - $types[0].encode(message.A, writer.uint32(/* id 1, wireType 2 =*/10).fork()).ldelim(); - return writer; - }; - - /** - * Encodes the specified B message, length delimited. - * @param {B|Object} message B message or plain object to encode - * @param {$protobuf.Writer} [writer] Writer to encode to - * @returns {$protobuf.Writer} Writer - */ - B.encodeDelimited = function encodeDelimited(message, writer) { - return this.encode(message, writer).ldelim(); - }; - - /** - * Decodes a B message from the specified reader or buffer. - * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from - * @param {number} [length] Message length if known beforehand - * @returns {B} B - */ - B.decode = function decode(reader, length) { - if (!(reader instanceof $Reader)) - reader = $Reader.create(reader); - var end = length === undefined ? reader.len : reader.pos + length, message = new $root.B(); - while (reader.pos < end) { - var tag = reader.uint32(); - switch (tag >>> 3) { - case 1: - message.A = $types[0].decode(reader, reader.uint32()); - break; - default: - reader.skipType(tag & 7); - break; - } - } - return message; - }; - - /** - * Decodes a B message from the specified reader or buffer, length delimited. - * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from - * @returns {B} B - */ - B.decodeDelimited = function decodeDelimited(reader) { - if (!(reader instanceof $Reader)) - reader = $Reader(reader); - return this.decode(reader, reader.uint32()); - }; - - /** - * Verifies a B message. - * @param {B|Object} message B message or plain object to verify - * @returns {?string} `null` if valid, otherwise the reason why it is not - */ - B.verify = function verify(message) { - if (message.A !== undefined && message.A !== null) { - var error = $types[0].verify(message.A); - if (error) - return "A." + error; - } - return null; - }; - - /** - * Creates a B message from a plain object. Also converts values to their respective internal types. - * @param {Object.} object Plain object - * @returns {B} B - */ - B.fromObject = function fromObject(object) { - var message = new $root.B(); - if (object.A !== undefined && object.A !== null) - message.A = $types[0].fromObject(object.A); - return message; - }; - - /** - * Creates a B message from a plain object. Also converts values to their respective internal types. - * This is an alias of {@link B.fromObject}. - * @param {Object.} object Plain object - * @returns {B} B - */ - B.from = B.fromObject; - - /** - * Creates a plain object from a B message. Also converts values to other types if specified. - * @param {B} message B - * @param {$protobuf.ConversionOptions} [options] Conversion options - * @returns {Object.} Plain object - */ - B.toObject = function toObject(message, options) { - if (!options) - options = {}; - var object = {}; - if (options.defaults) - object.A = null; - if (message.hasOwnProperty("A") && message.A !== undefined && message.A !== null) - object.A = $types[0].toObject(message.A, options); - return object; - }; - - /** - * Creates a plain object from this B message. Also converts values to other types if specified. - * @param {$protobuf.ConversionOptions} [options] Conversion options - * @returns {Object.} Plain object - */ - B.prototype.toObject = function toObject(options) { - return this.constructor.toObject(this, options); - }; - - /** - * Converts this B to JSON. - * @returns {Object.} JSON object - */ - B.prototype.toJSON = function toJSON() { - return this.constructor.toObject(this, $protobuf.util.toJSONOptions); - }; - - return B; -})(); - -// Resolve lazy type references to actual types -$util.lazyResolve($root, $lazyTypes); - -module.exports = $protobuf.roots["test_ambiguous-names"] = $root; diff --git a/tests/data/ambiguous-names.proto b/tests/data/ambiguous-names.proto deleted file mode 100644 index f99714107..000000000 --- a/tests/data/ambiguous-names.proto +++ /dev/null @@ -1,9 +0,0 @@ -syntax = "proto3"; - -message A { - string whatever = 1; -} - -message B { - A A = 1; -} \ No newline at end of file diff --git a/tests/data/extend.proto b/tests/data/extend.proto deleted file mode 100644 index c4f33d4bb..000000000 --- a/tests/data/extend.proto +++ /dev/null @@ -1,16 +0,0 @@ -syntax = "proto3"; - -message A { - message B { - message One { - extensions 1000 to max; - } - } - message C { - message Two { - extend B.One { - C.Two two = 1000; - } - } - } -} diff --git a/tests/data/file1.proto b/tests/data/file1.proto deleted file mode 100644 index c4b561fa8..000000000 --- a/tests/data/file1.proto +++ /dev/null @@ -1 +0,0 @@ -import "common.proto"; diff --git a/tests/data/file2.proto b/tests/data/file2.proto deleted file mode 100644 index c4b561fa8..000000000 --- a/tests/data/file2.proto +++ /dev/null @@ -1 +0,0 @@ -import "common.proto"; diff --git a/tests/data/groups.proto b/tests/data/groups.proto deleted file mode 100644 index b1b7ae16e..000000000 --- a/tests/data/groups.proto +++ /dev/null @@ -1,6 +0,0 @@ -message Test { - required group MyGroup = 1 { - option foo = "bar"; - required uint32 a = 2; - }; -} \ No newline at end of file diff --git a/tests/data/oneof.proto b/tests/data/oneof.proto deleted file mode 100644 index b3e0c2a4c..000000000 --- a/tests/data/oneof.proto +++ /dev/null @@ -1,8 +0,0 @@ -syntax = "proto3"; - -message Message { - oneof kind { - string str = 1; - int32 num = 2; - } -} diff --git a/tests/data/options-textformat.proto b/tests/data/options-textformat.proto deleted file mode 100644 index 79e18f678..000000000 --- a/tests/data/options-textformat.proto +++ /dev/null @@ -1,15 +0,0 @@ -syntax = "proto3"; -import "google/protobuf/descriptor.proto"; - -message MyOptions { - string a = 1; - string b = 2; -} - -extend google.protobuf.FieldOptions { - MyOptions my_options = 50000; -} - -message Test { - string value = 1 [(my_options) = { a: "foo" b: "bar" }]; -} \ No newline at end of file diff --git a/tests/data/service.proto b/tests/data/service.proto deleted file mode 100644 index 202a76fb4..000000000 --- a/tests/data/service.proto +++ /dev/null @@ -1,16 +0,0 @@ -syntax = "proto3"; -package myservice; - -message DoSomethingRequest { -} - -message DoSomethingResponse { -} - -service MyService { - rpc DoSomething(DoSomethingRequest) returns (DoSomethingResponse) { - option (google.api.http) = { - get: "/v1/dosomething" - }; - }; -} diff --git a/tests/data/simple.proto b/tests/data/simple.proto deleted file mode 100644 index b642fccde..000000000 --- a/tests/data/simple.proto +++ /dev/null @@ -1,9 +0,0 @@ -syntax = "proto2"; - -message A { - required uint32 a = 1; -} - -message B { - required string b = 1; -} diff --git a/tests/data/tsd-innerclass.js b/tests/data/tsd-innerclass.js deleted file mode 100644 index 45cde0da1..000000000 --- a/tests/data/tsd-innerclass.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Outer constructor. - * @constructor - */ -function Outer() { -} - -/** - * Inner constructor. - * @constructor - */ -Outer.Inner = function() { -}; diff --git a/tests/extend.js b/tests/extend.js index 04c798b2e..67f69486a 100644 --- a/tests/extend.js +++ b/tests/extend.js @@ -2,14 +2,25 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("extensions", function(test) { - - protobuf.load("tests/data/extend.proto", function(err, root) { - if (err) - return test.fail(err.message); - root.resolveAll(); - test.pass("should parse and resolve without errors"); - test.end(); - }); +var proto = "syntax = \"proto3\";\ +message A {\ + message B {\ + message One {\ + extensions 1000 to max;\ + }\ + }\ + message C {\ + message Two {\ + extend B.One {\ + C.Two two = 1000;\ + }\ + }\ + }\ +}"; -}); \ No newline at end of file +tape.test("extensions", function(test) { + var root = protobuf.parse(proto).root; + root.resolveAll(); + test.pass("should parse and resolve without errors"); + test.end(); +}); diff --git a/tests/groups.js b/tests/groups.js index ec81c41a4..1ea29bbe0 100644 --- a/tests/groups.js +++ b/tests/groups.js @@ -2,42 +2,46 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("legacy groups", function(test) { - protobuf.load("tests/data/groups.proto", function(err, root) { - if (err) - return test.fail(err.message); - - var Test = root.resolveAll().lookup("Test"); - var MyGroupType = Test.get("MyGroup"); - var MyGroupField = Test.get("myGroup"); - - test.ok(MyGroupType instanceof protobuf.Type && MyGroupField instanceof protobuf.Field, "should parse to a type and a field"); - test.equal(MyGroupType.group, true, "should have the group flag set on the type"); - test.equal(MyGroupField.resolvedType, MyGroupType, "should reference the type from the field"); - - var msg = { - myGroup: { - a: 111 - } - }; - - test.test("should encode", function(test) { - var buf = Test.encode(msg).finish(); - verifyEncode(test, buf); - test.deepEqual(Test.decode(buf), msg, "and decode back the original message"); - test.end(); - }); - - var json = MyGroupType.toJSON(); - test.equal(json.group, true, "should export group=true to JSON"); - - var MyGroupType2 = protobuf.Type.fromJSON("MyGroup", json); - test.equal(MyGroupType2.group, true, "should import group=true from JSON"); - // NOTE: fromJSON alone does not add the sister-field. - // The parser does this explicitly and the field is part of the exported JSON itself. +var proto = "message Test {\ + required group MyGroup = 1 {\ + option foo = \"bar\";\ + required uint32 a = 2;\ + };\ +}"; +tape.test("legacy groups", function(test) { + var root = protobuf.parse(proto).root; + + var Test = root.resolveAll().lookup("Test"); + var MyGroupType = Test.get("MyGroup"); + var MyGroupField = Test.get("myGroup"); + + test.ok(MyGroupType instanceof protobuf.Type && MyGroupField instanceof protobuf.Field, "should parse to a type and a field"); + test.equal(MyGroupType.group, true, "should have the group flag set on the type"); + test.equal(MyGroupField.resolvedType, MyGroupType, "should reference the type from the field"); + + var msg = { + myGroup: { + a: 111 + } + }; + + test.test("should encode", function(test) { + var buf = Test.encode(msg).finish(); + verifyEncode(test, buf); + test.deepEqual(Test.decode(buf), msg, "and decode back the original message"); test.end(); }); + + var json = MyGroupType.toJSON(); + test.equal(json.group, true, "should export group=true to JSON"); + + var MyGroupType2 = protobuf.Type.fromJSON("MyGroup", json); + test.equal(MyGroupType2.group, true, "should import group=true from JSON"); + // NOTE: fromJSON alone does not add the sister-field. + // The parser does this explicitly and the field is part of the exported JSON itself. + + test.end(); }); function verifyEncode(test, buf) { diff --git a/tests/oneof.js b/tests/oneof.js index 1e7d8d9a0..38eafb0ae 100644 --- a/tests/oneof.js +++ b/tests/oneof.js @@ -2,45 +2,49 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("oneofs", function(test) { +var proto = "syntax = \"proto3\";\ +message Message {\ + oneof kind {\ + string str = 1;\ + int32 num = 2;\ + }\ +}"; - protobuf.load("tests/data/oneof.proto", function(err, root) { - if (err) - return test.fail(err.message); +tape.test("oneofs", function(test) { + var root = protobuf.parse(proto).root; - var Message = root.lookup("Message"); + var Message = root.lookup("Message"); - var message = Message.create({ - str: "a", - num: 1 - }); + var message = Message.create({ + str: "a", + num: 1 + }); - test.equal(message.num, 1, "should initialize the last value"); - test.equal(message.kind, "num", "should reference the last value"); - - message.kind = 'num'; - test.notOk(message.hasOwnProperty('str'), "should delete other values"); + test.equal(message.num, 1, "should initialize the last value"); + test.equal(message.kind, "num", "should reference the last value"); + + message.kind = 'num'; + test.notOk(message.hasOwnProperty('str'), "should delete other values"); - message.str = "a"; - message.kind = 'str'; + message.str = "a"; + message.kind = 'str'; - test.notOk(message.hasOwnProperty('num'), "should delete the previous value"); - test.equal(message.str, "a", "should set the new value"); - test.equal(message.kind, "str", "should reference the new value"); + test.notOk(message.hasOwnProperty('num'), "should delete the previous value"); + test.equal(message.str, "a", "should set the new value"); + test.equal(message.kind, "str", "should reference the new value"); - message.num = 0; // default - message.kind = 'num'; - test.notOk(message.hasOwnProperty('str'), "should delete the previous value"); - test.equal(message.num, 0, "should set the new value"); - test.equal(message.kind, "num", "should reference the new value"); - test.equal(message.hasOwnProperty("num"), true, "should have the new value on the instance, not just the prototype"); + message.num = 0; // default + message.kind = 'num'; + test.notOk(message.hasOwnProperty('str'), "should delete the previous value"); + test.equal(message.num, 0, "should set the new value"); + test.equal(message.kind, "num", "should reference the new value"); + test.equal(message.hasOwnProperty("num"), true, "should have the new value on the instance, not just the prototype"); - var buf = Message.encode(message).finish(); - test.equal(buf.length, 2, "should write a total of 2 bytes"); - test.equal(buf[0], 16, "should write id 1, wireType 0"); - test.equal(buf[1], 0, "should write a value of 0"); + var buf = Message.encode(message).finish(); + test.equal(buf.length, 2, "should write a total of 2 bytes"); + test.equal(buf[0], 16, "should write id 1, wireType 0"); + test.equal(buf[1], 0, "should write a value of 0"); - test.end(); - }); + test.end(); }); diff --git a/tests/options-textformat.js b/tests/options-textformat.js index 426fe9fa7..d32d0a872 100644 --- a/tests/options-textformat.js +++ b/tests/options-textformat.js @@ -2,14 +2,22 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("options in textformat", function(test) { - - protobuf.load("tests/data/options-textformat.proto", function(err, root) { - if (err) - throw err; - var Test = root.lookup("Test"); - test.same(Test.fields.value.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly"); - test.end(); - }); +var proto = "syntax = \"proto3\";\ +import \"google/protobuf/descriptor.proto\";\ +message MyOptions {\ + string a = 1;\ + string b = 2;\ +}\ +extend google.protobuf.FieldOptions {\ + MyOptions my_options = 50000;\ +}\ +message Test {\ + string value = 1 [(my_options) = { a: \"foo\" b: \"bar\" }];\ +}"; -}); \ No newline at end of file +tape.test("options in textformat", function(test) { + var root = protobuf.parse(proto).root; + var Test = root.lookup("Test"); + test.same(Test.fields.value.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly"); + test.end(); +}); diff --git a/tests/reuse.js b/tests/reuse.js index 4ad6297d1..4dd0ac545 100644 --- a/tests/reuse.js +++ b/tests/reuse.js @@ -2,56 +2,58 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("reusing", function(test) { - - protobuf.load("tests/data/simple.proto", function(err, root) { - if (err) - return test.fail(err.message); +var proto = "message A {\ + required uint32 a = 1;\ +}\ +message B {\ + required string b = 1;\ +}"; - var A = root.lookup("A"), - B = root.lookup("B"); +tape.test("reusing", function(test) { + var root = protobuf.parse(proto).root; - test.test("a writer should write", function(test) { + var A = root.lookup("A"), + B = root.lookup("B"); - var writer = protobuf.Writer.create(); + test.test("a writer should write", function(test) { - A.encodeDelimited({ - a: 1 - }, writer); + var writer = protobuf.Writer.create(); - B.encodeDelimited({ - b: 'a' - }, writer); + A.encodeDelimited({ + a: 1 + }, writer); - var buffer = writer.finish(); + B.encodeDelimited({ + b: 'a' + }, writer); - 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 buffer = writer.finish(); - var reader = protobuf.Reader.create(buffer); + 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.test("and a reader should", function(test) { + var reader = protobuf.Reader.create(buffer); - var a = A.decodeDelimited(reader); - test.deepEqual(a, { a: 1 }, "read back the first message"); + test.test("and a reader should", function(test) { - var b = B.decodeDelimited(reader); - test.deepEqual(b, { b: 'a' }, "read back the second message"); + var a = A.decodeDelimited(reader); + test.deepEqual(a, { a: 1 }, "read back the first message"); - test.equal(reader.pos, reader.len, "consume the reader"); + var b = B.decodeDelimited(reader); + test.deepEqual(b, { b: 'a' }, "read back the second message"); - test.end(); - }); + test.equal(reader.pos, reader.len, "consume the reader"); test.end(); }); test.end(); - }); + + test.end(); }); diff --git a/tests/service.js b/tests/service.js index dbfc77417..fc16f455e 100644 --- a/tests/service.js +++ b/tests/service.js @@ -2,51 +2,61 @@ var tape = require("tape"); var protobuf = require(".."); -tape.test("services", function(test) { +var proto = "syntax = \"proto3\";\ +package myservice;\ +message DoSomethingRequest {\ +}\ +message DoSomethingResponse {\ +}\ +service MyService {\ + rpc DoSomething(DoSomethingRequest) returns (DoSomethingResponse) {\ + option (google.api.http) = {\ + get: \"/v1/dosomething\"\ + };\ + };\ +}"; - protobuf.load("tests/data/service.proto", function(err, root) { - if (err) - return test.fail(err.message); +tape.test("services", function(test) { + var root = protobuf.parse(proto).root; - var myservice = root.lookup("myservice").resolveAll(), - MyService = myservice.MyService, - DoSomethingRequest = myservice.DoSomethingRequest, - DoSomethingResponse = myservice.DoSomethingResponse, - DoSomething = MyService.get("DoSomething"); + var myservice = root.lookup("myservice").resolveAll(), + MyService = myservice.MyService, + DoSomethingRequest = myservice.DoSomethingRequest, + DoSomethingResponse = myservice.DoSomethingResponse, + DoSomething = MyService.get("DoSomething"); - function rpcImpl(method, requestData, callback) { - if (requestData) { - test.equal(method, DoSomething, "rpcImpl should reference the correct method"); - test.ok(callback, "rpcImpl should provide a callback"); - setTimeout(function() { - callback(null, DoSomethingResponse.create()); - }); - } else { - test.equal(method, null, "rpcImpl should not reference a method when closed"); - test.equal(callback, null, "rpcImpl should not provide a callback when closed"); - } + function rpcImpl(method, requestData, callback) { + if (requestData) { + test.equal(method, DoSomething, "rpcImpl should reference the correct method"); + test.ok(callback, "rpcImpl should provide a callback"); + setTimeout(function() { + callback(null, DoSomethingResponse.create()); + }); + } else { + test.equal(method, null, "rpcImpl should not reference a method when closed"); + test.equal(callback, null, "rpcImpl should not provide a callback when closed"); } + } - var service = MyService.create(rpcImpl); - var dataEmitted = false; - service.on("data", function(responseData) { - dataEmitted = true; - test.ok(responseData, "should emit the data event"); - }); - var endCalled = false; - service.on("end", function() { - test.notOk(endCalled, "should not emit the end event twice"); - endCalled = true; - test.pass("should call the end event"); - service.end(); - test.end(); - }); - service.doSomething(DoSomethingRequest.create(), function(err, res) { - test.notOk(err, "should not raise an error"); - test.ok(res instanceof DoSomethingResponse.ctor, "should return a properly typed response"); - test.ok(dataEmitted, "should have emitted the data event"); - service.end(); - }); + var service = MyService.create(rpcImpl); + var dataEmitted = false; + service.on("data", function(responseData) { + dataEmitted = true; + test.ok(responseData, "should emit the data event"); + }); + var endCalled = false; + service.on("end", function() { + test.notOk(endCalled, "should not emit the end event twice"); + endCalled = true; + test.pass("should call the end event"); + service.end(); + test.end(); + }); + service.doSomething(DoSomethingRequest.create(), function(err, res) { + test.notOk(err, "should not raise an error"); + test.ok(res instanceof DoSomethingResponse.ctor, "should return a properly typed response"); + test.ok(dataEmitted, "should have emitted the data event"); + service.end(); }); }); \ No newline at end of file