utils_test.go 1.4 KB
package utils

import (
	"fmt"
	"testing"
)

func Test_DeepCopy(t *testing.T) {
	type User1 struct {
		Name    string
		Age     int
		Address string
	}
	type User2 struct {
		Name string
		Age  int
		Job  string
	}
	var src = User1{Name: "foo", Age: 10, Address: "bar"}
	var dst *User2
	if err := DeepCopy(&dst, src); err != nil {
		t.Fatal(err)
	}
	if src.Name != dst.Name {
		t.Fatal("deep copy fail.")
	}
	//t.Log(src,"\n",dst)
}

func TestValidVersion(t *testing.T) {
	inputs := []struct {
		In      string
		Compare string
		Out     bool
	}{
		{In: "0.9.0", Compare: "0.8.0", Out: true},
		{In: "0.8.11", Compare: "0.8.0", Out: true},
		{In: "0.7.0", Compare: "0.8.0", Out: false},
		{In: "0.8.0", Compare: "0.8.0", Out: true},
		{In: "0.9", Compare: "0.8.0", Out: true},
		{In: "0.10", Compare: "0.8.0", Out: true},
		{In: "1.8.0", Compare: "0.8.0", Out: true},
		{In: "0.99.0", Compare: "0.8.0", Out: true},
		{In: "01.0.0", Compare: "0.8.0", Out: true},
	}
	for i := range inputs {
		input := inputs[i]
		if ValidVersion(input.In, input.Compare) != input.Out {
			t.Fatal(fmt.Sprintf("valid version fail. input :%v compare:%v want:%v", input.In, input.Compare, input.Out))
		}
	}
}

func TestMergeMap(t *testing.T) {
	to := map[string]interface{}{"1": 1, "2": 2}
	from := map[string]interface{}{"3": 3, "4": 4}
	t.Log("merge 前:", to)
	MergeMap(to, from)
	t.Log("merge 后:", to)
}