Skip to content

Commit

Permalink
Merge pull request #679 from schwiet/fix-allow-alias
Browse files Browse the repository at this point in the history
handle allow_alias option for Enums
  • Loading branch information
dcodeIO authored Feb 23, 2017
2 parents e3c74f2 + d65c229 commit a6563fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Enum.prototype.add = function(name, id, comment) {
if (this.values[name] !== undefined)
throw Error("duplicate name");

if (this.valuesById[id] !== undefined)
if (this.valuesById[id] !== undefined && !(this.options && this.options.allow_alias))
throw Error("duplicate id");

this.valuesById[this.values[name] = id] = name;
Expand Down
13 changes: 11 additions & 2 deletions tests/api_enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ tape.test("reflected enums", function(test) {
b: 2
});

var enm_allow_alias = new protobuf.Enum( 'AliasTest',
{ a: 0 }, { allow_alias: true } );

test.throws(function() {
new protobuf.Enum("Test", true);
}, TypeError, "should throw if values is specified but not an object");
Expand All @@ -32,7 +35,7 @@ tape.test("reflected enums", function(test) {

test.throws(function() {
enm.add("c", 2);
}, Error, "should throw if id is a duplicate");
}, Error, "should throw if id is a duplicate, without allow_alias option");

enm.add("c", 3);
test.same(enm.values, {
Expand Down Expand Up @@ -72,5 +75,11 @@ tape.test("reflected enums", function(test) {
}
}, "should export options and values to JSON");

enm_allow_alias.add( 'b', 0 );
test.same( enm_allow_alias.values, {
a: 0,
b: 0
});

test.end();
});
});

0 comments on commit a6563fe

Please sign in to comment.