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