github.com/vmware/govmomi@v0.37.2/property/match_test.go (about)

     1  /*
     2  Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package property_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/vmware/govmomi/property"
    23  	"github.com/vmware/govmomi/vim25/types"
    24  )
    25  
    26  func TestMatchProperty(t *testing.T) {
    27  	tests := []struct {
    28  		key  string
    29  		val  types.AnyType
    30  		pass types.AnyType
    31  		fail types.AnyType
    32  	}{
    33  		{"string", "bar", "bar", "foo"},
    34  		{"match", "foo.bar", "foo.*", "foobarbaz"},
    35  		{"moref", types.ManagedObjectReference{Type: "HostSystem", Value: "foo"}, "HostSystem:foo", "bar"}, // implements fmt.Stringer
    36  		{"morefm", types.ManagedObjectReference{Type: "HostSystem", Value: "foo"}, "*foo", "bar"},
    37  		{"morefs", types.ArrayOfManagedObjectReference{ManagedObjectReference: []types.ManagedObjectReference{{Type: "HostSystem", Value: "foo"}}}, "*foo", "bar"},
    38  		{"enum", types.VirtualMachinePowerStatePoweredOn, "poweredOn", "poweredOff"},
    39  		{"int16", int32(16), int32(16), int32(42)},
    40  		{"int32", int32(32), int32(32), int32(42)},
    41  		{"int32s", int32(32), "32", "42"},
    42  		{"int64", int64(64), int64(64), int64(42)},
    43  		{"int64s", int64(64), "64", "42"},
    44  		{"float32", float32(32.32), float32(32.32), float32(42.0)},
    45  		{"float32s", float32(32.32), "32.32", "42.0"},
    46  		{"float64", float64(64.64), float64(64.64), float64(42.0)},
    47  		{"float64s", float64(64.64), "64.64", "42.0"},
    48  	}
    49  
    50  	for _, test := range tests {
    51  		p := types.DynamicProperty{Name: test.key, Val: test.val}
    52  
    53  		for match, value := range map[bool]types.AnyType{true: test.pass, false: test.fail} {
    54  			result := property.Match{test.key: value}.Property(p)
    55  
    56  			if result != match {
    57  				t.Errorf("%s: %t", test.key, result)
    58  			}
    59  		}
    60  	}
    61  }