Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] implement rawOptions #1254

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ ReflectionObject.prototype.getOption = function getOption(name) {
ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {
if (!ifNotSet || !this.options || this.options[name] === undefined)
(this.options || (this.options = {}))[name] = value;
var rawOption = {};
rawOption[name] = value;
(this.rawOptions || (this.rawOptions = [])).push(rawOption);
return this;
};

Expand Down
40 changes: 34 additions & 6 deletions tests/comp_options-textformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,41 @@ message Test {\
string value = 1 [(my_options) = { a: \"foo\" b: \"bar\" }];\
string value2 = 2 [(my_options) = { a: \"foo\" b { c: \"bar\" } }];\
string value3 = 3 [(my_options) = { a: \"foo\", b: \"bar\" }];\
}";
}\
message TestRepeated {\
string value = 1 [(my_options) = { a: \"foo\" a:\"foo2\" b: \"bar\" } , (my_options) = { a: \"foo3\" a:\"foo4\" b: \"bar2\" }];\
string value2 = 2 [(my_options) = { a: \"foo\" b { c: \"bar\" }, b { c: \"bar2\"} }];\
string value3 = 3 [(my_options) = { a: \"foo\", b: \"bar\", b: \"bar2\" }];\
}\
";

tape.test("options in textformat", function(test) {
var root = protobuf.parse(proto).root;
var Test = root.lookup("Test");
test.same(Test.fields.value.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly");
test.same(Test.fields.value2.options, { "(my_options).a": "foo", "(my_options).b.c": "bar" }, "should parse correctly when nested");
test.same(Test.fields.value3.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly when comma-separated");
test.end();
test.test("test single options", function (test) {
var Test = root.lookup("Test");
test.same(Test.fields.value.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly");
test.same(Test.fields.value.rawOptions, [{"(my_options).a": "foo"}, {"(my_options).b": "bar"}], "should parse rawOptions correctly");

test.same(Test.fields.value2.options, { "(my_options).a": "foo", "(my_options).b.c": "bar" }, "should parse correctly when nested");
test.same(Test.fields.value2.rawOptions, [{"(my_options).a": "foo"}, {"(my_options).b.c": "bar"}], "should parse rawOptions correctly when nested");

test.same(Test.fields.value3.options, { "(my_options).a": "foo", "(my_options).b": "bar" }, "should parse correctly when comma-separated");
test.same(Test.fields.value3.rawOptions, [{"(my_options).a": "foo"}, {"(my_options).b": "bar"}], "should parse rawOptions correctly when comma-separated");
test.end();
});

test.test("test repeated options", function (test) {
var Test = root.lookup("TestRepeated");
test.same(Test.fields.value.options, { "(my_options).a": "foo4", "(my_options).b": "bar2" }, "should take last repeated option");
test.same(Test.fields.value.rawOptions, [{"(my_options).a": "foo"},{"(my_options).a": "foo2"}, {"(my_options).b": "bar"},
{"(my_options).a": "foo3"},{"(my_options).a": "foo4"}, {"(my_options).b": "bar2"}], "should parse all repeated option");

test.same(Test.fields.value2.options, { "(my_options).a": "foo", "(my_options).b.c": "bar2" }, "should take last repeated option when nested");
test.same(Test.fields.value2.rawOptions, [{"(my_options).a": "foo"}, {"(my_options).b.c": "bar"}, {"(my_options).b.c": "bar2"}], "should parse all repeated option when nested");

test.same(Test.fields.value3.options, { "(my_options).a": "foo", "(my_options).b": "bar2" }, "should take last repeated option when comma-separated");
test.same(Test.fields.value3.rawOptions, [{"(my_options).a": "foo"}, {"(my_options).b": "bar"}, {"(my_options).b": "bar2"}], "should parse all repeated option when comma-separated");
test.end();
});

});