| 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package containers |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/common/ast" |
| 23 | ) |
| 24 | |
| 25 | func TestContainers_ResolveCandidateNames(t *testing.T) { |
| 26 | c, err := NewContainer(Name("a.b.c.M.N")) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | names := c.ResolveCandidateNames("R.s") |
| 31 | want := []string{ |
| 32 | "a.b.c.M.N.R.s", |
| 33 | "a.b.c.M.R.s", |
| 34 | "a.b.c.R.s", |
| 35 | "a.b.R.s", |
| 36 | "a.R.s", |
| 37 | "R.s", |
| 38 | } |
| 39 | if !reflect.DeepEqual(names, want) { |
| 40 | t.Errorf("got %v, wanted %v", names, want) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestContainers_ResolveCandidateNames_FullyQualifiedName(t *testing.T) { |
| 45 | c, err := NewContainer(Name("a.b.c.M.N")) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | // The leading '.' indicates the name is already fully-qualified. |
| 50 | names := c.ResolveCandidateNames(".R.s") |
| 51 | want := []string{"R.s"} |
| 52 | if !reflect.DeepEqual(names, want) { |
| 53 | t.Errorf("got %v, wanted %v", names, want) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestContainers_ResolveCandidateNames_EmptyContainer(t *testing.T) { |
| 58 | names := DefaultContainer.ResolveCandidateNames("R.s") |
| 59 | want := []string{"R.s"} |
| 60 | if !reflect.DeepEqual(names, want) { |
| 61 | t.Errorf("got %v, wanted %v", names, want) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestContainers_Alias(t *testing.T) { |
| 66 | cont, err := DefaultContainer.Extend(Alias("my.example.pkg.verbose", "bigex")) |
| 67 | if err != nil { |
| 68 | t.Fatalf("Extend() failed: %v", err) |
| 69 | } |
| 70 | if !reflect.DeepEqual(cont.ResolveCandidateNames("bigex.Execute"), []string{"my.example.pkg.verbose.Execute"}) { |
| 71 | t.Errorf("ResolveCandidateNames() got %s, wanted %s", cont.ResolveCandidateNames("bigex.Execute"), "my.example.pkg.verbose.Execute") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestContainers_Abbrevs(t *testing.T) { |
| 76 | abbr, err := DefaultContainer.Extend(Abbrevs("my.alias.R")) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | names := abbr.ResolveCandidateNames("R") |
| 81 | want := []string{ |
| 82 | "my.alias.R", |
| 83 | } |
| 84 | if !reflect.DeepEqual(names, want) { |
| 85 | t.Errorf("got %v, wanted %v", names, want) |
| 86 | } |
| 87 | c, err := NewContainer(Name("a.b.c"), Abbrevs("my.alias.R")) |
| 88 | if err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | names = c.ResolveCandidateNames("R") |
| 92 | want = []string{ |
| 93 | "my.alias.R", |
| 94 | } |
| 95 | if !reflect.DeepEqual(names, want) { |
| 96 | t.Errorf("got %v, wanted %v", names, want) |
| 97 | } |
| 98 | names = c.ResolveCandidateNames("R.S.T") |
| 99 | want = []string{ |
| 100 | "my.alias.R.S.T", |
| 101 | } |
| 102 | if !reflect.DeepEqual(names, want) { |
| 103 | t.Errorf("got %v, wanted %v", names, want) |
| 104 | } |
| 105 | names = c.ResolveCandidateNames("S") |
| 106 | want = []string{ |
| 107 | "a.b.c.S", |
| 108 | "a.b.S", |
| 109 | "a.S", |
| 110 | "S", |
| 111 | } |
| 112 | if !reflect.DeepEqual(names, want) { |
| 113 | t.Errorf("got %v, wanted %v", names, want) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestContainers_Aliasing_Errors(t *testing.T) { |
| 118 | type aliasDef struct { |
| 119 | name string |
| 120 | alias string |
| 121 | } |
| 122 | tests := []struct { |
| 123 | container string |
| 124 | abbrevs []string |
| 125 | aliases []aliasDef |
| 126 | err string |
| 127 | }{ |
| 128 | { |
| 129 | abbrevs: []string{"my.alias.R", "yer.other.R"}, |
| 130 | err: "abbreviation collides with existing reference: " + |
| 131 | "name=yer.other.R, abbreviation=R, existing=my.alias.R", |
| 132 | }, |
| 133 | { |
| 134 | container: "a.b.c.M.N", |
| 135 | abbrevs: []string{"my.alias.a", "yer.other.b"}, |
| 136 | err: "abbreviation collides with container name: name=my.alias.a, " + |
| 137 | "abbreviation=a, container=a.b.c.M.N", |
| 138 | }, |
| 139 | { |
| 140 | abbrevs: []string{".bad"}, |
| 141 | err: "invalid qualified name: .bad, wanted name of the form 'qualified.name'", |
| 142 | }, |
| 143 | { |
| 144 | abbrevs: []string{"bad.alias."}, |
| 145 | err: "invalid qualified name: bad.alias., wanted name of the form 'qualified.name'", |
| 146 | }, |
| 147 | { |
| 148 | abbrevs: []string{" bad_alias1"}, |
| 149 | err: "invalid qualified name: bad_alias1, wanted name of the form 'qualified.name'", |
| 150 | }, |
| 151 | { |
| 152 | abbrevs: []string{" bad.alias! "}, |
| 153 | err: "invalid qualified name: bad.alias!, wanted name of the form 'qualified.name'", |
| 154 | }, |
| 155 | { |
| 156 | aliases: []aliasDef{{name: "a", alias: "b"}}, |
| 157 | err: "alias must refer to a valid qualified name: a", |
| 158 | }, |
| 159 | { |
| 160 | aliases: []aliasDef{{name: "my.alias", alias: "b.c"}}, |
| 161 | err: "alias must be non-empty and simple (not qualified): alias=b.c", |
| 162 | }, |
| 163 | { |
| 164 | aliases: []aliasDef{{name: ".my.qual.name", alias: "a'"}}, |
| 165 | err: "qualified name must not begin with a leading '.': .my.qual.name", |
| 166 | }, |
| 167 | } |
| 168 | for i, tst := range tests { |
| 169 | tc := tst |
| 170 | t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 171 | opts := []ContainerOption{} |
| 172 | if tc.container != "" { |
| 173 | opts = append(opts, Name(tc.container)) |
| 174 | } |
| 175 | if len(tc.abbrevs) != 0 { |
| 176 | opts = append(opts, Abbrevs(tc.abbrevs...)) |
| 177 | } |
| 178 | if len(tc.aliases) != 0 { |
| 179 | for _, a := range tc.aliases { |
| 180 | opts = append(opts, Alias(a.name, a.alias)) |
| 181 | } |
| 182 | } |
| 183 | _, err := NewContainer(opts...) |
| 184 | if err == nil { |
| 185 | t.Fatalf("NewContainer() succeeded, wanted err %s", tc.err) |
| 186 | } |
| 187 | if err.Error() != tc.err { |
| 188 | t.Errorf("NewContainer() got error %s, wanted error %s", err.Error(), tc.err) |
| 189 | } |
| 190 | }) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestContainers_Extend_Alias(t *testing.T) { |
| 195 | c, err := DefaultContainer.Extend(Alias("test.alias", "alias")) |
| 196 | if err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | if c.AliasSet()["alias"] != "test.alias" { |
| 200 | t.Errorf("got alias %v wanted 'test.alias'", c.AliasSet()) |
| 201 | } |
| 202 | c, err = c.Extend(Name("with.container")) |
| 203 | if err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | if c.Name() != "with.container" { |
| 207 | t.Errorf("got container name %s, wanted 'with.container'", c.Name()) |
| 208 | } |
| 209 | if c.AliasSet()["alias"] != "test.alias" { |
| 210 | t.Errorf("got alias %v wanted 'test.alias'", c.AliasSet()) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestContainers_Extend_Name(t *testing.T) { |
| 215 | c, err := DefaultContainer.Extend(Name("")) |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | if c.Name() != "" { |
| 220 | t.Errorf("got %v, wanted empty name", c.Name()) |
| 221 | } |
| 222 | c, err = DefaultContainer.Extend(Name("hello.container")) |
| 223 | if err != nil { |
| 224 | t.Fatal(err) |
| 225 | } |
| 226 | if c.Name() != "hello.container" { |
| 227 | t.Errorf("got container name %s, wanted 'hello.container'", c.Name()) |
| 228 | } |
| 229 | c, err = c.Extend(Name("goodbye.container")) |
| 230 | if err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | if c.Name() != "goodbye.container" { |
| 234 | t.Errorf("got container name %s, wanted 'goodbye.container'", c.Name()) |
| 235 | } |
| 236 | c, err = c.Extend(Name(".bad.container")) |
| 237 | if err == nil { |
| 238 | t.Errorf("got %v, wanted error", c.Name()) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func TestContainers_ToQualifiedName(t *testing.T) { |
| 243 | fac := ast.NewExprFactory() |
| 244 | ident := fac.NewIdent(1, "var") |
| 245 | idName, found := ToQualifiedName(ident) |
| 246 | if !found { |
| 247 | t.Errorf("got not found from %v expr, wanted found", ident) |
| 248 | } |
| 249 | if idName != "var" { |
| 250 | t.Errorf("got %v, wanted 'var'", idName) |
| 251 | } |
| 252 | sel := fac.NewSelect(2, ident, "qualifier") |
| 253 | qualName, found := ToQualifiedName(sel) |
| 254 | if !found { |
| 255 | t.Errorf("got not found from %v expr, wanted found", sel) |
| 256 | } |
| 257 | if qualName != "var.qualifier" { |
| 258 | t.Errorf("got %v, wanted 'var.qualifier'", qualName) |
| 259 | } |
| 260 | |
| 261 | pres := fac.NewPresenceTest(2, ident, "qualifier") |
| 262 | _, found = ToQualifiedName(pres) |
| 263 | if found { |
| 264 | t.Error("got found, wanted not found for test-only expression") |
| 265 | } |
| 266 | |
| 267 | unary := fac.NewCall(2, "!_", ident) |
| 268 | sel = fac.NewSelect(3, unary, "qualifier") |
| 269 | _, found = ToQualifiedName(sel) |
| 270 | if found { |
| 271 | t.Errorf("got found, wanted not found for %v", sel) |
| 272 | } |
| 273 | } |