| 1 | // Copyright (c) 2017 Ernest Micklei |
| 2 | // |
| 3 | // MIT License |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining |
| 6 | // a copy of this software and associated documentation files (the |
| 7 | // "Software"), to deal in the Software without restriction, including |
| 8 | // without limitation the rights to use, copy, modify, merge, publish, |
| 9 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | // permit persons to whom the Software is furnished to do so, subject to |
| 11 | // the following conditions: |
| 12 | // |
| 13 | // The above copyright notice and this permission notice shall be |
| 14 | // included in all copies or substantial portions of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | |
| 24 | package proto |
| 25 | |
| 26 | import ( |
| 27 | "testing" |
| 28 | ) |
| 29 | |
| 30 | func TestEnum(t *testing.T) { |
| 31 | proto := ` |
| 32 | // enum |
| 33 | enum EnumAllowingAlias { |
| 34 | reserved 998, 1000 to 2000; |
| 35 | reserved "HELLO", "WORLD"; |
| 36 | option allow_alias = true; |
| 37 | UNKNOWN = 0; |
| 38 | STARTED = 1; |
| 39 | RUNNING = 2 [(custom_option) = "hello world"]; |
| 40 | NEG = -42; |
| 41 | SOMETHING_FOO = 0 [ |
| 42 | (bar.enum_value_option) = true, |
| 43 | (bar.enum_value_dep_option) = { hello: 1 } |
| 44 | ]; |
| 45 | }` |
| 46 | p := newParserOn(proto) |
| 47 | pr, err := p.Parse() |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | enums := collect(pr).Enums() |
| 52 | if got, want := len(enums), 1; got != want { |
| 53 | t.Errorf("got [%v] want [%v]", got, want) |
| 54 | } |
| 55 | if got, want := len(enums[0].Elements), 8; got != want { |
| 56 | t.Errorf("got [%v] want [%v]", got, want) |
| 57 | } |
| 58 | if got, want := enums[0].Comment != nil, true; got != want { |
| 59 | t.Fatalf("got [%v] want [%v]", got, want) |
| 60 | } |
| 61 | if got, want := enums[0].Comment.Message(), " enum"; got != want { |
| 62 | t.Errorf("got [%v] want [%v]", enums[0].Comment, want) |
| 63 | } |
| 64 | if got, want := enums[0].Position.Line, 3; got != want { |
| 65 | t.Errorf("got [%d] want [%d]", got, want) |
| 66 | } |
| 67 | // enum reserved ids |
| 68 | e1 := enums[0].Elements[0].(*Reserved) |
| 69 | if got, want := len(e1.Ranges), 2; got != want { |
| 70 | t.Errorf("got [%d] want [%d]", got, want) |
| 71 | } |
| 72 | e1rg0 := e1.Ranges[0] |
| 73 | if got, want := e1rg0.From, 998; got != want { |
| 74 | t.Errorf("got [%d] want [%d]", got, want) |
| 75 | } |
| 76 | if got, want := e1rg0.From, e1rg0.To; got != want { |
| 77 | t.Errorf("got [%d] want [%d]", got, want) |
| 78 | } |
| 79 | e1rg1 := e1.Ranges[1] |
| 80 | if got, want := e1rg1.From, 1000; got != want { |
| 81 | t.Errorf("got [%d] want [%d]", got, want) |
| 82 | } |
| 83 | if got, want := e1rg1.To, 2000; got != want { |
| 84 | t.Errorf("got [%d] want [%d]", got, want) |
| 85 | } |
| 86 | // enum reserved field names |
| 87 | e2 := enums[0].Elements[1].(*Reserved) |
| 88 | if got, want := len(e2.FieldNames), 2; got != want { |
| 89 | t.Errorf("got [%d] want [%d]", got, want) |
| 90 | } |
| 91 | e2fn0 := e2.FieldNames[0] |
| 92 | if got, want := e2fn0, "HELLO"; got != want { |
| 93 | t.Errorf("got [%s] want [%s]", got, want) |
| 94 | } |
| 95 | e2fn1 := e2.FieldNames[1] |
| 96 | if got, want := e2fn1, "WORLD"; got != want { |
| 97 | t.Errorf("got [%s] want [%s]", got, want) |
| 98 | } |
| 99 | // enum option |
| 100 | checkParent(enums[0].Elements[2].(*Option), t) |
| 101 | // enum values |
| 102 | ef1 := enums[0].Elements[3].(*EnumField) |
| 103 | if got, want := ef1.Integer, 0; got != want { |
| 104 | t.Errorf("got [%v] want [%v]", got, want) |
| 105 | } |
| 106 | if got, want := ef1.Position.Line, 7; got != want { |
| 107 | t.Errorf("got [%d] want [%d]", got, want) |
| 108 | } |
| 109 | ef3 := enums[0].Elements[5].(*EnumField) |
| 110 | if got, want := ef3.Integer, 2; got != want { |
| 111 | t.Errorf("got [%v] want [%v]", got, want) |
| 112 | } |
| 113 | ef3opt := ef3.Elements[0].(*Option) |
| 114 | if got, want := ef3opt.Name, "(custom_option)"; got != want { |
| 115 | t.Errorf("got [%v] want [%v]", got, want) |
| 116 | } |
| 117 | checkParent(ef3.Elements[0].(*Option), t) |
| 118 | // test for deprecated field |
| 119 | if got, want := ef3opt, ef3.ValueOption; got != want { |
| 120 | t.Errorf("got [%v] want [%v]", got, want) |
| 121 | } |
| 122 | if got, want := ef3opt.Constant.Source, "hello world"; got != want { |
| 123 | t.Errorf("got [%v] want [%v]", got, want) |
| 124 | } |
| 125 | if got, want := ef3.Position.Line, 9; got != want { |
| 126 | t.Errorf("got [%d] want [%d]", got, want) |
| 127 | } |
| 128 | ef4 := enums[0].Elements[6].(*EnumField) |
| 129 | if got, want := ef4.Integer, -42; got != want { |
| 130 | t.Errorf("got [%v] want [%v]", got, want) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestEnumWithHex(t *testing.T) { |
| 135 | src := `enum Flags { |
| 136 | FLAG1 = 0x11; |
| 137 | }` |
| 138 | p := newParserOn(src) |
| 139 | enum := new(Enum) |
| 140 | p.next() |
| 141 | if err := enum.parse(p); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | if got, want := len(enum.Elements), 1; got != want { |
| 145 | t.Errorf("got [%v] want [%v]", got, want) |
| 146 | } |
| 147 | if got, want := enum.Elements[0].(*EnumField).Integer, 17; got != want { |
| 148 | t.Errorf("got [%v] want [%v]", got, want) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestEnumWithDeprecatedField(t *testing.T) { |
| 153 | src := `enum Flags { |
| 154 | OLD = 1 [deprecated = true]; |
| 155 | NEW = 2; |
| 156 | }` |
| 157 | p := newParserOn(src) |
| 158 | enum := new(Enum) |
| 159 | p.next() |
| 160 | if err := enum.parse(p); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | if got, want := len(enum.Elements), 2; got != want { |
| 164 | t.Errorf("got [%v] want [%v]", got, want) |
| 165 | } |
| 166 | if got, want := enum.Elements[0].(*EnumField).IsDeprecated(), true; got != want { |
| 167 | t.Errorf("got [%v] want [%v]", got, want) |
| 168 | } |
| 169 | if got, want := enum.Elements[1].(*EnumField).IsDeprecated(), false; got != want { |
| 170 | t.Errorf("got [%v] want [%v]", got, want) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestEnumInlineCommentBeforeBody(t *testing.T) { |
| 175 | src := `enum BarEnum // BarEnum |
| 176 | // with another line |
| 177 | { |
| 178 | BAR_TYPE_INVALID= 0; |
| 179 | BAR_TYPE_BAD = 1; |
| 180 | } |
| 181 | ` |
| 182 | p := newParserOn(src) |
| 183 | e := new(Enum) |
| 184 | p.next() |
| 185 | if err := e.parse(p); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | nestedComment := e.Elements[0].(*Comment) |
| 189 | if nestedComment == nil { |
| 190 | t.Fatal("expected comment present") |
| 191 | } |
| 192 | if got, want := len(nestedComment.Lines), 2; got != want { |
| 193 | t.Errorf("got %d want %d lines", got, want) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestEnumFieldWalkWithComment(t *testing.T) { |
| 198 | src := `enum HideIt |
| 199 | { |
| 200 | PRIVATE = 1 [ |
| 201 | // hidden |
| 202 | // field |
| 203 | (google.api.value_visibility).restriction = "HIDDEN" |
| 204 | ]; |
| 205 | } |
| 206 | ` |
| 207 | p := newParserOn(src) |
| 208 | e := new(Enum) |
| 209 | p.next() |
| 210 | if err := e.parse(p); err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | var name, msg string |
| 214 | walk(e, WithOption(func(o *Option) { |
| 215 | name = o.Name |
| 216 | msg = o.Comment.Message() |
| 217 | })) |
| 218 | if got, want := name, "(google.api.value_visibility).restriction"; got != want { |
| 219 | t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want) |
| 220 | } |
| 221 | if got, want := msg, " hidden"; got != want { |
| 222 | t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want) |
| 223 | } |
| 224 | } |