Skip to content

Commit

Permalink
tests: added and broke up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sofisl committed Oct 8, 2024
1 parent da2663a commit caad500
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 74 deletions.
2 changes: 1 addition & 1 deletion cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,21 @@ export class Enum extends ReflectionObject {
/** Values options, if any */
public valuesOptions?: { [k: string]: { [k: string]: any } };

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

/** Unresolved values features, if any */
public _valuesProtoFeatures?: { [k: string]: { [k: string]: any } };

/** Reserved ranges, if any. */
public reserved: (number[]|string)[];

/**
* Resolves value features
* @returns `this`
*/
public resolve(): Enum;

/**
* Constructs an enum from an enum descriptor.
* @param name Enum name
Expand Down Expand Up @@ -883,6 +892,9 @@ export abstract class ReflectionObject {
/** Resolved Features. */
public _features: any;

/** Unresolved Features. */
public _protoFeatures: any;

/** Parent namespace. */
public parent: (Namespace|null);

Expand Down Expand Up @@ -925,6 +937,9 @@ export abstract class ReflectionObject {
*/
public resolve(): ReflectionObject;

/** Resolves child features from parent features */
public _resolveFeatures(): void;

/**
* Gets an option value.
* @param name Option name
Expand Down Expand Up @@ -2200,9 +2215,10 @@ export namespace util {
* @param dst Destination object
* @param path dot '.' delimited path of the property to set
* @param value the value to set
* @param overWrite whether or not to concatenate the values into an array or overwrite; defaults to false.
* @returns Destination object
*/
function setProperty(dst: { [k: string]: any }, path: string, value: object): { [k: string]: any };
function setProperty(dst: { [k: string]: any }, path: string, value: object, overWrite: boolean): { [k: string]: any };

/** Decorator root (TypeScript). */
let decorateRoot: Root;
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
* Unresolved values features, if any
* @type {Object<string, Object<string, *>>|undefined}
*/
this._proto_valuesFeatures = {};
this._valuesProtoFeatures = {};

/**
* Reserved ranges, if any.
Expand All @@ -93,13 +93,13 @@ Enum.prototype.resolve = function resolve() {
if (this.resolved)
return this;

for (var key of Object.keys(this._proto_valuesFeatures)) {
for (var key of Object.keys(this._valuesProtoFeatures)) {

if (this.parent) {
var parentFeaturesCopy = Object.assign({}, this.parent._features);
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._proto_valuesFeatures[key] || {});
this._valuesFeatures[key] = Object.assign(parentFeaturesCopy, this._valuesProtoFeatures[key] || {});
} else {
this._valuesFeatures[key] = Object.assign({}, this._proto_valuesFeatures[key]);
this._valuesFeatures[key] = Object.assign({}, this._valuesProtoFeatures[key]);
}
}
return ReflectionObject.prototype.resolve.call(this);
Expand Down Expand Up @@ -186,16 +186,16 @@ Enum.prototype.add = function add(name, id, comment, options) {
for (var key of Object.keys(this.valuesOptions)) {
var features = Array.isArray(this.valuesOptions[key]) ? this.valuesOptions[key].find(x => {return Object.prototype.hasOwnProperty.call(x, "features");}) : this.valuesOptions[key] === "features";
if (features) {
this._proto_valuesFeatures[key] = features.features;
this._valuesProtoFeatures[key] = features.features;
} else {
this._proto_valuesFeatures[key] = {};
this._valuesProtoFeatures[key] = {};
}
}
}

for (var name of Object.keys(this.values)) {
if (!this._proto_valuesFeatures[name]) {
this._proto_valuesFeatures[name] = {};
for (var enumValue of Object.keys(this.values)) {
if (!this._valuesProtoFeatures[enumValue]) {
this._valuesProtoFeatures[enumValue] = {};
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var util = require("./util");

var Root; // cyclic

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

/**
* Parent namespace.
Expand Down Expand Up @@ -176,22 +177,22 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures() {
var defaults = {};

if (this instanceof Root) {
if (this.root.getOption("syntax") === "proto2") {
defaults = Object.assign({}, proto2Defaults);
} else if (this.root.getOption("syntax") === "proto3") {
if (this.root.getOption("syntax") === "proto3") {
defaults = Object.assign({}, proto3Defaults);
} else if (this.root.getOption("edition") === "2023") {
defaults = Object.assign({}, editions2023Defaults);
} else {
defaults = Object.assign({}, proto2Defaults);
}
}

if (this instanceof Root) {
this._features = Object.assign(defaults, this._proto_features || {});
this._features = Object.assign(defaults, this._protoFeatures || {});
} else if (this.parent) {
var parentFeaturesCopy = Object.assign({}, this.parent._features);
this._features = Object.assign(parentFeaturesCopy, this._proto_features || {});
this._features = Object.assign(parentFeaturesCopy, this._protoFeatures || {});
} else {
this._features = Object.assign({}, this._proto_features);
this._features = Object.assign({}, this._protoFeatures);
}
};

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

if (isFeature) {
var features = parsedOptions.find(x => {return Object.prototype.hasOwnProperty.call(x, "features");});
this._proto_features = features.features || {};
this._protoFeatures = features.features || {};
}

return this;
Expand Down
52 changes: 29 additions & 23 deletions tests/feature_grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,45 @@ var tape = require("tape");

var protobuf = require("..");

var protoEditionsRequired = `edition = "2023";
tape.test("editions required keyword", function(test) {
test.throws(function() {
protobuf.parse(`edition = "2023";
message A {\
required uint32 a = 1;\
}`;
var protoEditionsOptional = `edition = "2023";
message A {\
optional uint32 a = 1;\
}`;

var protoEditionsGroup = `edition = "2023";
message A {\
group uint32 a = 1;\
}`;

var noQuote = `edition = "2023";
message Foo {
reserved bar, baz;
}`;

tape.test("editions grammar", function(test) {
test.throws(function() {
protobuf.parse(protoEditionsRequired);
}`);
}, Error, "Error: illegal token 'required'");

test.end();
});

tape.test("editions optional keyword", function(test) {
test.throws(function() {
protobuf.parse(protoEditionsOptional);
protobuf.parse(`edition = "2023";
message A {\
optional uint32 a = 1;\
}`);
}, Error, "Error: illegal token 'optional'");

test.end();
});

tape.test("editions group keyword", function(test) {
test.throws(function() {
protobuf.parse(protoEditionsGroup);
protobuf.parse(`edition = "2023";
message A {\
group uint32 a = 1;\
}`);
}, Error, "Error: illegal token 'group'");

test.ok(protobuf.parse(noQuote));
test.end();
});

tape.test("editions no quote", function(test) {
test.ok(protobuf.parse(`edition = "2023";
message Foo {
reserved bar, baz;
}`));

test.end();
});

Loading

0 comments on commit caad500

Please sign in to comment.