func TestHas(t *testing.T) { a := assert.New(t) a.True(Has(tag, "name")) a.True(Has(tag, "name2")) a.True(Has(tag, "name3")) a.False(Has(tag, "name100")) }
func TestObj2Map(t *testing.T) { as := assert.New(t) // 普通 obja := &a1{6, "admin"} m, err := Obj2Map(obja, nil) as.Nil(err) as.Equal(m["Id"], 6) as.Equal(m["Name"], "admin") // 包含匿名字段 objb := &b1{a1{6, "admin"}, "password"} m, err = Obj2Map(objb, nil) as.Nil(err) as.Equal(m["Id"], 6) as.Equal(m["Name"], "admin") as.Equal(m["Password"], "password") // 包含子元素 objc := &c1{sub: &a1{6, "admin"}, Sub: &b1{a1{5, "test"}, "b-password"}, Password: "******"} m, err = Obj2Map(objc, nil) as.Nil(err) as.Equal(m["Password"], "password") sub := m["Sub"].(map[string]interface{}) as.Equal(sub["Password"], "b-password") as.Equal(sub["Id"], 5) as.Equal(sub["Name"], "test") // 带转换函数 m, err = Obj2Map(objc, ToUpperFieldConv) as.Nil(err) as.Equal(m["PASSWORD"], "password") sub = m["SUB"].(map[string]interface{}) as.Equal(sub["ID"], 5) }
func TestCompare(t *testing.T) { a := assert.New(t) const ( gt = iota lt eq ) type cmpType struct { v1, v2 string op int } vals := []cmpType{ {"0.1.0", "0.1.0", eq}, {"1...0.0", "1.0.0", eq}, {"1.0-alpha", "1.0-", lt}, {"1.0+build1", "1.0build1.1", lt}, {"1.0.build1.1", "1.0build", gt}, } for k, v := range vals { switch v.op { case gt: a.True(Compare(v.v1, v.v2) > 0, "在%v个元素[%v]出错", k, v) case lt: a.True(Compare(v.v1, v.v2) < 0, "在%v个元素[%v]出错", k, v) case eq: a.Equal(Compare(v.v1, v.v2), 0, "在%v个元素[%v]出错", k, v) } } }
func TestLeftRightUpDown(t *testing.T) { a := assert.New(t) a.Equal(Left(5), "\033[5D") a.Equal(Right(5), "\033[5C") a.Equal(Up(5), "\033[5A") a.Equal(Down(5), "\033[5B") }
func TestEraseMinus(t *testing.T) { a := assert.New(t) a.Equal(eraseMinus([]byte("abc-def-abc-")), []byte("abcdefabc")) a.Equal(eraseMinus([]byte("abc-def-abc")), []byte("abcdefabc")) a.Equal(eraseMinus([]byte("-_abc-def-abc-")), []byte("_abcdefabc")) a.Equal(eraseMinus([]byte("-abc-d_ef-abc-")), []byte("abcd_efabc")) }
func TestIsISBN(t *testing.T) { a := assert.New(t) a.True(IsISBN("1-919876-03-0")) a.True(IsISBN("0-471-00084-1")) a.True(IsISBN("978-7-301-04815-3")) a.True(IsISBN("978-986-181-728-6")) }
func TestEraseLine(t *testing.T) { a := assert.New(t) a.Panic(func() { EraseLine(-1) }) a.Panic(func() { EraseLine(3) }) a.Equal(EraseLine(0), "\033[0K") a.Equal(EraseLine(2), "\033[2K") a.Equal(EraseLine(1), "\033[1K") }
//////////////////////////////// MustBytes func TestMustBytes(t *testing.T) { a := assert.New(t) // 可解析 a.Equal(MustBytes("123", []byte("456")), []byte{49, 50, 51}) a.Equal(MustBytes(123, []byte("456")), []byte{49, 50, 51}) a.Equal(MustBytes(uint(123), []byte("456")), []byte{49, 50, 51}) // 不可解析 a.Equal(MustBytes([]int{1}, []byte("123")), []byte{49, 50, 51}) }
///////////////////////////////// MustString func TestMustString(t *testing.T) { a := assert.New(t) // 可解析 a.Equal(MustString(123, "222"), "123") a.Equal(MustString(-11, "22"), "-11") a.Equal(MustString(-11.111, "22"), "-11.111") a.Equal(MustString(true, "22"), "true") // 不可解析 a.Equal(MustString([]int{1}, "22"), "22") }
func TestIsIP4(t *testing.T) { a := assert.New(t) a.True(IsIP4("0.0.0.0")) a.True(IsIP4("255.255.255.255")) a.True(IsIP4("255.0.3.255")) a.True(IsIP4("127.010.0.1")) a.True(IsIP4("027.01.0.1")) a.False(IsIP4("1127.01.0.1")) }
/////////////////////////// MustMap func TestMustMap(t *testing.T) { def := map[string]interface{}{"1": 1, "str": "str"} v1 := map[string]interface{}{"1": 2, "str": "str"} a := assert.New(t) // 可解析 a.Equal(MustMap(v1, def), v1) // 不可解析 a.Equal(MustMap(123, def), def) a.Equal(MustMap("adb", def), def) }
func TestIsIP(t *testing.T) { a := assert.New(t) a.True(IsIP("fe80:0000:0000:0000:0204:61ff:fe9d:f156")) a.True(IsIP("fe80:0:0:0:204:61ff:fe9d:f156")) a.True(IsIP("0.0.0.0")) a.True(IsIP("255.255.255.255")) a.True(IsIP("255.0.3.255")) a.False(IsIP("255.0:3.255")) a.False(IsIP("275.0.3.255")) }
func TestMap2Obj(t *testing.T) { as := assert.New(t) // 一般 m := map[string]interface{}{ "Id": 5, "Name": "admin", } // 包含匿名元素 obja := &a1{} err := Map2Obj(m, obja, nil) as.Nil(err) as.Equal(obja.Id, 5) as.Equal(obja.Name, "admin") m = map[string]interface{}{ "Id": 5, "Name": "admin", "Password": "******", "lower": "lower", } objb := &b1{} err = Map2Obj(m, objb, nil) as.Nil(err) as.Equal(objb.Id, 5) as.Equal(objb.Name, "admin") as.Equal(objb.Password, "password") // 包含子元素 objc := &c1{Sub: &b1{}} m = map[string]interface{}{ "Password": "******", "Sub": map[string]interface{}{ "Id": 6, "Name": "test", "Password": "******", }, } err = Map2Obj(m, objc, nil) as.Nil(err) as.Equal(objc.Password, "password") as.Equal(objc.Sub.Id, 6) as.Equal(objc.Sub.Password, "sub-password") // 带转换函数 objC := &C{SUB: &b1{}} err = Map2Obj(m, objC, ToUpperFieldConv) as.Nil(err) as.Equal(objC.PASSWORD, "password") as.NotNil(objC.SUB) }
func TestIsIP6(t *testing.T) { a := assert.New(t) a.True(IsIP6("fe80:0000:0000:0000:0204:61ff:fe9d:f156")) // full form of IPv6 a.True(IsIP6("fe80:0:0:0:204:61ff:fe9d:f156")) // drop leading zeroes a.True(IsIP6("fe80::204:61ff:fe9d:f156")) // collapse multiple zeroes to :: in the IPv6 address a.True(IsIP6("fe80:0000:0000:0000:0204:61ff:254.157.241.86")) // IPv4 dotted quad at the end a.True(IsIP6("fe80:0:0:0:0204:61ff:254.157.241.86")) // drop leading zeroes, IPv4 dotted quad at the end a.True(IsIP6("fe80::204:61ff:254.157.241.86")) // dotted quad at the end, multiple zeroes collapsed a.True(IsIP6("::1")) // localhost a.True(IsIP6("fe80::")) // link-local prefix a.True(IsIP6("2001::")) //global unicast prefix }
func TestNew(t *testing.T) { err := errors.New(5, nil, "abc") a := assert.New(t) a.Equal(err.GetCode(), 5, "err.GetCode的值不等于5") a.Nil(err.GetPrevious()) a.Equal(err.Error(), "abc") err2 := errors.New(5, err, "abc") a.Equal(err2.GetCode(), 5) a.Equal(err2.GetPrevious(), err) a.Equal(err2.Error(), "abc") }
func TestGet(t *testing.T) { a := assert.New(t) fn := func(tag, name string, wont []string) { val, found := Get(tag, name) a.True(found) a.Equal(val, wont) } fn(tag, "name", []string{"abc"}) fn(tag, "name2", nil) fn(tag, "name3", []string{"n1", "n2"}) }
func TestIsCnPhone(t *testing.T) { a := assert.New(t) a.True(IsCnPhone("444488888888-4444")) a.True(IsCnPhone("3337777777-1")) a.True(IsCnPhone("333-7777777-1")) a.True(IsCnPhone("7777777")) a.True(IsCnPhone("88888888")) a.False(IsCnPhone("333-7777777-")) // 尾部没有分机号 a.False(IsCnPhone("333-999999999")) // 9位数的号码 a.False(IsCnPhone("22-88888888")) // 区号只有2位 a.False(IsCnPhone("33-88888888-55555")) // 分机号超过4位 }
func TestIsCnMobile(t *testing.T) { a := assert.New(t) a.True(IsCnMobile("15011111111")) a.True(IsCnMobile("015011111111")) a.True(IsCnMobile("8615011111111")) a.True(IsCnMobile("+8615011111111")) a.False(IsCnMobile("+86150111111112")) // 尾部多个2 a.False(IsCnMobile("50111111112")) // 开头少1 a.False(IsCnMobile("+8650111111112")) // 开头少1 a.False(IsCnMobile("8650111111112")) // 开头少1 a.False(IsCnMobile("154111111112")) // 不存在的前缀150 }
func TestReader(t *testing.T) { a := assert.New(t) str := ` [section1] key = val ;comment1 ### comment2 key2=val2 ` sectionVals := map[string]string{ "section1": "", } commentVals := map[string]string{ "## comment2\n": "", "comment1 \n": "", } elementVals := map[string]string{ "key": "val", "key2": "val2", } r := NewReaderString(str) a.NotNil(r) LOOP: for { token, err := r.Token() a.NotError(err) switch token.Type { case EOF: break LOOP case Comment: val := token.Value _, found := commentVals[val] a.True(found, "实际值为:[%v]", val) case Element: k, v := token.Key, token.Value vv, found := elementVals[k] a.True(found) a.Equal(vv, v) case Section: val := token.Value _, found := sectionVals[val] a.True(found) default: t.Error("未知的类型") break LOOP } } }
func TestBColor256(t *testing.T) { a := assert.New(t) // panic a.Panic(func() { BColor256(256) }) a.Panic(func() { BColor256(-1) }) // 临界点 a.Equal(BColor256(0), "\033[48;5;0m") a.Equal(BColor256(255), "\033[48;5;255m") // 正常测试 a.Equal(BColor256(211), "\033[48;5;211m") }
func TestIsEmail(t *testing.T) { a := assert.New(t) a.True(IsEmail("*****@*****.**")) a.True(IsEmail("*****@*****.**")) a.True(IsEmail("*****@*****.**")) a.True(IsEmail("*****@*****.**")) a.True(IsEmail("em2il@email")) // 2个@ a.False(IsEmail("em@[email protected]")) // 没有@ a.False(IsEmail("email2email.com.cn")) }
////////////////////////////////// MustSlice func TestMustSlice(t *testing.T) { def := []interface{}{4, 5, 5} a := assert.New(t) // 可解析 a.Equal(MustSlice([]int{1, 2, 3}, def), []interface{}{int(1), int(2), int(3)}) a.Equal(MustSlice([]uint64{1, 2, 3}, def), []interface{}{uint64(1), uint64(2), uint64(3)}) a.Equal(MustSlice([]interface{}{"1", 2, 3.0}, def), []interface{}{"1", 2, 3.0}) a.Equal(MustSlice([]string{"1", "2", "3"}, def), []interface{}{"1", "2", "3"}) a.Equal(MustSlice([]byte("123"), def), []interface{}{byte(49), byte(50), byte(51)}) a.Equal(MustSlice("123", def), []interface{}{rune(49), rune(50), rune(51)}) // 不可解析 a.Equal(MustSlice(7, def), []interface{}{4, 5, 5}) }
func TestMustGet(t *testing.T) { a := assert.New(t) fn := func(tag, name string, def []string, wont []string) { val := MustGet(tag, name, def...) a.Equal(val, wont) } // name1不存在,测试默认值 fn(tag, "name1", []string{"def"}, []string{"def"}) // abc不存在,测试默认值 fn(tag, "abc", []string{"defg", "abc"}, []string{"defg", "abc"}) // name3存在,测试返回值 fn(tag, "name3", []string{"n3", "n4"}, []string{"n1", "n2"}) }
func TestParse(t *testing.T) { a := assert.New(t) m := Parse(tag) a.Equal(3, len(m)) a.Equal(m["name"][0], "abc") a.Equal(len(m["name"]), 1) a.Empty(m["name2"]) a.Equal(len(m["name3"]), 2) a.Equal(m["name3"][0], "n1") a.Equal(m["name3"][1], "n2") }
func TestIsNumber(t *testing.T) { a := assert.New(t) a.True(IsNumber("123")) a.True(IsNumber("+123")) a.True(IsNumber("-123")) a.True(IsNumber(".123")) a.True(IsNumber("12.3")) a.True(IsNumber("+12.3")) a.True(IsNumber("-123.")) a.False(IsNumber("1-23")) a.False(IsNumber("+abc")) a.False(IsNumber(".12.3")) a.False(IsNumber("123")) // 全角 }
///////////////////////// MustFloat32 func TestMustFloat32(t *testing.T) { a := assert.New(t) // 可解析 a.Equal(MustFloat32("123", 456), float32(123)) a.Equal(MustFloat32(true, 5), float32(1)) a.Equal(MustFloat32(false, 5), float32(0)) a.Equal(MustFloat32(123, 456), float32(123)) a.Equal(MustFloat32(int64(-123), 99), float32(-123.0)) a.Equal(MustFloat32(uint(8), 9), float32(8)) a.Equal(MustFloat32(uint64(33), 99), float32(33)) a.Equal(MustFloat32([]byte("123"), 44), float32(123)) // 不可解析 a.Equal(MustFloat32(";sdf", 45), 45) a.Equal(MustFloat32("str", 45), 45) }
func TestLogWriterInitializer(t *testing.T) { a := assert.New(t) args := map[string]string{ "prefix": "[INFO]", "flag": "log.lstdflags", "misc": "misc", } w, err := logWriterInitializer(LevelInfo, args) a.NotError(err).NotNil(w) lw, ok := w.(*logWriter) a.True(ok).NotNil(lw) a.Equal(lw.prefix, "[INFO]"). Equal(lw.flag, log.LstdFlags) }
func TestIsGb11643(t *testing.T) { a := assert.New(t) // 网上扒来的身份证,不与现实中的对应。 a.True(IsGb11643("350303199002033073")) a.True(IsGb11643("350303900203307")) a.True(IsGb11643("331122197905116239")) a.True(IsGb11643("513330199111066159")) a.True(IsGb11643("33050219880702447x")) a.True(IsGb11643("33050219880702447X")) a.True(IsGb11643("330502880702447")) a.False(IsGb11643("111111111111111111")) a.False(IsGb11643("330502198807024471")) // 最后一位不正确 a.False(IsGb11643("33050288070244")) // 14位 a.False(IsGb11643("3305028807024411")) // 16位 }
func TestHostDomains(t *testing.T) { a := assert.New(t) var domains map[string]string var ok bool defHandler := func(w http.ResponseWriter, r *http.Request) bool { tmp, found := GetContext(r).Get("domains") a.True(found) domains, ok = tmp.(map[string]string) a.True(ok) return true } defFunc := MatcherFunc(defHandler) // host: http.Request.Host的值 fn := func(host string, hh *Host, wont map[string]string) { r, err := http.NewRequest("GET", "", nil) a.NotError(err) r.Host = host a.Equal(r.Host, host) a.True(hh.ServeHTTP2(nil, r), "域名[%v]无法正确匹配", host) a.Equal(domains, wont) } // 没有命名参数 domains = map[string]string{} h := NewHost(defFunc, "\\w+.example.com") fn("www.example.com", h, map[string]string{}) // 单个命名参数 domains = map[string]string{} h = NewHost(defFunc, "(?P<city>\\w+).example.com") fn("bj.example.com", h, map[string]string{"city": "bj"}) // 单个全名参数 domains = map[string]string{} h = NewHost(defFunc, "(?P<city>[a-z]*)\\.(?P<prov>[a-z]*).example.com") fn("hz.zj.example.com", h, map[string]string{"city": "hz", "prov": "zj"}) }
////////////////////////////////// MustBool func TestMustBool(t *testing.T) { a := assert.New(t) // 可解析 a.True(MustBool("on", false)) a.True(MustBool(true, false)) a.True(MustBool("123", false)) a.True(MustBool(123, false)) a.True(MustBool(-123, false)) a.True(MustBool(-1.23, false)) a.False(MustBool("off", true)) a.False(MustBool(false, true)) // 不可解析 a.False(MustBool("str", false)) a.True(MustBool(";adf", true)) }