| 1 | // Copyright 2023 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 decls |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | "time" |
| 22 | |
| 23 | "google.golang.org/protobuf/proto" |
| 24 | |
| 25 | chkdecls "github.com/google/cel-go/checker/decls" |
| 26 | "github.com/google/cel-go/common" |
| 27 | "github.com/google/cel-go/common/operators" |
| 28 | "github.com/google/cel-go/common/overloads" |
| 29 | "github.com/google/cel-go/common/types" |
| 30 | "github.com/google/cel-go/common/types/ref" |
| 31 | "github.com/google/cel-go/common/types/traits" |
| 32 | |
| 33 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 34 | ) |
| 35 | |
| 36 | func TestFunctionBindings(t *testing.T) { |
| 37 | sizeFunc, err := NewFunction("size", |
| 38 | MemberOverload("list_size", |
| 39 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 40 | ) |
| 41 | if err != nil { |
| 42 | t.Fatalf("NewFunction() failed: %v", err) |
| 43 | } |
| 44 | bindings, err := sizeFunc.Bindings() |
| 45 | if err != nil { |
| 46 | t.Fatalf("sizeFunc.Bindings() produced an err: %v", err) |
| 47 | } |
| 48 | if len(bindings) != 0 { |
| 49 | t.Errorf("sizeFunc.Bindings() produced %d bindings, wanted none", len(bindings)) |
| 50 | } |
| 51 | sizeFuncDef, err := NewFunction("size", |
| 52 | MemberOverload("list_size", |
| 53 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType, |
| 54 | UnaryBinding(func(list ref.Val) ref.Val { |
| 55 | sizer := list.(traits.Sizer) |
| 56 | return sizer.Size() |
| 57 | }), |
| 58 | ), |
| 59 | ) |
| 60 | if err != nil { |
| 61 | t.Fatalf("NewFunction() failed: %v", err) |
| 62 | } |
| 63 | sizeMerged, err := sizeFunc.Merge(sizeFuncDef) |
| 64 | if err != nil { |
| 65 | t.Fatalf("Merge() failed: %v", err) |
| 66 | } |
| 67 | bindings, err = sizeMerged.Bindings() |
| 68 | if err != nil { |
| 69 | t.Fatalf("sizeFunc.Bindings() produced an err: %v", err) |
| 70 | } |
| 71 | if len(bindings) != 2 { |
| 72 | t.Errorf("sizeFunc.Bindings() got %d bindings, wanted 2", len(bindings)) |
| 73 | } |
| 74 | empty := types.DefaultTypeAdapter.NativeToValue([]string{}) |
| 75 | in := types.DefaultTypeAdapter.NativeToValue([]string{"1", "2"}) |
| 76 | for _, binding := range bindings { |
| 77 | if binding.Unary == nil { |
| 78 | t.Errorf("binding missing unary implementation: %v", binding.Operator) |
| 79 | continue |
| 80 | } |
| 81 | if binding.Unary(in) != types.Int(2) { |
| 82 | t.Errorf("binding invocation got %v, wanted 2", binding.Unary(in)) |
| 83 | } |
| 84 | if binding.Unary(empty) != types.IntZero { |
| 85 | t.Errorf("binding invocation got %v, wanted 0", binding.Unary(empty)) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestFunctionVariableArgBindings(t *testing.T) { |
| 91 | splitImpl := func(str, delim string, count int64) ref.Val { |
| 92 | return types.DefaultTypeAdapter.NativeToValue(strings.SplitN(str, delim, int(count))) |
| 93 | } |
| 94 | splitFunc, err := NewFunction("split", |
| 95 | MemberOverload("string_split", |
| 96 | []*types.Type{types.StringType}, types.NewListType(types.StringType), |
| 97 | UnaryBinding(func(str ref.Val) ref.Val { |
| 98 | s := str.(types.String) |
| 99 | return splitImpl(string(s), "", -1) |
| 100 | })), |
| 101 | MemberOverload("string_split_string", |
| 102 | []*types.Type{types.StringType, types.StringType}, types.NewListType(types.StringType), |
| 103 | BinaryBinding(func(str, sep ref.Val) ref.Val { |
| 104 | s := str.(types.String) |
| 105 | delim := sep.(types.String) |
| 106 | return splitImpl(string(s), string(delim), -1) |
| 107 | })), |
| 108 | MemberOverload("string_split_string_int", |
| 109 | []*types.Type{types.StringType, types.StringType, types.IntType}, types.NewListType(types.StringType), |
| 110 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 111 | s := args[0].(types.String) |
| 112 | delim := args[1].(types.String) |
| 113 | count := args[2].(types.Int) |
| 114 | return splitImpl(string(s), string(delim), int64(count)) |
| 115 | })), |
| 116 | ) |
| 117 | if err != nil { |
| 118 | t.Fatalf("NewFunction() failed: %v", err) |
| 119 | } |
| 120 | bindings, err := splitFunc.Bindings() |
| 121 | if err != nil { |
| 122 | t.Fatalf("sizeFunc.Bindings() produced an err: %v", err) |
| 123 | } |
| 124 | if len(bindings) != 4 { |
| 125 | t.Errorf("sizeFunc.Bindings() produced %d bindings, wanted 4", len(bindings)) |
| 126 | } |
| 127 | in := types.String("hi") |
| 128 | sep := types.String("") |
| 129 | out := types.DefaultTypeAdapter.NativeToValue([]string{"h", "i"}) |
| 130 | for _, binding := range bindings { |
| 131 | if binding.Unary != nil { |
| 132 | if binding.Unary(in).Equal(out) != types.True { |
| 133 | t.Errorf("binding invocation got %v, wanted %v", binding.Unary(in), out) |
| 134 | } |
| 135 | celErr := binding.Unary(types.Bytes("hi")) |
| 136 | if !types.IsError(celErr) || !strings.Contains(celErr.(*types.Err).String(), "no such overload") { |
| 137 | t.Errorf("binding.Unary(bytes) got %v, wanted no such overload", celErr) |
| 138 | } |
| 139 | } |
| 140 | if binding.Binary != nil { |
| 141 | if binding.Binary(in, sep).Equal(out) != types.True { |
| 142 | t.Errorf("binding invocation got %v, wanted %v", binding.Binary(in, sep), out) |
| 143 | } |
| 144 | celErr := binding.Binary(types.Bytes("hi"), sep) |
| 145 | if !types.IsError(celErr) || !strings.Contains(celErr.(*types.Err).String(), "no such overload") { |
| 146 | t.Errorf("binding.Binary(bytes, string) got %v, wanted no such overload", celErr) |
| 147 | } |
| 148 | celUnk := binding.Binary(types.Bytes("hi"), types.NewUnknown(1, types.NewAttributeTrail("x"))) |
| 149 | if !types.IsUnknown(celUnk) { |
| 150 | t.Errorf("binding.Binary(bytes, unk) got %v, wanted unknown{1}", celUnk) |
| 151 | } |
| 152 | } |
| 153 | if binding.Function != nil { |
| 154 | if binding.Function(in, sep, types.IntNegOne).Equal(out) != types.True { |
| 155 | t.Errorf("binding invocation got %v, wanted %v", binding.Function(in, sep, types.IntNegOne), out) |
| 156 | } |
| 157 | celErr := binding.Function(types.Bytes("hi"), sep, types.IntOne) |
| 158 | if !types.IsError(celErr) || !strings.Contains(celErr.(*types.Err).String(), "no such overload") { |
| 159 | t.Errorf("binding.Function(bytes, string, int) got %v, wanted no such overload", celErr) |
| 160 | } |
| 161 | if binding.Operator == "split" { |
| 162 | if binding.Function(in).Equal(out) != types.True { |
| 163 | t.Errorf("binding invocation got %v, wanted %v", binding.Function(in), out) |
| 164 | } |
| 165 | if binding.Function(in, sep).Equal(out) != types.True { |
| 166 | t.Errorf("binding invocation got %v, wanted %v", binding.Function(in, sep), out) |
| 167 | } |
| 168 | out := binding.Function() |
| 169 | if !types.IsError(out) || out.(*types.Err).String() != "no such overload: split()" { |
| 170 | t.Fatalf("binding.Function() got %v, wanted error", out) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func TestFunctionZeroArityBinding(t *testing.T) { |
| 178 | now := types.DefaultTypeAdapter.NativeToValue(time.UnixMilli(1000)) |
| 179 | nowFunc, err := NewFunction("now", |
| 180 | Overload("now", []*types.Type{}, types.TimestampType, |
| 181 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 182 | return now |
| 183 | })), |
| 184 | ) |
| 185 | if err != nil { |
| 186 | t.Fatalf("NewFunction() failed: %v", err) |
| 187 | } |
| 188 | bindings, err := nowFunc.Bindings() |
| 189 | if err != nil { |
| 190 | t.Fatalf("nowFunc.Bindings() produced an err: %v", err) |
| 191 | } |
| 192 | if len(bindings) != 1 { |
| 193 | t.Errorf("nowFunc.Bindings() produced %d bindings, wanted one", len(bindings)) |
| 194 | } |
| 195 | out := bindings[0].Function() |
| 196 | if out != now { |
| 197 | t.Errorf("now() got %v, wanted %v", out, now) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func TestFunctionSingletonBinding(t *testing.T) { |
| 202 | size, err := NewFunction("size", |
| 203 | // Since the singleton requires that the operand is a traits.Sizer, the type guards can be |
| 204 | // disabled for a minor boost in performance, and because the argument type-checking |
| 205 | // doesn't actually give much additional benefit. The drawback is that invalid signatures |
| 206 | // at type-check might be valid at runtime. |
| 207 | DisableTypeGuards(true), |
| 208 | Overload("size_map", |
| 209 | []*types.Type{types.NewMapType(types.NewTypeParamType("K"), types.NewTypeParamType("V"))}, types.IntType), |
| 210 | Overload("size_list", |
| 211 | []*types.Type{types.NewListType(types.NewTypeParamType("V"))}, types.IntType), |
| 212 | Overload("size_string", |
| 213 | []*types.Type{types.StringType}, types.IntType), |
| 214 | MemberOverload("map_size", |
| 215 | []*types.Type{types.NewMapType(types.NewTypeParamType("K"), types.NewTypeParamType("V"))}, types.IntType), |
| 216 | MemberOverload("list_size", |
| 217 | []*types.Type{types.NewListType(types.NewTypeParamType("V"))}, types.IntType), |
| 218 | MemberOverload("string_size", |
| 219 | []*types.Type{types.StringType}, types.IntType), |
| 220 | SingletonUnaryBinding(func(arg ref.Val) ref.Val { |
| 221 | return arg.(traits.Sizer).Size() |
| 222 | }, traits.SizerType), |
| 223 | ) |
| 224 | if err != nil { |
| 225 | t.Fatalf("NewFunction() failed: %v", err) |
| 226 | } |
| 227 | bindings, err := size.Bindings() |
| 228 | if err != nil { |
| 229 | t.Fatalf("size.Bindings() failed: %v", err) |
| 230 | } |
| 231 | if len(bindings) != 1 { |
| 232 | t.Errorf("size.Bindings() got %d bindings, wanted 1", len(bindings)) |
| 233 | } |
| 234 | if bindings[0].Unary == nil { |
| 235 | t.Fatalf("size.Bindings() missing singleton unary binding") |
| 236 | } |
| 237 | result := bindings[0].Unary(types.String("hello")) |
| 238 | if result.Equal(types.Int(5)) != types.True { |
| 239 | t.Errorf("size('hello') got %v, wanted 5", result) |
| 240 | } |
| 241 | // Invalid at type-check, but valid since type guard checks have been disabled |
| 242 | result = bindings[0].Unary(types.Bytes("hello")) |
| 243 | if result.Equal(types.Int(5)) != types.True { |
| 244 | t.Errorf("size(b'hello') got %v, wanted 5", result) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestVariableDocumentation(t *testing.T) { |
| 249 | v := NewVariableWithDoc("var", types.StringType, "string variable") |
| 250 | doc := v.Documentation() |
| 251 | if doc.Description != v.Description() { |
| 252 | t.Errorf("doc.Description got %s, wanted %s", doc.Description, v.Description()) |
| 253 | } |
| 254 | if doc.Name != v.Name() { |
| 255 | t.Errorf("doc.Name got %s, wanted %s", doc.Name, v.Name()) |
| 256 | } |
| 257 | if doc.Type != "string" { |
| 258 | t.Errorf("doc.Type got %s, wanted string", doc.Type) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func TestFunctionDocumentation(t *testing.T) { |
| 263 | tests := []struct { |
| 264 | name string |
| 265 | fn *FunctionDecl |
| 266 | signatures []string |
| 267 | examples []string |
| 268 | }{ |
| 269 | { |
| 270 | name: "function", |
| 271 | fn: testFunction(t, "size", |
| 272 | FunctionDocs(`compute the number of entries in a list or map`), |
| 273 | MemberOverload("list_size", |
| 274 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType, |
| 275 | OverloadExamples(`[].size() // 0`, `[1, 2, 3].size() // 3`)), |
| 276 | Overload("size_list", |
| 277 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType, |
| 278 | OverloadExamples(`size([]) // 0`, `size([1, 2, 3]) // 3`))), |
| 279 | signatures: []string{ |
| 280 | "list(<T>).size() -> int", |
| 281 | "size(list(<T>)) -> int", |
| 282 | }, |
| 283 | examples: []string{ |
| 284 | `[].size() // 0`, |
| 285 | `[1, 2, 3].size() // 3`, |
| 286 | `size([]) // 0`, |
| 287 | `size([1, 2, 3]) // 3`, |
| 288 | }, |
| 289 | }, |
| 290 | { |
| 291 | name: "type", |
| 292 | fn: testFunction(t, overloads.TypeConvertType, |
| 293 | Overload(overloads.TypeConvertType, |
| 294 | []*types.Type{types.NewTypeTypeWithParam(types.NewTypeParamType("T"))}, |
| 295 | types.TypeType, |
| 296 | OverloadExamples(`type(int) // type`))), |
| 297 | signatures: []string{"type(type) -> type"}, |
| 298 | examples: []string{`type(int) // type`}, |
| 299 | }, |
| 300 | { |
| 301 | name: "unary operator", |
| 302 | fn: testFunction(t, operators.Negate, |
| 303 | FunctionDocs(`negate a numeric value`), |
| 304 | Overload(overloads.NegateInt64, []*types.Type{types.IntType}, types.IntType, |
| 305 | OverloadExamples(`-(1) // -1`))), |
| 306 | signatures: []string{"-int -> int"}, |
| 307 | examples: []string{`-(1) // -1`}, |
| 308 | }, |
| 309 | { |
| 310 | name: "binary operator", |
| 311 | fn: testFunction(t, operators.Add, |
| 312 | FunctionDocs(`add two numeric values`), |
| 313 | Overload(overloads.AddInt64, []*types.Type{types.IntType, types.IntType}, types.IntType, |
| 314 | OverloadExamples(`1 + 2 // 3`))), |
| 315 | signatures: []string{"int + int -> int"}, |
| 316 | examples: []string{`1 + 2 // 3`}, |
| 317 | }, |
| 318 | { |
| 319 | name: "index operator", |
| 320 | fn: testFunction(t, operators.Index, |
| 321 | FunctionDocs(`access a list by numeric index, zero-based`), |
| 322 | Overload(overloads.IndexList, []*types.Type{types.NewListType(types.NewTypeParamType("T")), types.IntType}, types.NewTypeParamType("T"), |
| 323 | OverloadExamples(`[1, 2, 3, 4][2] // 3`))), |
| 324 | signatures: []string{"list(<T>)[int] -> <T>"}, |
| 325 | examples: []string{`[1, 2, 3, 4][2] // 3`}, |
| 326 | }, |
| 327 | { |
| 328 | name: "conditional", |
| 329 | fn: testFunction(t, operators.Conditional, |
| 330 | FunctionDocs(`ternary operator`), |
| 331 | Overload(overloads.Conditional, |
| 332 | []*types.Type{types.BoolType, types.NewTypeParamType("T"), types.NewTypeParamType("T")}, |
| 333 | types.NewTypeParamType("T"), |
| 334 | OverloadExamples(`true ? 1 : 2 // 1`))), |
| 335 | signatures: []string{"bool ? <T> : <T> -> <T>"}, |
| 336 | examples: []string{`true ? 1 : 2 // 1`}, |
| 337 | }, |
| 338 | } |
| 339 | for _, tst := range tests { |
| 340 | tc := tst |
| 341 | t.Run(tc.name, func(t *testing.T) { |
| 342 | doc := tc.fn.Documentation() |
| 343 | if doc.Kind != common.DocFunction { |
| 344 | t.Errorf("fn.Documentation() kind got %v, wanted common.DocFunction", doc.Kind) |
| 345 | } |
| 346 | if doc.Description != tc.fn.Description() { |
| 347 | t.Errorf("doc.Description got %s, wanted %s", doc.Description, tc.fn.Description()) |
| 348 | } |
| 349 | if len(doc.Children) != len(tc.fn.OverloadDecls()) { |
| 350 | t.Fatalf("doc.Children count was %d, wanted %d", len(doc.Children), len(tc.fn.OverloadDecls())) |
| 351 | } |
| 352 | for _, child := range doc.Children { |
| 353 | found := false |
| 354 | for _, sig := range tc.signatures { |
| 355 | if child.Signature == sig { |
| 356 | found = true |
| 357 | break |
| 358 | } |
| 359 | } |
| 360 | if !found { |
| 361 | t.Errorf("unable to find signature: %s", child.Signature) |
| 362 | } |
| 363 | for _, childEx := range child.Children { |
| 364 | found = false |
| 365 | for _, ex := range tc.examples { |
| 366 | if strings.Contains(childEx.Description, ex) { |
| 367 | found = true |
| 368 | break |
| 369 | } |
| 370 | } |
| 371 | if !found { |
| 372 | t.Errorf("unable to find example: %v", childEx.Description) |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | }) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | func TestFunctionMerge(t *testing.T) { |
| 381 | sizeFunc, err := NewFunction("size", |
| 382 | FunctionDocs(`compute the number of entries in a list or map`), |
| 383 | MemberOverload("list_size", |
| 384 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 385 | MemberOverload("map_size", |
| 386 | []*types.Type{types.NewMapType(types.NewTypeParamType("K"), types.NewTypeParamType("V"))}, types.IntType), |
| 387 | ) |
| 388 | if err != nil { |
| 389 | t.Fatalf("NewFunction() failed: %v", err) |
| 390 | } |
| 391 | out, err := sizeFunc.Merge(sizeFunc) |
| 392 | if err != nil { |
| 393 | t.Errorf("sizeFunc.Merge(sizeFunc) failed: %v", err) |
| 394 | } |
| 395 | if out != sizeFunc { |
| 396 | t.Errorf("sizeFunc.Merge(sizeFunc) != sizeFunc: %v", out) |
| 397 | } |
| 398 | sizeVecFunc, err := NewFunction("size", |
| 399 | MemberOverload("vector_size", |
| 400 | []*types.Type{types.NewOpaqueType("vector", types.NewTypeParamType("T"))}, types.IntType), |
| 401 | SingletonUnaryBinding(func(sizer ref.Val) ref.Val { |
| 402 | return sizer.(traits.Sizer).Size() |
| 403 | }, traits.SizerType), |
| 404 | ) |
| 405 | if err != nil { |
| 406 | t.Fatalf("NewFunction() failed: %v", err) |
| 407 | } |
| 408 | sizeMerged, err := sizeFunc.Merge(sizeVecFunc) |
| 409 | if err != nil { |
| 410 | t.Fatalf("Merge() failed: %v", err) |
| 411 | } |
| 412 | if sizeMerged.Name() != "size" { |
| 413 | t.Errorf("Merge() produced a function with name %v, wanted 'size'", sizeMerged.Name()) |
| 414 | } |
| 415 | if len(sizeMerged.overloads) != 3 { |
| 416 | t.Errorf("Merge() produced %d overloads, wanted 3", len(sizeFunc.overloads)) |
| 417 | } |
| 418 | if sizeMerged.Description() != "compute the number of entries in a list or map" { |
| 419 | t.Errorf("Description() got %s, wanted %s", sizeMerged.Description(), |
| 420 | "compute the number of entries in a list or map") |
| 421 | } |
| 422 | overloads := map[string]bool{ |
| 423 | "list_size": true, |
| 424 | "map_size": true, |
| 425 | "vector_size": true, |
| 426 | } |
| 427 | for _, o := range sizeMerged.overloads { |
| 428 | delete(overloads, o.ID()) |
| 429 | } |
| 430 | if len(overloads) != 0 { |
| 431 | t.Errorf("Merge() did not include overloads: %v", overloads) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | func TestFunctionMergeWrongName(t *testing.T) { |
| 436 | sizeFunc, err := NewFunction("size", |
| 437 | MemberOverload("list_size", |
| 438 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 439 | ) |
| 440 | if err != nil { |
| 441 | t.Fatalf("NewFunction() failed: %v", err) |
| 442 | } |
| 443 | sizeVecFunc, err := NewFunction("sizeN", |
| 444 | MemberOverload("vector_size", |
| 445 | []*types.Type{types.NewOpaqueType("vector", types.NewTypeParamType("T"))}, types.IntType), |
| 446 | ) |
| 447 | if err != nil { |
| 448 | t.Fatalf("NewFunction() failed: %v", err) |
| 449 | } |
| 450 | _, err = sizeFunc.Merge(sizeVecFunc) |
| 451 | if err == nil || !strings.Contains(err.Error(), "unrelated functions") { |
| 452 | t.Fatalf("Merge() expected to fail, got: %v", err) |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | func TestFunctionMergeOverloadCollision(t *testing.T) { |
| 457 | sizeFunc, err := NewFunction("size", |
| 458 | MemberOverload("list_size", |
| 459 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 460 | ) |
| 461 | if err != nil { |
| 462 | t.Fatalf("NewFunction() failed: %v", err) |
| 463 | } |
| 464 | sizeVecFunc, err := NewFunction("size", |
| 465 | MemberOverload("list_size2", |
| 466 | []*types.Type{types.NewListType(types.NewTypeParamType("K"))}, types.IntType), |
| 467 | ) |
| 468 | if err != nil { |
| 469 | t.Fatalf("NewFunction() failed: %v", err) |
| 470 | } |
| 471 | _, err = sizeFunc.Merge(sizeVecFunc) |
| 472 | if err == nil || !strings.Contains(err.Error(), "declaration merge failed") { |
| 473 | t.Fatalf("Merge() expected to fail, got: %v", err) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func TestFunctionMergeOverloadArgCountRedefinition(t *testing.T) { |
| 478 | sizeFunc, err := NewFunction("size", |
| 479 | MemberOverload("list_size", |
| 480 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 481 | ) |
| 482 | if err != nil { |
| 483 | t.Fatalf("NewFunction() failed: %v", err) |
| 484 | } |
| 485 | sizeVecFunc, err := NewFunction("size", |
| 486 | MemberOverload("list_size", |
| 487 | []*types.Type{types.NewListType(types.NewTypeParamType("T")), types.IntType}, types.IntType), |
| 488 | ) |
| 489 | if err != nil { |
| 490 | t.Fatalf("NewFunction() failed: %v", err) |
| 491 | } |
| 492 | _, err = sizeFunc.Merge(sizeVecFunc) |
| 493 | if err == nil || !strings.Contains(err.Error(), "redefinition") { |
| 494 | t.Fatalf("Merge() expected to fail, got: %v", err) |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | func TestFunctionMergeOverloadArgTypeRedefinition(t *testing.T) { |
| 499 | sizeFunc, err := NewFunction("size", |
| 500 | MemberOverload("arg_size", |
| 501 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 502 | ) |
| 503 | if err != nil { |
| 504 | t.Fatalf("NewFunction() failed: %v", err) |
| 505 | } |
| 506 | sizeVecFunc, err := NewFunction("size", |
| 507 | MemberOverload("arg_size", |
| 508 | []*types.Type{types.NewMapType(types.IntType, types.StringType)}, types.IntType), |
| 509 | ) |
| 510 | if err != nil { |
| 511 | t.Fatalf("NewFunction() failed: %v", err) |
| 512 | } |
| 513 | _, err = sizeFunc.Merge(sizeVecFunc) |
| 514 | if err == nil || !strings.Contains(err.Error(), "redefinition") { |
| 515 | t.Fatalf("Merge() expected to fail, got: %v", err) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func TestFunctionMergeSingletonRedefinition(t *testing.T) { |
| 520 | sizeFunc, err := NewFunction("size", |
| 521 | MemberOverload("list_size", |
| 522 | []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 523 | SingletonUnaryBinding(func(ref.Val) ref.Val { |
| 524 | return types.IntZero |
| 525 | }), |
| 526 | ) |
| 527 | if err != nil { |
| 528 | t.Fatalf("NewFunction() failed: %v", err) |
| 529 | } |
| 530 | sizeVecFunc, err := NewFunction("size", |
| 531 | MemberOverload("string_size", |
| 532 | []*types.Type{types.StringType}, types.IntType), |
| 533 | SingletonUnaryBinding(func(ref.Val) ref.Val { |
| 534 | return types.IntZero |
| 535 | }), |
| 536 | ) |
| 537 | if err != nil { |
| 538 | t.Fatalf("NewFunction() failed: %v", err) |
| 539 | } |
| 540 | _, err = sizeFunc.Merge(sizeVecFunc) |
| 541 | if err == nil || !strings.Contains(err.Error(), "already has a singleton") { |
| 542 | t.Fatalf("Merge() expected to fail, got: %v", err) |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | func TestFunctionAddDuplicateOverloads(t *testing.T) { |
| 547 | _, err := NewFunction("max", |
| 548 | Overload("max_int", []*types.Type{types.IntType}, types.IntType), |
| 549 | Overload("max_int", []*types.Type{types.IntType}, types.IntType), |
| 550 | ) |
| 551 | if err != nil { |
| 552 | t.Fatalf("NewFunction() with duplicate overload signature failed: %v", err) |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | func TestFunctionAddDuplicateOverloadsPreservesBinding(t *testing.T) { |
| 557 | f, err := NewFunction("max", |
| 558 | Overload("max_int", []*types.Type{types.IntType}, types.IntType), |
| 559 | Overload("max_int", []*types.Type{types.IntType}, types.IntType, |
| 560 | UnaryBinding(func(v ref.Val) ref.Val { |
| 561 | return v |
| 562 | })), |
| 563 | Overload("max_int", []*types.Type{types.IntType}, types.IntType), |
| 564 | ) |
| 565 | if err != nil { |
| 566 | t.Fatalf("NewFunction() with duplicate overload signature failed: %v", err) |
| 567 | } |
| 568 | if len(f.overloads) != 1 { |
| 569 | t.Fatal("Duplicate overloads were not merged") |
| 570 | } |
| 571 | o := f.overloads["max_int"] |
| 572 | if o.unaryOp == nil { |
| 573 | t.Error("Duplicate overloads purged the overload binding") |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | func TestFunctionAddCollidingOverloads(t *testing.T) { |
| 578 | _, err := NewFunction("max", |
| 579 | Overload("max_int", []*types.Type{types.IntType}, types.IntType), |
| 580 | Overload("max_int2", []*types.Type{types.IntType}, types.IntType), |
| 581 | ) |
| 582 | if err == nil || !strings.Contains(err.Error(), "max_int collides with max_int2") { |
| 583 | t.Fatalf("NewFunction() got %v, wanted collision error", err) |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | func TestFunctionNoOverloads(t *testing.T) { |
| 588 | _, err := NewFunction("right", SingletonBinaryBinding(func(arg1, arg2 ref.Val) ref.Val { |
| 589 | return arg2 |
| 590 | })) |
| 591 | if err == nil || !strings.Contains(err.Error(), "must have at least one overload") { |
| 592 | t.Errorf("NewFunction() got %v, wanted 'must have at least one overload'", err) |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | func TestSingletonOverloadCollision(t *testing.T) { |
| 597 | fn, err := NewFunction("id", |
| 598 | Overload("id_any", []*types.Type{types.AnyType}, types.AnyType, |
| 599 | UnaryBinding(func(arg ref.Val) ref.Val { |
| 600 | return arg |
| 601 | }), |
| 602 | ), |
| 603 | SingletonUnaryBinding(func(arg ref.Val) ref.Val { |
| 604 | return arg |
| 605 | }), |
| 606 | ) |
| 607 | if err != nil { |
| 608 | t.Fatalf("NewFunction() failed: %v", err) |
| 609 | } |
| 610 | _, err = fn.Bindings() |
| 611 | if err == nil || !strings.Contains(err.Error(), "incompatible with specialized overloads") { |
| 612 | t.Errorf("NewFunction() got %v, wanted incompatible with specialized overloads", err) |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | func TestSingletonOverloadLateBindingCollision(t *testing.T) { |
| 617 | fn, err := NewFunction("id", |
| 618 | Overload("id_any", []*types.Type{types.AnyType}, types.AnyType, |
| 619 | LateFunctionBinding(), |
| 620 | ), |
| 621 | SingletonUnaryBinding(func(arg ref.Val) ref.Val { |
| 622 | return arg |
| 623 | }), |
| 624 | ) |
| 625 | if err != nil { |
| 626 | t.Fatalf("NewFunction() failed: %v", err) |
| 627 | } |
| 628 | _, err = fn.Bindings() |
| 629 | if err == nil || !strings.Contains(err.Error(), "incompatible with late bindings") { |
| 630 | t.Errorf("NewFunction() got %v, wanted incompatible with late bindings", err) |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | func TestSingletonUnaryBindingRedefinition(t *testing.T) { |
| 635 | _, err := NewFunction("id", |
| 636 | Overload("id_any", []*types.Type{types.AnyType}, types.AnyType), |
| 637 | SingletonUnaryBinding(func(arg ref.Val) ref.Val { |
| 638 | return arg |
| 639 | }), |
| 640 | SingletonUnaryBinding(func(arg ref.Val) ref.Val { |
| 641 | return arg |
| 642 | }), |
| 643 | ) |
| 644 | if err == nil || !strings.Contains(err.Error(), "already has a singleton binding") { |
| 645 | t.Errorf("NewFunction() got %v, wanted already has a singleton binding", err) |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | func TestSingletonBinaryBindingRedefinition(t *testing.T) { |
| 650 | _, err := NewFunction("right", |
| 651 | Overload("right_double_double", []*types.Type{types.DoubleType, types.DoubleType}, types.DoubleType), |
| 652 | SingletonBinaryBinding(func(arg1, arg2 ref.Val) ref.Val { |
| 653 | return arg2 |
| 654 | }, traits.ComparerType), |
| 655 | SingletonBinaryBinding(func(arg1, arg2 ref.Val) ref.Val { |
| 656 | return arg2 |
| 657 | }), |
| 658 | ) |
| 659 | if err == nil || !strings.Contains(err.Error(), "already has a singleton binding") { |
| 660 | t.Errorf("NewCustomEnv() got %v, wanted already has a singleton binding", err) |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | func TestSingletonFunctionBindingRedefinition(t *testing.T) { |
| 665 | _, err := NewFunction("id", |
| 666 | Overload("id_any", []*types.Type{types.AnyType}, types.AnyType), |
| 667 | SingletonFunctionBinding(func(args ...ref.Val) ref.Val { |
| 668 | return args[0] |
| 669 | }, traits.ComparerType), |
| 670 | SingletonFunctionBinding(func(args ...ref.Val) ref.Val { |
| 671 | return args[0] |
| 672 | }, traits.ComparerType), |
| 673 | ) |
| 674 | if err == nil || !strings.Contains(err.Error(), "already has a singleton binding") { |
| 675 | t.Errorf("NewCustomEnv() got %v, wanted already has a singleton binding", err) |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | func TestOverloadUnaryBindingRedefinition(t *testing.T) { |
| 680 | _, err := NewFunction("id", |
| 681 | Overload("id_any", []*types.Type{types.AnyType}, types.AnyType, |
| 682 | UnaryBinding(func(arg ref.Val) ref.Val { |
| 683 | return arg |
| 684 | }), |
| 685 | UnaryBinding(func(arg ref.Val) ref.Val { |
| 686 | return arg |
| 687 | }), |
| 688 | ), |
| 689 | ) |
| 690 | if err == nil || !strings.Contains(err.Error(), "already has a binding") { |
| 691 | t.Errorf("NewFunction() got %v, wanted already has a binding", err) |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | func TestOverloadUnaryBindingArgCountMismatch(t *testing.T) { |
| 696 | _, err := NewFunction("id", |
| 697 | Overload("id_any", []*types.Type{}, types.AnyType, |
| 698 | UnaryBinding(func(arg ref.Val) ref.Val { |
| 699 | return arg |
| 700 | }), |
| 701 | ), |
| 702 | ) |
| 703 | if err == nil || !strings.Contains(err.Error(), "non-unary overload") { |
| 704 | t.Errorf("NewFunction() got %v, wanted non-unary overload", err) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | func TestOverloadBinaryBindingArgCountMismatch(t *testing.T) { |
| 709 | _, err := NewFunction("id", |
| 710 | Overload("id_any", []*types.Type{}, types.AnyType, |
| 711 | BinaryBinding(func(lhs, rhs ref.Val) ref.Val { |
| 712 | return lhs |
| 713 | }), |
| 714 | ), |
| 715 | ) |
| 716 | if err == nil || !strings.Contains(err.Error(), "non-binary overload") { |
| 717 | t.Errorf("NewFunction() got %v, wanted non-binary overload", err) |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | func TestOverloadBinaryBindingRedefinition(t *testing.T) { |
| 722 | _, err := NewFunction("right", |
| 723 | Overload("right_double_double", []*types.Type{types.DoubleType, types.DoubleType}, types.DoubleType, |
| 724 | BinaryBinding(func(arg1, arg2 ref.Val) ref.Val { |
| 725 | return arg2 |
| 726 | }), |
| 727 | BinaryBinding(func(arg1, arg2 ref.Val) ref.Val { |
| 728 | return arg2 |
| 729 | }), |
| 730 | ), |
| 731 | ) |
| 732 | if err == nil || !strings.Contains(err.Error(), "already has a binding") { |
| 733 | t.Errorf("NewCustomEnv() got %v, wanted already has a binding", err) |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | func TestOverloadFunctionBindingRedefinition(t *testing.T) { |
| 738 | _, err := NewFunction("id", |
| 739 | Overload("id_any", []*types.Type{types.AnyType}, types.AnyType, |
| 740 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 741 | return args[0] |
| 742 | }), |
| 743 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 744 | return args[0] |
| 745 | }), |
| 746 | ), |
| 747 | ) |
| 748 | if err == nil || !strings.Contains(err.Error(), "already has a binding") { |
| 749 | t.Errorf("NewCustomEnv() got %v, wanted already has a binding", err) |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | func TestOverloadFunctionLateBinding(t *testing.T) { |
| 754 | function, err := NewFunction("id", |
| 755 | Overload("id_bool", []*types.Type{types.BoolType}, types.AnyType, LateFunctionBinding(), LateFunctionBinding()), |
| 756 | ) |
| 757 | if err != nil { |
| 758 | t.Fatalf("NewFunction() failed: %v", err) |
| 759 | } |
| 760 | if len(function.OverloadDecls()) != 1 { |
| 761 | t.Fatalf("NewFunction() got %v, wanted 1 overload", function.OverloadDecls()) |
| 762 | } |
| 763 | if !function.OverloadDecls()[0].HasLateBinding() { |
| 764 | t.Errorf("overload %v did not have a late binding", function.OverloadDecls()[0]) |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | func TestOverloadFunctionMixLateAndNonLateBinding(t *testing.T) { |
| 769 | _, err := NewFunction("id", |
| 770 | Overload("id_bool", []*types.Type{types.BoolType}, types.AnyType, LateFunctionBinding()), |
| 771 | Overload("id_int", []*types.Type{types.IntType}, types.AnyType), |
| 772 | ) |
| 773 | if err == nil || !strings.Contains(err.Error(), "cannot mix late and non-late bindings") { |
| 774 | t.Errorf("NewCustomEnv() got %v, wanted cannot mix late and non-late bindings", err) |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | func TestOverloadFunctionBindingWithLateBinding(t *testing.T) { |
| 779 | _, err := NewFunction("id", |
| 780 | Overload("id_bool", []*types.Type{types.BoolType}, types.AnyType, FunctionBinding(func(args ...ref.Val) ref.Val { |
| 781 | return args[0] |
| 782 | }), LateFunctionBinding()), |
| 783 | ) |
| 784 | if err == nil || !strings.Contains(err.Error(), "already has a binding") { |
| 785 | t.Errorf("NewCustomEnv() got %v, wanted already has a binding", err) |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | func TestOverloadFunctionLateBindingWithBinding(t *testing.T) { |
| 790 | _, err := NewFunction("id", |
| 791 | Overload("id_bool", []*types.Type{types.BoolType}, types.AnyType, LateFunctionBinding(), |
| 792 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 793 | return args[0] |
| 794 | })), |
| 795 | ) |
| 796 | if err == nil || !strings.Contains(err.Error(), "already has a late binding") { |
| 797 | t.Errorf("NewCustomEnv() got %v, wanted already has a late binding", err) |
| 798 | } |
| 799 | |
| 800 | _, err = NewFunction("id", |
| 801 | Overload("id_bool", []*types.Type{types.BoolType}, types.AnyType, LateFunctionBinding(), |
| 802 | UnaryBinding(func(arg ref.Val) ref.Val { |
| 803 | return arg |
| 804 | })), |
| 805 | ) |
| 806 | if err == nil || !strings.Contains(err.Error(), "already has a late binding") { |
| 807 | t.Errorf("NewCustomEnv() got %v, wanted already has a late binding", err) |
| 808 | } |
| 809 | |
| 810 | _, err = NewFunction("id", |
| 811 | Overload("id_bool", []*types.Type{types.BoolType, types.BoolType}, types.AnyType, LateFunctionBinding(), |
| 812 | BinaryBinding(func(arg1 ref.Val, arg2 ref.Val) ref.Val { |
| 813 | return arg1 |
| 814 | })), |
| 815 | ) |
| 816 | if err == nil || !strings.Contains(err.Error(), "already has a late binding") { |
| 817 | t.Errorf("NewCustomEnv() got %v, wanted already has a late binding", err) |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | func TestOverloadIsNonStrict(t *testing.T) { |
| 822 | fn, err := NewFunction("getOrDefault", |
| 823 | MemberOverload("get", |
| 824 | []*types.Type{types.NewMapType( |
| 825 | types.NewTypeParamType("K"), types.NewTypeParamType("V")), |
| 826 | types.NewTypeParamType("K"), |
| 827 | types.NewTypeParamType("V"), |
| 828 | }, |
| 829 | types.NewTypeParamType("V"), |
| 830 | OverloadOperandTrait(traits.ContainerType|traits.IndexerType), |
| 831 | OverloadIsNonStrict(), |
| 832 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 833 | container := args[0].(traits.Container) |
| 834 | key := args[1] |
| 835 | orValue := args[2] |
| 836 | if types.Bool(types.IsUnknownOrError(key)) { |
| 837 | return orValue |
| 838 | } |
| 839 | if container.Contains(key) == types.True { |
| 840 | return args[0].(traits.Indexer).Get(key) |
| 841 | } |
| 842 | return orValue |
| 843 | }), |
| 844 | ), |
| 845 | ) |
| 846 | if err != nil { |
| 847 | t.Fatalf("NewFunction() failed: %v", err) |
| 848 | } |
| 849 | bindings, err := fn.Bindings() |
| 850 | if err != nil { |
| 851 | t.Fatalf("fn.Binding() failed: %v", err) |
| 852 | } |
| 853 | m := types.DefaultTypeAdapter.NativeToValue(map[string]string{"hello": "world"}) |
| 854 | out := bindings[0].Function(m, types.String("hello"), types.String("goodbye")) |
| 855 | if out.Equal(types.String("world")) != types.True { |
| 856 | t.Errorf("function got %v, wanted 'world'", out) |
| 857 | } |
| 858 | out = bindings[0].Function(m, types.String("missing"), types.String("goodbye")) |
| 859 | if out.Equal(types.String("goodbye")) != types.True { |
| 860 | t.Errorf("function got %v, wanted 'goodbye'", out) |
| 861 | } |
| 862 | out = bindings[0].Function(m, types.NewErr("no such key"), types.String("goodbye")) |
| 863 | if out.Equal(types.String("goodbye")) != types.True { |
| 864 | t.Errorf("function got %v, wanted 'goodbye'", out) |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | func TestOverloadOperandTrait(t *testing.T) { |
| 869 | fn, err := NewFunction("getOrDefault", |
| 870 | MemberOverload("get", |
| 871 | []*types.Type{types.NewMapType( |
| 872 | types.NewTypeParamType("K"), types.NewTypeParamType("V")), |
| 873 | types.NewTypeParamType("K"), |
| 874 | types.NewTypeParamType("V"), |
| 875 | }, |
| 876 | types.NewTypeParamType("V"), |
| 877 | OverloadOperandTrait(traits.ContainerType|traits.IndexerType), |
| 878 | FunctionBinding(func(args ...ref.Val) ref.Val { |
| 879 | container := args[0].(traits.Container) |
| 880 | key := args[1] |
| 881 | orValue := args[2] |
| 882 | if container.Contains(key) == types.True { |
| 883 | return args[0].(traits.Indexer).Get(key) |
| 884 | } |
| 885 | return orValue |
| 886 | }), |
| 887 | ), |
| 888 | ) |
| 889 | if err != nil { |
| 890 | t.Fatalf("NewFunction() failed: %v", err) |
| 891 | } |
| 892 | bindings, err := fn.Bindings() |
| 893 | if err != nil { |
| 894 | t.Fatalf("fn.Binding() failed: %v", err) |
| 895 | } |
| 896 | m := types.DefaultTypeAdapter.NativeToValue(map[string]string{"hello": "world"}) |
| 897 | out := bindings[0].Function(m, types.String("hello"), types.String("goodbye")) |
| 898 | if out.Equal(types.String("world")) != types.True { |
| 899 | t.Errorf("function got %v, wanted 'world'", out) |
| 900 | } |
| 901 | out = bindings[0].Function(m, types.String("missing"), types.String("goodbye")) |
| 902 | if out.Equal(types.String("goodbye")) != types.True { |
| 903 | t.Errorf("function got %v, wanted 'goodbye'", out) |
| 904 | } |
| 905 | noSuchKey := types.NewErr("no such key") |
| 906 | out = bindings[0].Function(m, noSuchKey, types.String("goodbye")) |
| 907 | if out != noSuchKey { |
| 908 | t.Errorf("function got %v, wanted 'no such key'", out) |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | func TestFunctionGetTypeParams(t *testing.T) { |
| 913 | fn, err := NewFunction("deep_type_params", |
| 914 | Overload("no_type_params", []*types.Type{}, types.DynType), |
| 915 | Overload("one_type_param", []*types.Type{types.BoolType}, types.NewTypeParamType("K")), |
| 916 | Overload("deep_type_params", |
| 917 | []*types.Type{types.NewTypeParamType("E1"), |
| 918 | types.NewMapType(types.NewTypeParamType("K"), types.NewTypeParamType("V"))}, |
| 919 | types.NewTypeParamType("V"), |
| 920 | ), |
| 921 | ) |
| 922 | if err != nil { |
| 923 | t.Fatalf("NewFunction() failed: %v", err) |
| 924 | } |
| 925 | if len(fn.OverloadDecls()) != 3 { |
| 926 | t.Fatal("fn.OverloadDecls() not equal to 3") |
| 927 | } |
| 928 | o1 := fn.OverloadDecls()[0] |
| 929 | o2 := fn.OverloadDecls()[1] |
| 930 | o3 := fn.OverloadDecls()[2] |
| 931 | if len(o1.TypeParams()) != 0 { |
| 932 | t.Errorf("overload %v did not have zero type-params", o1) |
| 933 | } |
| 934 | if len(o2.TypeParams()) != 1 && !reflect.DeepEqual(o2.TypeParams(), []string{"K"}) { |
| 935 | t.Errorf("overload %v did not have a single type param", o2) |
| 936 | } |
| 937 | if len(o3.TypeParams()) != 3 { |
| 938 | t.Errorf("overload %v did not have three type params", o3) |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | func TestFunctionDisableDeclaration(t *testing.T) { |
| 943 | fn, err := NewFunction("in", |
| 944 | DisableDeclaration(true), |
| 945 | Overload("in_list", |
| 946 | []*types.Type{types.NewListType(types.NewTypeParamType("K")), types.NewTypeParamType("K")}, |
| 947 | types.BoolType, |
| 948 | ), |
| 949 | ) |
| 950 | if err != nil { |
| 951 | t.Fatalf("NewFunction() failed: %v", err) |
| 952 | } |
| 953 | if !fn.IsDeclarationDisabled() { |
| 954 | t.Error("got declaration enabled, wanted disabled") |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | func TestFunctionEnableDeclaration(t *testing.T) { |
| 959 | fn, err := NewFunction("in", |
| 960 | DisableDeclaration(false), |
| 961 | Overload("in_list", |
| 962 | []*types.Type{types.NewListType(types.NewTypeParamType("K")), types.NewTypeParamType("K")}, |
| 963 | types.BoolType), |
| 964 | ) |
| 965 | if err != nil { |
| 966 | t.Fatalf("NewFunction() failed: %v", err) |
| 967 | } |
| 968 | if fn.IsDeclarationDisabled() { |
| 969 | t.Error("got declaration disabled, wanted enabled") |
| 970 | } |
| 971 | fn2, err := NewFunction("in", |
| 972 | DisableDeclaration(true), |
| 973 | Overload("in_list", |
| 974 | []*types.Type{types.NewListType(types.NewTypeParamType("K")), types.NewTypeParamType("K")}, |
| 975 | types.BoolType), |
| 976 | ) |
| 977 | if err != nil { |
| 978 | t.Fatalf("NewFunction() failed: %v", err) |
| 979 | } |
| 980 | if !fn2.IsDeclarationDisabled() { |
| 981 | t.Error("got declaration enabled, wanted disabled") |
| 982 | } |
| 983 | |
| 984 | // enabled -> disabled |
| 985 | merged, err := fn.Merge(fn2) |
| 986 | if err != nil { |
| 987 | t.Fatalf("fn.Merge(fn2) failed: %v", err) |
| 988 | } |
| 989 | if !merged.IsDeclarationDisabled() { |
| 990 | t.Error("got declaration enabled, wanted disabled") |
| 991 | } |
| 992 | // disabled -> enabled |
| 993 | merged2, err := fn2.Merge(fn) |
| 994 | if err != nil { |
| 995 | t.Fatalf("fn.Merge(fn2) failed: %v", err) |
| 996 | } |
| 997 | if merged2.IsDeclarationDisabled() { |
| 998 | t.Error("got declaration disabled, wanted enabled") |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | func TestFunctionDeclToExprDecl(t *testing.T) { |
| 1003 | tests := []struct { |
| 1004 | fn *FunctionDecl |
| 1005 | exDecl *exprpb.Decl |
| 1006 | }{ |
| 1007 | { |
| 1008 | fn: testFunction(t, "equals", |
| 1009 | Overload("equals_value_value", |
| 1010 | []*types.Type{types.NewTypeParamType("T"), types.NewTypeParamType("T")}, types.BoolType)), |
| 1011 | exDecl: &exprpb.Decl{ |
| 1012 | Name: "equals", |
| 1013 | DeclKind: &exprpb.Decl_Function{ |
| 1014 | Function: &exprpb.Decl_FunctionDecl{ |
| 1015 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1016 | { |
| 1017 | OverloadId: "equals_value_value", |
| 1018 | Params: []*exprpb.Type{ |
| 1019 | chkdecls.NewTypeParamType("T"), |
| 1020 | chkdecls.NewTypeParamType("T"), |
| 1021 | }, |
| 1022 | TypeParams: []string{"T"}, |
| 1023 | ResultType: chkdecls.Bool, |
| 1024 | }, |
| 1025 | }, |
| 1026 | }, |
| 1027 | }, |
| 1028 | }, |
| 1029 | }, |
| 1030 | { |
| 1031 | fn: testFunction(t, "equals", |
| 1032 | MemberOverload("value_equals_value", |
| 1033 | []*types.Type{types.NewTypeParamType("T"), types.NewTypeParamType("T")}, types.BoolType)), |
| 1034 | exDecl: &exprpb.Decl{ |
| 1035 | Name: "equals", |
| 1036 | DeclKind: &exprpb.Decl_Function{ |
| 1037 | Function: &exprpb.Decl_FunctionDecl{ |
| 1038 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1039 | { |
| 1040 | OverloadId: "value_equals_value", |
| 1041 | IsInstanceFunction: true, |
| 1042 | Params: []*exprpb.Type{ |
| 1043 | chkdecls.NewTypeParamType("T"), |
| 1044 | chkdecls.NewTypeParamType("T"), |
| 1045 | }, |
| 1046 | TypeParams: []string{"T"}, |
| 1047 | ResultType: chkdecls.Bool, |
| 1048 | }, |
| 1049 | }, |
| 1050 | }, |
| 1051 | }, |
| 1052 | }, |
| 1053 | }, |
| 1054 | { |
| 1055 | fn: testFunction(t, "equals", |
| 1056 | Overload("equals_int_uint", |
| 1057 | []*types.Type{types.IntType, types.UintType}, types.BoolType)), |
| 1058 | exDecl: &exprpb.Decl{ |
| 1059 | Name: "equals", |
| 1060 | DeclKind: &exprpb.Decl_Function{ |
| 1061 | Function: &exprpb.Decl_FunctionDecl{ |
| 1062 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1063 | { |
| 1064 | OverloadId: "equals_int_uint", |
| 1065 | Params: []*exprpb.Type{ |
| 1066 | chkdecls.Int, |
| 1067 | chkdecls.Uint, |
| 1068 | }, |
| 1069 | ResultType: chkdecls.Bool, |
| 1070 | }, |
| 1071 | }, |
| 1072 | }, |
| 1073 | }, |
| 1074 | }, |
| 1075 | }, |
| 1076 | { |
| 1077 | fn: testFunction(t, "equals", |
| 1078 | MemberOverload("int_equals_uint", |
| 1079 | []*types.Type{types.IntType, types.UintType}, types.BoolType)), |
| 1080 | exDecl: &exprpb.Decl{ |
| 1081 | Name: "equals", |
| 1082 | DeclKind: &exprpb.Decl_Function{ |
| 1083 | Function: &exprpb.Decl_FunctionDecl{ |
| 1084 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1085 | { |
| 1086 | OverloadId: "int_equals_uint", |
| 1087 | IsInstanceFunction: true, |
| 1088 | Params: []*exprpb.Type{ |
| 1089 | chkdecls.Int, |
| 1090 | chkdecls.Uint, |
| 1091 | }, |
| 1092 | ResultType: chkdecls.Bool, |
| 1093 | }, |
| 1094 | }, |
| 1095 | }, |
| 1096 | }, |
| 1097 | }, |
| 1098 | }, |
| 1099 | { |
| 1100 | fn: testFunction(t, "equals", |
| 1101 | MemberOverload("list_optional_value_equals_list_optional_value", []*types.Type{ |
| 1102 | types.NewListType(types.NewOptionalType(types.NewTypeParamType("T"))), |
| 1103 | types.NewListType(types.NewOptionalType(types.NewTypeParamType("T"))), |
| 1104 | }, types.BoolType)), |
| 1105 | exDecl: &exprpb.Decl{ |
| 1106 | Name: "equals", |
| 1107 | DeclKind: &exprpb.Decl_Function{ |
| 1108 | Function: &exprpb.Decl_FunctionDecl{ |
| 1109 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1110 | { |
| 1111 | OverloadId: "list_optional_value_equals_list_optional_value", |
| 1112 | IsInstanceFunction: true, |
| 1113 | Params: []*exprpb.Type{ |
| 1114 | chkdecls.NewListType(chkdecls.NewOptionalType(chkdecls.NewTypeParamType("T"))), |
| 1115 | chkdecls.NewListType(chkdecls.NewOptionalType(chkdecls.NewTypeParamType("T"))), |
| 1116 | }, |
| 1117 | TypeParams: []string{"T"}, |
| 1118 | ResultType: chkdecls.Bool, |
| 1119 | }, |
| 1120 | }, |
| 1121 | }, |
| 1122 | }, |
| 1123 | }, |
| 1124 | }, |
| 1125 | { |
| 1126 | fn: testFunction(t, "equals", |
| 1127 | MemberOverload("int_equals_uint", |
| 1128 | []*types.Type{types.IntType, types.UintType}, types.BoolType), |
| 1129 | MemberOverload("uint_equals_int", |
| 1130 | []*types.Type{types.UintType, types.IntType}, types.BoolType)), |
| 1131 | exDecl: &exprpb.Decl{ |
| 1132 | Name: "equals", |
| 1133 | DeclKind: &exprpb.Decl_Function{ |
| 1134 | Function: &exprpb.Decl_FunctionDecl{ |
| 1135 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1136 | { |
| 1137 | OverloadId: "int_equals_uint", |
| 1138 | IsInstanceFunction: true, |
| 1139 | Params: []*exprpb.Type{ |
| 1140 | chkdecls.Int, |
| 1141 | chkdecls.Uint, |
| 1142 | }, |
| 1143 | ResultType: chkdecls.Bool, |
| 1144 | }, |
| 1145 | { |
| 1146 | OverloadId: "uint_equals_int", |
| 1147 | IsInstanceFunction: true, |
| 1148 | Params: []*exprpb.Type{ |
| 1149 | chkdecls.Uint, |
| 1150 | chkdecls.Int, |
| 1151 | }, |
| 1152 | ResultType: chkdecls.Bool, |
| 1153 | }, |
| 1154 | }, |
| 1155 | }, |
| 1156 | }, |
| 1157 | }, |
| 1158 | }, |
| 1159 | // Test to make sure the overloads are in stable order: int_uint | uint_int | int_int | uint_uint |
| 1160 | { |
| 1161 | fn: testMerge(t, |
| 1162 | testFunction(t, "equals", |
| 1163 | FunctionDocs(`test equality between an int and uint only`), |
| 1164 | Overload("int_equals_uint", []*types.Type{types.IntType, types.UintType}, types.BoolType, |
| 1165 | OverloadExamples(`1 == 1u // true`)), |
| 1166 | Overload("uint_equals_int", []*types.Type{types.UintType, types.IntType}, types.BoolType, |
| 1167 | OverloadExamples(`1u == -1 // false`))), |
| 1168 | testFunction(t, "equals", |
| 1169 | FunctionDocs(`test equality between two int-like values`), |
| 1170 | Overload("int_equals_int", []*types.Type{types.IntType, types.IntType}, types.BoolType, |
| 1171 | OverloadExamples(`1 == 1 // true`, `1 == 2 // false`)), |
| 1172 | Overload("int_equals_uint", []*types.Type{types.IntType, types.UintType}, types.BoolType), |
| 1173 | Overload("uint_equals_uint", []*types.Type{types.UintType, types.UintType}, types.BoolType, |
| 1174 | OverloadExamples(`1u == 1u // true`)))), |
| 1175 | exDecl: &exprpb.Decl{ |
| 1176 | Name: "equals", |
| 1177 | DeclKind: &exprpb.Decl_Function{ |
| 1178 | Function: &exprpb.Decl_FunctionDecl{ |
| 1179 | Overloads: []*exprpb.Decl_FunctionDecl_Overload{ |
| 1180 | { |
| 1181 | OverloadId: "int_equals_uint", |
| 1182 | Params: []*exprpb.Type{ |
| 1183 | chkdecls.Int, |
| 1184 | chkdecls.Uint, |
| 1185 | }, |
| 1186 | ResultType: chkdecls.Bool, |
| 1187 | Doc: "1 == 1u // true", |
| 1188 | }, |
| 1189 | { |
| 1190 | OverloadId: "uint_equals_int", |
| 1191 | Params: []*exprpb.Type{ |
| 1192 | chkdecls.Uint, |
| 1193 | chkdecls.Int, |
| 1194 | }, |
| 1195 | ResultType: chkdecls.Bool, |
| 1196 | Doc: "1u == -1 // false", |
| 1197 | }, |
| 1198 | { |
| 1199 | OverloadId: "int_equals_int", |
| 1200 | Params: []*exprpb.Type{ |
| 1201 | chkdecls.Int, |
| 1202 | chkdecls.Int, |
| 1203 | }, |
| 1204 | ResultType: chkdecls.Bool, |
| 1205 | Doc: "1 == 1 // true\n1 == 2 // false", |
| 1206 | }, |
| 1207 | { |
| 1208 | OverloadId: "uint_equals_uint", |
| 1209 | Params: []*exprpb.Type{ |
| 1210 | chkdecls.Uint, |
| 1211 | chkdecls.Uint, |
| 1212 | }, |
| 1213 | ResultType: chkdecls.Bool, |
| 1214 | Doc: "1u == 1u // true", |
| 1215 | }, |
| 1216 | }, |
| 1217 | }, |
| 1218 | }, |
| 1219 | }, |
| 1220 | }, |
| 1221 | } |
| 1222 | for _, tst := range tests { |
| 1223 | exDecl, err := FunctionDeclToExprDecl(tst.fn) |
| 1224 | if err != nil { |
| 1225 | t.Fatalf("FunctionDeclToExprDecl(%v) failed: %v", tst.fn, err) |
| 1226 | } |
| 1227 | if !proto.Equal(exDecl, tst.exDecl) { |
| 1228 | t.Errorf("got not equal, wanted %v == %v", exDecl, tst.exDecl) |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | func TestNewVariable(t *testing.T) { |
| 1234 | a := NewVariable("a", types.BoolType) |
| 1235 | if !a.DeclarationIsEquivalent(a) { |
| 1236 | t.Error("NewVariable(a, bool) does not equal itself") |
| 1237 | } |
| 1238 | if !a.DeclarationIsEquivalent(NewVariable("a", types.BoolType)) { |
| 1239 | t.Error("NewVariable(a, bool) does not equal itself") |
| 1240 | } |
| 1241 | a1 := NewVariable("a", types.IntType) |
| 1242 | if a.DeclarationIsEquivalent(a1) { |
| 1243 | t.Error("NewVariable(a, int).DeclarationEquals(NewVariable(a, bool))") |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | func TestNewConstant(t *testing.T) { |
| 1248 | a := NewConstant("a", types.IntType, types.Int(42)) |
| 1249 | if !a.DeclarationIsEquivalent(a) { |
| 1250 | t.Error("NewConstant(a, int) does not equal itself") |
| 1251 | } |
| 1252 | if !a.DeclarationIsEquivalent(NewVariable("a", types.IntType)) { |
| 1253 | t.Error("NewConstant(a, int) is not declaration equivalent to int variable") |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | func TestTypeVariable(t *testing.T) { |
| 1258 | tests := []struct { |
| 1259 | t *types.Type |
| 1260 | v *VariableDecl |
| 1261 | }{ |
| 1262 | { |
| 1263 | t: types.AnyType, |
| 1264 | v: NewVariable("google.protobuf.Any", types.NewTypeTypeWithParam(types.AnyType)), |
| 1265 | }, |
| 1266 | { |
| 1267 | t: types.DynType, |
| 1268 | v: NewVariable("dyn", types.NewTypeTypeWithParam(types.DynType)), |
| 1269 | }, |
| 1270 | { |
| 1271 | t: types.NewObjectType("google.protobuf.Int32Value"), |
| 1272 | v: NewVariable("int", types.NewTypeTypeWithParam(types.NewNullableType(types.IntType))), |
| 1273 | }, |
| 1274 | { |
| 1275 | t: types.NewObjectType("google.protobuf.Int32Value"), |
| 1276 | v: NewVariable("int", types.NewTypeTypeWithParam(types.NewNullableType(types.IntType))), |
| 1277 | }, |
| 1278 | } |
| 1279 | for _, tst := range tests { |
| 1280 | if !TypeVariable(tst.t).DeclarationIsEquivalent(tst.v) { |
| 1281 | t.Errorf("got not equal %v.Equals(%v)", TypeVariable(tst.t), tst.v) |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | func TestVariableDeclToExprDecl(t *testing.T) { |
| 1287 | a, err := VariableDeclToExprDecl(NewVariable("a", types.BoolType)) |
| 1288 | if err != nil { |
| 1289 | t.Fatalf("VariableDeclToExprDecl() failed: %v", err) |
| 1290 | } |
| 1291 | if !proto.Equal(a, chkdecls.NewVar("a", chkdecls.Bool)) { |
| 1292 | t.Error("proto.Equal() returned false, wanted true") |
| 1293 | } |
| 1294 | |
| 1295 | docVar := NewVariableWithDoc("a", types.BoolType, "doc") |
| 1296 | a, err = VariableDeclToExprDecl(docVar) |
| 1297 | if err != nil { |
| 1298 | t.Fatalf("VariableDeclToExprDecl() failed: %v", err) |
| 1299 | } |
| 1300 | if docVar.Description() != a.GetIdent().GetDoc() { |
| 1301 | t.Errorf("docVar.Description() got %s, wanted %s", docVar.Description(), a.GetIdent().GetDoc()) |
| 1302 | } |
| 1303 | if !proto.Equal(a, chkdecls.NewVarWithDoc("a", chkdecls.Bool, "doc")) { |
| 1304 | t.Error("proto.Equal() returned false, wanted true") |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | func TestVariableDeclToExprDeclInvalid(t *testing.T) { |
| 1309 | out, err := VariableDeclToExprDecl(NewVariable("bad", &types.Type{})) |
| 1310 | if err == nil { |
| 1311 | t.Fatalf("VariableDeclToExprDecl() succeeded: %v, wanted error", out) |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | func TestNilFunction(t *testing.T) { |
| 1316 | var f *FunctionDecl |
| 1317 | if f.Name() != "" { |
| 1318 | t.Errorf("f.Name() got %s, wanted ''", f.Name()) |
| 1319 | } |
| 1320 | if f.Description() != "" { |
| 1321 | t.Errorf("f.Description() got %s, wanted ''", f.Description()) |
| 1322 | } |
| 1323 | if f.Documentation() != nil { |
| 1324 | t.Errorf("f.Documentation() got %v, wanted nil", f.Documentation()) |
| 1325 | } |
| 1326 | if !f.IsDeclarationDisabled() { |
| 1327 | t.Errorf("f.IsDeclarationDisabled() got %t, wanted true", f.IsDeclarationDisabled()) |
| 1328 | } |
| 1329 | if len(f.OverloadDecls()) != 0 { |
| 1330 | t.Errorf("f.OverloadDecls() got %d overloads, wanted 0", len(f.OverloadDecls())) |
| 1331 | } |
| 1332 | if b, err := f.Bindings(); err != nil || len(b) != 0 { |
| 1333 | t.Error("f.Bindings() got non-empty result") |
| 1334 | } |
| 1335 | other := &FunctionDecl{} |
| 1336 | if fn, err := f.Merge(other); err == nil { |
| 1337 | t.Errorf("f.Merge(nil) wanted error, got %v", fn) |
| 1338 | } |
| 1339 | if err := f.AddOverload(nil); err == nil { |
| 1340 | t.Error("f.AddOverload(nil) did not error") |
| 1341 | } |
| 1342 | if err := other.AddOverload(nil); err == nil { |
| 1343 | t.Error("f.AddOverload(nil) did not error") |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | func TestNilVariable(t *testing.T) { |
| 1348 | var v *VariableDecl |
| 1349 | if v.Name() != "" { |
| 1350 | t.Errorf("v.Name() got %s, wanted ''", v.Name()) |
| 1351 | } |
| 1352 | if v.Type() != nil { |
| 1353 | t.Errorf("v.Type() got %v, wanted nil", v.Type()) |
| 1354 | } |
| 1355 | if v.Value() != nil { |
| 1356 | t.Errorf("v.Type() got %v, wanted nil", v.Value()) |
| 1357 | } |
| 1358 | if v.Description() != "" { |
| 1359 | t.Errorf("v.Description() got %s, wanted ''", v.Description()) |
| 1360 | } |
| 1361 | if v.Documentation() != nil { |
| 1362 | t.Errorf("v.Documentation() got %v, wanted nil", v.Documentation()) |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | func testMerge(t *testing.T, funcs ...*FunctionDecl) *FunctionDecl { |
| 1367 | t.Helper() |
| 1368 | fn := funcs[0] |
| 1369 | var err error |
| 1370 | for i := 1; i < len(funcs); i++ { |
| 1371 | fn, err = fn.Merge(funcs[i]) |
| 1372 | if err != nil { |
| 1373 | t.Fatalf("%v.Merge(%v) failed: %v", fn, funcs[i], err) |
| 1374 | } |
| 1375 | } |
| 1376 | return fn |
| 1377 | } |
| 1378 | |
| 1379 | func testFunction(t *testing.T, name string, opts ...FunctionOpt) *FunctionDecl { |
| 1380 | t.Helper() |
| 1381 | fn, err := NewFunction(name, opts...) |
| 1382 | if err != nil { |
| 1383 | t.Fatalf("NewFunction() failed: %v", err) |
| 1384 | } |
| 1385 | return fn |
| 1386 | } |