Skip to content

Commit caad500

Browse files
committed
tests: added and broke up tests
1 parent da2663a commit caad500

File tree

7 files changed

+222
-74
lines changed

7 files changed

+222
-74
lines changed

cli/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,21 @@ export class Enum extends ReflectionObject {
179179
/** Values options, if any */
180180
public valuesOptions?: { [k: string]: { [k: string]: any } };
181181

182-
/** Values features, if any */
182+
/** Resolved values features, if any */
183183
public _valuesFeatures?: { [k: string]: { [k: string]: any } };
184184

185+
/** Unresolved values features, if any */
186+
public _valuesProtoFeatures?: { [k: string]: { [k: string]: any } };
187+
185188
/** Reserved ranges, if any. */
186189
public reserved: (number[]|string)[];
187190

191+
/**
192+
* Resolves value features
193+
* @returns `this`
194+
*/
195+
public resolve(): Enum;
196+
188197
/**
189198
* Constructs an enum from an enum descriptor.
190199
* @param name Enum name
@@ -883,6 +892,9 @@ export abstract class ReflectionObject {
883892
/** Resolved Features. */
884893
public _features: any;
885894

895+
/** Unresolved Features. */
896+
public _protoFeatures: any;
897+
886898
/** Parent namespace. */
887899
public parent: (Namespace|null);
888900

@@ -925,6 +937,9 @@ export abstract class ReflectionObject {
925937
*/
926938
public resolve(): ReflectionObject;
927939

940+
/** Resolves child features from parent features */
941+
public _resolveFeatures(): void;
942+
928943
/**
929944
* Gets an option value.
930945
* @param name Option name
@@ -2200,9 +2215,10 @@ export namespace util {
22002215
* @param dst Destination object
22012216
* @param path dot '.' delimited path of the property to set
22022217
* @param value the value to set
2218+
* @param overWrite whether or not to concatenate the values into an array or overwrite; defaults to false.
22032219
* @returns Destination object
22042220
*/
2205-
function setProperty(dst: { [k: string]: any }, path: string, value: object): { [k: string]: any };
2221+
function setProperty(dst: { [k: string]: any }, path: string, value: object, overWrite: boolean): { [k: string]: any };
22062222

22072223
/** Decorator root (TypeScript). */
22082224
let decorateRoot: Root;

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/enum.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
6666
* Unresolved values features, if any
6767
* @type {Object<string, Object<string, *>>|undefined}
6868
*/
69-
this._proto_valuesFeatures = {};
69+
this._valuesProtoFeatures = {};
7070

7171
/**
7272
* Reserved ranges, if any.
@@ -93,13 +93,13 @@ Enum.prototype.resolve = function resolve() {
9393
if (this.resolved)
9494
return this;
9595

96-
for (var key of Object.keys(this._proto_valuesFeatures)) {
96+
for (var key of Object.keys(this._valuesProtoFeatures)) {
9797

9898
if (this.parent) {
9999
var parentFeaturesCopy = Object.assign({}, this.parent._features);
100-
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._proto_valuesFeatures[key] || {});
100+
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._valuesProtoFeatures[key] || {});
101101
} else {
102-
this._valuesFeatures[key] = Object.assign({}, this._proto_valuesFeatures[key]);
102+
this._valuesFeatures[key] = Object.assign({}, this._valuesProtoFeatures[key]);
103103
}
104104
}
105105
return ReflectionObject.prototype.resolve.call(this);
@@ -186,16 +186,16 @@ Enum.prototype.add = function add(name, id, comment, options) {
186186
for (var key of Object.keys(this.valuesOptions)) {
187187
var features = Array.isArray(this.valuesOptions[key]) ? this.valuesOptions[key].find(x => {return Object.prototype.hasOwnProperty.call(x, "features");}) : this.valuesOptions[key] === "features";
188188
if (features) {
189-
this._proto_valuesFeatures[key] = features.features;
189+
this._valuesProtoFeatures[key] = features.features;
190190
} else {
191-
this._proto_valuesFeatures[key] = {};
191+
this._valuesProtoFeatures[key] = {};
192192
}
193193
}
194194
}
195195

196-
for (var name of Object.keys(this.values)) {
197-
if (!this._proto_valuesFeatures[name]) {
198-
this._proto_valuesFeatures[name] = {};
196+
for (var enumValue of Object.keys(this.values)) {
197+
if (!this._valuesProtoFeatures[enumValue]) {
198+
this._valuesProtoFeatures[enumValue] = {};
199199
}
200200
}
201201

src/object.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var util = require("./util");
77

88
var Root; // cyclic
99

10+
/* eslint-disable no-warning-comments */
1011
// TODO: Replace with embedded proto.
1112
var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};
1213
var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"};
@@ -54,7 +55,7 @@ function ReflectionObject(name, options) {
5455
/**
5556
* Unresolved Features.
5657
*/
57-
this._proto_features = null;
58+
this._protoFeatures = null;
5859

5960
/**
6061
* Parent namespace.
@@ -176,22 +177,22 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() {
176177
var defaults = {};
177178

178179
if (this instanceof Root) {
179-
if (this.root.getOption("syntax") === "proto2") {
180-
defaults = Object.assign({}, proto2Defaults);
181-
} else if (this.root.getOption("syntax") === "proto3") {
180+
if (this.root.getOption("syntax") === "proto3") {
182181
defaults = Object.assign({}, proto3Defaults);
183182
} else if (this.root.getOption("edition") === "2023") {
184183
defaults = Object.assign({}, editions2023Defaults);
184+
} else {
185+
defaults = Object.assign({}, proto2Defaults);
185186
}
186187
}
187188

188189
if (this instanceof Root) {
189-
this._features = Object.assign(defaults, this._proto_features || {});
190+
this._features = Object.assign(defaults, this._protoFeatures || {});
190191
} else if (this.parent) {
191192
var parentFeaturesCopy = Object.assign({}, this.parent._features);
192-
this._features = Object.assign(parentFeaturesCopy, this._proto_features || {});
193+
this._features = Object.assign(parentFeaturesCopy, this._protoFeatures || {});
193194
} else {
194-
this._features = Object.assign({}, this._proto_features);
195+
this._features = Object.assign({}, this._protoFeatures);
195196
}
196197
};
197198

@@ -259,7 +260,7 @@ ReflectionObject.prototype.setParsedOption = function setParsedOption(name, valu
259260

260261
if (isFeature) {
261262
var features = parsedOptions.find(x => {return Object.prototype.hasOwnProperty.call(x, "features");});
262-
this._proto_features = features.features || {};
263+
this._protoFeatures = features.features || {};
263264
}
264265

265266
return this;

tests/feature_grammar.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,45 @@ var tape = require("tape");
22

33
var protobuf = require("..");
44

5-
var protoEditionsRequired = `edition = "2023";
5+
tape.test("editions required keyword", function(test) {
6+
test.throws(function() {
7+
protobuf.parse(`edition = "2023";
68
message A {\
79
required uint32 a = 1;\
8-
}`;
9-
var protoEditionsOptional = `edition = "2023";
10-
message A {\
11-
optional uint32 a = 1;\
12-
}`;
13-
14-
var protoEditionsGroup = `edition = "2023";
15-
message A {\
16-
group uint32 a = 1;\
17-
}`;
18-
19-
var noQuote = `edition = "2023";
20-
message Foo {
21-
reserved bar, baz;
22-
}`;
23-
24-
tape.test("editions grammar", function(test) {
25-
test.throws(function() {
26-
protobuf.parse(protoEditionsRequired);
10+
}`);
2711
}, Error, "Error: illegal token 'required'");
12+
13+
test.end();
14+
});
2815

16+
tape.test("editions optional keyword", function(test) {
2917
test.throws(function() {
30-
protobuf.parse(protoEditionsOptional);
18+
protobuf.parse(`edition = "2023";
19+
message A {\
20+
optional uint32 a = 1;\
21+
}`);
3122
}, Error, "Error: illegal token 'optional'");
3223

24+
test.end();
25+
});
26+
27+
tape.test("editions group keyword", function(test) {
3328
test.throws(function() {
34-
protobuf.parse(protoEditionsGroup);
29+
protobuf.parse(`edition = "2023";
30+
message A {\
31+
group uint32 a = 1;\
32+
}`);
3533
}, Error, "Error: illegal token 'group'");
3634

37-
test.ok(protobuf.parse(noQuote));
35+
test.end();
36+
});
37+
38+
tape.test("editions no quote", function(test) {
39+
test.ok(protobuf.parse(`edition = "2023";
40+
message Foo {
41+
reserved bar, baz;
42+
}`));
3843

3944
test.end();
4045
});
46+

0 commit comments

Comments
 (0)