| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package env |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"math" |
| 21 |
"os" |
| 22 |
"reflect" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"go.yaml.in/yaml/v3" |
| 27 |
|
| 28 |
"github.com/google/cel-go/common" |
| 29 |
"github.com/google/cel-go/common/decls" |
| 30 |
"github.com/google/cel-go/common/operators" |
| 31 |
"github.com/google/cel-go/common/overloads" |
| 32 |
"github.com/google/cel-go/common/types" |
| 33 |
) |
| 34 |
|
| 35 |
func TestConfig(t *testing.T) { |
| 36 |
tests := []struct { |
| 37 |
name string |
| 38 |
want *Config |
| 39 |
}{ |
| 40 |
{ |
| 41 |
name: "context_env", |
| 42 |
want: NewConfig("context-env"). |
| 43 |
SetContainer("google.expr"). |
| 44 |
AddImports(NewImport("google.expr.proto3.test.TestAllTypes")). |
| 45 |
SetStdLib(NewLibrarySubset(). |
| 46 |
AddIncludedMacros("has"). |
| 47 |
AddIncludedFunctions([]*Function{ |
| 48 |
{Name: operators.Equals}, |
| 49 |
{Name: operators.NotEquals}, |
| 50 |
{Name: operators.LogicalNot}, |
| 51 |
{Name: operators.Less}, |
| 52 |
{Name: operators.LessEquals}, |
| 53 |
{Name: operators.Greater}, |
| 54 |
{Name: operators.GreaterEquals}, |
| 55 |
}...)). |
| 56 |
AddExtensions(NewExtension("optional", math.MaxUint32), NewExtension("strings", 1)). |
| 57 |
SetContextVariable(NewContextVariable("google.expr.proto3.test.TestAllTypes")). |
| 58 |
AddFunctions( |
| 59 |
NewFunctionWithDoc("coalesce", |
| 60 |
"Converts a potentially null wrapper-type to a default value.", |
| 61 |
NewOverload("coalesce_wrapped_int", |
| 62 |
[]*TypeDesc{NewTypeDesc("google.protobuf.Int64Value"), NewTypeDesc("int")}, |
| 63 |
NewTypeDesc("int"), |
| 64 |
`coalesce(null, 1) // 1`, |
| 65 |
`coalesce(2, 1) // 2`), |
| 66 |
NewOverload("coalesce_wrapped_double", |
| 67 |
[]*TypeDesc{NewTypeDesc("google.protobuf.DoubleValue"), NewTypeDesc("double")}, |
| 68 |
NewTypeDesc("double"), |
| 69 |
`coalesce(null, 1.3) // 1.3`), |
| 70 |
NewOverload("coalesce_wrapped_uint", |
| 71 |
[]*TypeDesc{NewTypeDesc("google.protobuf.UInt64Value"), NewTypeDesc("uint")}, |
| 72 |
NewTypeDesc("uint"), |
| 73 |
`coalesce(null, 14u) // 14u`), |
| 74 |
), |
| 75 |
), |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "extended_env", |
| 79 |
want: NewConfig("extended-env"). |
| 80 |
SetContainer("google.expr"). |
| 81 |
AddExtensions( |
| 82 |
NewExtension("optional", 2), |
| 83 |
NewExtension("math", math.MaxUint32), |
| 84 |
).AddVariables( |
| 85 |
NewVariableWithDoc("msg", |
| 86 |
NewTypeDesc("google.expr.proto3.test.TestAllTypes"), |
| 87 |
`msg represents all possible type permutation which CEL understands from a proto perspective`), |
| 88 |
).AddFunctions( |
| 89 |
NewFunctionWithDoc("isEmpty", |
| 90 |
common.MultilineDescription( |
| 91 |
`determines whether a list is empty,`, |
| 92 |
`or a string has no characters`), |
| 93 |
NewMemberOverload("wrapper_string_isEmpty", |
| 94 |
NewTypeDesc("google.protobuf.StringValue"), nil, |
| 95 |
NewTypeDesc("bool"), |
| 96 |
`''.isEmpty() // true`), |
| 97 |
NewMemberOverload("list_isEmpty", |
| 98 |
NewTypeDesc("list", NewTypeParam("T")), nil, |
| 99 |
NewTypeDesc("bool"), |
| 100 |
`[].isEmpty() // true`, |
| 101 |
`[1].isEmpty() // false`), |
| 102 |
), |
| 103 |
).AddFeatures( |
| 104 |
NewFeature("cel.feature.macro_call_tracking", true), |
| 105 |
).AddValidators( |
| 106 |
NewValidator("cel.validator.duration"), |
| 107 |
NewValidator("cel.validator.matches"), |
| 108 |
NewValidator("cel.validator.timestamp"), |
| 109 |
NewValidator("cel.validator.comprehension_nesting_limit"). |
| 110 |
SetConfig(map[string]any{"limit": 2}), |
| 111 |
), |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "subset_env", |
| 115 |
want: NewConfig("subset-env"). |
| 116 |
SetStdLib(NewLibrarySubset(). |
| 117 |
AddExcludedMacros("map", "filter"). |
| 118 |
AddExcludedFunctions( |
| 119 |
[]*Function{ |
| 120 |
{Name: operators.Add, Overloads: []*Overload{ |
| 121 |
{ID: overloads.AddBytes}, |
| 122 |
{ID: overloads.AddList}, |
| 123 |
{ID: overloads.AddString}, |
| 124 |
}}, |
| 125 |
{Name: overloads.Matches}, |
| 126 |
{Name: overloads.TypeConvertTimestamp, Overloads: []*Overload{ |
| 127 |
{ID: overloads.StringToTimestamp}, |
| 128 |
}}, |
| 129 |
{Name: overloads.TypeConvertDuration, Overloads: []*Overload{ |
| 130 |
{ID: overloads.StringToDuration}, |
| 131 |
}}, |
| 132 |
}..., |
| 133 |
)).AddVariables( |
| 134 |
NewVariable("x", NewTypeDesc("int")), |
| 135 |
NewVariable("y", NewTypeDesc("double")), |
| 136 |
NewVariable("z", NewTypeDesc("uint")), |
| 137 |
), |
| 138 |
}, |
| 139 |
} |
| 140 |
for _, tst := range tests { |
| 141 |
tc := tst |
| 142 |
t.Run(tc.name, func(t *testing.T) { |
| 143 |
fileName := fmt.Sprintf("testdata/%s.yaml", tc.name) |
| 144 |
data, err := os.ReadFile(fileName) |
| 145 |
if err != nil { |
| 146 |
t.Fatalf("os.ReadFile(%q) failed: %v", fileName, err) |
| 147 |
} |
| 148 |
got := unmarshalYAML(t, data) |
| 149 |
if err := got.Validate(); err != nil { |
| 150 |
t.Errorf("Validate() got %v, wanted nil error", err) |
| 151 |
} |
| 152 |
if got.Container != tc.want.Container { |
| 153 |
t.Errorf("Container got %s, wanted %s", got.Container, tc.want.Container) |
| 154 |
} |
| 155 |
if !reflect.DeepEqual(got.Imports, tc.want.Imports) { |
| 156 |
t.Errorf("Imports got %v, wanted %v", got.Imports, tc.want.Imports) |
| 157 |
} |
| 158 |
if !reflect.DeepEqual(got.StdLib, tc.want.StdLib) { |
| 159 |
t.Errorf("StdLib got %v, wanted %v", got.StdLib, tc.want.StdLib) |
| 160 |
} |
| 161 |
if !reflect.DeepEqual(got.ContextVariable, tc.want.ContextVariable) { |
| 162 |
t.Errorf("ContextVariable got %v, wanted %v", got.ContextVariable, tc.want.ContextVariable) |
| 163 |
} |
| 164 |
if len(got.Variables) != len(tc.want.Variables) { |
| 165 |
t.Errorf("Variables count got %d, wanted %d", len(got.Variables), len(tc.want.Variables)) |
| 166 |
} else { |
| 167 |
for i, v := range got.Variables { |
| 168 |
wv := tc.want.Variables[i] |
| 169 |
if !reflect.DeepEqual(v, wv) { |
| 170 |
t.Errorf("Variables[%d] not equal, got %v, wanted %v", i, v.Description, wv.Description) |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
if len(got.Functions) != len(tc.want.Functions) { |
| 175 |
t.Errorf("Functions count got %d, wanted %d", len(got.Functions), len(tc.want.Functions)) |
| 176 |
} else { |
| 177 |
for i, f := range got.Functions { |
| 178 |
wf := tc.want.Functions[i] |
| 179 |
if f.Name != wf.Name { |
| 180 |
t.Errorf("Functions[%d] not equal, got %v, wanted %v", i, f.Name, wf.Name) |
| 181 |
} |
| 182 |
if f.Description != wf.Description { |
| 183 |
t.Errorf("Functions[%d] description not equal, got %v, wanted %v", i, f.Description, wf.Description) |
| 184 |
} |
| 185 |
if len(f.Overloads) != len(wf.Overloads) { |
| 186 |
t.Errorf("Function %s got overload count: %d, wanted %d", f.Name, len(f.Overloads), len(wf.Overloads)) |
| 187 |
} |
| 188 |
for j, o := range f.Overloads { |
| 189 |
wo := wf.Overloads[j] |
| 190 |
if !reflect.DeepEqual(o, wo) { |
| 191 |
t.Error(o.Examples) |
| 192 |
t.Errorf("Overload[%d] got %v, wanted %v", j, o, wo) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
if len(got.Features) != len(tc.want.Features) { |
| 198 |
t.Errorf("Features count got %d, wanted %d", len(got.Features), len(tc.want.Features)) |
| 199 |
} else { |
| 200 |
for i, f := range got.Features { |
| 201 |
wf := tc.want.Features[i] |
| 202 |
if f.Name != wf.Name { |
| 203 |
t.Errorf("Features[%d] got name %s, wanted %s", i, f.Name, wf.Name) |
| 204 |
} |
| 205 |
if f.Enabled != wf.Enabled { |
| 206 |
t.Errorf("Features[%d] got enabled %t, wanted %t", i, f.Enabled, wf.Enabled) |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
if len(got.Validators) != len(tc.want.Validators) { |
| 211 |
t.Errorf("Validators count got %d, wanted %d", len(got.Validators), len(tc.want.Validators)) |
| 212 |
} else { |
| 213 |
for i, f := range got.Validators { |
| 214 |
wf := tc.want.Validators[i] |
| 215 |
if f.Name != wf.Name { |
| 216 |
t.Errorf("Validators[%d] got name %s, wanted %s", i, f.Name, wf.Name) |
| 217 |
} |
| 218 |
if !reflect.DeepEqual(f.Config, wf.Config) { |
| 219 |
t.Errorf("Validators[%d] got enabled %v, wanted %v", i, f.Config, wf.Config) |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
}) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestConfigValidateErrors(t *testing.T) { |
| 228 |
tests := []struct { |
| 229 |
name string |
| 230 |
in *Config |
| 231 |
want error |
| 232 |
}{ |
| 233 |
{ |
| 234 |
name: "nil config valid", |
| 235 |
}, |
| 236 |
{ |
| 237 |
name: "invalid import", |
| 238 |
in: NewConfig("invalid import").AddImports(NewImport("")), |
| 239 |
want: errors.New("invalid import"), |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "invalid subset", |
| 243 |
in: NewConfig("invalid subset").SetStdLib(NewLibrarySubset().AddExcludedMacros("has").AddIncludedMacros("exists")), |
| 244 |
want: errors.New("invalid subset"), |
| 245 |
}, |
| 246 |
{ |
| 247 |
name: "invalid extension", |
| 248 |
in: NewConfig("invalid extension").AddExtensions(NewExtension("", 0)), |
| 249 |
want: errors.New("invalid extension"), |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "invalid context variable", |
| 253 |
in: NewConfig("invalid context variable").SetContextVariable(NewContextVariable("")), |
| 254 |
want: errors.New("invalid context variable"), |
| 255 |
}, |
| 256 |
{ |
| 257 |
name: "invalid variable", |
| 258 |
in: NewConfig("invalid variable").AddVariables(NewVariable("", nil)), |
| 259 |
want: errors.New("invalid variable"), |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "colliding context variable", |
| 263 |
in: NewConfig("colliding context variable"). |
| 264 |
SetContextVariable(NewContextVariable("msg.type.Name")). |
| 265 |
AddVariables(NewVariable("local", NewTypeDesc("string"))), |
| 266 |
want: errors.New("invalid config"), |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "invalid function", |
| 270 |
in: NewConfig("invalid function").AddFunctions(NewFunction("", nil)), |
| 271 |
want: errors.New("invalid function"), |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "invalid feature", |
| 275 |
in: NewConfig("invalid feature").AddFeatures(NewFeature("", false)), |
| 276 |
want: errors.New("invalid feature"), |
| 277 |
}, |
| 278 |
{ |
| 279 |
name: "invalid validator", |
| 280 |
in: NewConfig("invalid validator").AddValidators(NewValidator("")), |
| 281 |
want: errors.New("invalid validator"), |
| 282 |
}, |
| 283 |
} |
| 284 |
|
| 285 |
for _, tst := range tests { |
| 286 |
tc := tst |
| 287 |
t.Run(tc.name, func(t *testing.T) { |
| 288 |
err := tc.in.Validate() |
| 289 |
if err == nil && tc.want == nil { |
| 290 |
return |
| 291 |
} |
| 292 |
if err == nil && tc.want != nil { |
| 293 |
t.Fatalf("config.Validate() got valid, wanted error %v", tc.want) |
| 294 |
} |
| 295 |
if err != nil && tc.want == nil { |
| 296 |
t.Fatalf("config.Validate() got error %v, wanted nil error", err) |
| 297 |
} |
| 298 |
if !strings.Contains(err.Error(), tc.want.Error()) { |
| 299 |
t.Errorf("config.Validate() got error %v, wanted %v", err, tc.want) |
| 300 |
} |
| 301 |
}) |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
func TestConfigAddVariableDecls(t *testing.T) { |
| 306 |
tests := []struct { |
| 307 |
name string |
| 308 |
in *decls.VariableDecl |
| 309 |
out *Variable |
| 310 |
}{ |
| 311 |
{ |
| 312 |
name: "nil var decl", |
| 313 |
}, |
| 314 |
{ |
| 315 |
name: "simple var decl", |
| 316 |
in: decls.NewVariable("var", types.StringType), |
| 317 |
out: NewVariable("var", NewTypeDesc("string")), |
| 318 |
}, |
| 319 |
{ |
| 320 |
name: "parameterized var decl", |
| 321 |
in: decls.NewVariable("var", types.NewListType(types.NewTypeParamType("T"))), |
| 322 |
out: NewVariable("var", NewTypeDesc("list", NewTypeParam("T"))), |
| 323 |
}, |
| 324 |
{ |
| 325 |
name: "opaque var decl", |
| 326 |
in: decls.NewVariable("var", types.NewOpaqueType("bitvector")), |
| 327 |
out: NewVariable("var", NewTypeDesc("bitvector")), |
| 328 |
}, |
| 329 |
{ |
| 330 |
name: "proto var decl", |
| 331 |
in: decls.NewVariable("var", types.NewObjectType("google.type.Expr")), |
| 332 |
out: NewVariable("var", NewTypeDesc("google.type.Expr")), |
| 333 |
}, |
| 334 |
{ |
| 335 |
name: "proto var decl with doc", |
| 336 |
in: decls.NewVariableWithDoc("var", types.NewObjectType("google.type.Expr"), "API-friendly CEL expression type"), |
| 337 |
out: NewVariableWithDoc("var", NewTypeDesc("google.type.Expr"), "API-friendly CEL expression type"), |
| 338 |
}, |
| 339 |
} |
| 340 |
for _, tst := range tests { |
| 341 |
tc := tst |
| 342 |
t.Run(tc.name, func(t *testing.T) { |
| 343 |
conf := NewConfig(tc.name).AddVariableDecls(tc.in) |
| 344 |
if len(conf.Variables) != 1 { |
| 345 |
t.Fatalf("AddVariableDecls() did not add declaration to conf: %v", conf) |
| 346 |
} |
| 347 |
if !reflect.DeepEqual(conf.Variables[0], tc.out) { |
| 348 |
t.Errorf("AddVariableDecls() added %v, wanted %v", conf.Variables, tc.out) |
| 349 |
} |
| 350 |
}) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
func TestConfigAddVariableDeclsEmpty(t *testing.T) { |
| 355 |
if len(NewConfig("").AddVariables().Variables) != 0 { |
| 356 |
t.Error("AddVariables() with no args failed") |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func TestConfigAddFunctionDecls(t *testing.T) { |
| 361 |
tests := []struct { |
| 362 |
name string |
| 363 |
in *decls.FunctionDecl |
| 364 |
out *Function |
| 365 |
}{ |
| 366 |
{ |
| 367 |
name: "nil function decl", |
| 368 |
}, |
| 369 |
{ |
| 370 |
name: "global function decl", |
| 371 |
in: mustNewFunction(t, "size", |
| 372 |
decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType), |
| 373 |
), |
| 374 |
out: NewFunction("size", |
| 375 |
NewOverload("size_string", []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("int")), |
| 376 |
), |
| 377 |
}, |
| 378 |
{ |
| 379 |
name: "global function decl - nullable arg", |
| 380 |
in: mustNewFunction(t, "size", |
| 381 |
decls.Overload("size_wrapper_string", []*types.Type{types.NewNullableType(types.StringType)}, types.IntType), |
| 382 |
), |
| 383 |
out: NewFunction("size", |
| 384 |
NewOverload("size_wrapper_string", []*TypeDesc{NewTypeDesc("google.protobuf.StringValue")}, NewTypeDesc("int")), |
| 385 |
), |
| 386 |
}, |
| 387 |
{ |
| 388 |
name: "member function decl - nullable arg", |
| 389 |
in: mustNewFunction(t, "size", |
| 390 |
decls.MemberOverload("list_size", []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 391 |
decls.MemberOverload("string_size", []*types.Type{types.StringType}, types.IntType), |
| 392 |
), |
| 393 |
out: NewFunction("size", |
| 394 |
NewMemberOverload("list_size", NewTypeDesc("list", NewTypeParam("T")), []*TypeDesc{}, NewTypeDesc("int")), |
| 395 |
NewMemberOverload("string_size", NewTypeDesc("string"), []*TypeDesc{}, NewTypeDesc("int")), |
| 396 |
), |
| 397 |
}, |
| 398 |
{ |
| 399 |
name: "global function decl - with doc", |
| 400 |
in: mustNewFunction(t, "size", |
| 401 |
decls.FunctionDocs("return the number of unicode code points", "in a string"), |
| 402 |
decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType, |
| 403 |
decls.OverloadExamples(`'hello'.size() // 5`)), |
| 404 |
), |
| 405 |
out: NewFunctionWithDoc("size", |
| 406 |
"return the number of unicode code points\nin a string", |
| 407 |
NewOverload("size_string", []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("int"), |
| 408 |
`'hello'.size() // 5`), |
| 409 |
), |
| 410 |
}, |
| 411 |
} |
| 412 |
for _, tst := range tests { |
| 413 |
tc := tst |
| 414 |
t.Run(tc.name, func(t *testing.T) { |
| 415 |
conf := NewConfig(tc.name).AddFunctionDecls(tc.in) |
| 416 |
if len(conf.Functions) != 1 { |
| 417 |
t.Fatalf("AddFunctionDecls() did not add declaration to conf: %v", conf) |
| 418 |
} |
| 419 |
if !reflect.DeepEqual(conf.Functions[0], tc.out) { |
| 420 |
t.Errorf("AddFunctionDecls() added %v, wanted %v", conf.Functions, tc.out) |
| 421 |
} |
| 422 |
}) |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
func TestNewImport(t *testing.T) { |
| 427 |
imp := NewImport("qualified.type.name") |
| 428 |
if imp.Name != "qualified.type.name" { |
| 429 |
t.Errorf("NewImport() got name: %s, wanted %s", imp.Name, "qualified.type.name") |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
func TestImportValidate(t *testing.T) { |
| 434 |
var imp *Import |
| 435 |
err := imp.Validate() |
| 436 |
if err == nil || !strings.Contains(err.Error(), "invalid import") { |
| 437 |
t.Errorf("imp.Validate() got %v, wanted error 'invalid import'", err) |
| 438 |
} |
| 439 |
|
| 440 |
imp = NewImport("") |
| 441 |
err = imp.Validate() |
| 442 |
if err == nil || !strings.Contains(err.Error(), "invalid import") { |
| 443 |
t.Errorf("imp.Validate() got %v, wanted error 'invalid import'", err) |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
func TestNewContextVariable(t *testing.T) { |
| 448 |
ctx := NewContextVariable("qualified.type.name") |
| 449 |
if ctx.TypeName != "qualified.type.name" { |
| 450 |
t.Errorf("NewContextVariable() got name: %s, wanted %s", ctx.TypeName, "qualified.type.name") |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
func TestContextVariableValidate(t *testing.T) { |
| 455 |
ctx := NewContextVariable("") |
| 456 |
err := ctx.Validate() |
| 457 |
if err == nil || !strings.Contains(err.Error(), "invalid context variable") { |
| 458 |
t.Errorf("ctx.Validate() got %v, wanted error 'invalid context variable'", err) |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
func TestVariableGetType(t *testing.T) { |
| 463 |
tests := []struct { |
| 464 |
name string |
| 465 |
v *Variable |
| 466 |
t *TypeDesc |
| 467 |
}{ |
| 468 |
{ |
| 469 |
name: "nil-safety check", |
| 470 |
v: nil, |
| 471 |
t: nil, |
| 472 |
}, |
| 473 |
{ |
| 474 |
name: "nil type access", |
| 475 |
v: &Variable{}, |
| 476 |
t: nil, |
| 477 |
}, |
| 478 |
{ |
| 479 |
name: "nested type desc", |
| 480 |
v: &Variable{TypeDesc: &TypeDesc{}}, |
| 481 |
t: &TypeDesc{}, |
| 482 |
}, |
| 483 |
{ |
| 484 |
name: "field type desc", |
| 485 |
v: &Variable{Type: &TypeDesc{}}, |
| 486 |
t: &TypeDesc{}, |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "nested type desc precedence", |
| 490 |
v: &Variable{ |
| 491 |
TypeDesc: &TypeDesc{TypeName: "type.name.EmbeddedType"}, |
| 492 |
Type: &TypeDesc{TypeName: "type.name.FieldType"}, |
| 493 |
}, |
| 494 |
t: &TypeDesc{TypeName: "type.name.EmbeddedType"}, |
| 495 |
}, |
| 496 |
} |
| 497 |
|
| 498 |
for _, tst := range tests { |
| 499 |
tc := tst |
| 500 |
t.Run(tc.name, func(t *testing.T) { |
| 501 |
if !reflect.DeepEqual(tc.v.GetType(), tc.t) { |
| 502 |
t.Errorf("GetType() got %v, wanted %v", tc.v.GetType(), tc.t) |
| 503 |
} |
| 504 |
}) |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
func TestVariableAsCELVariable(t *testing.T) { |
| 509 |
tests := []struct { |
| 510 |
name string |
| 511 |
v *Variable |
| 512 |
want any |
| 513 |
}{ |
| 514 |
{ |
| 515 |
name: "nil-safety check", |
| 516 |
v: nil, |
| 517 |
want: errors.New("invalid variable: nil"), |
| 518 |
}, |
| 519 |
{ |
| 520 |
name: "no variable name", |
| 521 |
v: &Variable{}, |
| 522 |
want: errors.New("invalid variable"), |
| 523 |
}, |
| 524 |
{ |
| 525 |
name: "no type", |
| 526 |
v: &Variable{ |
| 527 |
Name: "hello", |
| 528 |
}, |
| 529 |
want: errors.New("invalid type: nil"), |
| 530 |
}, |
| 531 |
{ |
| 532 |
name: "bad type", |
| 533 |
v: &Variable{ |
| 534 |
Name: "hello", |
| 535 |
TypeDesc: &TypeDesc{}, |
| 536 |
}, |
| 537 |
want: errors.New("missing type name"), |
| 538 |
}, |
| 539 |
{ |
| 540 |
name: "undefined type", |
| 541 |
v: &Variable{ |
| 542 |
Name: "hello", |
| 543 |
TypeDesc: &TypeDesc{TypeName: "undefined"}, |
| 544 |
}, |
| 545 |
want: errors.New("undefined type name"), |
| 546 |
}, |
| 547 |
{ |
| 548 |
name: "int type", |
| 549 |
v: NewVariable("int_var", NewTypeDesc("int")), |
| 550 |
want: decls.NewVariable("int_var", types.IntType), |
| 551 |
}, |
| 552 |
{ |
| 553 |
name: "uint type", |
| 554 |
v: &Variable{ |
| 555 |
Name: "uint_var", |
| 556 |
TypeDesc: NewTypeDesc("uint"), |
| 557 |
}, |
| 558 |
want: decls.NewVariable("uint_var", types.UintType), |
| 559 |
}, |
| 560 |
{ |
| 561 |
name: "dyn type", |
| 562 |
v: &Variable{ |
| 563 |
Name: "dyn_var", |
| 564 |
TypeDesc: NewTypeDesc("dyn"), |
| 565 |
}, |
| 566 |
want: decls.NewVariable("dyn_var", types.DynType), |
| 567 |
}, |
| 568 |
{ |
| 569 |
name: "list type", |
| 570 |
v: &Variable{ |
| 571 |
Name: "list_var", |
| 572 |
TypeDesc: NewTypeDesc("list", NewTypeParam("T")), |
| 573 |
}, |
| 574 |
want: decls.NewVariable("list_var", types.NewListType(types.NewTypeParamType("T"))), |
| 575 |
}, |
| 576 |
{ |
| 577 |
name: "map type", |
| 578 |
v: &Variable{ |
| 579 |
Name: "map_var", |
| 580 |
TypeDesc: &TypeDesc{ |
| 581 |
TypeName: "map", |
| 582 |
Params: []*TypeDesc{ |
| 583 |
NewTypeDesc("string"), |
| 584 |
NewTypeDesc("optional_type", NewTypeParam("T")), |
| 585 |
}, |
| 586 |
}, |
| 587 |
}, |
| 588 |
want: decls.NewVariable("map_var", |
| 589 |
types.NewMapType(types.StringType, types.NewOptionalType(types.NewTypeParamType("T")))), |
| 590 |
}, |
| 591 |
{ |
| 592 |
name: "set type", |
| 593 |
v: &Variable{ |
| 594 |
Name: "set_var", |
| 595 |
TypeDesc: NewTypeDesc("set", NewTypeDesc("string")), |
| 596 |
}, |
| 597 |
want: decls.NewVariable("set_var", types.NewOpaqueType("set", types.StringType)), |
| 598 |
}, |
| 599 |
{ |
| 600 |
name: "string type - nested type precedence", |
| 601 |
v: &Variable{ |
| 602 |
Name: "hello", |
| 603 |
TypeDesc: NewTypeDesc("string"), |
| 604 |
Type: NewTypeDesc("int"), |
| 605 |
}, |
| 606 |
want: decls.NewVariable("hello", types.StringType), |
| 607 |
}, |
| 608 |
{ |
| 609 |
name: "wrapper type variable", |
| 610 |
v: &Variable{ |
| 611 |
Name: "msg", |
| 612 |
TypeDesc: NewTypeDesc("google.protobuf.StringValue"), |
| 613 |
}, |
| 614 |
want: decls.NewVariable("msg", types.NewNullableType(types.StringType)), |
| 615 |
}, |
| 616 |
} |
| 617 |
|
| 618 |
tp, err := types.NewRegistry() |
| 619 |
if err != nil { |
| 620 |
t.Fatalf("types.NewRegistry() failed: %v", err) |
| 621 |
} |
| 622 |
tp.RegisterType(types.NewOpaqueType("set", types.NewTypeParamType("T"))) |
| 623 |
for _, tst := range tests { |
| 624 |
tc := tst |
| 625 |
t.Run(tc.name, func(t *testing.T) { |
| 626 |
gotVar, err := tc.v.AsCELVariable(tp) |
| 627 |
if err != nil { |
| 628 |
wantErr, ok := tc.want.(error) |
| 629 |
if !ok { |
| 630 |
t.Fatalf("AsCELVariable() got error %v, wanted %v", err, tc.want) |
| 631 |
} |
| 632 |
if !strings.Contains(err.Error(), wantErr.Error()) { |
| 633 |
t.Fatalf("AsCELVariable() got error %v, wanted error containing %v", err, wantErr) |
| 634 |
} |
| 635 |
return |
| 636 |
} |
| 637 |
if !gotVar.DeclarationIsEquivalent(tc.want.(*decls.VariableDecl)) { |
| 638 |
t.Errorf("AsCELVariable() got %v, wanted %v", gotVar, tc.want) |
| 639 |
} |
| 640 |
}) |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
func TestTypeDescString(t *testing.T) { |
| 645 |
tests := []struct { |
| 646 |
desc *TypeDesc |
| 647 |
want string |
| 648 |
}{ |
| 649 |
{desc: NewTypeDesc("string"), want: "string"}, |
| 650 |
{desc: NewTypeDesc("list", NewTypeParam("T")), want: "list(T)"}, |
| 651 |
{desc: NewTypeDesc("map", NewTypeDesc("string"), NewTypeParam("T")), want: "map(string,T)"}, |
| 652 |
} |
| 653 |
for _, tc := range tests { |
| 654 |
if tc.desc.String() != tc.want { |
| 655 |
t.Errorf("String() got %s, wanted %s", tc.desc.String(), tc.want) |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
func TestFunctionAsCELFunction(t *testing.T) { |
| 661 |
tests := []struct { |
| 662 |
name string |
| 663 |
f *Function |
| 664 |
want any |
| 665 |
}{ |
| 666 |
{ |
| 667 |
name: "nil function", |
| 668 |
f: nil, |
| 669 |
want: errors.New("invalid function: nil"), |
| 670 |
}, |
| 671 |
{ |
| 672 |
name: "unnamed function", |
| 673 |
f: &Function{}, |
| 674 |
want: errors.New("invalid function"), |
| 675 |
}, |
| 676 |
{ |
| 677 |
name: "no overloads", |
| 678 |
f: NewFunction("no_overloads"), |
| 679 |
want: errors.New("missing overloads"), |
| 680 |
}, |
| 681 |
{ |
| 682 |
name: "nil overload", |
| 683 |
f: NewFunction("no_overloads", nil), |
| 684 |
want: errors.New("invalid overload: nil"), |
| 685 |
}, |
| 686 |
{ |
| 687 |
name: "missing overload id", |
| 688 |
f: NewFunction("size", &Overload{}), |
| 689 |
want: errors.New("missing overload id"), |
| 690 |
}, |
| 691 |
{ |
| 692 |
name: "no return type", |
| 693 |
f: NewFunction("size", |
| 694 |
NewOverload("size_string", []*TypeDesc{NewTypeDesc("string")}, nil), |
| 695 |
), |
| 696 |
want: errors.New("return: invalid type"), |
| 697 |
}, |
| 698 |
{ |
| 699 |
name: "bad return type", |
| 700 |
f: NewFunction("size", |
| 701 |
NewOverload("size_string", []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("")), |
| 702 |
), |
| 703 |
want: errors.New("invalid type"), |
| 704 |
}, |
| 705 |
{ |
| 706 |
name: "bad arg type", |
| 707 |
f: NewFunction("size", |
| 708 |
NewOverload("size_string", []*TypeDesc{NewTypeDesc("")}, NewTypeDesc("")), |
| 709 |
), |
| 710 |
want: errors.New("invalid type"), |
| 711 |
}, |
| 712 |
{ |
| 713 |
name: "undefined arg type", |
| 714 |
f: NewFunction("size", |
| 715 |
NewOverload("size_undefined", []*TypeDesc{NewTypeDesc("undefined")}, NewTypeDesc("int")), |
| 716 |
), |
| 717 |
want: errors.New("undefined type"), |
| 718 |
}, |
| 719 |
{ |
| 720 |
name: "undefined return type", |
| 721 |
f: NewFunction("size", |
| 722 |
NewOverload("size_undefined", []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("undefined")), |
| 723 |
), |
| 724 |
want: errors.New("undefined type"), |
| 725 |
}, |
| 726 |
{ |
| 727 |
name: "undefined target type", |
| 728 |
f: NewFunction("size", |
| 729 |
NewMemberOverload("size_undefined", NewTypeDesc("undefined"), []*TypeDesc{NewTypeDesc("string")}, NewTypeDesc("int")), |
| 730 |
), |
| 731 |
want: errors.New("undefined type"), |
| 732 |
}, |
| 733 |
{ |
| 734 |
name: "bad target type", |
| 735 |
f: &Function{Name: "size", |
| 736 |
Overloads: []*Overload{ |
| 737 |
{ID: "string_size", |
| 738 |
Target: &TypeDesc{}, |
| 739 |
Args: []*TypeDesc{}, |
| 740 |
Return: &TypeDesc{TypeName: "int"}, |
| 741 |
}, |
| 742 |
}, |
| 743 |
}, |
| 744 |
want: errors.New("invalid type"), |
| 745 |
}, |
| 746 |
{ |
| 747 |
name: "global function", |
| 748 |
f: &Function{Name: "size", |
| 749 |
Overloads: []*Overload{ |
| 750 |
{ID: "size_string", |
| 751 |
Args: []*TypeDesc{{TypeName: "string"}}, |
| 752 |
Return: &TypeDesc{TypeName: "int"}}, |
| 753 |
}, |
| 754 |
}, |
| 755 |
want: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 756 |
}, |
| 757 |
{ |
| 758 |
name: "member function", |
| 759 |
f: &Function{Name: "size", |
| 760 |
Overloads: []*Overload{ |
| 761 |
{ID: "string_size", |
| 762 |
Target: &TypeDesc{TypeName: "string"}, |
| 763 |
Return: &TypeDesc{TypeName: "int"}}, |
| 764 |
}, |
| 765 |
}, |
| 766 |
want: mustNewFunction(t, "size", decls.MemberOverload("string_size", []*types.Type{types.StringType}, types.IntType)), |
| 767 |
}, |
| 768 |
{ |
| 769 |
name: "member function", |
| 770 |
f: &Function{Name: "size", |
| 771 |
Description: "return the number of unicode code points in a string", |
| 772 |
Overloads: []*Overload{{ |
| 773 |
ID: "string_size", |
| 774 |
Target: &TypeDesc{TypeName: "string"}, |
| 775 |
Return: &TypeDesc{TypeName: "int"}, |
| 776 |
Examples: []string{ |
| 777 |
`'hello'.size() // 5`, |
| 778 |
`'hello world'.size() // 11`, |
| 779 |
}, |
| 780 |
}}, |
| 781 |
}, |
| 782 |
want: mustNewFunction(t, "size", |
| 783 |
decls.FunctionDocs("return the number of unicode code points in a string"), |
| 784 |
decls.MemberOverload("string_size", []*types.Type{types.StringType}, types.IntType, |
| 785 |
decls.OverloadExamples( |
| 786 |
`'hello'.size() // 5`, |
| 787 |
`'hello world'.size() // 11`))), |
| 788 |
}, |
| 789 |
} |
| 790 |
tp, err := types.NewRegistry() |
| 791 |
if err != nil { |
| 792 |
t.Fatalf("types.NewRegistry() failed: %v", err) |
| 793 |
} |
| 794 |
tp.RegisterType(types.NewOpaqueType("set", types.NewTypeParamType("T"))) |
| 795 |
for _, tst := range tests { |
| 796 |
tc := tst |
| 797 |
t.Run(tc.name, func(t *testing.T) { |
| 798 |
gotFn, err := tc.f.AsCELFunction(tp) |
| 799 |
if err != nil { |
| 800 |
wantErr, ok := tc.want.(error) |
| 801 |
if !ok { |
| 802 |
t.Fatalf("AsCELFunction() got error %v, wanted %v", err, tc.want) |
| 803 |
} |
| 804 |
if !strings.Contains(err.Error(), wantErr.Error()) { |
| 805 |
t.Fatalf("AsCELFunction() got error %v, wanted error containing %v", err, wantErr) |
| 806 |
} |
| 807 |
return |
| 808 |
} |
| 809 |
assertFuncEquals(t, gotFn, tc.want.(*decls.FunctionDecl)) |
| 810 |
}) |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
func TestTypeDescAsCELTypeErrors(t *testing.T) { |
| 815 |
tests := []struct { |
| 816 |
name string |
| 817 |
t *TypeDesc |
| 818 |
want any |
| 819 |
}{ |
| 820 |
{ |
| 821 |
name: "nil-safety check", |
| 822 |
t: nil, |
| 823 |
want: errors.New("invalid type: nil"), |
| 824 |
}, |
| 825 |
{ |
| 826 |
name: "no type name", |
| 827 |
t: &TypeDesc{}, |
| 828 |
want: errors.New("missing type name"), |
| 829 |
}, |
| 830 |
{ |
| 831 |
name: "invalid optional_type", |
| 832 |
t: &TypeDesc{TypeName: "optional_type"}, |
| 833 |
want: errors.New("expects 1 parameter"), |
| 834 |
}, |
| 835 |
{ |
| 836 |
name: "invalid optional param type", |
| 837 |
t: &TypeDesc{TypeName: "optional_type", Params: []*TypeDesc{{}}}, |
| 838 |
want: errors.New("invalid type"), |
| 839 |
}, |
| 840 |
{ |
| 841 |
name: "undefined optional param type", |
| 842 |
t: &TypeDesc{TypeName: "optional_type", Params: []*TypeDesc{{TypeName: "undefined"}}}, |
| 843 |
want: errors.New("undefined type"), |
| 844 |
}, |
| 845 |
{ |
| 846 |
name: "invalid param type", |
| 847 |
t: &TypeDesc{TypeName: "T", IsTypeParam: true, Params: []*TypeDesc{{TypeName: "string"}}}, |
| 848 |
want: errors.New("invalid type: param type"), |
| 849 |
}, |
| 850 |
{ |
| 851 |
name: "invalid list", |
| 852 |
t: &TypeDesc{TypeName: "list"}, |
| 853 |
want: errors.New("expects 1 parameter"), |
| 854 |
}, |
| 855 |
{ |
| 856 |
name: "invalid list param type", |
| 857 |
t: &TypeDesc{TypeName: "list", Params: []*TypeDesc{{}}}, |
| 858 |
want: errors.New("invalid type"), |
| 859 |
}, |
| 860 |
{ |
| 861 |
name: "undefined list param type", |
| 862 |
t: &TypeDesc{TypeName: "list", Params: []*TypeDesc{{TypeName: "undefined"}}}, |
| 863 |
want: errors.New("undefined type name"), |
| 864 |
}, |
| 865 |
{ |
| 866 |
name: "invalid map", |
| 867 |
t: &TypeDesc{TypeName: "map"}, |
| 868 |
want: errors.New("expects 2 parameters"), |
| 869 |
}, |
| 870 |
{ |
| 871 |
name: "invalid map key type", |
| 872 |
t: &TypeDesc{TypeName: "map", Params: []*TypeDesc{{}, {}}}, |
| 873 |
want: errors.New("invalid type"), |
| 874 |
}, |
| 875 |
{ |
| 876 |
name: "invalid map value type", |
| 877 |
t: &TypeDesc{TypeName: "map", Params: []*TypeDesc{{TypeName: "string"}, {}}}, |
| 878 |
want: errors.New("invalid type"), |
| 879 |
}, |
| 880 |
{ |
| 881 |
name: "undefined map key type", |
| 882 |
t: &TypeDesc{TypeName: "map", Params: []*TypeDesc{{TypeName: "undefined"}, {TypeName: "undefined"}}}, |
| 883 |
want: errors.New("undefined type name"), |
| 884 |
}, |
| 885 |
{ |
| 886 |
name: "undefined map value type", |
| 887 |
t: &TypeDesc{TypeName: "map", Params: []*TypeDesc{{TypeName: "string"}, {TypeName: "undefined"}}}, |
| 888 |
want: errors.New("undefined type name"), |
| 889 |
}, |
| 890 |
{ |
| 891 |
name: "invalid set", |
| 892 |
t: &TypeDesc{TypeName: "set", Params: []*TypeDesc{{}}}, |
| 893 |
want: errors.New("invalid type"), |
| 894 |
}, |
| 895 |
{ |
| 896 |
name: "undefined type identifier", |
| 897 |
t: &TypeDesc{TypeName: "undefined"}, |
| 898 |
want: errors.New("undefined type"), |
| 899 |
}, |
| 900 |
} |
| 901 |
tp, err := types.NewRegistry() |
| 902 |
if err != nil { |
| 903 |
t.Fatalf("types.NewRegistry() failed: %v", err) |
| 904 |
} |
| 905 |
tp.RegisterType(types.NewOpaqueType("set", types.NewTypeParamType("T"))) |
| 906 |
for _, tst := range tests { |
| 907 |
tc := tst |
| 908 |
t.Run(tc.name, func(t *testing.T) { |
| 909 |
gotVar, err := tc.t.AsCELType(tp) |
| 910 |
if err != nil { |
| 911 |
wantErr, ok := tc.want.(error) |
| 912 |
if !ok { |
| 913 |
t.Fatalf("AsCELType() got error %v, wanted %v", err, tc.want) |
| 914 |
} |
| 915 |
if !strings.Contains(err.Error(), wantErr.Error()) { |
| 916 |
t.Fatalf("AsCELType() got error %v, wanted error containing %v", err, wantErr) |
| 917 |
} |
| 918 |
return |
| 919 |
} |
| 920 |
if !reflect.DeepEqual(gotVar, tc.want.(*decls.VariableDecl)) { |
| 921 |
t.Errorf("AsCELType() got %v, wanted %v", gotVar, tc.want) |
| 922 |
} |
| 923 |
}) |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
func TestLibrarySubsetValidate(t *testing.T) { |
| 928 |
tests := []struct { |
| 929 |
name string |
| 930 |
lib *LibrarySubset |
| 931 |
want error |
| 932 |
}{ |
| 933 |
{ |
| 934 |
name: "nil library", |
| 935 |
lib: NewLibrarySubset(), |
| 936 |
}, |
| 937 |
{ |
| 938 |
name: "empty library", |
| 939 |
lib: NewLibrarySubset(), |
| 940 |
}, |
| 941 |
{ |
| 942 |
name: "only excluded funcs", |
| 943 |
lib: NewLibrarySubset().AddExcludedFunctions(NewFunction("size", nil)), |
| 944 |
}, |
| 945 |
{ |
| 946 |
name: "only included funcs", |
| 947 |
lib: NewLibrarySubset().AddIncludedFunctions(NewFunction("size", nil)), |
| 948 |
}, |
| 949 |
{ |
| 950 |
name: "only excluded macros", |
| 951 |
lib: NewLibrarySubset().AddExcludedMacros("has"), |
| 952 |
}, |
| 953 |
{ |
| 954 |
name: "only included macros", |
| 955 |
lib: NewLibrarySubset().AddIncludedMacros("exists"), |
| 956 |
}, |
| 957 |
{ |
| 958 |
name: "both included and excluded funcs", |
| 959 |
lib: NewLibrarySubset(). |
| 960 |
AddIncludedFunctions(NewFunction("size", nil)). |
| 961 |
AddExcludedFunctions(NewFunction("size", nil)), |
| 962 |
want: errors.New("invalid subset"), |
| 963 |
}, |
| 964 |
{ |
| 965 |
name: "both included and excluded macros", |
| 966 |
lib: NewLibrarySubset(). |
| 967 |
AddIncludedMacros("has"). |
| 968 |
AddExcludedMacros("exists"), |
| 969 |
want: errors.New("invalid subset"), |
| 970 |
}, |
| 971 |
} |
| 972 |
|
| 973 |
for _, tst := range tests { |
| 974 |
tc := tst |
| 975 |
t.Run(tc.name, func(t *testing.T) { |
| 976 |
err := tc.lib.Validate() |
| 977 |
if err == nil && tc.want == nil { |
| 978 |
return |
| 979 |
} |
| 980 |
if err == nil && tc.want != nil { |
| 981 |
t.Fatalf("lib.Validate() got valid, wanted error %v", tc.want) |
| 982 |
} |
| 983 |
if err != nil && tc.want == nil { |
| 984 |
t.Fatalf("lib.Validate() got error %v, wanted nil error", err) |
| 985 |
} |
| 986 |
if !strings.Contains(err.Error(), tc.want.Error()) { |
| 987 |
t.Errorf("lib.Validate() got error %v, wanted %v", err, tc.want) |
| 988 |
} |
| 989 |
}) |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
func TestSubsetFunction(t *testing.T) { |
| 994 |
tests := []struct { |
| 995 |
name string |
| 996 |
lib *LibrarySubset |
| 997 |
orig *decls.FunctionDecl |
| 998 |
subset *decls.FunctionDecl |
| 999 |
included bool |
| 1000 |
}{ |
| 1001 |
{ |
| 1002 |
name: "nil lib, included", |
| 1003 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1004 |
subset: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1005 |
included: true, |
| 1006 |
}, |
| 1007 |
{ |
| 1008 |
name: "empty, included", |
| 1009 |
lib: NewLibrarySubset(), |
| 1010 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1011 |
subset: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1012 |
included: true, |
| 1013 |
}, |
| 1014 |
{ |
| 1015 |
name: "empty, disabled", |
| 1016 |
lib: NewLibrarySubset().SetDisabled(true), |
| 1017 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1018 |
included: false, |
| 1019 |
}, |
| 1020 |
{ |
| 1021 |
name: "lib, not included allow-list", |
| 1022 |
lib: NewLibrarySubset().AddIncludedFunctions([]*Function{ |
| 1023 |
{Name: "int"}, |
| 1024 |
}...), |
| 1025 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1026 |
included: false, |
| 1027 |
}, |
| 1028 |
{ |
| 1029 |
name: "lib, included whole function", |
| 1030 |
lib: NewLibrarySubset().AddIncludedFunctions([]*Function{ |
| 1031 |
{Name: "size"}, |
| 1032 |
}...), |
| 1033 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1034 |
subset: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1035 |
included: true, |
| 1036 |
}, |
| 1037 |
{ |
| 1038 |
name: "lib, included overload subset", |
| 1039 |
lib: NewLibrarySubset().AddIncludedFunctions([]*Function{ |
| 1040 |
{Name: "size", Overloads: []*Overload{{ID: "size_string"}}}, |
| 1041 |
}...), |
| 1042 |
orig: mustNewFunction(t, "size", |
| 1043 |
decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType), |
| 1044 |
decls.Overload("size_list", []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 1045 |
), |
| 1046 |
subset: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1047 |
included: true, |
| 1048 |
}, |
| 1049 |
{ |
| 1050 |
name: "lib, included deny-list", |
| 1051 |
lib: NewLibrarySubset().AddExcludedFunctions([]*Function{ |
| 1052 |
{Name: "int"}, |
| 1053 |
}...), |
| 1054 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1055 |
subset: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1056 |
included: true, |
| 1057 |
}, |
| 1058 |
{ |
| 1059 |
name: "lib, excluded whole function", |
| 1060 |
lib: NewLibrarySubset().AddExcludedFunctions([]*Function{ |
| 1061 |
{Name: "size"}, |
| 1062 |
}...), |
| 1063 |
orig: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1064 |
included: false, |
| 1065 |
}, |
| 1066 |
{ |
| 1067 |
name: "lib, excluded partial function", |
| 1068 |
lib: NewLibrarySubset().AddExcludedFunctions([]*Function{ |
| 1069 |
{Name: "size", Overloads: []*Overload{{ID: "size_list"}}}, |
| 1070 |
}...), |
| 1071 |
orig: mustNewFunction(t, "size", |
| 1072 |
decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType), |
| 1073 |
decls.Overload("size_list", []*types.Type{types.NewListType(types.NewTypeParamType("T"))}, types.IntType), |
| 1074 |
), |
| 1075 |
subset: mustNewFunction(t, "size", decls.Overload("size_string", []*types.Type{types.StringType}, types.IntType)), |
| 1076 |
included: true, |
| 1077 |
}, |
| 1078 |
} |
| 1079 |
for _, tst := range tests { |
| 1080 |
tc := tst |
| 1081 |
t.Run(tc.name, func(t *testing.T) { |
| 1082 |
got, included := tc.lib.SubsetFunction(tc.orig) |
| 1083 |
if included != tc.included { |
| 1084 |
t.Fatalf("SubsetFunction() got included %t, wanted %t", included, tc.included) |
| 1085 |
} |
| 1086 |
if !tc.included { |
| 1087 |
return |
| 1088 |
} |
| 1089 |
assertFuncEquals(t, got, tc.subset) |
| 1090 |
}) |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
func TestSubsetMacro(t *testing.T) { |
| 1095 |
tests := []struct { |
| 1096 |
name string |
| 1097 |
lib *LibrarySubset |
| 1098 |
macroName string |
| 1099 |
included bool |
| 1100 |
}{ |
| 1101 |
{ |
| 1102 |
name: "nil lib, included", |
| 1103 |
macroName: "has", |
| 1104 |
included: true, |
| 1105 |
}, |
| 1106 |
{ |
| 1107 |
name: "empty, included", |
| 1108 |
lib: NewLibrarySubset(), |
| 1109 |
macroName: "has", |
| 1110 |
included: true, |
| 1111 |
}, |
| 1112 |
{ |
| 1113 |
name: "empty, disabled", |
| 1114 |
lib: NewLibrarySubset().SetDisabled(true), |
| 1115 |
macroName: "has", |
| 1116 |
included: false, |
| 1117 |
}, |
| 1118 |
{ |
| 1119 |
name: "empty, included", |
| 1120 |
lib: NewLibrarySubset().SetDisableMacros(true), |
| 1121 |
macroName: "has", |
| 1122 |
included: false, |
| 1123 |
}, |
| 1124 |
{ |
| 1125 |
name: "lib, not included allow-list", |
| 1126 |
lib: NewLibrarySubset().AddIncludedMacros("exists"), |
| 1127 |
macroName: "has", |
| 1128 |
included: false, |
| 1129 |
}, |
| 1130 |
{ |
| 1131 |
name: "lib, included allow-list", |
| 1132 |
lib: NewLibrarySubset().AddIncludedMacros("exists"), |
| 1133 |
macroName: "exists", |
| 1134 |
included: true, |
| 1135 |
}, |
| 1136 |
{ |
| 1137 |
name: "lib, not included deny-list", |
| 1138 |
lib: NewLibrarySubset().AddExcludedMacros("exists"), |
| 1139 |
macroName: "exists", |
| 1140 |
included: false, |
| 1141 |
}, |
| 1142 |
{ |
| 1143 |
name: "lib, included deny-list", |
| 1144 |
lib: NewLibrarySubset().AddExcludedMacros("exists"), |
| 1145 |
macroName: "has", |
| 1146 |
included: true, |
| 1147 |
}, |
| 1148 |
} |
| 1149 |
for _, tst := range tests { |
| 1150 |
tc := tst |
| 1151 |
t.Run(tc.name, func(t *testing.T) { |
| 1152 |
included := tc.lib.SubsetMacro(tc.macroName) |
| 1153 |
if included != tc.included { |
| 1154 |
t.Fatalf("SubsetMacro() got included %t, wanted %t", included, tc.included) |
| 1155 |
} |
| 1156 |
}) |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
func TestNewExtension(t *testing.T) { |
| 1161 |
tests := []struct { |
| 1162 |
name string |
| 1163 |
version uint32 |
| 1164 |
want *Extension |
| 1165 |
}{ |
| 1166 |
{ |
| 1167 |
name: "strings", |
| 1168 |
version: math.MaxUint32, |
| 1169 |
want: &Extension{Name: "strings", Version: "latest"}, |
| 1170 |
}, |
| 1171 |
{ |
| 1172 |
name: "bindings", |
| 1173 |
version: 1, |
| 1174 |
want: &Extension{Name: "bindings", Version: "1"}, |
| 1175 |
}, |
| 1176 |
} |
| 1177 |
for _, tst := range tests { |
| 1178 |
tc := tst |
| 1179 |
t.Run(tc.name, func(t *testing.T) { |
| 1180 |
got := NewExtension(tc.name, tc.version) |
| 1181 |
if !reflect.DeepEqual(got, tc.want) { |
| 1182 |
t.Errorf("NewExtension() got %v, wanted %v", got, tc.want) |
| 1183 |
} |
| 1184 |
}) |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
func TestExtensionGetVersion(t *testing.T) { |
| 1189 |
tests := []struct { |
| 1190 |
name string |
| 1191 |
ext *Extension |
| 1192 |
want any |
| 1193 |
}{ |
| 1194 |
{ |
| 1195 |
name: "nil extension", |
| 1196 |
want: errors.New("invalid extension: nil"), |
| 1197 |
}, |
| 1198 |
{ |
| 1199 |
name: "missing name", |
| 1200 |
ext: &Extension{}, |
| 1201 |
want: errors.New("missing name"), |
| 1202 |
}, |
| 1203 |
{ |
| 1204 |
name: "unset version", |
| 1205 |
ext: &Extension{Name: "test"}, |
| 1206 |
want: uint32(0), |
| 1207 |
}, |
| 1208 |
{ |
| 1209 |
name: "numeric version", |
| 1210 |
ext: &Extension{Name: "test", Version: "1"}, |
| 1211 |
want: uint32(1), |
| 1212 |
}, |
| 1213 |
{ |
| 1214 |
name: "latest version", |
| 1215 |
ext: &Extension{Name: "test", Version: "latest"}, |
| 1216 |
want: uint32(math.MaxUint32), |
| 1217 |
}, |
| 1218 |
{ |
| 1219 |
name: "bad version", |
| 1220 |
ext: &Extension{Name: "test", Version: "1.0"}, |
| 1221 |
want: errors.New("invalid syntax"), |
| 1222 |
}, |
| 1223 |
} |
| 1224 |
for _, tst := range tests { |
| 1225 |
tc := tst |
| 1226 |
t.Run(tc.name, func(t *testing.T) { |
| 1227 |
ver, err := tc.ext.VersionNumber() |
| 1228 |
if err != nil { |
| 1229 |
wantErr, ok := tc.want.(error) |
| 1230 |
if !ok { |
| 1231 |
t.Fatalf("GetVersion() got error %v, wanted %v", err, tc.want) |
| 1232 |
} |
| 1233 |
if !strings.Contains(err.Error(), wantErr.Error()) { |
| 1234 |
t.Fatalf("GetVersion() got error %v, wanted error containing %v", err, wantErr) |
| 1235 |
} |
| 1236 |
return |
| 1237 |
} |
| 1238 |
if tc.want.(uint32) != ver { |
| 1239 |
t.Fatalf("GetVersion() got %d, wanted %v", ver, tc.want) |
| 1240 |
} |
| 1241 |
}) |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
func TestValidatorValidate(t *testing.T) { |
| 1246 |
tests := []struct { |
| 1247 |
name string |
| 1248 |
v *Validator |
| 1249 |
want error |
| 1250 |
}{ |
| 1251 |
{ |
| 1252 |
name: "nil validator", |
| 1253 |
v: nil, |
| 1254 |
want: errors.New("invalid validator: nil"), |
| 1255 |
}, |
| 1256 |
{ |
| 1257 |
name: "empty validator", |
| 1258 |
v: NewValidator(""), |
| 1259 |
want: errors.New("missing name"), |
| 1260 |
}, |
| 1261 |
} |
| 1262 |
|
| 1263 |
for _, tst := range tests { |
| 1264 |
tc := tst |
| 1265 |
t.Run(tc.name, func(t *testing.T) { |
| 1266 |
err := tc.v.Validate() |
| 1267 |
if err == nil && tc.want == nil { |
| 1268 |
return |
| 1269 |
} |
| 1270 |
if err == nil && tc.want != nil { |
| 1271 |
t.Fatalf("v.Validate() got valid, wanted error %v", tc.want) |
| 1272 |
} |
| 1273 |
if err != nil && tc.want == nil { |
| 1274 |
t.Fatalf("v.Validate() got error %v, wanted nil error", err) |
| 1275 |
} |
| 1276 |
if !strings.Contains(err.Error(), tc.want.Error()) { |
| 1277 |
t.Errorf("v.Validate() got error %v, wanted %v", err, tc.want) |
| 1278 |
} |
| 1279 |
}) |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
func TestValidatorConfigValue(t *testing.T) { |
| 1284 |
var v *Validator |
| 1285 |
if _, found := v.ConfigValue("limit"); found { |
| 1286 |
t.Error("v.ConfigValue() got value from nil validator") |
| 1287 |
} |
| 1288 |
v = NewValidator("validator").SetConfig(map[string]any{"limit": 2}) |
| 1289 |
if _, found := v.ConfigValue("absent"); found { |
| 1290 |
t.Error("v.ConfigValue() found absent key") |
| 1291 |
} |
| 1292 |
if val, found := v.ConfigValue("limit"); !found || val != 2 { |
| 1293 |
t.Errorf("v.ConfigValue() got %v, %t -- wanted 2, true", val, found) |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
func TestFeatureValidate(t *testing.T) { |
| 1298 |
tests := []struct { |
| 1299 |
name string |
| 1300 |
f *Feature |
| 1301 |
want error |
| 1302 |
}{ |
| 1303 |
{ |
| 1304 |
name: "nil feature", |
| 1305 |
f: nil, |
| 1306 |
want: errors.New("invalid feature: nil"), |
| 1307 |
}, |
| 1308 |
{ |
| 1309 |
name: "empty feature", |
| 1310 |
f: NewFeature("", true), |
| 1311 |
want: errors.New("missing name"), |
| 1312 |
}, |
| 1313 |
} |
| 1314 |
|
| 1315 |
for _, tst := range tests { |
| 1316 |
tc := tst |
| 1317 |
t.Run(tc.name, func(t *testing.T) { |
| 1318 |
err := tc.f.Validate() |
| 1319 |
if err == nil && tc.want == nil { |
| 1320 |
return |
| 1321 |
} |
| 1322 |
if err == nil && tc.want != nil { |
| 1323 |
t.Fatalf("f.Validate() got valid, wanted error %v", tc.want) |
| 1324 |
} |
| 1325 |
if err != nil && tc.want == nil { |
| 1326 |
t.Fatalf("f.Validate() got error %v, wanted nil error", err) |
| 1327 |
} |
| 1328 |
if !strings.Contains(err.Error(), tc.want.Error()) { |
| 1329 |
t.Errorf("f.Validate() got error %v, wanted %v", err, tc.want) |
| 1330 |
} |
| 1331 |
}) |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
func mustNewFunction(t *testing.T, name string, opts ...decls.FunctionOpt) *decls.FunctionDecl { |
| 1336 |
t.Helper() |
| 1337 |
fn, err := decls.NewFunction(name, opts...) |
| 1338 |
if err != nil { |
| 1339 |
t.Fatalf("decls.NewFunction() failed: %v", err) |
| 1340 |
} |
| 1341 |
return fn |
| 1342 |
} |
| 1343 |
|
| 1344 |
func assertFuncEquals(t *testing.T, got, want *decls.FunctionDecl) { |
| 1345 |
t.Helper() |
| 1346 |
if got.Name() != want.Name() { |
| 1347 |
t.Fatalf("got function name %s, wanted %s", got.Name(), want.Name()) |
| 1348 |
} |
| 1349 |
if len(got.OverloadDecls()) != len(want.OverloadDecls()) { |
| 1350 |
t.Fatalf("got overload count %d, wanted %d", len(got.OverloadDecls()), len(want.OverloadDecls())) |
| 1351 |
} |
| 1352 |
for i, gotOverload := range got.OverloadDecls() { |
| 1353 |
wantOverload := want.OverloadDecls()[i] |
| 1354 |
if gotOverload.ID() != wantOverload.ID() { |
| 1355 |
t.Errorf("got overload id: %s, wanted: %s", gotOverload.ID(), wantOverload.ID()) |
| 1356 |
} |
| 1357 |
if gotOverload.IsMemberFunction() != wantOverload.IsMemberFunction() { |
| 1358 |
t.Errorf("got is member function %t, wanted %t", gotOverload.IsMemberFunction(), wantOverload.IsMemberFunction()) |
| 1359 |
} |
| 1360 |
if len(gotOverload.ArgTypes()) != len(wantOverload.ArgTypes()) { |
| 1361 |
t.Fatalf("got arg count %d, wanted %d", len(gotOverload.ArgTypes()), len(wantOverload.ArgTypes())) |
| 1362 |
} |
| 1363 |
for i, p := range gotOverload.ArgTypes() { |
| 1364 |
wp := wantOverload.ArgTypes()[i] |
| 1365 |
if !p.IsExactType(wp) { |
| 1366 |
t.Errorf("got arg[%d] type %v, wanted %v", i, p, wp) |
| 1367 |
} |
| 1368 |
} |
| 1369 |
if len(gotOverload.TypeParams()) != len(wantOverload.TypeParams()) { |
| 1370 |
t.Fatalf("got type param count %d, wanted %d", len(gotOverload.TypeParams()), len(wantOverload.TypeParams())) |
| 1371 |
} |
| 1372 |
for i, p := range gotOverload.TypeParams() { |
| 1373 |
wp := wantOverload.TypeParams()[i] |
| 1374 |
if p != wp { |
| 1375 |
t.Errorf("got type param[%d] %s, wanted %s", i, p, wp) |
| 1376 |
} |
| 1377 |
} |
| 1378 |
if !gotOverload.ResultType().IsExactType(wantOverload.ResultType()) { |
| 1379 |
t.Errorf("got result type %v, wanted %v", gotOverload.ResultType(), wantOverload.ResultType()) |
| 1380 |
} |
| 1381 |
} |
| 1382 |
} |
| 1383 |
|
| 1384 |
func unmarshalYAML(t *testing.T, data []byte) *Config { |
| 1385 |
t.Helper() |
| 1386 |
config := &Config{} |
| 1387 |
if err := yaml.Unmarshal(data, config); err != nil { |
| 1388 |
t.Fatalf("yaml.Unmarshal(%q) failed: %v", string(data), err) |
| 1389 |
} |
| 1390 |
return config |
| 1391 |
} |
| 1392 |
|
| 1393 |
func marshalYAML(t *testing.T, config *Config) []byte { |
| 1394 |
t.Helper() |
| 1395 |
data, err := yaml.Marshal(config) |
| 1396 |
if err != nil { |
| 1397 |
t.Fatalf("yaml.Marshal(%q) failed: %v", string(data), err) |
| 1398 |
} |
| 1399 |
return data |
| 1400 |
} |