github.com/vmware/govmomi@v0.51.0/vim25/types/base_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 types
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/vmware/govmomi/vim25/xml"
    13  )
    14  
    15  func TestAnyType(t *testing.T) {
    16  	x := func(s string) []byte {
    17  		s = `<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">` + s
    18  		s += `</root>`
    19  		return []byte(s)
    20  	}
    21  
    22  	tests := []struct {
    23  		Input []byte
    24  		Value any
    25  	}{
    26  		{
    27  			Input: x(`<name xsi:type="xsd:string">test</name>`),
    28  			Value: "test",
    29  		},
    30  		{
    31  			Input: x(`<name xsi:type="ArrayOfString"><string>AA</string><string>BB</string></name>`),
    32  			Value: ArrayOfString{String: []string{"AA", "BB"}},
    33  		},
    34  	}
    35  
    36  	for _, test := range tests {
    37  		var r struct {
    38  			A any `xml:"name,typeattr"`
    39  		}
    40  
    41  		dec := xml.NewDecoder(bytes.NewReader(test.Input))
    42  		dec.TypeFunc = TypeFunc()
    43  
    44  		err := dec.Decode(&r)
    45  		if err != nil {
    46  			t.Fatalf("Decode: %s", err)
    47  		}
    48  
    49  		if !reflect.DeepEqual(r.A, test.Value) {
    50  			t.Errorf("Expected: %#v, actual: %#v", r.A, test.Value)
    51  		}
    52  	}
    53  }