Skip to content

Commit

Permalink
allow_alias enum option was not being honored. This case is now handl…
Browse files Browse the repository at this point in the history
…ed and a test case was added
  • Loading branch information
sschwiet committed Feb 13, 2017
1 parent 2ddb76b commit d65c229
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 d65c229

Please sign in to comment.