forked from protobufjs/protobuf.js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parsed options (protobufjs#1256)
* Add option parsing tests to ensure no regressions are caused * Properly parse option values into objects in addition to the regular option parsing. Repeated options are preserved as well as repeated fields within nested options. Parsed options are kept in a parsedOptions field on every level (message, field etc.) * fix: bad merge * fix: lint * fix: lint * fix: lint * fix: lint * fix: lint * fix: build types Co-authored-by: Alexander Fenster <fenster@google.com> Co-authored-by: Alexander Fenster <github@fenster.name>
- Loading branch information
1 parent
712ee69
commit 04b4173
Showing
8 changed files
with
396 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
var tape = require("tape"); | ||
var protobuf = require(".."); | ||
|
||
tape.test("Options", function (test) { | ||
var root = protobuf.loadSync("tests/data/options_test.proto"); | ||
|
||
test.test(test.name + " - field options (Int)", function (test) { | ||
var TestFieldOptionsInt = root.lookup("TestFieldOptionsInt"); | ||
test.equal(TestFieldOptionsInt.fields.field1.options["(fo_rep_int)"], 2, "should take second repeated int option"); | ||
test.same(TestFieldOptionsInt.fields.field1.parsedOptions, [{"(fo_rep_int)": 1}, {"(fo_rep_int)": 2}], "should take all repeated int option"); | ||
|
||
test.equal(TestFieldOptionsInt.fields.field2.options["(fo_single_int)"], 3, "should correctly parse single int option"); | ||
test.same(TestFieldOptionsInt.fields.field2.parsedOptions, [{"(fo_single_int)": 3}], "should correctly parse single int option"); | ||
test.end(); | ||
}); | ||
|
||
test.test(test.name + " - message options (Int)", function (test) { | ||
var TestMessageOptionsInt = root.lookup("TestMessageOptionsInt"); | ||
test.equal(TestMessageOptionsInt.options["(mo_rep_int)"], 2, "should take second repeated int message option"); | ||
test.equal(TestMessageOptionsInt.options["(mo_single_int)"], 3, "should correctly parse single int message option"); | ||
test.same(TestMessageOptionsInt.parsedOptions, [{"(mo_rep_int)": 1}, {"(mo_rep_int)": 2}, {"(mo_single_int)": 3}], "should take all int message option"); | ||
test.end(); | ||
}); | ||
|
||
test.test(test.name + " - field options (Message)", function (test) { | ||
var TestFieldOptionsMsg = root.lookup("TestFieldOptionsMsg"); | ||
test.equal(TestFieldOptionsMsg.fields.field1.options["(fo_rep_msg).value"], 4, "should take second repeated message option"); | ||
test.equal(TestFieldOptionsMsg.fields.field1.options["(fo_rep_msg).rep_value"], 6, "should take second repeated int in second repeated option"); | ||
test.same(TestFieldOptionsMsg.fields.field1.parsedOptions, [ | ||
{"(fo_rep_msg)": {value: 1, rep_value: [2, 3]}}, | ||
{"(fo_rep_msg)": {value: 4, rep_value: [5, 6]}}], "should take all repeated message option"); | ||
test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).value"], 7, "should correctly parse single msg option"); | ||
test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).rep_value"], 9, "should take second repeated int in single msg option"); | ||
test.same(TestFieldOptionsMsg.fields.field2.parsedOptions, [{"(fo_single_msg)": {value: 7, rep_value: [8,9]}}], "should take all repeated message option"); | ||
test.end(); | ||
}); | ||
|
||
test.test(test.name + " - message options (Message)", function (test) { | ||
var TestMessageOptionsMsg = root.lookup("TestMessageOptionsMsg"); | ||
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).value"], 4, "should take second repeated message option"); | ||
test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).rep_value"], 6, "should take second repeated int in second repeated option"); | ||
test.equal(TestMessageOptionsMsg.options["(mo_single_msg).value"], 7, "should correctly parse single msg option"); | ||
test.equal(TestMessageOptionsMsg.options["(mo_single_msg).rep_value"], 9, "should take second repeated int in single msg option"); | ||
test.same(TestMessageOptionsMsg.parsedOptions, [ | ||
{"(mo_rep_msg)": {value: 1, rep_value: [2, 3]}}, | ||
{"(mo_rep_msg)": {value: 4, rep_value: [5, 6]}}, | ||
{"(mo_single_msg)": {value: 7, rep_value: [8, 9]}}, | ||
], "should take all message options"); | ||
test.end(); | ||
}); | ||
|
||
test.test(test.name + " - field options (Nested)", function (test) { | ||
var TestFieldOptionsNested = root.lookup("TestFieldOptionsNested"); | ||
test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).value"], 1, "should merge repeated options messages"); | ||
test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).rep_value"], 3, "should parse in any order"); | ||
test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).nested.nested.value"], "x", "should correctly parse nested field options"); | ||
test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).rep_nested.value"], "z", "should take second repeated nested options"); | ||
test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).nested.value"], "w", "should merge nested options"); | ||
test.same(TestFieldOptionsNested.fields.field1.parsedOptions,[ | ||
{"(fo_rep_msg)": {value: 1, nested: { nested: { value: "x"}}, rep_nested: [{value: "y"},{value: "z"}], rep_value: 3}}, | ||
{"(fo_rep_msg)": { nested: { value: "w"}}}, | ||
],"should parse all options including nested"); | ||
|
||
test.equal(TestFieldOptionsNested.fields.field2.options["(fo_single_msg).nested.value"], "x", "should correctly parse nested property name"); | ||
test.equal(TestFieldOptionsNested.fields.field2.options["(fo_single_msg).rep_nested.value"], "y", "should take second repeated nested options"); | ||
test.same(TestFieldOptionsNested.fields.field2.parsedOptions, [{ | ||
"(fo_single_msg)": { | ||
nested: {value: "x"}, | ||
rep_nested: [{value: "x"}, {value: "y"}] | ||
} | ||
} | ||
], "should parse single nested option correctly"); | ||
|
||
test.equal(TestFieldOptionsNested.fields.field3.options["(fo_single_msg).nested.value"], "x", "should correctly parse nested field options"); | ||
test.equal(TestFieldOptionsNested.fields.field3.options["(fo_single_msg).nested.nested.nested.value"], "y", "should correctly parse several nesting levels"); | ||
test.same(TestFieldOptionsNested.fields.field3.parsedOptions, [{ | ||
"(fo_single_msg)": { | ||
nested: { | ||
value: "x", | ||
nested: {nested: {value: "y"}} | ||
} | ||
} | ||
}], "should correctly parse several nesting levels"); | ||
|
||
test.end(); | ||
}); | ||
|
||
test.test(test.name + " - message options (Nested)", function (test) { | ||
var TestMessageOptionsNested = root.lookup("TestMessageOptionsNested"); | ||
test.equal(TestMessageOptionsNested.options["(mo_rep_msg).value"], 1, "should merge repeated options messages"); | ||
test.equal(TestMessageOptionsNested.options["(mo_rep_msg).rep_value"], 3, "should parse in any order"); | ||
test.equal(TestMessageOptionsNested.options["(mo_rep_msg).nested.nested.value"], "x", "should correctly parse nested field options"); | ||
test.equal(TestMessageOptionsNested.options["(mo_rep_msg).rep_nested.value"], "z", "should take second repeated nested options"); | ||
test.equal(TestMessageOptionsNested.options["(mo_rep_msg).nested.value"], "w", "should merge nested options"); | ||
|
||
test.equal(TestMessageOptionsNested.options["(mo_single_msg).nested.value"], "x", "should correctly parse nested property name"); | ||
test.equal(TestMessageOptionsNested.options["(mo_single_msg).rep_nested.value"], "y", "should take second repeated nested options"); | ||
test.equal(TestMessageOptionsNested.options["(mo_single_msg).rep_nested.nested.nested.value"], "y", "should correctly parse several nesting levels"); | ||
|
||
test.same(TestMessageOptionsNested.parsedOptions, [ | ||
{ | ||
"(mo_rep_msg)": { | ||
value: 1, | ||
nested: {nested: {value: "x"}}, | ||
rep_nested: [{value: "y"}, {value: "z"}], | ||
rep_value: 3 | ||
} | ||
}, | ||
{"(mo_rep_msg)": {nested: {value: "w"}}}, | ||
{ | ||
"(mo_single_msg)": { | ||
nested: {value: "x"}, | ||
rep_nested: [{value: "x", nested: {nested: {value: "y"}}}, {value: "y"}] | ||
} | ||
} | ||
], "should correctly parse all nested message options"); | ||
test.end(); | ||
}); | ||
|
||
test.end(); | ||
}); |
Oops, something went wrong.