Skip to content

Commit d65c229

Browse files
author
sschwiet
committed
allow_alias enum option was not being honored. This case is now handled and a test case was added
1 parent 2ddb76b commit d65c229

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/enum.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Enum.prototype.add = function(name, id, comment) {
9191
if (this.values[name] !== undefined)
9292
throw Error("duplicate name");
9393

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

9797
this.valuesById[this.values[name] = id] = name;

tests/api_enum.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ tape.test("reflected enums", function(test) {
99
b: 2
1010
});
1111

12+
var enm_allow_alias = new protobuf.Enum( 'AliasTest',
13+
{ a: 0 }, { allow_alias: true } );
14+
1215
test.throws(function() {
1316
new protobuf.Enum("Test", true);
1417
}, TypeError, "should throw if values is specified but not an object");
@@ -32,7 +35,7 @@ tape.test("reflected enums", function(test) {
3235

3336
test.throws(function() {
3437
enm.add("c", 2);
35-
}, Error, "should throw if id is a duplicate");
38+
}, Error, "should throw if id is a duplicate, without allow_alias option");
3639

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

78+
enm_allow_alias.add( 'b', 0 );
79+
test.same( enm_allow_alias.values, {
80+
a: 0,
81+
b: 0
82+
});
83+
7584
test.end();
76-
});
85+
});

0 commit comments

Comments
 (0)