github.com/vmware/govmomi@v0.43.0/vim25/mo/type_info_test.go (about) 1 /* 2 Copyright (c) 2014-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 mo 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/vmware/govmomi/vim25/types" 24 ) 25 26 func TestLoadAll(*testing.T) { 27 for _, typ := range t { 28 newTypeInfo(typ) 29 } 30 } 31 32 func TestApplyPropertyChange(t *testing.T) { 33 changes := []types.PropertyChange{ 34 {Name: "snapshot.currentSnapshot", Val: nil}, 35 {Name: "snapshot.currentSnapshot", Val: types.ManagedObjectReference{}}, 36 {Name: "snapshot.currentSnapshot", Val: &types.ManagedObjectReference{}}, 37 } 38 var vm VirtualMachine 39 vm.Self = types.ManagedObjectReference{Type: "VirtualMachine"} 40 vm.Snapshot = new(types.VirtualMachineSnapshotInfo) 41 ApplyPropertyChange(vm, changes) 42 } 43 44 // The virtual machine managed object has about 500 nested properties. 45 // It's likely to be indicative of the function's performance in general. 46 func BenchmarkLoadVirtualMachine(b *testing.B) { 47 vmtyp := reflect.TypeOf((*VirtualMachine)(nil)).Elem() 48 for i := 0; i < b.N; i++ { 49 newTypeInfo(vmtyp) 50 } 51 } 52 53 func TestPropertyPathFromString(t *testing.T) { 54 tests := []struct { 55 path string 56 expect *Field 57 }{ 58 {`foo.bar`, &Field{Path: "foo.bar"}}, 59 {`foo.bar["biz"]`, &Field{Path: "foo.bar", Key: "biz"}}, 60 {`foo.bar["biz"].baz`, &Field{Path: "foo.bar", Key: "biz", Item: "baz"}}, 61 {`foo.bar[0]`, &Field{Path: "foo.bar", Key: int32(0)}}, 62 {`foo.bar[1].baz`, &Field{Path: "foo.bar", Key: int32(1), Item: "baz"}}, 63 {`foo.bar[1].baz.buz`, &Field{Path: "foo.bar", Key: int32(1), Item: "baz.buz"}}, 64 } 65 66 for i, tp := range tests { 67 var field Field 68 if field.FromString(tp.path) { 69 if field.String() != tp.expect.String() { 70 t.Errorf("%d: %s != %s", i, field, tp.expect) 71 } 72 if field != *tp.expect { 73 t.Errorf("%d: %#v != %#v", i, field, *tp.expect) 74 } 75 } else { 76 t.Error(tp.path) 77 } 78 } 79 }