Beispiel #1
0
func TestManager(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	testCaseTable := []struct {
		in  [][]string
		out [][]string
	}{
		{
			[][]string{{}}, [][]string{},
		},
		{
			[][]string{{" ", " "}, {""}}, [][]string{},
		},
		{
			[][]string{
				{"1", "2", " "},
				{"1", " ", " "},
				{" ", " ", " "},
				{"1"},
				{" ", " "},
			},
			[][]string{
				{"1", "2"},
				{"1", " "},
				{"1", ""},
			},
		},
	}
	for _, testCase := range testCaseTable {
		ret := Trim2DArray(testCase.in)
		t.Equal(ret, testCase.out)
	}
}
Beispiel #2
0
func TestFloatCompare(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	for i, testCase := range []struct {
		f      func(x float64, y float64) bool
		x      float64
		y      float64
		result bool
	}{
		{Float64LessThan, 1.0, 2.0, true},
		{Float64LessThan, 2.0, 1.0, false},
		{Float64LessThan, 2.0, 2.0, false},
		{Float64LessEqualThan, 1.0, 2.0, true},
		{Float64LessEqualThan, 1.0, 1.0, true},
		{Float64LessEqualThan, 2.0, 1.0, false}, //5

		{Float64GreaterThan, 1.0, 2.0, false},
		{Float64GreaterThan, 2.0, 1.0, true},
		{Float64GreaterThan, 1.0, 1.0, false},
		{Float64Equal, 1.0, 1.0, true},
		{Float64Equal, 1.0, 2.0, false}, //10

		{Float64Equal, 1.0 / 3.0 * 3.0, 1.0, true},
		{Float64GreaterEqualThan, 1.0, 2.0, false},
		{Float64GreaterEqualThan, 2.0, 1.0, true},
		{Float64GreaterEqualThan, 1.0, 1.0, true},
	} {
		t.EqualMsg(testCase.f(testCase.x, testCase.y), testCase.result,
			"fail at %d", i)
	}
}
Beispiel #3
0
func TestNewUrlByString(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	url, err := NewUrlByString("http://www.google.com")
	t.Equal(nil, err)
	t.Equal("http://www.google.com", url.String())

}
Beispiel #4
0
func TestUnmarshalWithFalseSetterIgnoresValue(ot *testing.T) {
	c := kmgTest.NewTestTools(ot)
	setterResult[2] = false
	setterResult[4] = false
	defer func() {
		delete(setterResult, 2)
		delete(setterResult, 4)
	}()

	m := map[string]*typeWithSetter{}
	data := "{abc: 1, def: 2, ghi: 3, jkl: 4}"
	err := Unmarshal([]byte(data), m)
	c.Equal(err, nil)
	c.Ok(m["abc"] != nil)
	c.Equal(m["def"], nil)
	c.Ok(m["ghi"] != nil)
	c.Equal(m["jkl"], nil)
	c.Equal(m["abc"].value, 1)
	c.Equal(m["ghi"].value, 3)
	/*
		c.Assert(err, IsNil)
		c.Assert(m["abc"], NotNil)
		c.Assert(m["def"], IsNil)
		c.Assert(m["ghi"], NotNil)
		c.Assert(m["jkl"], IsNil)

		c.Assert(m["abc"].value, Equals, 1)
		c.Assert(m["ghi"].value, Equals, 3)
	*/
}
Beispiel #5
0
func TestPeriodList(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	itemList := ScratchItemList3{
		[]int{1, 2},
		[]int{2, 3},
		[]int{3, 4},
	}
	periodList := PeriodList{
		{
			Period:      Period{Start: MustFromMysqlFormat("2001-01-00 23:30:00"), End: MustFromMysqlFormat("2001-01-01 23:30:00")},
			OriginIndex: 0,
		},
		{
			Period:      Period{Start: MustFromMysqlFormat("2001-01-03 23:30:00"), End: MustFromMysqlFormat("2001-01-04 23:30:00")},
			OriginIndex: 1,
		},
		{
			Period:      Period{Start: MustFromMysqlFormat("2001-01-02 23:30:00"), End: MustFromMysqlFormat("2001-01-03 23:30:00")},
			OriginIndex: 2,
		},
	}
	periodList.Sort()
	i, exist := periodList.SelectFromTime(MustFromMysqlFormat("2001-01-01 23:00:00"))
	t.Equal(exist, true)
	t.Equal(itemList[i], []int{1, 2})
	i, exist = periodList.SelectFromTime(MustFromMysqlFormat("2001-01-03 23:00:00"))
	t.Equal(exist, true)
	t.Equal(itemList[i], []int{3, 4})
}
Beispiel #6
0
func TestGetPeriodFromSortedList(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	SortedList := []Period{
		{
			Start: MustFromMysqlFormat("2001-01-01 00:00:00"),
			End:   MustFromMysqlFormat("2001-01-01 01:00:00"),
		},
		{
			Start: MustFromMysqlFormat("2001-01-01 02:00:00"),
			End:   MustFromMysqlFormat("2001-01-01 03:00:00"),
		},
		{
			Start: MustFromMysqlFormat("2001-01-01 03:00:00"),
			End:   MustFromMysqlFormat("2001-01-01 04:00:00"),
		},
	}
	for _, testcase := range []struct {
		t  time.Time
		i  int
		ok bool
	}{
		{MustFromMysqlFormat("2001-01-00 23:30:00"), 0, false},
		{MustFromMysqlFormat("2001-01-01 00:30:00"), 0, true},
		{MustFromMysqlFormat("2001-01-01 03:00:00"), 2, true},
		{MustFromMysqlFormat("2001-01-01 04:30:00"), 0, false},
	} {
		i, ok := GetPeriodFromSortedList(testcase.t, SortedList)
		t.Equal(i, testcase.i)
		t.Equal(ok, testcase.ok)
	}
}
Beispiel #7
0
func TestUnmarshalErrors(ot *testing.T) {
	c := kmgTest.NewTestTools(ot)
	for _, item := range unmarshalErrorTests {
		var value interface{}
		err := Unmarshal([]byte(item.data), &value)
		c.Equal(err.Error(), item.error)
	}
}
Beispiel #8
0
func TestCreateCert(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	config, err := CreateTlsConfig()
	t.Equal(err, nil)
	t.Ok(config != nil)

	_ = tls.NewListener(&mockListener{}, config)
}
Beispiel #9
0
func TestUnmarshalNaN(ot *testing.T) {
	c := kmgTest.NewTestTools(ot)
	value := map[string]interface{}{}
	err := Unmarshal([]byte("notanum: .NaN"), &value)
	c.Equal(err, nil)
	c.Equal(math.IsNaN(value["notanum"].(float64)), true)
	//c.Assert(err, IsNil)
	//c.Assert(math.IsNaN(value["notanum"].(float64)), Equals, true)
}
Beispiel #10
0
func TestStringType(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	var data *string
	data = new(string)
	m, err := NewContext(data)
	t.Equal(err, nil)

	err = m.SaveByPath(Path{"ptr"}, "123")
	t.Equal(err, nil)
	t.Ok(data != nil)
	t.Equal(*data, "123")
}
Beispiel #11
0
func Test1(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	//加密数据,可以正确解密测试
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		ob, err := Encrypt([]byte("1"), origin)
		t.Equal(err, nil)
		ret, err := Decrypt([]byte("1"), ob)
		t.Equal(err, nil)
		t.Equal(ret, origin)

		sob, err := EncryptString("1", origin)
		t.Equal(err, nil)
		sret, err := DecryptString("1", sob)
		t.Equal(err, nil)
		t.Equal(sret, origin)
	}
	//任意数据传入解密不会挂掉,并且会报错
	for _, origin := range [][]byte{
		[]byte(""),
		[]byte("1"),
		[]byte("12"),
		[]byte("123"),
		[]byte("1234"),
		[]byte("12345"),
		[]byte("123456"),
		[]byte("1234567"),
		[]byte("12345678"),
		[]byte("123456789"),
		[]byte("1234567890"),
		[]byte("123456789012345"),
		[]byte("1234567890123456"),
		[]byte("12345678901234567"),
	} {
		_, err := Decrypt([]byte("1"), origin)
		t.Ok(err != nil)
	}
}
func TestStringTransformSubType(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	in := &StringTranT1{
		T2: "6",
	}
	err := StringTransformSubType(in, map[string]map[string]string{
		"github.com/bronze1man/kmg/typeTransform.StringTranT2": {
			"6": "Fire",
		},
	})
	t.Equal(err, nil)
	t.Equal(in.T2, StringTranT2("Fire"))
}
Beispiel #13
0
func TestStructType(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	data := &struct {
		A string
	}{}
	m, err := NewContext(data)
	t.Equal(err, nil)

	err = m.SaveByPath(Path{"ptr", "A"}, "123")
	t.Equal(err, nil)
	t.Ok(data != nil)
	t.Equal(data.A, "123")
}
Beispiel #14
0
func TestStructGetAllFieldMap(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	t1 := reflect.TypeOf(&GetAllFieldT1{})
	ret := StructGetAllFieldMap(t1)
	t.Equal(ret["A"].Index, []int{0, 0})
	t.Equal(ret["B"].Index, []int{2})
	t.Equal(ret["C"].Index, []int{0, 2, 2})
	t.Equal(ret["D"].Index, []int{1, 1})
	t.Equal(len(ret), 7)

	ret = StructGetAllFieldMap(reflect.TypeOf(&GetAllFieldT5{}))
	t.Equal(ret["A"].Index, []int{1})
	t.Equal(len(ret), 2)
}
Beispiel #15
0
func TestGetFullName(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	name := GetTypeFullName(reflect.TypeOf(""))
	t.Equal(name, "string")

	name = GetTypeFullName(reflect.TypeOf(1))
	t.Equal(name, "int")

	name = GetTypeFullName(reflect.TypeOf(&ta{}))
	t.Equal(name, "github.com/bronze1man/kmg/kmgReflect.ta")

	name = GetTypeFullName(reflect.TypeOf([]string{}))
	t.Equal(name, "")

}
Beispiel #16
0
func TestUnmarshalWholeDocumentWithSetter(ot *testing.T) {
	c := kmgTest.NewTestTools(ot)
	obj := &typeWithSetter{}
	err := Unmarshal([]byte(setterTests[0].data), obj)
	c.Equal(err, nil)
	c.Equal(obj.tag, setterTests[0].tag)
	//c.Assert(err, IsNil)
	//c.Assert(obj.tag, Equals, setterTests[0].tag)

	value, ok := obj.value.(map[interface{}]interface{})
	c.Equal(ok, true)
	c.Equal(value["_"], setterTests[0].value)

	//c.Assert(ok, Equals, true)
	//c.Assert(value["_"], DeepEquals, setterTests[0].value)
}
Beispiel #17
0
func TestUnicodeMarshal(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	testCaseTable := []struct {
		in  string
		out string
	}{
		{
			`中文`, "中文\n",
		},
	}
	for _, testCase := range testCaseTable {
		outByte, err := Marshal(testCase.in)
		t.Equal(err, nil)
		t.Equal(string(outByte), testCase.out)
	}
}
Beispiel #18
0
func TestRsaPublicDecryptPKCS1v15(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	priv, err := rsa.GenerateKey(rand.Reader, 256)
	t.Equal(err, nil)
	for _, datas := range []string{
		"\000",
		"\000\000",
		"123456789012345678901",
	} {
		data := []byte(datas)
		enc, err := RsaPrivateEncryptPKCS1v15(priv, data)
		t.Equal(err, nil)
		dout, err := RsaPublicDecryptPKCS1v15(&priv.PublicKey, enc)
		t.Equal(err, nil)
		t.Equal(dout, data)
	}
}
Beispiel #19
0
func TestWriteRead(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	buf := &bytes.Buffer{}
	inData := [][]string{
		{"中文"},
		{"1", "", "2"},
	}
	err := Array2XlsxIo(inData, buf)
	t.Equal(err, nil)
	r := bytes.NewReader(buf.Bytes())
	zr, err := zip.NewReader(r, int64(buf.Len()))
	t.Equal(err, nil)
	xlsxFile, err := xlsx.ReadZipReader(zr)
	t.Equal(err, nil)
	outData, err := xlsx2ArrayXlsxFile(xlsxFile)
	t.Equal(err, nil)
	t.Equal(outData[0], inData)
}
Beispiel #20
0
func TestUnmarshalWithSetter(ot *testing.T) {
	c := kmgTest.NewTestTools(ot)
	for _, item := range setterTests {
		obj := &typeWithSetterField{}
		err := Unmarshal([]byte(item.data), obj)
		c.Equal(err, nil)
		c.Ok(obj.Field != nil)
		c.Equal(obj.Field.tag, item.tag)
		c.Equal(obj.Field.value, item.value)
		/*
			c.Assert(err, IsNil)
			c.Assert(obj.Field, NotNil,
				Commentf("Pointer not initialized (%#v)", item.value))
			c.Assert(obj.Field.tag, Equals, item.tag)
			c.Assert(obj.Field.value, DeepEquals, item.value)
		*/
	}
}
Beispiel #21
0
func TestKeyListLess(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	keyList := keyList{
		reflect.ValueOf("Quest2Name_12"),  //3
		reflect.ValueOf("Quest2Name_101"), //5
	}

	testCaseTable := []struct {
		i   int
		j   int
		ret bool
	}{
		{0, 1, false},
		{1, 0, true},
	}
	for _, testCase := range testCaseTable {
		t.EqualMsg(keyList.Less(testCase.i, testCase.j), testCase.ret,
			"%d %d %v", testCase.i, testCase.j, testCase.ret)
	}
	sort.Sort(keyList)
}
Beispiel #22
0
func TestCoordinateXy2Excel(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	for _, c := range []struct {
		x   int
		y   int
		out string
	}{
		{0, 0, "A1"},
		{25, 0, "Z1"},
		{25, 1000, "Z1001"},
		{26, 1000, "AA1001"},
		{27, 1000, "AB1001"},
		{676, 0, "ZA1"},
		{701, 0, "ZZ1"},
		{702, 0, "AAA1"},
		{703, 0, "AAB1"},
		{728, 0, "ABA1"},
	} {
		t.Equal(CoordinateXy2Excel(c.x, c.y), c.out)
	}
}
Beispiel #23
0
func TestStructGetAllField(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	t1 := reflect.TypeOf(&GetAllFieldT1{})
	ret := StructGetAllField(t1)
	t.Equal(len(ret), 7)
	t.Equal(ret[0].Name, "GetAllFieldT3")
	t.Equal(ret[1].Name, "GetAllFieldT4")
	t.Equal(ret[2].Name, "B")
	t.Equal(ret[2].Index, []int{2})
	t.Equal(ret[3].Name, "A")
	t.Equal(ret[3].Index, []int{0, 0})
	t.Equal(ret[4].Name, "GetAllFieldT2")
	t.Equal(ret[5].Name, "C")
	t.Equal(ret[5].Index, []int{0, 2, 2})
	t.Equal(ret[6].Name, "D")
	t.Equal(ret[6].Index, []int{1, 1})

	ret = StructGetAllField(reflect.TypeOf(&GetAllFieldT5{}))
	t.Equal(len(ret), 2)

}
Beispiel #24
0
func TestRsaOpensslVerify(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	var err error
	privateKey := []byte(`-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDA4E8H2qksOnCSoBkq+HH3Dcu0/iWt3iNcpC/BCg0F8tnMhF1Q
OQ98cRUM8eeI9h+S6g/5UmO4hBKMOP3vg/u7kI0ujrCN1RXpsrTbWaqry/xTDgTM
8HkKkNhRSyDJWJVye0mPgbvVnx76en+K6LLzDaQH8yKI/dbswSq65XFcIwIDAQAB
AoGAU+uFF3LBdtf6kSGNsc+lrovXHWoTNOJZWn6ptIFOB0+SClVxUG1zWn7NXPOH
/WSxejfTOXTqpKb6dv55JpSzmzf8fZphVE9Dfr8pU68x8z5ft4yv314qLXFDkNgl
MeQht4n6mo1426dyoOcCfmWc5r7LQCi7WmOsKvATe3nzk/kCQQDp1gyDIVAbUvwe
tpsxZpAd3jLD49OVHUIy2eYGzZZLK3rA1uNWWZGsjrJQvfGf+mW+/zeUMYPBpk0B
XYqlgHJNAkEA0yhhu/2SPJYxIS9umCry1mj7rwji5O2qVSssChFyOctcbysbNJLH
qoF7wumr9PAjjWFWdmCzzEJyxMMurL3gLwJBAIEoeNrJQL0G9jlktY3wz7Ofsrye
j5Syh4kc8EBbuCMnDfOL/iAI8zyzyOxuLhMmNKLtx140h0kkOS6C430M2JUCQCnM
a5RX/JOrs2v7RKwwjENvIqsiWi+w8C/NzPjtPSw9mj2TTd5ZU9bnrMUHlnd09cSt
yPzD5bOAT9GtRVcCexcCQBxXHRleikPTJC90GqrC2l6erYJaiSOtv6QYIh0SEDVm
1o6Whw4FEHUPqMW0Z5PobPFiEQT+fFR02xU3NJrjYy0=
-----END RSA PRIVATE KEY-----`)
	publicKey := []byte(`-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDA4E8H2qksOnCSoBkq+HH3Dcu0/
iWt3iNcpC/BCg0F8tnMhF1QOQ98cRUM8eeI9h+S6g/5UmO4hBKMOP3vg/u7kI0ujr
CN1RXpsrTbWaqry/xTDgTM8HkKkNhRSyDJWJVye0mPgbvVnx76en+K6LLzDaQH8yK
I/dbswSq65XFcIwIDAQAB
-----END PUBLIC KEY-----`)
	signed := kmgBase64.MustStdBase64DecodeString(`AqDW/m+aGn2kFo54Bt5XnXniBDtCxmPS6FMfHrLizh7d4jgnz4LbwBfRvXywI6HEKgr7Vk37duTM8P+XqmT+uQU2R1h4nRwOf2fCstXmgeD3qGk/XI+XMafgMkTnV/B9dOXpdUbxEpL1fDhmo7A6J0rcJotG7TP7i1zcvY4oiXk=`)
	msg := []byte("this is a test!")
	rsaKey, err := RsaParseOpensslPrivateKey(privateKey)
	s, err := RsaOpensslSign(rsaKey, crypto.SHA1, msg)
	t.Equal(err, nil)
	t.Equal(s, signed)

	rsapk, err := RsaParseOpensslPublicKey(publicKey)
	t.Equal(err, nil)
	err = RsaOpensslVerify(rsapk, crypto.SHA1, msg, signed)
	t.Equal(err, nil)
}
Beispiel #25
0
func TestPeriodListInterface(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	periodList := ScratchPeriodList2{
		{
			Period:   Period{Start: MustFromMysqlFormat("2001-01-00 23:30:00"), End: MustFromMysqlFormat("2001-01-01 23:30:00")},
			ItemList: []int{1, 2},
		},
		{
			Period:   Period{Start: MustFromMysqlFormat("2001-01-03 23:30:00"), End: MustFromMysqlFormat("2001-01-04 23:30:00")},
			ItemList: []int{2, 3},
		},
		{
			Period:   Period{Start: MustFromMysqlFormat("2001-01-02 23:30:00"), End: MustFromMysqlFormat("2001-01-03 23:30:00")},
			ItemList: []int{3, 4},
		},
	}
	PeriodListSort(periodList)
	i, exist := SelectPeriodFromSortedPeriodList(MustFromMysqlFormat("2001-01-01 23:00:00"), periodList)
	t.Equal(exist, true)
	t.Equal(periodList[i].ItemList, []int{1, 2})
	i, exist = SelectPeriodFromSortedPeriodList(MustFromMysqlFormat("2001-01-03 23:00:00"), periodList)
	t.Equal(exist, true)
	t.Equal(periodList[i].ItemList, []int{3, 4})
}
Beispiel #26
0
func TestGetPeriodFromGenericSortedList(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	periodList1 := ScratchPeriodList1{
		{
			Period:   Period{Start: MustFromMysqlFormat("2001-01-00 23:30:00"), End: MustFromMysqlFormat("2001-01-01 23:30:00")},
			ItemList: []int{1, 2},
		},
		{
			Period:   Period{Start: MustFromMysqlFormat("2001-01-03 23:30:00"), End: MustFromMysqlFormat("2001-01-04 23:30:00")},
			ItemList: []int{2, 3},
		},
		{
			Period:   Period{Start: MustFromMysqlFormat("2001-01-02 23:30:00"), End: MustFromMysqlFormat("2001-01-03 23:30:00")},
			ItemList: []int{3, 4},
		},
	}
	sort.Sort(periodList1)
	i, err := GetPeriodFromGenericSortedList(MustFromMysqlFormat("2001-01-01 23:00:00"), periodList1)
	t.Equal(err, nil)
	t.Equal(periodList1[i].ItemList, []int{1, 2})
	i, err = GetPeriodFromGenericSortedList(MustFromMysqlFormat("2001-01-03 23:00:00"), periodList1)
	t.Equal(err, nil)
	t.Equal(periodList1[i].ItemList, []int{3, 4})
}
Beispiel #27
0
func TestType(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	data := &T{}
	m, err := NewContext(data)
	t.Equal(err, nil)

	err = m.SaveByPath(Path{"ptr", "String1"}, "B")
	t.Equal(err, nil)
	t.Equal(data.String1, "B")

	m.SaveByPath(Path{"ptr", "Map1", "A"}, "1123")
	_, ok := data.Map1["A"]
	t.Equal(ok, true)
	t.Equal(data.Map1["A"], "1123")

	err = m.SaveByPath(Path{"ptr", "Map1", "A"}, "1124")
	t.Equal(err, nil)
	t.Equal(data.Map1["A"], "1124")

	err = m.DeleteByPath(Path{"ptr", "Map1", "A"})
	t.Equal(err, nil)
	_, ok = data.Map1["A"]
	t.Equal(ok, false)

	err = m.SaveByPath(Path{"ptr", "Map2", "B", "ptr"}, "1")
	t.Equal(err, nil)
	rpString, ok := data.Map2["B"]
	t.Equal(ok, true)
	t.Equal(*rpString, "1")

	err = m.SaveByPath(Path{"ptr", "Map2", "B", "ptr"}, "2")
	t.Equal(err, nil)
	t.Equal(*rpString, "2")

	err = m.DeleteByPath(Path{"ptr", "Map2", "B", "ptr"})
	t.Equal(err, nil)
	_, ok = data.Map2["B"]
	t.Equal(ok, true)
	t.Equal(data.Map2["B"], nil)

	err = m.DeleteByPath(Path{"ptr", "Map2", "B"})
	t.Equal(err, nil)
	_, ok = data.Map2["B"]
	t.Equal(ok, false)

	err = m.SaveByPath(Path{"ptr", "Map3", "C", "A"}, "1")
	t.Equal(err, nil)
	t.Equal(data.Map3["C"].A, "1")

	err = m.DeleteByPath(Path{"ptr", "Map3", "C"})
	t.Equal(err, nil)
	t.Ok(data.Map3 != nil)
	_, ok = data.Map3["C"]
	t.Equal(ok, false)

	err = m.SaveByPath(Path{"ptr", "Map4", "D", "F"}, "1234")
	t.Equal(err, nil)
	t.Equal(data.Map4["D"]["F"], "1234")

	err = m.SaveByPath(Path{"ptr", "Map4", "D", "H"}, "12345")
	t.Equal(err, nil)
	t.Equal(data.Map4["D"]["H"], "12345")

	err = m.SaveByPath(Path{"ptr", "Map4", "D", "H"}, "12346")
	t.Equal(err, nil)
	t.Equal(data.Map4["D"]["H"], "12346")

	err = m.DeleteByPath(Path{"ptr", "Map4", "D", "F"})
	t.Equal(err, nil)
	t.Ok(data.Map4["D"] != nil)
	_, ok = data.Map4["D"]["F"]
	t.Equal(ok, false)

	_, ok = data.Map4["D"]["H"]
	t.Equal(ok, true)

	err = m.SaveByPath(Path{"ptr", "Map5", "D", ""}, "1234")
	t.Equal(err, nil)
	t.Equal(len(data.Map5["D"]), 1)
	t.Equal(data.Map5["D"][0], "1234")

	err = m.DeleteByPath(Path{"ptr", "Map5", "D", "0"})
	t.Equal(err, nil)
	t.Equal(len(data.Map5["D"]), 0)

	err = m.SaveByPath(Path{"ptr", "Slice1", ""}, "1234")
	t.Equal(err, nil)
	t.Equal(len(data.Slice1), 1)
	t.Equal(data.Slice1[0], "1234")

	err = m.SaveByPath(Path{"ptr", "Slice1", ""}, "12345")
	t.Equal(err, nil)
	t.Equal(data.Slice1[1], "12345")
	t.Equal(len(data.Slice1), 2)

	err = m.DeleteByPath(Path{"ptr", "Slice1", "0"})
	t.Equal(err, nil)
	t.Equal(len(data.Slice1), 1)
	t.Equal(data.Slice1[0], "12345")

	err = m.SaveByPath(Path{"ptr", "Ptr1", "ptr"}, "12345")
	t.Equal(err, nil)
	t.Equal(*data.Ptr1, "12345")

	err = m.SaveByPath(Path{"ptr", "Ptr2", "ptr"}, "")
	t.Equal(err, nil)
	t.Equal(data.Ptr2.A, "")

	err = m.SaveByPath(Path{"ptr", "Array1", "1"}, "12345")
	t.Equal(err, nil)
	t.Equal(data.Array1[1], "12345")
}
Beispiel #28
0
func TestManager(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	Int := 0
	ArrMapStringInt := []map[string]int{}
	type T1 struct {
		A int
		B string
	}
	ArrStruct := []T1{}
	StringSlice := []string{}
	testCaseTable := []struct {
		in  interface{}
		out interface{}
		exp interface{}
	}{
		{1, &Int, 1}, //0
		{int64(1), &Int, 1},
		{ //2
			[]map[string]string{
				{
					"a": "1",
				},
				{
					"b": "1",
				},
			},
			&ArrMapStringInt,
			[]map[string]int{
				{
					"a": 1,
				},
				{
					"b": 1,
				},
			},
		},
		{ //3
			[]map[string]string{
				{
					"A": "1",
					"B": "abc",
					"C": "abd",
				},
				{
					"A": "",
					"B": "",
					"C": "abd",
				},
			},
			&ArrStruct,
			[]T1{
				{
					A: 1,
					B: "abc",
				},
				{
					A: 0,
					B: "",
				},
			},
		},
		{ //4
			[]interface{}{
				"1",
				"2",
			},
			&StringSlice,
			[]string{
				"1",
				"2",
			},
		},
	}
	for i, testCase := range testCaseTable {
		err := Transform(testCase.in, testCase.out)
		t.EqualMsg(err, nil, "fail at %d", i)
		t.EqualMsg(reflect.ValueOf(testCase.out).Elem().Interface(), testCase.exp, "fail at %d", i)
	}
}
Beispiel #29
0
func TestFormat(ot *testing.T) {
	tc := kmgTest.NewTestTools(ot)
	t, err := time.Parse(AppleJsonFormat, "2014-04-16 18:26:18 Etc/GMT")
	tc.Equal(err, nil)
	tc.Ok(t.Equal(MustFromMysqlFormat("2014-04-16 18:26:18")))
}
Beispiel #30
0
/*
func TestBytes(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	expect_bytes, _ := hex.DecodeString("d41d8cd98f00b204e9800998ecf8427e")
	t.Equal(Bytes([]byte("")), expect_bytes)
	t.Equal(Bytes([]byte("")), expect_bytes)
	expect_bytes, _ = hex.DecodeString("9e107d9d372bb6826bd81d3542a419d6")
	t.Equal(Bytes([]byte("The quick brown fox jumps over the lazy dog")),
		expect_bytes)

}
*/
func TestMd5Hex(ot *testing.T) {
	t := kmgTest.NewTestTools(ot)
	t.Equal(Md5Hex([]byte("")), "d41d8cd98f00b204e9800998ecf8427e")
	t.Equal(Md5Hex([]byte("The quick brown fox jumps over the lazy dog")),
		"9e107d9d372bb6826bd81d3542a419d6")
}