|  | 
|  | 1 | +var tape = require("tape"); | 
|  | 2 | +var protobuf = require(".."); | 
|  | 3 | + | 
|  | 4 | +tape.test("Options", function (test) { | 
|  | 5 | +    var root = protobuf.loadSync("tests/data/options_test.proto"); | 
|  | 6 | + | 
|  | 7 | +    test.test(test.name + " - field options (Int)", function (test) { | 
|  | 8 | +        var TestFieldOptionsInt = root.lookup("TestFieldOptionsInt"); | 
|  | 9 | +        test.equal(TestFieldOptionsInt.fields.field1.options["(fo_rep_int)"], 2, "should take second repeated int option"); | 
|  | 10 | +        test.same(TestFieldOptionsInt.fields.field1.parsedOptions, [{"(fo_rep_int)": 1}, {"(fo_rep_int)": 2}], "should take all repeated int option"); | 
|  | 11 | + | 
|  | 12 | +        test.equal(TestFieldOptionsInt.fields.field2.options["(fo_single_int)"], 3, "should correctly parse single int option"); | 
|  | 13 | +        test.same(TestFieldOptionsInt.fields.field2.parsedOptions, [{"(fo_single_int)": 3}], "should correctly parse single int option"); | 
|  | 14 | +        test.end(); | 
|  | 15 | +    }); | 
|  | 16 | + | 
|  | 17 | +    test.test(test.name + " - message options (Int)", function (test) { | 
|  | 18 | +        var TestMessageOptionsInt = root.lookup("TestMessageOptionsInt"); | 
|  | 19 | +        test.equal(TestMessageOptionsInt.options["(mo_rep_int)"], 2, "should take second repeated int message option"); | 
|  | 20 | +        test.equal(TestMessageOptionsInt.options["(mo_single_int)"], 3, "should correctly parse single int message option"); | 
|  | 21 | +        test.same(TestMessageOptionsInt.parsedOptions, [{"(mo_rep_int)": 1}, {"(mo_rep_int)": 2}, {"(mo_single_int)": 3}], "should take all int message option"); | 
|  | 22 | +        test.end(); | 
|  | 23 | +    }); | 
|  | 24 | + | 
|  | 25 | +    test.test(test.name + " - field options (Message)", function (test) { | 
|  | 26 | +        var TestFieldOptionsMsg = root.lookup("TestFieldOptionsMsg"); | 
|  | 27 | +        test.equal(TestFieldOptionsMsg.fields.field1.options["(fo_rep_msg).value"], 4, "should take second repeated message option"); | 
|  | 28 | +        test.equal(TestFieldOptionsMsg.fields.field1.options["(fo_rep_msg).rep_value"], 6, "should take second repeated int in second repeated option"); | 
|  | 29 | +        test.same(TestFieldOptionsMsg.fields.field1.parsedOptions, [ | 
|  | 30 | +            {"(fo_rep_msg)": {value: 1, rep_value: [2, 3]}}, | 
|  | 31 | +            {"(fo_rep_msg)": {value: 4, rep_value: [5, 6]}}], "should take all repeated message option"); | 
|  | 32 | +        test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).value"], 7, "should correctly parse single msg option"); | 
|  | 33 | +        test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).rep_value"], 9, "should take second repeated int in single msg option"); | 
|  | 34 | +        test.same(TestFieldOptionsMsg.fields.field2.parsedOptions, [{"(fo_single_msg)": {value: 7, rep_value: [8,9]}}], "should take all repeated message option"); | 
|  | 35 | +        test.end(); | 
|  | 36 | +    }); | 
|  | 37 | + | 
|  | 38 | +    test.test(test.name + " - message options (Message)", function (test) { | 
|  | 39 | +        var TestMessageOptionsMsg = root.lookup("TestMessageOptionsMsg"); | 
|  | 40 | +        test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).value"], 4, "should take second repeated message option"); | 
|  | 41 | +        test.equal(TestMessageOptionsMsg.options["(mo_rep_msg).rep_value"], 6, "should take second repeated int in second repeated option"); | 
|  | 42 | +        test.equal(TestMessageOptionsMsg.options["(mo_single_msg).value"], 7, "should correctly parse single msg option"); | 
|  | 43 | +        test.equal(TestMessageOptionsMsg.options["(mo_single_msg).rep_value"], 9, "should take second repeated int in single msg option"); | 
|  | 44 | +        test.same(TestMessageOptionsMsg.parsedOptions, [ | 
|  | 45 | +            {"(mo_rep_msg)": {value: 1, rep_value: [2, 3]}}, | 
|  | 46 | +            {"(mo_rep_msg)": {value: 4, rep_value: [5, 6]}}, | 
|  | 47 | +            {"(mo_single_msg)": {value: 7, rep_value: [8, 9]}}, | 
|  | 48 | +        ], "should take all message options"); | 
|  | 49 | +        test.end(); | 
|  | 50 | +    }); | 
|  | 51 | + | 
|  | 52 | +    test.test(test.name + " - field options (Nested)", function (test) { | 
|  | 53 | +        var TestFieldOptionsNested = root.lookup("TestFieldOptionsNested"); | 
|  | 54 | +        test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).value"], 1, "should merge repeated options messages"); | 
|  | 55 | +        test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).rep_value"], 3, "should parse in any order"); | 
|  | 56 | +        test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).nested.nested.value"], "x", "should correctly parse nested field options"); | 
|  | 57 | +        test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).rep_nested.value"], "z", "should take second repeated nested options"); | 
|  | 58 | +        test.equal(TestFieldOptionsNested.fields.field1.options["(fo_rep_msg).nested.value"], "w", "should merge nested options"); | 
|  | 59 | +        test.same(TestFieldOptionsNested.fields.field1.parsedOptions,[ | 
|  | 60 | +            {"(fo_rep_msg)": {value: 1, nested: { nested: { value: "x"}}, rep_nested: [{value: "y"},{value: "z"}], rep_value: 3}}, | 
|  | 61 | +            {"(fo_rep_msg)": { nested: { value: "w"}}}, | 
|  | 62 | +        ],"should parse all options including nested"); | 
|  | 63 | + | 
|  | 64 | +        test.equal(TestFieldOptionsNested.fields.field2.options["(fo_single_msg).nested.value"], "x", "should correctly parse nested property name"); | 
|  | 65 | +        test.equal(TestFieldOptionsNested.fields.field2.options["(fo_single_msg).rep_nested.value"], "y", "should take second repeated nested options"); | 
|  | 66 | +        test.same(TestFieldOptionsNested.fields.field2.parsedOptions, [{ | 
|  | 67 | +            "(fo_single_msg)": { | 
|  | 68 | +                nested: {value: "x"}, | 
|  | 69 | +                rep_nested: [{value: "x"}, {value: "y"}] | 
|  | 70 | +            } | 
|  | 71 | +        } | 
|  | 72 | +        ], "should parse single nested option correctly"); | 
|  | 73 | + | 
|  | 74 | +        test.equal(TestFieldOptionsNested.fields.field3.options["(fo_single_msg).nested.value"], "x", "should correctly parse nested field options"); | 
|  | 75 | +        test.equal(TestFieldOptionsNested.fields.field3.options["(fo_single_msg).nested.nested.nested.value"], "y", "should correctly parse several nesting levels"); | 
|  | 76 | +        test.same(TestFieldOptionsNested.fields.field3.parsedOptions, [{ | 
|  | 77 | +            "(fo_single_msg)": { | 
|  | 78 | +                nested: { | 
|  | 79 | +                    value: "x", | 
|  | 80 | +                    nested: {nested: {value: "y"}} | 
|  | 81 | +                } | 
|  | 82 | +            } | 
|  | 83 | +        }], "should correctly parse several nesting levels"); | 
|  | 84 | + | 
|  | 85 | +        test.end(); | 
|  | 86 | +    }); | 
|  | 87 | + | 
|  | 88 | +    test.test(test.name + " - message options (Nested)", function (test) { | 
|  | 89 | +        var TestMessageOptionsNested = root.lookup("TestMessageOptionsNested"); | 
|  | 90 | +        test.equal(TestMessageOptionsNested.options["(mo_rep_msg).value"], 1, "should merge repeated options messages"); | 
|  | 91 | +        test.equal(TestMessageOptionsNested.options["(mo_rep_msg).rep_value"], 3, "should parse in any order"); | 
|  | 92 | +        test.equal(TestMessageOptionsNested.options["(mo_rep_msg).nested.nested.value"], "x", "should correctly parse nested field options"); | 
|  | 93 | +        test.equal(TestMessageOptionsNested.options["(mo_rep_msg).rep_nested.value"], "z", "should take second repeated nested options"); | 
|  | 94 | +        test.equal(TestMessageOptionsNested.options["(mo_rep_msg).nested.value"], "w", "should merge nested options"); | 
|  | 95 | + | 
|  | 96 | +        test.equal(TestMessageOptionsNested.options["(mo_single_msg).nested.value"], "x", "should correctly parse nested property name"); | 
|  | 97 | +        test.equal(TestMessageOptionsNested.options["(mo_single_msg).rep_nested.value"], "y", "should take second repeated nested options"); | 
|  | 98 | +        test.equal(TestMessageOptionsNested.options["(mo_single_msg).rep_nested.nested.nested.value"], "y", "should correctly parse several nesting levels"); | 
|  | 99 | + | 
|  | 100 | +        test.same(TestMessageOptionsNested.parsedOptions, [ | 
|  | 101 | +            { | 
|  | 102 | +                "(mo_rep_msg)": { | 
|  | 103 | +                    value: 1, | 
|  | 104 | +                    nested: {nested: {value: "x"}}, | 
|  | 105 | +                    rep_nested: [{value: "y"}, {value: "z"}], | 
|  | 106 | +                    rep_value: 3 | 
|  | 107 | +                } | 
|  | 108 | +            }, | 
|  | 109 | +            {"(mo_rep_msg)": {nested: {value: "w"}}}, | 
|  | 110 | +            { | 
|  | 111 | +                "(mo_single_msg)": { | 
|  | 112 | +                    nested: {value: "x"}, | 
|  | 113 | +                    rep_nested: [{value: "x", nested: {nested: {value: "y"}}}, {value: "y"}] | 
|  | 114 | +                } | 
|  | 115 | +            } | 
|  | 116 | +        ], "should correctly parse all nested message options"); | 
|  | 117 | +        test.end(); | 
|  | 118 | +    }); | 
|  | 119 | + | 
|  | 120 | +    test.end(); | 
|  | 121 | +}); | 
0 commit comments