mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
Merge branch 'master' of https://github.com/gogf/gf into gogf-master
This commit is contained in:
@ -13,6 +13,7 @@
|
||||
|[zfan_codes](https://gitee.com/zfan_codes)|gitee|¥10.00
|
||||
|[arden](https://github.com/arden)|alipay|¥10.00
|
||||
|潘兄|wechat|¥100.00
|
||||
|Fly的狐狸|wechat|¥100.00
|
||||
|土豆相公|alipay|¥66.60
|
||||
|上海金保证网络科技|bank|¥2000.00
|
||||
|
||||
|
||||
@ -286,11 +286,17 @@ func (a *SortedIntArray) Contains(value int) bool {
|
||||
// Search searches array by <value>, returns the index of <value>,
|
||||
// or returns -1 if not exists.
|
||||
func (a *SortedIntArray) Search(value int) (index int) {
|
||||
index, _ = a.binSearch(value, true)
|
||||
return
|
||||
if i, r := a.binSearch(value, true); r == 0 {
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// Binary search.
|
||||
// It returns the last compared index and the result.
|
||||
// If <result> equals to 0, it means the value at <index> is equals to <value>.
|
||||
// If <result> lesser than 0, it means the value at <index> is lesser than <value>.
|
||||
// If <result> greater than 0, it means the value at <index> is greater than <value>.
|
||||
func (a *SortedIntArray) binSearch(value int, lock bool) (index int, result int) {
|
||||
if len(a.array) == 0 {
|
||||
return -1, -2
|
||||
|
||||
@ -287,11 +287,17 @@ func (a *SortedArray) Contains(value interface{}) bool {
|
||||
// Search searches array by <value>, returns the index of <value>,
|
||||
// or returns -1 if not exists.
|
||||
func (a *SortedArray) Search(value interface{}) (index int) {
|
||||
index, _ = a.binSearch(value, true)
|
||||
return
|
||||
if i, r := a.binSearch(value, true); r == 0 {
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// Binary search.
|
||||
// It returns the last compared index and the result.
|
||||
// If <result> equals to 0, it means the value at <index> is equals to <value>.
|
||||
// If <result> lesser than 0, it means the value at <index> is lesser than <value>.
|
||||
// If <result> greater than 0, it means the value at <index> is greater than <value>.
|
||||
func (a *SortedArray) binSearch(value interface{}, lock bool)(index int, result int) {
|
||||
if len(a.array) == 0 {
|
||||
return -1, -2
|
||||
|
||||
@ -281,11 +281,17 @@ func (a *SortedStringArray) Contains(value string) bool {
|
||||
// Search searches array by <value>, returns the index of <value>,
|
||||
// or returns -1 if not exists.
|
||||
func (a *SortedStringArray) Search(value string) (index int) {
|
||||
index, _ = a.binSearch(value, true)
|
||||
return
|
||||
if i, r := a.binSearch(value, true); r == 0 {
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// Binary search.
|
||||
// It returns the last compared index and the result.
|
||||
// If <result> equals to 0, it means the value at <index> is equals to <value>.
|
||||
// If <result> lesser than 0, it means the value at <index> is lesser than <value>.
|
||||
// If <result> greater than 0, it means the value at <index> is greater than <value>.
|
||||
func (a *SortedStringArray) binSearch(value string, lock bool) (index int, result int) {
|
||||
if len(a.array) == 0 {
|
||||
return -1, -2
|
||||
|
||||
@ -11,12 +11,32 @@ package gset_test
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/container/gset"
|
||||
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"strings"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSet_New(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s := gset.New()
|
||||
s.Add(1).Add(1).Add(2)
|
||||
s.Add([]interface{}{3, 4}...)
|
||||
gtest.Assert(s.Size(), 4)
|
||||
gtest.AssertIN(1, s.Slice())
|
||||
gtest.AssertIN(2, s.Slice())
|
||||
gtest.AssertIN(3, s.Slice())
|
||||
gtest.AssertIN(4, s.Slice())
|
||||
gtest.AssertNI(0, s.Slice())
|
||||
gtest.Assert(s.Contains(4), true)
|
||||
gtest.Assert(s.Contains(5), false)
|
||||
s.Remove(1)
|
||||
gtest.Assert(s.Size(), 3)
|
||||
s.Clear()
|
||||
gtest.Assert(s.Size(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSet_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s := gset.NewSet()
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
package gaes_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/encoding/gbase64"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/g/crypto/gaes"
|
||||
@ -16,52 +17,111 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
content = []byte("pibigstar")
|
||||
content = []byte("pibigstar")
|
||||
content_16, _ = gbase64.Decode("v1jqsGHId/H8onlVHR8Vaw==")
|
||||
content_24, _ = gbase64.Decode("0TXOaj5KMoLhNWmJ3lxY1A==")
|
||||
content_32, _ = gbase64.Decode("qM/Waw1kkWhrwzek24rCSA==")
|
||||
content_16_iv, _ = gbase64.Decode("DqQUXiHgW/XFb6Qs98+hrA==")
|
||||
content_32_iv, _ = gbase64.Decode("ZuLgAOii+lrD5KJoQ7yQ8Q==")
|
||||
// iv 长度必须等于blockSize,只能为16
|
||||
iv = []byte("Hello My GoFrame")
|
||||
key_16 = []byte("1234567891234567")
|
||||
key_24 = []byte("123456789123456789123456")
|
||||
key_32 = []byte("12345678912345678912345678912345")
|
||||
keys = []byte("12345678912345678912345678912346")
|
||||
iv = []byte("Hello My GoFrame")
|
||||
key_16 = []byte("1234567891234567")
|
||||
key_17 = []byte("12345678912345670")
|
||||
key_24 = []byte("123456789123456789123456")
|
||||
key_32 = []byte("12345678912345678912345678912345")
|
||||
keys = []byte("12345678912345678912345678912346")
|
||||
key_err = []byte("1234")
|
||||
key_32_err = []byte("1234567891234567891234567891234 ")
|
||||
)
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
_, err := gaes.Encrypt(content, key_16)
|
||||
data, err := gaes.Encrypt(content, key_16)
|
||||
gtest.Assert(err, nil)
|
||||
_, err = gaes.Encrypt(content, key_24)
|
||||
gtest.Assert(data, []byte(content_16))
|
||||
data, err = gaes.Encrypt(content, key_24)
|
||||
gtest.Assert(err, nil)
|
||||
_, err = gaes.Encrypt(content, key_32)
|
||||
gtest.Assert(data, []byte(content_24))
|
||||
data, err = gaes.Encrypt(content, key_32)
|
||||
gtest.Assert(err, nil)
|
||||
_, err = gaes.Encrypt(content, key_16, iv)
|
||||
gtest.Assert(data, []byte(content_32))
|
||||
data, err = gaes.Encrypt(content, key_16, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_16_iv))
|
||||
data, err = gaes.Encrypt(content, key_32, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(data, []byte(content_32_iv))
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
encrypt, err := gaes.Encrypt(content, key_16)
|
||||
decrypt, err := gaes.Decrypt(encrypt, key_16)
|
||||
decrypt, err := gaes.Decrypt([]byte(content_16), key_16)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(decrypt), string(content))
|
||||
gtest.Assert(decrypt, content)
|
||||
|
||||
encrypt, err = gaes.Encrypt(content, key_24)
|
||||
decrypt, err = gaes.Decrypt(encrypt, key_24)
|
||||
decrypt, err = gaes.Decrypt([]byte(content_24), key_24)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(decrypt), string(content))
|
||||
gtest.Assert(decrypt, content)
|
||||
|
||||
encrypt, err = gaes.Encrypt(content, key_32)
|
||||
decrypt, err = gaes.Decrypt(encrypt, key_32)
|
||||
decrypt, err = gaes.Decrypt([]byte(content_32), key_32)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(decrypt), string(content))
|
||||
gtest.Assert(decrypt, content)
|
||||
|
||||
encrypt, err = gaes.Encrypt(content, key_32, iv)
|
||||
decrypt, err = gaes.Decrypt(encrypt, key_32, iv)
|
||||
decrypt, err = gaes.Decrypt([]byte(content_16_iv), key_16, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(decrypt), string(content))
|
||||
gtest.Assert(decrypt, content)
|
||||
|
||||
encrypt, err = gaes.Encrypt(content, key_32, iv)
|
||||
decrypt, err = gaes.Decrypt(encrypt, keys, iv)
|
||||
decrypt, err = gaes.Decrypt([]byte(content_32_iv), key_32, iv)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(decrypt, content)
|
||||
|
||||
decrypt, err = gaes.Decrypt([]byte(content_32_iv), keys, iv)
|
||||
gtest.Assert(err, "invalid padding")
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncryptErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
// encrypt key error
|
||||
_, err := gaes.Encrypt(content, key_err)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecryptErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
// decrypt key error
|
||||
encrypt, err := gaes.Encrypt(content, key_16)
|
||||
_, err = gaes.Decrypt(encrypt, key_err)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
// decrypt content too short error
|
||||
_, err = gaes.Decrypt([]byte("test"), key_16)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
// decrypt content size error
|
||||
_, err = gaes.Decrypt(key_17, key_16)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPKCS5UnPaddingErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
// PKCS5UnPadding blockSize zero
|
||||
_, err := gaes.PKCS5UnPadding(content, 0)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
// PKCS5UnPadding src len zero
|
||||
_, err = gaes.PKCS5UnPadding([]byte(""), 16)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
// PKCS5UnPadding src len > blockSize
|
||||
_, err = gaes.PKCS5UnPadding(key_17, 16)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
// PKCS5UnPadding src len > blockSize
|
||||
_, err = gaes.PKCS5UnPadding(key_32_err, 32)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -9,13 +9,14 @@
|
||||
package gcrc32_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/crypto/gmd5"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/g/crypto/gcrc32"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
)
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
func TestEncryptString(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s := "pibigstar"
|
||||
result := 693191136
|
||||
@ -25,3 +26,19 @@ func TestEncrypt(t *testing.T) {
|
||||
gtest.AssertEQ(int(encrypt2), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s := "pibigstar"
|
||||
result := 693191136
|
||||
encrypt1 := gcrc32.Encrypt(s)
|
||||
encrypt2 := gcrc32.Encrypt([]byte(s))
|
||||
gtest.AssertEQ(int(encrypt1), result)
|
||||
gtest.AssertEQ(int(encrypt2), result)
|
||||
|
||||
strmd5 := gmd5.Encrypt(s)
|
||||
test1 := gcrc32.Encrypt(strmd5)
|
||||
test2 := gcrc32.Encrypt([]byte(strmd5))
|
||||
gtest.AssertEQ(test2, test1)
|
||||
})
|
||||
}
|
||||
|
||||
@ -9,57 +9,265 @@
|
||||
package gcache_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gcache"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/container/gset"
|
||||
"github.com/gogf/gf/g/os/gcache"
|
||||
"github.com/gogf/gf/g/os/grpool"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
//clear 用于清除全局缓存,因gcache api 暂未暴露 Clear 方法
|
||||
//暂定所有测试用例key的集合为1,2,3,避免不同测试用例间因全局cache共享带来的问题,每个测试用例在测试gcache.XXX之前,先调用clear()
|
||||
func clear() {
|
||||
gcache.Removes(g.Slice{1, 2, 3})
|
||||
}
|
||||
|
||||
func TestCache_Set(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Set(1, 11, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Set(1, 11, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
gtest.Assert(cache.Contains(1), true)
|
||||
|
||||
clear()
|
||||
gcache.Set(1, 11, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
gtest.Assert(gcache.Contains(1), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_Set_Expire(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Set(2, 22, 100)
|
||||
gtest.Assert(cache.Get(2), 22)
|
||||
time.Sleep(200*time.Millisecond)
|
||||
gtest.Assert(cache.Get(2), nil)
|
||||
time.Sleep(3*time.Second)
|
||||
gtest.Assert(cache.Size(), 0)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Set(2, 22, 100)
|
||||
gtest.Assert(cache.Get(2), 22)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(cache.Get(2), nil)
|
||||
time.Sleep(3 * time.Second)
|
||||
gtest.Assert(cache.Size(), 0)
|
||||
cache.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_Keys_Values(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
for i := 0; i < 10; i++ {
|
||||
cache.Set(i, i*10, 0)
|
||||
}
|
||||
gtest.Assert(len(cache.Keys()), 10)
|
||||
gtest.Assert(len(cache.Values()), 10)
|
||||
gtest.AssertIN(0, cache.Keys())
|
||||
gtest.AssertIN(90, cache.Values())
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
for i := 0; i < 10; i++ {
|
||||
cache.Set(i, i*10, 0)
|
||||
}
|
||||
gtest.Assert(len(cache.Keys()), 10)
|
||||
gtest.Assert(len(cache.Values()), 10)
|
||||
gtest.AssertIN(0, cache.Keys())
|
||||
gtest.AssertIN(90, cache.Values())
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_LRU(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New(2)
|
||||
for i := 0; i < 10; i++ {
|
||||
cache.Set(i, i, 0)
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New(2)
|
||||
for i := 0; i < 10; i++ {
|
||||
cache.Set(i, i, 0)
|
||||
}
|
||||
gtest.Assert(cache.Size(), 10)
|
||||
gtest.Assert(cache.Get(6), 6)
|
||||
time.Sleep(4 * time.Second)
|
||||
gtest.Assert(cache.Size(), 2)
|
||||
gtest.Assert(cache.Get(6), 6)
|
||||
gtest.Assert(cache.Get(1), nil)
|
||||
cache.Close()
|
||||
})
|
||||
}
|
||||
|
||||
gtest.Assert(cache.Size(), 10)
|
||||
gtest.Assert(cache.Get(6), 6)
|
||||
time.Sleep(4*time.Second)
|
||||
gtest.Assert(cache.Size(), 2)
|
||||
gtest.Assert(cache.Get(6), 6)
|
||||
gtest.Assert(cache.Get(1), nil)
|
||||
})
|
||||
}
|
||||
func TestCache_LRU_expire(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New(2)
|
||||
cache.Set(1, nil, 1000)
|
||||
gtest.Assert(cache.Size(), 1)
|
||||
gtest.Assert(cache.Get(1), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_SetIfNotExist(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.SetIfNotExist(1, 11, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
cache.SetIfNotExist(1, 22, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
cache.SetIfNotExist(2, 22, 0)
|
||||
gtest.Assert(cache.Get(2), 22)
|
||||
|
||||
clear()
|
||||
gcache.SetIfNotExist(1, 11, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
gcache.SetIfNotExist(1, 22, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_Sets(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
|
||||
clear()
|
||||
gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_GetOrSet(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.GetOrSet(1, 11, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
cache.GetOrSet(1, 111, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
|
||||
clear()
|
||||
gcache.GetOrSet(1, 11, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
gcache.GetOrSet(1, 111, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_GetOrSetFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.GetOrSetFunc(1, func() interface{} {
|
||||
return 11
|
||||
}, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
cache.GetOrSetFunc(1, func() interface{} {
|
||||
return 111
|
||||
}, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
|
||||
clear()
|
||||
gcache.GetOrSetFunc(1, func() interface{} {
|
||||
return 11
|
||||
}, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
gcache.GetOrSetFunc(1, func() interface{} {
|
||||
return 111
|
||||
}, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_GetOrSetFuncLock(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.GetOrSetFuncLock(1, func() interface{} {
|
||||
return 11
|
||||
}, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
cache.GetOrSetFuncLock(1, func() interface{} {
|
||||
return 111
|
||||
}, 0)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
|
||||
clear()
|
||||
gcache.GetOrSetFuncLock(1, func() interface{} {
|
||||
return 11
|
||||
}, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
gcache.GetOrSetFuncLock(1, func() interface{} {
|
||||
return 111
|
||||
}, 0)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_Clear(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
|
||||
cache.Clear()
|
||||
gtest.Assert(cache.Size(), 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_SetConcurrency(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
cache := gcache.New()
|
||||
pool := grpool.New(4)
|
||||
go func() {
|
||||
for {
|
||||
pool.Add(func() {
|
||||
cache.SetIfNotExist(1, 11, 10)
|
||||
})
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Log("first part end")
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
pool.Add(func() {
|
||||
cache.SetIfNotExist(1, nil, 10)
|
||||
})
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Log("second part end")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCache_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
{
|
||||
cache := gcache.New()
|
||||
cache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
|
||||
gtest.Assert(cache.Contains(1), true)
|
||||
gtest.Assert(cache.Get(1), 11)
|
||||
data := cache.Data()
|
||||
gtest.Assert(data[1], 11)
|
||||
gtest.Assert(data[2], 22)
|
||||
gtest.Assert(data[3], nil)
|
||||
gtest.Assert(cache.Size(), 2)
|
||||
keys := cache.Keys()
|
||||
gtest.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true)
|
||||
keyStrs := cache.KeyStrings()
|
||||
gtest.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true)
|
||||
values := cache.Values()
|
||||
gtest.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true)
|
||||
removeData1 := cache.Remove(1)
|
||||
gtest.Assert(removeData1, 11)
|
||||
gtest.Assert(cache.Size(), 1)
|
||||
cache.Removes(g.Slice{2})
|
||||
gtest.Assert(cache.Size(), 0)
|
||||
}
|
||||
|
||||
clear()
|
||||
{
|
||||
gcache.Sets(g.MapAnyAny{1: 11, 2: 22}, 0)
|
||||
gtest.Assert(gcache.Contains(1), true)
|
||||
gtest.Assert(gcache.Get(1), 11)
|
||||
data := gcache.Data()
|
||||
gtest.Assert(data[1], 11)
|
||||
gtest.Assert(data[2], 22)
|
||||
gtest.Assert(data[3], nil)
|
||||
gtest.Assert(gcache.Size(), 2)
|
||||
keys := gcache.Keys()
|
||||
gtest.Assert(gset.NewFrom(g.Slice{1, 2}).Equal(gset.NewFrom(keys)), true)
|
||||
keyStrs := gcache.KeyStrings()
|
||||
gtest.Assert(gset.NewFrom(g.Slice{"1", "2"}).Equal(gset.NewFrom(keyStrs)), true)
|
||||
values := gcache.Values()
|
||||
gtest.Assert(gset.NewFrom(g.Slice{11, 22}).Equal(gset.NewFrom(values)), true)
|
||||
removeData1 := gcache.Remove(1)
|
||||
gtest.Assert(removeData1, 11)
|
||||
gtest.Assert(gcache.Size(), 1)
|
||||
gcache.Removes(g.Slice{2})
|
||||
gtest.Assert(gcache.Size(), 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
43
g/os/genv/genv_test.go
Normal file
43
g/os/genv/genv_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
package genv_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/genv"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Genv_All(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(os.Environ(), genv.All())
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Genv_Get(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "TEST_GET_ENV"
|
||||
err := os.Setenv(key, "TEST")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertEQ(genv.Get(key), "TEST")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Genv_Set(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "TEST_SET_ENV"
|
||||
err := genv.Set(key, "TEST")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertEQ(os.Getenv(key), "TEST")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Genv_Remove(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
key := "TEST_REMOVE_ENV"
|
||||
err := os.Setenv(key, "TEST")
|
||||
gtest.Assert(err, nil)
|
||||
err = genv.Remove(key)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertEQ(os.Getenv(key), "")
|
||||
})
|
||||
}
|
||||
@ -425,8 +425,7 @@ func MainPkgPath() string {
|
||||
continue
|
||||
}
|
||||
// separator of <file> '/' will be converted to Separator.
|
||||
path = Dir(file)
|
||||
for path[len(path) - 1] != os.PathSeparator {
|
||||
for path = Dir(file); len(path) > 1 && Exists(path) && path[len(path) - 1] != os.PathSeparator; {
|
||||
files, _ := ScanDir(path, "*.go")
|
||||
for _, v := range files {
|
||||
if gregex.IsMatchString(`package\s+main`, GetContents(v)) {
|
||||
|
||||
101
g/os/gspath/gspath_unit_test.go
Normal file
101
g/os/gspath/gspath_unit_test.go
Normal file
@ -0,0 +1,101 @@
|
||||
package gspath_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gspath"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSPath_Api(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
pwd := gfile.Pwd()
|
||||
root := pwd + gfile.Separator
|
||||
gfile.Create(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
defer gfile.Remove(root + "gf_tmp")
|
||||
fp, isDir := gspath.Search(root, "gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gspath.Search(root, "gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
|
||||
fp, isDir = gspath.SearchWithCache(root, "gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gspath.SearchWithCache(root, "gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSPath_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
pwd := gfile.Pwd()
|
||||
root := pwd + gfile.Separator
|
||||
gfile.Create(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
defer gfile.Remove(root + "gf_tmp")
|
||||
gsp := gspath.New(root, false)
|
||||
realPath, err := gsp.Add(root + "gf_tmp")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(realPath, root+"gf_tmp")
|
||||
realPath, err = gsp.Add("gf_tmp1")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
realPath, err = gsp.Add(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
gsp.Remove("gf_tmp1")
|
||||
gtest.Assert(gsp.Size(), 2)
|
||||
gtest.Assert(len(gsp.Paths()), 2)
|
||||
gtest.Assert(len(gsp.AllPaths()), 0)
|
||||
realPath, err = gsp.Set(root + "gf_tmp1")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
realPath, err = gsp.Set(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
gsp.Set(root)
|
||||
fp, isDir := gsp.Search("gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gsp.Search("gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
fp, isDir = gsp.Search("/", "gf.txt")
|
||||
gtest.Assert(fp, root+gfile.Separator)
|
||||
gtest.Assert(isDir, true)
|
||||
|
||||
gsp = gspath.New(root, true)
|
||||
realPath, err = gsp.Add(root + "gf_tmp")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(realPath, root+"gf_tmp")
|
||||
|
||||
gfile.Mkdir(root + "gf_tmp1")
|
||||
gfile.Rename(root+"gf_tmp1", root+"gf_tmp2")
|
||||
gfile.Rename(root+"gf_tmp2", root+"gf_tmp1")
|
||||
defer gfile.Remove(root + "gf_tmp1")
|
||||
realPath, err = gsp.Add("gf_tmp1")
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(realPath, root+"gf_tmp1")
|
||||
realPath, err = gsp.Add("gf_tmp3")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
gsp.Remove(root + "gf_tmp")
|
||||
gsp.Remove(root + "gf_tmp1")
|
||||
gsp.Remove(root + "gf_tmp3")
|
||||
gtest.Assert(gsp.Size(), 3)
|
||||
gtest.Assert(len(gsp.Paths()), 3)
|
||||
gsp.AllPaths()
|
||||
gsp.Set(root)
|
||||
fp, isDir = gsp.Search("gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gsp.Search("gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
fp, isDir = gsp.Search("/", "gf.txt")
|
||||
gtest.Assert(fp, pwd)
|
||||
gtest.Assert(isDir, true)
|
||||
})
|
||||
}
|
||||
236
g/os/gview/gview_unit_test.go
Normal file
236
g/os/gview/gview_unit_test.go
Normal file
@ -0,0 +1,236 @@
|
||||
package gview_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gview"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestView_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
str := `hello {{.name}},version:{{.version}};hello {{GetName}},version:{{GetVersion}};{{.other}}`
|
||||
pwd := gfile.Pwd()
|
||||
view := gview.New()
|
||||
view.SetDelimiters("{{", "}}")
|
||||
view.AddPath(pwd)
|
||||
view.SetPath(pwd)
|
||||
view.Assign("name", "gf")
|
||||
view.Assigns(g.Map{"version": "1.7.0"})
|
||||
view.BindFunc("GetName", func() string { return "gf" })
|
||||
view.BindFuncMap(gview.FuncMap{"GetVersion": func() string { return "1.7.0" }})
|
||||
result, err := view.ParseContent(str, g.Map{"other": "that's all"})
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, "hello gf,version:1.7.0;hello gf,version:1.7.0;that's all")
|
||||
|
||||
//测试api方法
|
||||
str = `hello {{.name}}`
|
||||
result, err = gview.ParseContent(str, g.Map{"name": "gf"})
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, "hello gf")
|
||||
|
||||
//测试instance方法
|
||||
result, err = gview.Instance().ParseContent(str, g.Map{"name": "gf"})
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, "hello gf")
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_Func(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
str := `{{eq 1 1}};{{eq 1 2}};{{eq "A" "B"}}`
|
||||
result, err := gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;false;false`)
|
||||
|
||||
str = `{{ne 1 2}};{{ne 1 1}};{{ne "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;false;true`)
|
||||
|
||||
str = `{{lt 1 2}};{{lt 1 1}};{{lt 1 0}};{{lt "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;false;false;true`)
|
||||
|
||||
str = `{{le 1 2}};{{le 1 1}};{{le 1 0}};{{le "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;true;false;true`)
|
||||
|
||||
str = `{{gt 1 2}};{{gt 1 1}};{{gt 1 0}};{{gt "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `false;false;true;false`)
|
||||
|
||||
str = `{{ge 1 2}};{{ge 1 1}};{{ge 1 0}};{{ge "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `false;true;true;false`)
|
||||
|
||||
str = `{{"<div>测试</div>"|text}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `测试`)
|
||||
|
||||
str = `{{"<div>测试</div>"|html}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<div>测试</div>`)
|
||||
|
||||
str = `{{"<div>测试</div>"|htmlencode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<div>测试</div>`)
|
||||
|
||||
str = `{{"<div>测试</div>"|htmldecode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<div>测试</div>`)
|
||||
|
||||
str = `{{"https://goframe.org"|url}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `https%3A%2F%2Fgoframe.org`)
|
||||
|
||||
str = `{{"https://goframe.org"|urlencode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `https%3A%2F%2Fgoframe.org`)
|
||||
|
||||
str = `{{"https%3A%2F%2Fgoframe.org"|urldecode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `https://goframe.org`)
|
||||
str = `{{"https%3NA%2F%2Fgoframe.org"|urldecode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(gstr.Contains(result, "invalid URL escape"), true)
|
||||
|
||||
str = `{{1540822968 | date "Y-m-d"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `2018-10-29`)
|
||||
str = `{{date "Y-m-d"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
|
||||
str = `{{"我是中国人" | substr 2 -1}};{{"我是中国人" | substr 2 2}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `中国人;中国`)
|
||||
|
||||
str = `{{"我是中国人" | strlimit 2 "..."}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `我是...`)
|
||||
|
||||
str = `{{compare "A" "B"}};{{compare "1" "2"}};{{compare 2 1}};{{compare 1 1}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `-1;-1;1;0`)
|
||||
|
||||
str = `{{"热爱GF热爱生活" | hidestr 20 "*"}};{{"热爱GF热爱生活" | hidestr 50 "*"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `热爱GF*爱生活;热爱****生活`)
|
||||
|
||||
str = `{{"热爱GF热爱生活" | highlight "GF" "red"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `热爱<span style="color:red;">GF</span>热爱生活`)
|
||||
|
||||
str = `{{"gf" | toupper}};{{"GF" | tolower}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `GF;gf`)
|
||||
|
||||
str = `{{"Go\nFrame" | nl2br}}`
|
||||
view := gview.New()
|
||||
result, err = view.ParseContent(str)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `Go<br>Frame`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_FuncInclude(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
header := `<h1>HEADER</h1>`
|
||||
main := `<h1>hello gf</h1>`
|
||||
footer := `<h1>FOOTER</h1>`
|
||||
layout := `{{include "header.html" .}}
|
||||
{{include "main.html" .}}
|
||||
{{include "footer.html" .}}`
|
||||
templatePath := gfile.Pwd() + gfile.Separator + "template"
|
||||
gfile.Mkdir(templatePath)
|
||||
defer gfile.Remove(templatePath)
|
||||
//headerFile, _ := gfile.Create(templatePath + gfile.Separator + "header.html")
|
||||
err := ioutil.WriteFile(templatePath+gfile.Separator+"header.html", []byte(header), 0644)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ioutil.WriteFile(templatePath+gfile.Separator+"main.html", []byte(main), 0644)
|
||||
ioutil.WriteFile(templatePath+gfile.Separator+"footer.html", []byte(footer), 0644)
|
||||
ioutil.WriteFile(templatePath+gfile.Separator+"layout.html", []byte(layout), 0644)
|
||||
view := gview.New(templatePath)
|
||||
result, err := view.Parse("notfound.html")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(result, ``)
|
||||
result, err = view.Parse("layout.html")
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<h1>HEADER</h1>
|
||||
<h1>hello gf</h1>
|
||||
<h1>FOOTER</h1>`)
|
||||
notfoundPath := templatePath + gfile.Separator + "template" + gfile.Separator + "notfound.html"
|
||||
gfile.Mkdir(templatePath + gfile.Separator + "template")
|
||||
gfile.Create(notfoundPath)
|
||||
ioutil.WriteFile(notfoundPath, []byte("notfound"), 0644)
|
||||
result, err = view.Parse("notfound.html")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(result, ``)
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_SetPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
view := gview.Instance("addpath")
|
||||
err := view.AddPath("tmp")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
err = view.AddPath("gview.go")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
os.Setenv("GF_GVIEW_PATH", "tmp")
|
||||
view = gview.Instance("setpath")
|
||||
err = view.SetPath("tmp")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
err = view.SetPath("gview.go")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
view = gview.New(gfile.Pwd())
|
||||
err = view.SetPath("tmp")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
err = view.SetPath("gview.go")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
os.Setenv("GF_GVIEW_PATH", "template")
|
||||
gfile.Mkdir(gfile.Pwd() + gfile.Separator + "template")
|
||||
view = gview.New()
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_ParseContent(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
str := `{{.name}}`
|
||||
view := gview.New()
|
||||
result, err := view.ParseContent(str, g.Map{"name": func() {}})
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(result, ``)
|
||||
})
|
||||
}
|
||||
@ -15,6 +15,10 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
PatternErr = `([\d+`
|
||||
)
|
||||
|
||||
func Test_Quote(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := `[foo]` //`\[foo\]`
|
||||
@ -40,6 +44,8 @@ func Test_IsMatch(t *testing.T) {
|
||||
gtest.Assert(gregex.IsMatch(pattern, s1), false)
|
||||
s1 = []byte(`sfs:`)
|
||||
gtest.Assert(gregex.IsMatch(pattern, s1), false)
|
||||
// error pattern
|
||||
gtest.Assert(gregex.IsMatch(PatternErr, s1), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -52,6 +58,8 @@ func Test_IsMatchString(t *testing.T) {
|
||||
gtest.Assert(gregex.IsMatchString(pattern, s1), false)
|
||||
s1 = `sfs:`
|
||||
gtest.Assert(gregex.IsMatchString(pattern, s1), false)
|
||||
// error pattern
|
||||
gtest.Assert(gregex.IsMatchString(PatternErr, s1), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -68,6 +76,9 @@ func Test_Match(t *testing.T) {
|
||||
if string(subs[1]) != "aab" {
|
||||
t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1], "aab")
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.Match(PatternErr, []byte(s))
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -84,6 +95,9 @@ func Test_MatchString(t *testing.T) {
|
||||
if string(subs[1]) != "aab" {
|
||||
t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1], "aab")
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.MatchString(PatternErr, s)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -108,6 +122,9 @@ func Test_MatchAll(t *testing.T) {
|
||||
if string(subs[1][1]) != "aab" {
|
||||
t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1][1], "aab")
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.MatchAll(PatternErr, []byte(s))
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -131,6 +148,9 @@ func Test_MatchAllString(t *testing.T) {
|
||||
if string(subs[1][1]) != "aab" {
|
||||
t.Fatalf("Match(%q)[1] = %q; want %q", s, subs[1][1], "aab")
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.MatchAllString(PatternErr, s)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -146,6 +166,9 @@ func Test_Replace(t *testing.T) {
|
||||
if string(replacedStr) != wanted {
|
||||
t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.Replace(PatternErr, []byte(replace), []byte(s))
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -161,6 +184,9 @@ func Test_ReplaceString(t *testing.T) {
|
||||
if replacedStr != wanted {
|
||||
t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.ReplaceString(PatternErr, replace, s)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -182,6 +208,11 @@ func Test_ReplaceFun(t *testing.T) {
|
||||
if string(replacedStr) != wanted {
|
||||
t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.ReplaceFunc(PatternErr, []byte(s), func(s []byte) []byte {
|
||||
return []byte("")
|
||||
})
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -209,6 +240,11 @@ func Test_ReplaceFuncMatch(t *testing.T) {
|
||||
})
|
||||
gtest.Assert(e3, nil)
|
||||
gtest.Assert(s3, []byte("7890"))
|
||||
// error pattern
|
||||
_, err := gregex.ReplaceFuncMatch(PatternErr, s, func(match [][]byte) []byte {
|
||||
return match[3]
|
||||
})
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -230,6 +266,11 @@ func Test_ReplaceStringFunc(t *testing.T) {
|
||||
if replacedStr != wanted {
|
||||
t.Fatalf("regex:%s,old:%s; want %q", re, s, wanted)
|
||||
}
|
||||
// error pattern
|
||||
_, err = gregex.ReplaceStringFunc(PatternErr, s, func(s string) string {
|
||||
return ""
|
||||
})
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -257,6 +298,11 @@ func Test_ReplaceStringFuncMatch(t *testing.T) {
|
||||
})
|
||||
gtest.Assert(e3, nil)
|
||||
gtest.Assert(s3, "7890")
|
||||
// error pattern
|
||||
_, err := gregex.ReplaceStringFuncMatch(PatternErr, s, func(match []string) string {
|
||||
return ""
|
||||
})
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -288,6 +334,9 @@ func Test_Split(t *testing.T) {
|
||||
if items[0] != s {
|
||||
t.Fatalf("regex:%s,Split(%q) want %q", re, s, item0)
|
||||
}
|
||||
// error pattern
|
||||
items = gregex.Split(PatternErr, s)
|
||||
gtest.AssertEQ(items, nil)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(gtime.Now().Format("U"))
|
||||
fmt.Println(gtime.Second())
|
||||
a2 := []int{1}
|
||||
array2 := garray.NewSortedIntArrayFrom(a2)
|
||||
gtest.Assert(array2.Search(2),-1)
|
||||
}
|
||||
Reference in New Issue
Block a user