github.com/free5gc/openapi@v1.0.8/convert_test.go (about)

     1  package openapi
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type Struct struct {
    11  	A  []string
    12  	S  string
    13  	N  int
    14  	M  map[int]string
    15  	DT *time.Time
    16  }
    17  
    18  func TestConvert(t *testing.T) {
    19  	from := map[string]interface{}{
    20  		"A": []string{"Hello", "World"},
    21  		"S": "This is a string",
    22  		"N": 10,
    23  		"M": map[int]interface{}{
    24  			1: "one",
    25  			2: "two",
    26  			3: "three",
    27  		},
    28  		"DT": "2020-05-01T12:04:05+08:00",
    29  	}
    30  
    31  	var to Struct
    32  
    33  	err := Convert(from, &to)
    34  	assert.Nil(t, err, "convert failed")
    35  
    36  	// check data is correct
    37  	assert.Equal(t, []string{"Hello", "World"}, to.A)
    38  	assert.Equal(t, "This is a string", to.S)
    39  	assert.Equal(t, 10, to.N)
    40  	assert.Equal(t, map[int]string{
    41  		1: "one",
    42  		2: "two",
    43  		3: "three",
    44  	}, to.M)
    45  	expectTime, err := time.Parse(time.RFC3339, "2020-05-01T12:04:05+08:00")
    46  	assert.Equal(t, expectTime, *to.DT)
    47  }