From 020b050fb1105a374dc532423c8b29dc174a8f6f Mon Sep 17 00:00:00 2001 From: John Date: Fri, 1 Feb 2019 11:42:25 +0800 Subject: [PATCH] fix unit test cases for gstr/gconv/ghttp --- g/net/ghttp/ghttp_unit_3_test.go | 2 +- g/string/gstr/gstr.go | 69 --------------------------- g/string/gstr/gstr_z_unit_test.go | 8 +--- g/test/gtest/gtest.go | 2 +- g/util/gconv/gconv_z_unit_map_test.go | 17 +++++-- 5 files changed, 15 insertions(+), 83 deletions(-) diff --git a/g/net/ghttp/ghttp_unit_3_test.go b/g/net/ghttp/ghttp_unit_3_test.go index 3ad93d7b3..2193f374e 100644 --- a/g/net/ghttp/ghttp_unit_3_test.go +++ b/g/net/ghttp/ghttp_unit_3_test.go @@ -116,7 +116,7 @@ func Test_Params(t *testing.T) { gtest.Assert(client.GetContent("/get", "float64=0.22"), `0.22`) gtest.Assert(client.GetContent("/get", "int=-10000"), `-10000`) gtest.Assert(client.GetContent("/get", "int=10000"), `10000`) - gtest.Assert(client.GetContent("/get", "uint=-10000"), `10000`) + gtest.Assert(client.GetContent("/get", "uint=10000"), `10000`) gtest.Assert(client.GetContent("/get", "uint=9"), `9`) gtest.Assert(client.GetContent("/get", "string=key"), `key`) diff --git a/g/string/gstr/gstr.go b/g/string/gstr/gstr.go index 8a1e9df31..72458ede9 100644 --- a/g/string/gstr/gstr.go +++ b/g/string/gstr/gstr.go @@ -338,75 +338,6 @@ func Str(haystack string, needle string) string { return haystack[idx + len([]byte(needle)) - 1 : ] } -// Translate characters or replace substrings. -// -// If the params length is 1, type is: map[string]string -// Tr("baab", map[string]string{"ab": "01"}) will return "ba01". -// If the params length is 2, type is: string, string -// Tr("baab", "ab", "01") will return "1001", a => 0; b => 1. -// See http://php.net/manual/en/function.strtr.php. -// -// 转换或者替换指定字符。 -func Tr(haystack string, params ...interface{}) string { - ac := len(params) - if ac == 1 { - pairs := params[0].(map[string]string) - length := len(pairs) - if length == 0 { - return haystack - } - oldnew := make([]string, length*2) - for o, n := range pairs { - if o == "" { - return haystack - } - oldnew = append(oldnew, o, n) - } - return strings.NewReplacer(oldnew...).Replace(haystack) - } else if ac == 2 { - from := params[0].(string) - to := params[1].(string) - trlen, lt := len(from), len(to) - if trlen > lt { - trlen = lt - } - if trlen == 0 { - return haystack - } - - str := make([]uint8, len(haystack)) - var xlat [256]uint8 - var i int - var j uint8 - if trlen == 1 { - for i = 0; i < len(haystack); i++ { - if haystack[i] == from[0] { - str[i] = to[0] - } else { - str[i] = haystack[i] - } - } - return string(str) - } - // trlen != 1 - for { - xlat[j] = j - if j++; j == 0 { - break - } - } - for i = 0; i < trlen; i++ { - xlat[from[i]] = to[i] - } - for i = 0; i < len(haystack); i++ { - str[i] = xlat[haystack[i]] - } - return string(str) - } - - return haystack -} - // Randomly shuffles a string. // // 将字符串打乱。 diff --git a/g/string/gstr/gstr_z_unit_test.go b/g/string/gstr/gstr_z_unit_test.go index 194b07f23..fde9bdc42 100644 --- a/g/string/gstr/gstr_z_unit_test.go +++ b/g/string/gstr/gstr_z_unit_test.go @@ -159,7 +159,7 @@ func Test_WordCount(t *testing.T) { func Test_WordWrap(t *testing.T) { gtest.Case(t, func() { gtest.Assert(gstr.WordWrap("12 34", 2, "
"), "12
34") - gtest.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 2, "
"), + gtest.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 7, "
"), "A very
long
woooooooooooooooooord.
and
something") }) } @@ -184,12 +184,6 @@ func Test_Str(t *testing.T) { }) } -func Test_Tr(t *testing.T) { - gtest.Case(t, func() { - gtest.Assert(gstr.Tr("name@example.com", "@"), "@example.com") - }) -} - func Test_Shuffle(t *testing.T) { gtest.Case(t, func() { gtest.Assert(len(gstr.Shuffle("123456")), 6) diff --git a/g/test/gtest/gtest.go b/g/test/gtest/gtest.go index c4260eeba..298a63be1 100644 --- a/g/test/gtest/gtest.go +++ b/g/test/gtest/gtest.go @@ -240,7 +240,7 @@ func compareMap(value, expect interface{}) error { if rvExpect.Len() == rvValue.Len() { ksExpect := rvExpect.MapKeys() for _, key := range ksExpect { - if rvValue.MapIndex(key).Interface() != rvExpect.MapIndex(key).Interface() { + if fmt.Sprintf("%v", rvValue.MapIndex(key).Interface()) != rvExpect.MapIndex(key).Interface() { return fmt.Errorf(`[ASSERT] EXPECT VALUE map["%v"]:%v == %v`, key, rvValue.MapIndex(key).Interface(), diff --git a/g/util/gconv/gconv_z_unit_map_test.go b/g/util/gconv/gconv_z_unit_map_test.go index 530a0294f..6815c4f52 100644 --- a/g/util/gconv/gconv_z_unit_map_test.go +++ b/g/util/gconv/gconv_z_unit_map_test.go @@ -7,8 +7,9 @@ package gconv_test import ( - "gitee.com/johng/gf/g/util/gconv" + "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/test/gtest" + "gitee.com/johng/gf/g/util/gconv" "testing" ) @@ -22,10 +23,16 @@ func Test_Map(t *testing.T) { 3 : "v", } m3 := map[float64]float32{ - 1.22 : 3.1, + 1.22 : 3.1, } - gtest.Assert(gconv.Map(m1), m1) - gtest.Assert(gconv.Map(m2), m2) - gtest.Assert(gconv.Map(m3), m3) + gtest.Assert(gconv.Map(m1), g.Map{ + "k" : "v", + }) + gtest.Assert(gconv.Map(m2), g.Map{ + "3" : "v", + }) + gtest.Assert(gconv.Map(m3), g.Map{ + "1.22" : "3.1", + }) }) }