fix unit test cases for gstr/gconv/ghttp

This commit is contained in:
John
2019-02-01 11:42:25 +08:00
parent 08d71cede3
commit 020b050fb1
5 changed files with 15 additions and 83 deletions

View File

@ -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`)

View File

@ -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.
//
// 将字符串打乱。

View File

@ -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, "<br>"), "12<br>34")
gtest.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 2, "<br>"),
gtest.Assert(gstr.WordWrap("A very long woooooooooooooooooord. and something", 7, "<br>"),
"A very<br>long<br>woooooooooooooooooord.<br>and<br>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)

View File

@ -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(),

View File

@ -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",
})
})
}