github.com/vmware/govmomi@v0.51.0/property/match_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package property_test 6 7 import ( 8 "testing" 9 10 "github.com/vmware/govmomi/property" 11 "github.com/vmware/govmomi/vim25/types" 12 ) 13 14 func TestMatchProperty(t *testing.T) { 15 for _, test := range []struct { 16 key string 17 val types.AnyType 18 pass types.AnyType 19 fail types.AnyType 20 }{ 21 {"string", "bar", "bar", "foo"}, 22 {"match", "foo.bar", "foo.*", "foobarbaz"}, 23 {"moref", types.ManagedObjectReference{Type: "HostSystem", Value: "foo"}, "HostSystem:foo", "bar"}, // implements fmt.Stringer 24 {"morefm", types.ManagedObjectReference{Type: "HostSystem", Value: "foo"}, "*foo", "bar"}, 25 {"morefs", types.ArrayOfManagedObjectReference{ManagedObjectReference: []types.ManagedObjectReference{{Type: "HostSystem", Value: "foo"}}}, "*foo", "bar"}, 26 {"enum", types.VirtualMachinePowerStatePoweredOn, "poweredOn", "poweredOff"}, 27 {"int16", int32(16), int32(16), int32(42)}, 28 {"int32", int32(32), int32(32), int32(42)}, 29 {"int32s", int32(32), "32", "42"}, 30 {"int64", int64(64), int64(64), int64(42)}, 31 {"int64s", int64(64), "64", "42"}, 32 {"float32", float32(32.32), float32(32.32), float32(42.0)}, 33 {"float32s", float32(32.32), "32.32", "42.0"}, 34 {"float64", float64(64.64), float64(64.64), float64(42.0)}, 35 {"float64s", float64(64.64), "64.64", "42.0"}, 36 {"matchFunc", "bar", func(s any) bool { return s.(string) == "bar" }, func(s any) bool { return s.(string) == "foo" }}, 37 } { 38 p := types.DynamicProperty{Name: test.key, Val: test.val} 39 40 for match, value := range map[bool]types.AnyType{true: test.pass, false: test.fail} { 41 result := property.Match{test.key: value}.Property(p) 42 43 if result != match { 44 t.Errorf("%s: %t", test.key, result) 45 } 46 } 47 } 48 }