utils_test.go 2.0 KB
package utils

import (
	"encoding/json"
	"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},
		{In: "1.4.1", Compare: "1.3.0", Out: true},
		{In: "1.3.0", Compare: "1.3.0", Out: true},
		{In: "1.3.1", Compare: "1.3.0", Out: true},
		{In: "1.2.1", Compare: "1.3.0", Out: false},
		{In: "1.1.0", Compare: "1.3.0", Out: false},
		{In: "1.1", Compare: "1.3.0", Out: false},
		{In: "0.9.99", Compare: "1.3.0", Out: false},
		{In: "0.0.1", Compare: "1.3.0", Out: false},
	}
	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)
}

func TestIntUnmarsh(t *testing.T) {
	jsonString := `{"id":8242051651122944}`
	type param struct {
		Id int `json:"id"`
	}
	var p param
	json.Unmarshal([]byte(jsonString), &p)
	t.Log(p)
	jsonByte, _ := json.Marshal(p)
	t.Log(string(jsonByte))
}