mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve unit testing cases
This commit is contained in:
@ -46,22 +46,22 @@ var pairs = []testPair{
|
||||
}
|
||||
|
||||
func Test_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
for k := range pairs {
|
||||
// Encode
|
||||
gtest.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded))
|
||||
gtest.Assert(gbase64.EncodeToString([]byte(pairs[k].decoded)), pairs[k].encoded)
|
||||
gtest.Assert(gbase64.EncodeString(pairs[k].decoded), pairs[k].encoded)
|
||||
t.Assert(gbase64.Encode([]byte(pairs[k].decoded)), []byte(pairs[k].encoded))
|
||||
t.Assert(gbase64.EncodeToString([]byte(pairs[k].decoded)), pairs[k].encoded)
|
||||
t.Assert(gbase64.EncodeString(pairs[k].decoded), pairs[k].encoded)
|
||||
|
||||
// Decode
|
||||
r1, _ := gbase64.Decode([]byte(pairs[k].encoded))
|
||||
gtest.Assert(r1, []byte(pairs[k].decoded))
|
||||
t.Assert(r1, []byte(pairs[k].decoded))
|
||||
|
||||
r2, _ := gbase64.DecodeString(pairs[k].encoded)
|
||||
gtest.Assert(r2, []byte(pairs[k].decoded))
|
||||
t.Assert(r2, []byte(pairs[k].decoded))
|
||||
|
||||
r3, _ := gbase64.DecodeToString(pairs[k].encoded)
|
||||
gtest.Assert(r3, pairs[k].decoded)
|
||||
t.Assert(r3, pairs[k].decoded)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -69,29 +69,29 @@ func Test_Basic(t *testing.T) {
|
||||
func Test_File(t *testing.T) {
|
||||
path := gfile.Join(gdebug.TestDataPath(), "test")
|
||||
expect := "dGVzdA=="
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b, err := gbase64.EncodeFile(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(b), expect)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(b), expect)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s, err := gbase64.EncodeFileToString(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(s, expect)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(s, expect)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_File_Error(t *testing.T) {
|
||||
path := "none-exist-file"
|
||||
expect := ""
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b, err := gbase64.EncodeFile(path)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(string(b), expect)
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(string(b), expect)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
s, err := gbase64.EncodeFileToString(path)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(s, expect)
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(s, expect)
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,47 +21,47 @@ func Test_BeEncodeAndBeDecode(t *testing.T) {
|
||||
//t.Logf("%s:%v, encoded:%v\n", k, v, ve)
|
||||
switch v.(type) {
|
||||
case int:
|
||||
gtest.Assert(gbinary.BeDecodeToInt(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToInt(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToInt(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToInt(ve1), v)
|
||||
case int8:
|
||||
gtest.Assert(gbinary.BeDecodeToInt8(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToInt8(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToInt8(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToInt8(ve1), v)
|
||||
case int16:
|
||||
gtest.Assert(gbinary.BeDecodeToInt16(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToInt16(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToInt16(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToInt16(ve1), v)
|
||||
case int32:
|
||||
gtest.Assert(gbinary.BeDecodeToInt32(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToInt32(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToInt32(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToInt32(ve1), v)
|
||||
case int64:
|
||||
gtest.Assert(gbinary.BeDecodeToInt64(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToInt64(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToInt64(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToInt64(ve1), v)
|
||||
case uint:
|
||||
gtest.Assert(gbinary.BeDecodeToUint(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToUint(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToUint(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToUint(ve1), v)
|
||||
case uint8:
|
||||
gtest.Assert(gbinary.BeDecodeToUint8(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToUint8(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToUint8(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToUint8(ve1), v)
|
||||
case uint16:
|
||||
gtest.Assert(gbinary.BeDecodeToUint16(ve1), v)
|
||||
gtest.Assert(gbinary.BeDecodeToUint16(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToUint16(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToUint16(ve), v)
|
||||
case uint32:
|
||||
gtest.Assert(gbinary.BeDecodeToUint32(ve1), v)
|
||||
gtest.Assert(gbinary.BeDecodeToUint32(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToUint32(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToUint32(ve), v)
|
||||
case uint64:
|
||||
gtest.Assert(gbinary.BeDecodeToUint64(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToUint64(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToUint64(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToUint64(ve1), v)
|
||||
case bool:
|
||||
gtest.Assert(gbinary.BeDecodeToBool(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToBool(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToBool(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToBool(ve1), v)
|
||||
case string:
|
||||
gtest.Assert(gbinary.BeDecodeToString(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToString(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToString(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToString(ve1), v)
|
||||
case float32:
|
||||
gtest.Assert(gbinary.BeDecodeToFloat32(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToFloat32(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToFloat32(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToFloat32(ve1), v)
|
||||
case float64:
|
||||
gtest.Assert(gbinary.BeDecodeToFloat64(ve), v)
|
||||
gtest.Assert(gbinary.BeDecodeToFloat64(ve1), v)
|
||||
t.Assert(gbinary.BeDecodeToFloat64(ve), v)
|
||||
t.Assert(gbinary.BeDecodeToFloat64(ve1), v)
|
||||
default:
|
||||
if v == nil {
|
||||
continue
|
||||
@ -71,7 +71,7 @@ func Test_BeEncodeAndBeDecode(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("test data: %s, %v, error:%v", k, v, err)
|
||||
}
|
||||
gtest.Assert(res, v)
|
||||
t.Assert(res, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,5 +80,5 @@ func Test_BeEncodeStruct(t *testing.T) {
|
||||
user := User{"wenzi1", 999, "www.baidu.com"}
|
||||
ve := gbinary.BeEncode(user)
|
||||
s := gbinary.BeDecodeToString(ve)
|
||||
gtest.Assert(string(s), s)
|
||||
t.Assert(string(s), s)
|
||||
}
|
||||
|
||||
@ -21,47 +21,47 @@ func Test_LeEncodeAndLeDecode(t *testing.T) {
|
||||
//t.Logf("%s:%v, encoded:%v\n", k, v, ve)
|
||||
switch v.(type) {
|
||||
case int:
|
||||
gtest.Assert(gbinary.LeDecodeToInt(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToInt(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToInt(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToInt(ve1), v)
|
||||
case int8:
|
||||
gtest.Assert(gbinary.LeDecodeToInt8(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToInt8(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToInt8(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToInt8(ve1), v)
|
||||
case int16:
|
||||
gtest.Assert(gbinary.LeDecodeToInt16(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToInt16(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToInt16(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToInt16(ve1), v)
|
||||
case int32:
|
||||
gtest.Assert(gbinary.LeDecodeToInt32(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToInt32(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToInt32(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToInt32(ve1), v)
|
||||
case int64:
|
||||
gtest.Assert(gbinary.LeDecodeToInt64(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToInt64(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToInt64(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToInt64(ve1), v)
|
||||
case uint:
|
||||
gtest.Assert(gbinary.LeDecodeToUint(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToUint(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToUint(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToUint(ve1), v)
|
||||
case uint8:
|
||||
gtest.Assert(gbinary.LeDecodeToUint8(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToUint8(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToUint8(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToUint8(ve1), v)
|
||||
case uint16:
|
||||
gtest.Assert(gbinary.LeDecodeToUint16(ve1), v)
|
||||
gtest.Assert(gbinary.LeDecodeToUint16(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToUint16(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToUint16(ve), v)
|
||||
case uint32:
|
||||
gtest.Assert(gbinary.LeDecodeToUint32(ve1), v)
|
||||
gtest.Assert(gbinary.LeDecodeToUint32(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToUint32(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToUint32(ve), v)
|
||||
case uint64:
|
||||
gtest.Assert(gbinary.LeDecodeToUint64(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToUint64(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToUint64(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToUint64(ve1), v)
|
||||
case bool:
|
||||
gtest.Assert(gbinary.LeDecodeToBool(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToBool(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToBool(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToBool(ve1), v)
|
||||
case string:
|
||||
gtest.Assert(gbinary.LeDecodeToString(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToString(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToString(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToString(ve1), v)
|
||||
case float32:
|
||||
gtest.Assert(gbinary.LeDecodeToFloat32(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToFloat32(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToFloat32(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToFloat32(ve1), v)
|
||||
case float64:
|
||||
gtest.Assert(gbinary.LeDecodeToFloat64(ve), v)
|
||||
gtest.Assert(gbinary.LeDecodeToFloat64(ve1), v)
|
||||
t.Assert(gbinary.LeDecodeToFloat64(ve), v)
|
||||
t.Assert(gbinary.LeDecodeToFloat64(ve1), v)
|
||||
default:
|
||||
if v == nil {
|
||||
continue
|
||||
@ -71,7 +71,7 @@ func Test_LeEncodeAndLeDecode(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("test data: %s, %v, error:%v", k, v, err)
|
||||
}
|
||||
gtest.Assert(res, v)
|
||||
t.Assert(res, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,5 +80,5 @@ func Test_LeEncodeStruct(t *testing.T) {
|
||||
user := User{"wenzi1", 999, "www.baidu.com"}
|
||||
ve := gbinary.LeEncode(user)
|
||||
s := gbinary.LeDecodeToString(ve)
|
||||
gtest.Assert(string(s), s)
|
||||
t.Assert(string(s), s)
|
||||
}
|
||||
|
||||
@ -56,47 +56,47 @@ func Test_EncodeAndDecode(t *testing.T) {
|
||||
//t.Logf("%s:%v, encoded:%v\n", k, v, ve)
|
||||
switch v.(type) {
|
||||
case int:
|
||||
gtest.Assert(gbinary.DecodeToInt(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToInt(ve1), v)
|
||||
t.Assert(gbinary.DecodeToInt(ve), v)
|
||||
t.Assert(gbinary.DecodeToInt(ve1), v)
|
||||
case int8:
|
||||
gtest.Assert(gbinary.DecodeToInt8(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToInt8(ve1), v)
|
||||
t.Assert(gbinary.DecodeToInt8(ve), v)
|
||||
t.Assert(gbinary.DecodeToInt8(ve1), v)
|
||||
case int16:
|
||||
gtest.Assert(gbinary.DecodeToInt16(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToInt16(ve1), v)
|
||||
t.Assert(gbinary.DecodeToInt16(ve), v)
|
||||
t.Assert(gbinary.DecodeToInt16(ve1), v)
|
||||
case int32:
|
||||
gtest.Assert(gbinary.DecodeToInt32(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToInt32(ve1), v)
|
||||
t.Assert(gbinary.DecodeToInt32(ve), v)
|
||||
t.Assert(gbinary.DecodeToInt32(ve1), v)
|
||||
case int64:
|
||||
gtest.Assert(gbinary.DecodeToInt64(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToInt64(ve1), v)
|
||||
t.Assert(gbinary.DecodeToInt64(ve), v)
|
||||
t.Assert(gbinary.DecodeToInt64(ve1), v)
|
||||
case uint:
|
||||
gtest.Assert(gbinary.DecodeToUint(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToUint(ve1), v)
|
||||
t.Assert(gbinary.DecodeToUint(ve), v)
|
||||
t.Assert(gbinary.DecodeToUint(ve1), v)
|
||||
case uint8:
|
||||
gtest.Assert(gbinary.DecodeToUint8(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToUint8(ve1), v)
|
||||
t.Assert(gbinary.DecodeToUint8(ve), v)
|
||||
t.Assert(gbinary.DecodeToUint8(ve1), v)
|
||||
case uint16:
|
||||
gtest.Assert(gbinary.DecodeToUint16(ve1), v)
|
||||
gtest.Assert(gbinary.DecodeToUint16(ve), v)
|
||||
t.Assert(gbinary.DecodeToUint16(ve1), v)
|
||||
t.Assert(gbinary.DecodeToUint16(ve), v)
|
||||
case uint32:
|
||||
gtest.Assert(gbinary.DecodeToUint32(ve1), v)
|
||||
gtest.Assert(gbinary.DecodeToUint32(ve), v)
|
||||
t.Assert(gbinary.DecodeToUint32(ve1), v)
|
||||
t.Assert(gbinary.DecodeToUint32(ve), v)
|
||||
case uint64:
|
||||
gtest.Assert(gbinary.DecodeToUint64(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToUint64(ve1), v)
|
||||
t.Assert(gbinary.DecodeToUint64(ve), v)
|
||||
t.Assert(gbinary.DecodeToUint64(ve1), v)
|
||||
case bool:
|
||||
gtest.Assert(gbinary.DecodeToBool(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToBool(ve1), v)
|
||||
t.Assert(gbinary.DecodeToBool(ve), v)
|
||||
t.Assert(gbinary.DecodeToBool(ve1), v)
|
||||
case string:
|
||||
gtest.Assert(gbinary.DecodeToString(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToString(ve1), v)
|
||||
t.Assert(gbinary.DecodeToString(ve), v)
|
||||
t.Assert(gbinary.DecodeToString(ve1), v)
|
||||
case float32:
|
||||
gtest.Assert(gbinary.DecodeToFloat32(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToFloat32(ve1), v)
|
||||
t.Assert(gbinary.DecodeToFloat32(ve), v)
|
||||
t.Assert(gbinary.DecodeToFloat32(ve1), v)
|
||||
case float64:
|
||||
gtest.Assert(gbinary.DecodeToFloat64(ve), v)
|
||||
gtest.Assert(gbinary.DecodeToFloat64(ve1), v)
|
||||
t.Assert(gbinary.DecodeToFloat64(ve), v)
|
||||
t.Assert(gbinary.DecodeToFloat64(ve1), v)
|
||||
default:
|
||||
if v == nil {
|
||||
continue
|
||||
@ -106,7 +106,7 @@ func Test_EncodeAndDecode(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("test data: %s, %v, error:%v", k, v, err)
|
||||
}
|
||||
gtest.Assert(res, v)
|
||||
t.Assert(res, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -115,7 +115,7 @@ func Test_EncodeStruct(t *testing.T) {
|
||||
user := User{"wenzi1", 999, "www.baidu.com"}
|
||||
ve := gbinary.Encode(user)
|
||||
s := gbinary.DecodeToString(ve)
|
||||
gtest.Assert(string(s), s)
|
||||
t.Assert(string(s), s)
|
||||
}
|
||||
|
||||
func Test_Bits(t *testing.T) {
|
||||
@ -123,10 +123,10 @@ func Test_Bits(t *testing.T) {
|
||||
bits := make([]gbinary.Bit, 0)
|
||||
res := gbinary.EncodeBits(bits, testBitData[i], 64)
|
||||
|
||||
gtest.Assert(gbinary.DecodeBits(res), testBitData[i])
|
||||
gtest.Assert(gbinary.DecodeBitsToUint(res), uint(testBitData[i]))
|
||||
t.Assert(gbinary.DecodeBits(res), testBitData[i])
|
||||
t.Assert(gbinary.DecodeBitsToUint(res), uint(testBitData[i]))
|
||||
|
||||
gtest.Assert(gbinary.DecodeBytesToBits(gbinary.EncodeBitsToBytes(res)), res)
|
||||
t.Assert(gbinary.DecodeBytesToBits(gbinary.EncodeBitsToBytes(res)), res)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -138,28 +138,28 @@ func TestConvert(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConvertErr(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
srcCharset := "big5"
|
||||
dstCharset := "gbk"
|
||||
src := "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed"
|
||||
|
||||
s1, e1 := gcharset.Convert(srcCharset, srcCharset, src)
|
||||
gtest.Assert(e1, nil)
|
||||
gtest.Assert(s1, src)
|
||||
t.Assert(e1, nil)
|
||||
t.Assert(s1, src)
|
||||
|
||||
s2, e2 := gcharset.Convert(dstCharset, "no this charset", src)
|
||||
gtest.AssertNE(e2, nil)
|
||||
gtest.Assert(s2, src)
|
||||
t.AssertNE(e2, nil)
|
||||
t.Assert(s2, src)
|
||||
|
||||
s3, e3 := gcharset.Convert("no this charset", srcCharset, src)
|
||||
gtest.AssertNE(e3, nil)
|
||||
gtest.Assert(s3, src)
|
||||
t.AssertNE(e3, nil)
|
||||
t.Assert(s3, src)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSupported(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gcharset.Supported("UTF-8"), true)
|
||||
gtest.Assert(gcharset.Supported("UTF-80"), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gcharset.Supported("UTF-8"), true)
|
||||
t.Assert(gcharset.Supported("UTF-80"), false)
|
||||
})
|
||||
}
|
||||
|
||||
@ -29,16 +29,16 @@ func Test_Gzip_UnGzip(t *testing.T) {
|
||||
0x24, 0xa8, 0xd1, 0x0d, 0x00,
|
||||
0x00, 0x00,
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
arr := []byte(src)
|
||||
data, _ := gcompress.Gzip(arr)
|
||||
gtest.Assert(data, gzip)
|
||||
t.Assert(data, gzip)
|
||||
|
||||
data, _ = gcompress.UnGzip(gzip)
|
||||
gtest.Assert(data, arr)
|
||||
t.Assert(data, arr)
|
||||
|
||||
data, _ = gcompress.UnGzip(gzip[1:])
|
||||
gtest.Assert(data, nil)
|
||||
t.Assert(data, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -48,18 +48,18 @@ func Test_Gzip_UnGzip_File(t *testing.T) {
|
||||
dstPath2 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "file.txt")
|
||||
|
||||
// Compress.
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
err := gcompress.GzipFile(srcPath, dstPath1, 9)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(dstPath1)
|
||||
gtest.Assert(gfile.Exists(dstPath1), true)
|
||||
t.Assert(gfile.Exists(dstPath1), true)
|
||||
|
||||
// Decompress.
|
||||
err = gcompress.UnGzipFile(dstPath1, dstPath2)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(dstPath2)
|
||||
gtest.Assert(gfile.Exists(dstPath2), true)
|
||||
t.Assert(gfile.Exists(dstPath2), true)
|
||||
|
||||
gtest.Assert(gfile.GetContents(srcPath), gfile.GetContents(dstPath2))
|
||||
t.Assert(gfile.GetContents(srcPath), gfile.GetContents(dstPath2))
|
||||
})
|
||||
}
|
||||
|
||||
@ -19,64 +19,64 @@ import (
|
||||
|
||||
func Test_ZipPath(t *testing.T) {
|
||||
// file
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
srcPath := gfile.Join(gdebug.TestDataPath(), "zip", "path1", "1.txt")
|
||||
dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip")
|
||||
|
||||
gtest.Assert(gfile.Exists(dstPath), false)
|
||||
t.Assert(gfile.Exists(dstPath), false)
|
||||
err := gcompress.ZipPath(srcPath, dstPath)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.Exists(dstPath), true)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(gfile.Exists(dstPath), true)
|
||||
defer gfile.Remove(dstPath)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
})
|
||||
// directory
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
srcPath := gfile.Join(gdebug.TestDataPath(), "zip")
|
||||
dstPath := gfile.Join(gdebug.TestDataPath(), "zip", "zip.zip")
|
||||
|
||||
pwd := gfile.Pwd()
|
||||
err := gfile.Chdir(srcPath)
|
||||
defer gfile.Chdir(pwd)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
gtest.Assert(gfile.Exists(dstPath), false)
|
||||
t.Assert(gfile.Exists(dstPath), false)
|
||||
err = gcompress.ZipPath(srcPath, dstPath)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.Exists(dstPath), true)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(gfile.Exists(dstPath), true)
|
||||
defer gfile.Remove(dstPath)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "zip", "path1", "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "zip", "path2", "2.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
||||
)
|
||||
})
|
||||
// multiple paths joined using char ','
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
srcPath := gfile.Join(gdebug.TestDataPath(), "zip")
|
||||
srcPath1 := gfile.Join(gdebug.TestDataPath(), "zip", "path1")
|
||||
srcPath2 := gfile.Join(gdebug.TestDataPath(), "zip", "path2")
|
||||
@ -85,29 +85,29 @@ func Test_ZipPath(t *testing.T) {
|
||||
pwd := gfile.Pwd()
|
||||
err := gfile.Chdir(srcPath)
|
||||
defer gfile.Chdir(pwd)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
gtest.Assert(gfile.Exists(dstPath), false)
|
||||
t.Assert(gfile.Exists(dstPath), false)
|
||||
err = gcompress.ZipPath(srcPath1+", "+srcPath2, dstPath)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(gfile.Exists(dstPath), true)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(gfile.Exists(dstPath), true)
|
||||
defer gfile.Remove(dstPath)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
zipContent := gfile.GetBytes(dstPath)
|
||||
gtest.AssertGT(len(zipContent), 0)
|
||||
t.AssertGT(len(zipContent), 0)
|
||||
err = gcompress.UnZipContent(zipContent, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
||||
)
|
||||
@ -115,7 +115,7 @@ func Test_ZipPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_ZipPathWriter(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
srcPath := gfile.Join(gdebug.TestDataPath(), "zip")
|
||||
srcPath1 := gfile.Join(gdebug.TestDataPath(), "zip", "path1")
|
||||
srcPath2 := gfile.Join(gdebug.TestDataPath(), "zip", "path2")
|
||||
@ -123,29 +123,29 @@ func Test_ZipPathWriter(t *testing.T) {
|
||||
pwd := gfile.Pwd()
|
||||
err := gfile.Chdir(srcPath)
|
||||
defer gfile.Chdir(pwd)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
writer := bytes.NewBuffer(nil)
|
||||
gtest.Assert(writer.Len(), 0)
|
||||
t.Assert(writer.Len(), 0)
|
||||
err = gcompress.ZipPathWriter(srcPath1+", "+srcPath2, writer)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertGT(writer.Len(), 0)
|
||||
t.Assert(err, nil)
|
||||
t.AssertGT(writer.Len(), 0)
|
||||
|
||||
tempDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err = gfile.Mkdir(tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
zipContent := writer.Bytes()
|
||||
gtest.AssertGT(len(zipContent), 0)
|
||||
t.AssertGT(len(zipContent), 0)
|
||||
err = gcompress.UnZipContent(zipContent, tempDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
defer gfile.Remove(tempDirPath)
|
||||
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
||||
)
|
||||
gtest.Assert(
|
||||
t.Assert(
|
||||
gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
|
||||
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
||||
)
|
||||
|
||||
@ -14,21 +14,21 @@ import (
|
||||
)
|
||||
|
||||
func Test_Zlib_UnZlib(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
src := "hello, world\n"
|
||||
dst := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
|
||||
data, _ := gcompress.Zlib([]byte(src))
|
||||
gtest.Assert(data, dst)
|
||||
t.Assert(data, dst)
|
||||
|
||||
data, _ = gcompress.UnZlib(dst)
|
||||
gtest.Assert(data, []byte(src))
|
||||
t.Assert(data, []byte(src))
|
||||
|
||||
data, _ = gcompress.Zlib(nil)
|
||||
gtest.Assert(data, nil)
|
||||
t.Assert(data, nil)
|
||||
data, _ = gcompress.UnZlib(nil)
|
||||
gtest.Assert(data, nil)
|
||||
t.Assert(data, nil)
|
||||
|
||||
data, _ = gcompress.UnZlib(dst[1:])
|
||||
gtest.Assert(data, nil)
|
||||
t.Assert(data, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -13,128 +13,128 @@ var (
|
||||
|
||||
func Test_BKDRHash(t *testing.T) {
|
||||
var x uint32 = 200645773
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.BKDRHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_BKDRHash64(t *testing.T) {
|
||||
var x uint64 = 4214762819217104013
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.BKDRHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SDBMHash(t *testing.T) {
|
||||
var x uint32 = 1069170245
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.SDBMHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SDBMHash64(t *testing.T) {
|
||||
var x uint64 = 9881052176572890693
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.SDBMHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_RSHash(t *testing.T) {
|
||||
var x uint32 = 1944033799
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.RSHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_RSHash64(t *testing.T) {
|
||||
var x uint64 = 13439708950444349959
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.RSHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_JSHash(t *testing.T) {
|
||||
var x uint32 = 498688898
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.JSHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_JSHash64(t *testing.T) {
|
||||
var x uint64 = 13410163655098759877
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.JSHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PJWHash(t *testing.T) {
|
||||
var x uint32 = 7244206
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.PJWHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_PJWHash64(t *testing.T) {
|
||||
var x uint64 = 31150
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.PJWHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ELFHash(t *testing.T) {
|
||||
var x uint32 = 7244206
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.ELFHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ELFHash64(t *testing.T) {
|
||||
var x uint64 = 31150
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.ELFHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DJBHash(t *testing.T) {
|
||||
var x uint32 = 959862602
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.DJBHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DJBHash64(t *testing.T) {
|
||||
var x uint64 = 2519720351310960458
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.DJBHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_APHash(t *testing.T) {
|
||||
var x uint32 = 3998202516
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.APHash(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_APHash64(t *testing.T) {
|
||||
var x uint64 = 2531023058543352243
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := ghash.APHash64(strBasic)
|
||||
gtest.Assert(j, x)
|
||||
t.Assert(j, x)
|
||||
})
|
||||
}
|
||||
|
||||
@ -15,19 +15,19 @@ import (
|
||||
func TestStripTags(t *testing.T) {
|
||||
src := `<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>`
|
||||
dst := `Test paragraph. Other text`
|
||||
gtest.Assert(ghtml.StripTags(src), dst)
|
||||
t.Assert(ghtml.StripTags(src), dst)
|
||||
}
|
||||
|
||||
func TestEntities(t *testing.T) {
|
||||
src := `A 'quote' "is" <b>bold</b>`
|
||||
dst := `A 'quote' "is" <b>bold</b>`
|
||||
gtest.Assert(ghtml.Entities(src), dst)
|
||||
gtest.Assert(ghtml.EntitiesDecode(dst), src)
|
||||
t.Assert(ghtml.Entities(src), dst)
|
||||
t.Assert(ghtml.EntitiesDecode(dst), src)
|
||||
}
|
||||
|
||||
func TestSpecialChars(t *testing.T) {
|
||||
src := `A 'quote' "is" <b>bold</b>`
|
||||
dst := `A 'quote' "is" <b>bold</b>`
|
||||
gtest.Assert(ghtml.SpecialChars(src), dst)
|
||||
gtest.Assert(ghtml.SpecialCharsDecode(dst), src)
|
||||
t.Assert(ghtml.SpecialChars(src), dst)
|
||||
t.Assert(ghtml.SpecialCharsDecode(dst), src)
|
||||
}
|
||||
|
||||
@ -33,19 +33,19 @@ enable=true
|
||||
`
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
res, err := gini.Decode([]byte(iniContent))
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
|
||||
gtest.Assert(res["addr"].(map[string]interface{})["port"], "9001")
|
||||
gtest.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
|
||||
gtest.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
|
||||
gtest.Assert(res["键"].(map[string]interface{})["呵呵"], "值")
|
||||
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
|
||||
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
|
||||
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
|
||||
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
|
||||
t.Assert(res["键"].(map[string]interface{})["呵呵"], "值")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
errContent := `
|
||||
a = b
|
||||
`
|
||||
@ -57,7 +57,7 @@ func TestDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
iniMap, err := gini.Decode([]byte(iniContent))
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
@ -73,16 +73,16 @@ func TestEncode(t *testing.T) {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
gtest.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
|
||||
gtest.Assert(res["addr"].(map[string]interface{})["port"], "9001")
|
||||
gtest.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
|
||||
gtest.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
|
||||
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
|
||||
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
|
||||
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
|
||||
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestToJson(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
jsonStr, err := gini.ToJson([]byte(iniContent))
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
@ -94,11 +94,11 @@ func TestToJson(t *testing.T) {
|
||||
}
|
||||
|
||||
iniMap, err := gini.Decode([]byte(iniContent))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
gtest.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.GetString("addr.ip"))
|
||||
gtest.Assert(iniMap["addr"].(map[string]interface{})["port"], json.GetString("addr.port"))
|
||||
gtest.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.GetString("DBINFO.user"))
|
||||
gtest.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.GetString("DBINFO.type"))
|
||||
t.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.GetString("addr.ip"))
|
||||
t.Assert(iniMap["addr"].(map[string]interface{})["port"], json.GetString("addr.port"))
|
||||
t.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.GetString("DBINFO.user"))
|
||||
t.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.GetString("DBINFO.type"))
|
||||
})
|
||||
}
|
||||
|
||||
@ -72,8 +72,9 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
|
||||
}
|
||||
case reflect.Map, reflect.Struct:
|
||||
i := interface{}(nil)
|
||||
// Note that it uses MapDeep function implementing the converting.
|
||||
i = gconv.MapDeep(data, tags)
|
||||
// Note that it uses Map function implementing the converting.
|
||||
// Note that it here should not use MapDeep function if you really know what it means.
|
||||
i = gconv.Map(data, tags)
|
||||
j = &Json{
|
||||
p: &i,
|
||||
c: byte(gDEFAULT_SPLIT_CHAR),
|
||||
|
||||
@ -17,49 +17,49 @@ import (
|
||||
|
||||
func Test_New(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(data)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := gmap.NewAnyAnyMapFrom(g.MapAnyAny{
|
||||
"k1": "v1",
|
||||
"k2": "v2",
|
||||
})
|
||||
j := gjson.New(m)
|
||||
gtest.Assert(j.Get("k1"), "v1")
|
||||
gtest.Assert(j.Get("k2"), "v2")
|
||||
gtest.Assert(j.Get("k3"), nil)
|
||||
t.Assert(j.Get("k1"), "v1")
|
||||
t.Assert(j.Get("k2"), "v2")
|
||||
t.Assert(j.Get("k3"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Valid(t *testing.T) {
|
||||
data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gjson.Valid(data1), true)
|
||||
gtest.Assert(gjson.Valid(data2), false)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gjson.Valid(data1), true)
|
||||
t.Assert(gjson.Valid(data2), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Encode(t *testing.T) {
|
||||
value := g.Slice{1, 2, 3}
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b, err := gjson.Encode(value)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(b, []byte(`[1,2,3]`))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(b, []byte(`[1,2,3]`))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Decode(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := gjson.Decode(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(v, g.Map{
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v, g.Map{
|
||||
"n": 123456789,
|
||||
"a": g.Slice{1, 2, 3},
|
||||
"m": g.Map{
|
||||
@ -67,11 +67,11 @@ func Test_Decode(t *testing.T) {
|
||||
},
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v interface{}
|
||||
err := gjson.DecodeTo(data, &v)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(v, g.Map{
|
||||
t.Assert(err, nil)
|
||||
t.Assert(v, g.Map{
|
||||
"n": 123456789,
|
||||
"a": g.Slice{1, 2, 3},
|
||||
"m": g.Map{
|
||||
@ -79,216 +79,216 @@ func Test_Decode(t *testing.T) {
|
||||
},
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SplitChar(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
j.SetSplitChar(byte('#'))
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m#k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a#1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m#k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a#1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ViolenceCheck(t *testing.T) {
|
||||
data := []byte(`{"m":{"a":[1,2,3], "v1.v2":"4"}}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("m.a.2"), 3)
|
||||
gtest.Assert(j.Get("m.v1.v2"), nil)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("m.a.2"), 3)
|
||||
t.Assert(j.Get("m.v1.v2"), nil)
|
||||
j.SetViolenceCheck(true)
|
||||
gtest.Assert(j.Get("m.v1.v2"), 4)
|
||||
t.Assert(j.Get("m.v1.v2"), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetVar(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.GetVar("n").String(), "123456789")
|
||||
gtest.Assert(j.GetVar("m").Map(), g.Map{"k": "v"})
|
||||
gtest.Assert(j.GetVar("a").Interfaces(), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.GetVar("a").Slice(), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.GetVar("a").Array(), g.Slice{1, 2, 3})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.GetVar("n").String(), "123456789")
|
||||
t.Assert(j.GetVar("m").Map(), g.Map{"k": "v"})
|
||||
t.Assert(j.GetVar("a").Interfaces(), g.Slice{1, 2, 3})
|
||||
t.Assert(j.GetVar("a").Slice(), g.Slice{1, 2, 3})
|
||||
t.Assert(j.GetVar("a").Array(), g.Slice{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetMap(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.GetMap("n"), nil)
|
||||
gtest.Assert(j.GetMap("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.GetMap("a"), g.Map{"1": "2", "3": nil})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.GetMap("n"), nil)
|
||||
t.Assert(j.GetMap("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.GetMap("a"), g.Map{"1": "2", "3": nil})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetJson(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
j2 := j.GetJson("m")
|
||||
gtest.AssertNE(j2, nil)
|
||||
gtest.Assert(j2.Get("k"), "v")
|
||||
gtest.Assert(j2.Get("a"), nil)
|
||||
gtest.Assert(j2.Get("n"), nil)
|
||||
t.AssertNE(j2, nil)
|
||||
t.Assert(j2.Get("k"), "v")
|
||||
t.Assert(j2.Get("a"), nil)
|
||||
t.Assert(j2.Get("n"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetArray(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.GetArray("n"), g.Array{123456789})
|
||||
gtest.Assert(j.GetArray("m"), g.Array{g.Map{"k": "v"}})
|
||||
gtest.Assert(j.GetArray("a"), g.Array{1, 2, 3})
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.GetArray("n"), g.Array{123456789})
|
||||
t.Assert(j.GetArray("m"), g.Array{g.Map{"k": "v"}})
|
||||
t.Assert(j.GetArray("a"), g.Array{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetString(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertEQ(j.GetString("n"), "123456789")
|
||||
gtest.AssertEQ(j.GetString("m"), `{"k":"v"}`)
|
||||
gtest.AssertEQ(j.GetString("a"), `[1,2,3]`)
|
||||
gtest.AssertEQ(j.GetString("i"), "")
|
||||
t.Assert(err, nil)
|
||||
t.AssertEQ(j.GetString("n"), "123456789")
|
||||
t.AssertEQ(j.GetString("m"), `{"k":"v"}`)
|
||||
t.AssertEQ(j.GetString("a"), `[1,2,3]`)
|
||||
t.AssertEQ(j.GetString("i"), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetStrings(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertEQ(j.GetStrings("n"), g.SliceStr{"123456789"})
|
||||
gtest.AssertEQ(j.GetStrings("m"), g.SliceStr{`{"k":"v"}`})
|
||||
gtest.AssertEQ(j.GetStrings("a"), g.SliceStr{"1", "2", "3"})
|
||||
gtest.AssertEQ(j.GetStrings("i"), nil)
|
||||
t.Assert(err, nil)
|
||||
t.AssertEQ(j.GetStrings("n"), g.SliceStr{"123456789"})
|
||||
t.AssertEQ(j.GetStrings("m"), g.SliceStr{`{"k":"v"}`})
|
||||
t.AssertEQ(j.GetStrings("a"), g.SliceStr{"1", "2", "3"})
|
||||
t.AssertEQ(j.GetStrings("i"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetInterfaces(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertEQ(j.GetInterfaces("n"), g.Array{123456789})
|
||||
gtest.AssertEQ(j.GetInterfaces("m"), g.Array{g.Map{"k": "v"}})
|
||||
gtest.AssertEQ(j.GetInterfaces("a"), g.Array{1, 2, 3})
|
||||
t.Assert(err, nil)
|
||||
t.AssertEQ(j.GetInterfaces("n"), g.Array{123456789})
|
||||
t.AssertEQ(j.GetInterfaces("m"), g.Array{g.Map{"k": "v"}})
|
||||
t.AssertEQ(j.GetInterfaces("a"), g.Array{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Len(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New(nil)
|
||||
p.Append("a", 1)
|
||||
p.Append("a", 2)
|
||||
gtest.Assert(p.Len("a"), 2)
|
||||
t.Assert(p.Len("a"), 2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New(nil)
|
||||
p.Append("a.b", 1)
|
||||
p.Append("a.c", 2)
|
||||
gtest.Assert(p.Len("a"), 2)
|
||||
t.Assert(p.Len("a"), 2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New(nil)
|
||||
p.Set("a", 1)
|
||||
gtest.Assert(p.Len("a"), -1)
|
||||
t.Assert(p.Len("a"), -1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Append(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New(nil)
|
||||
p.Append("a", 1)
|
||||
p.Append("a", 2)
|
||||
gtest.Assert(p.Get("a"), g.Slice{1, 2})
|
||||
t.Assert(p.Get("a"), g.Slice{1, 2})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New(nil)
|
||||
p.Append("a.b", 1)
|
||||
p.Append("a.c", 2)
|
||||
gtest.Assert(p.Get("a"), g.Map{
|
||||
t.Assert(p.Get("a"), g.Map{
|
||||
"b": g.Slice{1},
|
||||
"c": g.Slice{2},
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New(nil)
|
||||
p.Set("a", 1)
|
||||
err := p.Append("a", 2)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(p.Get("a"), 1)
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(p.Get("a"), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestJson_ToJson(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New("1")
|
||||
s, e := p.ToJsonString()
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(s, "1")
|
||||
t.Assert(e, nil)
|
||||
t.Assert(s, "1")
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gjson.New("a")
|
||||
s, e := p.ToJsonString()
|
||||
gtest.Assert(e, nil)
|
||||
gtest.Assert(s, `"a"`)
|
||||
t.Assert(e, nil)
|
||||
t.Assert(s, `"a"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestJson_Default(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(nil)
|
||||
gtest.AssertEQ(j.Get("no", 100), 100)
|
||||
gtest.AssertEQ(j.GetString("no", 100), "100")
|
||||
gtest.AssertEQ(j.GetBool("no", "on"), true)
|
||||
gtest.AssertEQ(j.GetInt("no", 100), 100)
|
||||
gtest.AssertEQ(j.GetInt8("no", 100), int8(100))
|
||||
gtest.AssertEQ(j.GetInt16("no", 100), int16(100))
|
||||
gtest.AssertEQ(j.GetInt32("no", 100), int32(100))
|
||||
gtest.AssertEQ(j.GetInt64("no", 100), int64(100))
|
||||
gtest.AssertEQ(j.GetUint("no", 100), uint(100))
|
||||
gtest.AssertEQ(j.GetUint8("no", 100), uint8(100))
|
||||
gtest.AssertEQ(j.GetUint16("no", 100), uint16(100))
|
||||
gtest.AssertEQ(j.GetUint32("no", 100), uint32(100))
|
||||
gtest.AssertEQ(j.GetUint64("no", 100), uint64(100))
|
||||
gtest.AssertEQ(j.GetFloat32("no", 123.456), float32(123.456))
|
||||
gtest.AssertEQ(j.GetFloat64("no", 123.456), float64(123.456))
|
||||
gtest.AssertEQ(j.GetArray("no", g.Slice{1, 2, 3}), g.Slice{1, 2, 3})
|
||||
gtest.AssertEQ(j.GetInts("no", g.Slice{1, 2, 3}), g.SliceInt{1, 2, 3})
|
||||
gtest.AssertEQ(j.GetFloats("no", g.Slice{1, 2, 3}), []float64{1, 2, 3})
|
||||
gtest.AssertEQ(j.GetMap("no", g.Map{"k": "v"}), g.Map{"k": "v"})
|
||||
gtest.AssertEQ(j.GetVar("no", 123.456).Float64(), float64(123.456))
|
||||
gtest.AssertEQ(j.GetJson("no", g.Map{"k": "v"}).Get("k"), "v")
|
||||
gtest.AssertEQ(j.GetJsons("no", g.Slice{
|
||||
t.AssertEQ(j.Get("no", 100), 100)
|
||||
t.AssertEQ(j.GetString("no", 100), "100")
|
||||
t.AssertEQ(j.GetBool("no", "on"), true)
|
||||
t.AssertEQ(j.GetInt("no", 100), 100)
|
||||
t.AssertEQ(j.GetInt8("no", 100), int8(100))
|
||||
t.AssertEQ(j.GetInt16("no", 100), int16(100))
|
||||
t.AssertEQ(j.GetInt32("no", 100), int32(100))
|
||||
t.AssertEQ(j.GetInt64("no", 100), int64(100))
|
||||
t.AssertEQ(j.GetUint("no", 100), uint(100))
|
||||
t.AssertEQ(j.GetUint8("no", 100), uint8(100))
|
||||
t.AssertEQ(j.GetUint16("no", 100), uint16(100))
|
||||
t.AssertEQ(j.GetUint32("no", 100), uint32(100))
|
||||
t.AssertEQ(j.GetUint64("no", 100), uint64(100))
|
||||
t.AssertEQ(j.GetFloat32("no", 123.456), float32(123.456))
|
||||
t.AssertEQ(j.GetFloat64("no", 123.456), float64(123.456))
|
||||
t.AssertEQ(j.GetArray("no", g.Slice{1, 2, 3}), g.Slice{1, 2, 3})
|
||||
t.AssertEQ(j.GetInts("no", g.Slice{1, 2, 3}), g.SliceInt{1, 2, 3})
|
||||
t.AssertEQ(j.GetFloats("no", g.Slice{1, 2, 3}), []float64{1, 2, 3})
|
||||
t.AssertEQ(j.GetMap("no", g.Map{"k": "v"}), g.Map{"k": "v"})
|
||||
t.AssertEQ(j.GetVar("no", 123.456).Float64(), float64(123.456))
|
||||
t.AssertEQ(j.GetJson("no", g.Map{"k": "v"}).Get("k"), "v")
|
||||
t.AssertEQ(j.GetJsons("no", g.Slice{
|
||||
g.Map{"k1": "v1"},
|
||||
g.Map{"k2": "v2"},
|
||||
g.Map{"k3": "v3"},
|
||||
})[0].Get("k1"), "v1")
|
||||
gtest.AssertEQ(j.GetJsonMap("no", g.Map{
|
||||
t.AssertEQ(j.GetJsonMap("no", g.Map{
|
||||
"m1": g.Map{"k1": "v1"},
|
||||
"m2": g.Map{"k2": "v2"},
|
||||
})["m2"].Get("k2"), "v2")
|
||||
@ -296,169 +296,169 @@ func TestJson_Default(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Convert(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(`{"name":"gf"}`)
|
||||
arr, err := j.ToXml()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "<name>gf</name>")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "<name>gf</name>")
|
||||
arr, err = j.ToXmlIndent()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "<name>gf</name>")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "<name>gf</name>")
|
||||
str, err := j.ToXmlString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(str, "<name>gf</name>")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(str, "<name>gf</name>")
|
||||
str, err = j.ToXmlIndentString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(str, "<name>gf</name>")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(str, "<name>gf</name>")
|
||||
|
||||
arr, err = j.ToJsonIndent()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "{\n\t\"name\": \"gf\"\n}")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "{\n\t\"name\": \"gf\"\n}")
|
||||
str, err = j.ToJsonIndentString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "{\n\t\"name\": \"gf\"\n}")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "{\n\t\"name\": \"gf\"\n}")
|
||||
|
||||
arr, err = j.ToYaml()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "name: gf\n")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "name: gf\n")
|
||||
str, err = j.ToYamlString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "name: gf\n")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "name: gf\n")
|
||||
|
||||
arr, err = j.ToToml()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "name = \"gf\"\n")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "name = \"gf\"\n")
|
||||
str, err = j.ToTomlString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(string(arr), "name = \"gf\"\n")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(string(arr), "name = \"gf\"\n")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Convert2(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
name := struct {
|
||||
Name string
|
||||
}{}
|
||||
j := gjson.New(`{"name":"gf","time":"2019-06-12"}`)
|
||||
gtest.Assert(j.Value().(g.Map)["name"], "gf")
|
||||
gtest.Assert(j.GetMap("name1"), nil)
|
||||
gtest.AssertNE(j.GetJson("name1"), nil)
|
||||
gtest.Assert(j.GetJsons("name1"), nil)
|
||||
gtest.Assert(j.GetJsonMap("name1"), nil)
|
||||
gtest.Assert(j.Contains("name1"), false)
|
||||
gtest.Assert(j.GetVar("name1").IsNil(), true)
|
||||
gtest.Assert(j.GetVar("name").IsNil(), false)
|
||||
gtest.Assert(j.Len("name1"), -1)
|
||||
gtest.Assert(j.GetTime("time").Format("2006-01-02"), "2019-06-12")
|
||||
gtest.Assert(j.GetGTime("time").Format("Y-m-d"), "2019-06-12")
|
||||
gtest.Assert(j.GetDuration("time").String(), "0s")
|
||||
t.Assert(j.Value().(g.Map)["name"], "gf")
|
||||
t.Assert(j.GetMap("name1"), nil)
|
||||
t.AssertNE(j.GetJson("name1"), nil)
|
||||
t.Assert(j.GetJsons("name1"), nil)
|
||||
t.Assert(j.GetJsonMap("name1"), nil)
|
||||
t.Assert(j.Contains("name1"), false)
|
||||
t.Assert(j.GetVar("name1").IsNil(), true)
|
||||
t.Assert(j.GetVar("name").IsNil(), false)
|
||||
t.Assert(j.Len("name1"), -1)
|
||||
t.Assert(j.GetTime("time").Format("2006-01-02"), "2019-06-12")
|
||||
t.Assert(j.GetGTime("time").Format("Y-m-d"), "2019-06-12")
|
||||
t.Assert(j.GetDuration("time").String(), "0s")
|
||||
|
||||
err := j.ToStruct(&name)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(name.Name, "gf")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(name.Name, "gf")
|
||||
//j.Dump()
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
j = gjson.New(`{"person":{"name":"gf"}}`)
|
||||
err = j.GetStruct("person", &name)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(name.Name, "gf")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(name.Name, "gf")
|
||||
|
||||
j = gjson.New(`{"name":"gf""}`)
|
||||
//j.Dump()
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
gtest.Assert(len(j.ToArray()), 3)
|
||||
t.Assert(len(j.ToArray()), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(`{"name":"gf","time":"2019-06-12"}`)
|
||||
j.SetViolenceCheck(true)
|
||||
gtest.Assert(j.Get(""), nil)
|
||||
gtest.Assert(j.Get(".").(g.Map)["name"], "gf")
|
||||
gtest.Assert(j.Get(".").(g.Map)["name1"], nil)
|
||||
t.Assert(j.Get(""), nil)
|
||||
t.Assert(j.Get(".").(g.Map)["name"], "gf")
|
||||
t.Assert(j.Get(".").(g.Map)["name1"], nil)
|
||||
j.SetViolenceCheck(false)
|
||||
gtest.Assert(j.Get(".").(g.Map)["name"], "gf")
|
||||
t.Assert(j.Get(".").(g.Map)["name"], "gf")
|
||||
|
||||
err := j.Set("name", "gf1")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("name"), "gf1")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("name"), "gf1")
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
err = j.Set("\"0\".1", 11)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("1"), 11)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("1"), 11)
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
err = j.Set("11111111111111111111111", 11)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
err = j.Remove("1")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("0"), 1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("0"), 1)
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
err = j.Remove("3")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("0"), 1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("0"), 1)
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
err = j.Remove("0.3")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(j.Get("0").([]interface{})), 3)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(len(j.Get("0").([]interface{})), 3)
|
||||
|
||||
j = gjson.New(`[1,2,3]`)
|
||||
err = j.Remove("0.a")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(len(j.Get("0").(g.Map)), 0)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(len(j.Get("0").(g.Map)), 0)
|
||||
|
||||
name := struct {
|
||||
Name string
|
||||
}{Name: "gf"}
|
||||
j = gjson.New(name)
|
||||
gtest.Assert(j.Get("Name"), "gf")
|
||||
t.Assert(j.Get("Name"), "gf")
|
||||
err = j.Remove("Name")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name"), nil)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name"), nil)
|
||||
|
||||
err = j.Set("Name", "gf1")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name"), "gf1")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name"), "gf1")
|
||||
|
||||
j = gjson.New(nil)
|
||||
err = j.Remove("Name")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name"), nil)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name"), nil)
|
||||
|
||||
j = gjson.New(name)
|
||||
gtest.Assert(j.Get("Name"), "gf")
|
||||
t.Assert(j.Get("Name"), "gf")
|
||||
err = j.Set("Name1", g.Map{"Name": "gf1"})
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name1").(g.Map)["Name"], "gf1")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name1").(g.Map)["Name"], "gf1")
|
||||
err = j.Set("Name2", g.Slice{1, 2, 3})
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name2").(g.Slice)[0], 1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name2").(g.Slice)[0], 1)
|
||||
err = j.Set("Name3", name)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name3").(g.Map)["Name"], "gf")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name3").(g.Map)["Name"], "gf")
|
||||
err = j.Set("Name4", &name)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name4").(g.Map)["Name"], "gf")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name4").(g.Map)["Name"], "gf")
|
||||
arr := [3]int{1, 2, 3}
|
||||
err = j.Set("Name5", arr)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Name5").(g.Array)[0], 1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Name5").(g.Array)[0], 1)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_IsNil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(nil)
|
||||
gtest.Assert(j.IsNil(), true)
|
||||
t.Assert(j.IsNil(), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -18,15 +18,15 @@ import (
|
||||
|
||||
func TestJson_UnmarshalJSON(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(nil)
|
||||
err := json.Unmarshal(data, j)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -36,22 +36,22 @@ func TestJson_UnmarshalValue(t *testing.T) {
|
||||
Json *gjson.Json
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
"json": []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`),
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Json.Get("n"), "123456789")
|
||||
gtest.Assert(t.Json.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(t.Json.Get("m.k"), "v")
|
||||
gtest.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(t.Json.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(t.Name, "john")
|
||||
t.Assert(t.Json.Get("n"), "123456789")
|
||||
t.Assert(t.Json.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(t.Json.Get("m.k"), "v")
|
||||
t.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(t.Json.Get("a.1"), 2)
|
||||
})
|
||||
// Map
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var t *T
|
||||
err := gconv.Struct(g.Map{
|
||||
"name": "john",
|
||||
@ -61,12 +61,12 @@ func TestJson_UnmarshalValue(t *testing.T) {
|
||||
"a": g.Slice{1, 2, 3},
|
||||
},
|
||||
}, &t)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(t.Name, "john")
|
||||
gtest.Assert(t.Json.Get("n"), "123456789")
|
||||
gtest.Assert(t.Json.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(t.Json.Get("m.k"), "v")
|
||||
gtest.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(t.Json.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(t.Name, "john")
|
||||
t.Assert(t.Json.Get("n"), "123456789")
|
||||
t.Assert(t.Json.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(t.Json.Get("m.k"), "v")
|
||||
t.Assert(t.Json.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(t.Json.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
48
encoding/gjson/gjson_z_unit_json_test.go
Normal file
48
encoding/gjson/gjson_z_unit_json_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gjson_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_ToJson(t *testing.T) {
|
||||
type ModifyFieldInfoType struct {
|
||||
Id int64 `json:"id"`
|
||||
New string `json:"new"`
|
||||
}
|
||||
type ModifyFieldInfosType struct {
|
||||
Duration ModifyFieldInfoType `json:"duration"`
|
||||
OMLevel ModifyFieldInfoType `json:"om_level"`
|
||||
}
|
||||
|
||||
type MediaRequestModifyInfo struct {
|
||||
Modify ModifyFieldInfosType `json:"modifyFieldInfos"`
|
||||
Field ModifyFieldInfosType `json:"fieldInfos"`
|
||||
FeedID string `json:"feed_id"`
|
||||
Vid string `json:"id"`
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
jsonContent := `{"dataSetId":2001,"fieldInfos":{"duration":{"id":80079,"value":"59"},"om_level":{"id":2409,"value":"4"}},"id":"g0936lt1u0f","modifyFieldInfos":{"om_level":{"id":2409,"new":"4","old":""}},"timeStamp":1584599734}`
|
||||
var info MediaRequestModifyInfo
|
||||
err := gjson.DecodeTo(jsonContent, &info)
|
||||
t.Assert(err, nil)
|
||||
content := gjson.New(info).MustToJsonString()
|
||||
t.Assert(gstr.Contains(content, `"feed_id":""`), true)
|
||||
t.Assert(gstr.Contains(content, `"fieldInfos":{`), true)
|
||||
t.Assert(gstr.Contains(content, `"id":80079`), true)
|
||||
t.Assert(gstr.Contains(content, `"om_level":{`), true)
|
||||
t.Assert(gstr.Contains(content, `"id":2409,`), true)
|
||||
t.Assert(gstr.Contains(content, `"id":"g0936lt1u0f"`), true)
|
||||
t.Assert(gstr.Contains(content, `"new":"4"`), true)
|
||||
})
|
||||
|
||||
}
|
||||
@ -18,71 +18,71 @@ import (
|
||||
func Test_Load_JSON1(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.json"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gjson.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_JSON2(t *testing.T) {
|
||||
data := []byte(`{"n":123456789000000000000, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789000000000000")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789000000000000")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_XML(t *testing.T) {
|
||||
data := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n></doc>`)
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("doc.n"), "123456789")
|
||||
gtest.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("doc.m.k"), "v")
|
||||
gtest.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("doc.a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("doc.n"), "123456789")
|
||||
t.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("doc.m.k"), "v")
|
||||
t.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("doc.a.1"), 2)
|
||||
})
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.xml"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gjson.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("doc.n"), "123456789")
|
||||
gtest.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("doc.m.k"), "v")
|
||||
gtest.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("doc.a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("doc.n"), "123456789")
|
||||
t.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("doc.m.k"), "v")
|
||||
t.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("doc.a.1"), 2)
|
||||
})
|
||||
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
xml := `<?xml version="1.0"?>
|
||||
|
||||
<Output type="o">
|
||||
@ -94,9 +94,9 @@ func Test_Load_XML(t *testing.T) {
|
||||
<nworkOrderFrontXML/>
|
||||
</Output>`
|
||||
j, err := gjson.LoadContent(xml)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Output.ipageIndex"), "2")
|
||||
gtest.Assert(j.Get("Output.itotalRecords"), "GF框架")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Output.ipageIndex"), "2")
|
||||
t.Assert(j.Get("Output.itotalRecords"), "GF框架")
|
||||
})
|
||||
}
|
||||
|
||||
@ -111,36 +111,36 @@ m:
|
||||
"n": 123456789
|
||||
`)
|
||||
// YAML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
// YAML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.yaml"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gjson.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_YAML2(t *testing.T) {
|
||||
data := []byte("i : 123456789")
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("i"), "123456789")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("i"), "123456789")
|
||||
})
|
||||
}
|
||||
|
||||
@ -153,59 +153,59 @@ n = 123456789
|
||||
k = "v"
|
||||
`)
|
||||
// TOML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
// TOML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.toml"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gjson.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_TOML2(t *testing.T) {
|
||||
data := []byte("i=123456789")
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gjson.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("i"), "123456789")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("i"), "123456789")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(nil)
|
||||
gtest.Assert(j.Value(), nil)
|
||||
t.Assert(j.Value(), nil)
|
||||
_, err := gjson.Decode(nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
_, err = gjson.DecodeToJson(nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
j, err = gjson.LoadContent(nil)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Value(), nil)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Value(), nil)
|
||||
|
||||
j, err = gjson.LoadContent(`{"name": "gf"}`)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
j, err = gjson.LoadContent(`{"name": "gf"""}`)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
j = gjson.New(&g.Map{"name": "gf"})
|
||||
gtest.Assert(j.GetString("name"), "gf")
|
||||
t.Assert(j.GetString("name"), "gf")
|
||||
|
||||
})
|
||||
}
|
||||
@ -228,18 +228,18 @@ enable=true
|
||||
|
||||
`
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
json, err := gjson.LoadContent(data)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
gtest.Assert(json.GetString("addr.ip"), "127.0.0.1")
|
||||
gtest.Assert(json.GetString("addr.port"), "9001")
|
||||
gtest.Assert(json.GetString("addr.enable"), "true")
|
||||
gtest.Assert(json.GetString("DBINFO.type"), "mysql")
|
||||
gtest.Assert(json.GetString("DBINFO.user"), "root")
|
||||
gtest.Assert(json.GetString("DBINFO.password"), "password")
|
||||
t.Assert(json.GetString("addr.ip"), "127.0.0.1")
|
||||
t.Assert(json.GetString("addr.port"), "9001")
|
||||
t.Assert(json.GetString("addr.enable"), "true")
|
||||
t.Assert(json.GetString("DBINFO.type"), "mysql")
|
||||
t.Assert(json.GetString("DBINFO.user"), "root")
|
||||
t.Assert(json.GetString("DBINFO.password"), "password")
|
||||
|
||||
_, err = json.ToIni()
|
||||
if err != nil {
|
||||
|
||||
@ -25,25 +25,25 @@ func Test_Load_NewWithTag(t *testing.T) {
|
||||
Addr: "chengdu",
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("age-xml"), nil)
|
||||
gtest.Assert(j.Get("age-json"), data.Age)
|
||||
gtest.Assert(j.Get("name-xml"), nil)
|
||||
gtest.Assert(j.Get("name-json"), data.Name)
|
||||
gtest.Assert(j.Get("addr-xml"), nil)
|
||||
gtest.Assert(j.Get("addr-json"), data.Addr)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("age-xml"), nil)
|
||||
t.Assert(j.Get("age-json"), data.Age)
|
||||
t.Assert(j.Get("name-xml"), nil)
|
||||
t.Assert(j.Get("name-json"), data.Name)
|
||||
t.Assert(j.Get("addr-xml"), nil)
|
||||
t.Assert(j.Get("addr-json"), data.Addr)
|
||||
})
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gjson.NewWithTag(data, "xml")
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("age-xml"), data.Age)
|
||||
gtest.Assert(j.Get("age-json"), nil)
|
||||
gtest.Assert(j.Get("name-xml"), data.Name)
|
||||
gtest.Assert(j.Get("name-json"), nil)
|
||||
gtest.Assert(j.Get("addr-xml"), data.Addr)
|
||||
gtest.Assert(j.Get("addr-json"), nil)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("age-xml"), data.Age)
|
||||
t.Assert(j.Get("age-json"), nil)
|
||||
t.Assert(j.Get("name-xml"), data.Name)
|
||||
t.Assert(j.Get("name-json"), nil)
|
||||
t.Assert(j.Get("addr-xml"), data.Addr)
|
||||
t.Assert(j.Get("addr-json"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func Test_ToStruct1(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type BaseInfoItem struct {
|
||||
IdCardNumber string `db:"id_card_number" json:"idCardNumber" field:"id_card_number"`
|
||||
IsHouseholder bool `db:"is_householder" json:"isHouseholder" field:"is_householder"`
|
||||
@ -88,15 +88,15 @@ func Test_ToStruct1(t *testing.T) {
|
||||
}`
|
||||
data := new(UserCollectionAddReq)
|
||||
j, err := gjson.LoadJson(jsonContent)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
err = j.ToStruct(data)
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
g.Dump(data)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ToStructDeep(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Item struct {
|
||||
Title string `json:"title"`
|
||||
Key string `json:"key"`
|
||||
@ -117,16 +117,16 @@ func Test_ToStructDeep(t *testing.T) {
|
||||
}`
|
||||
|
||||
j, err := gjson.LoadContent(txt)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.GetString("me.name"), "mikey")
|
||||
gtest.Assert(j.GetString("items"), "")
|
||||
gtest.Assert(j.GetBool("items"), false)
|
||||
gtest.Assert(j.GetArray("items"), nil)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.GetString("me.name"), "mikey")
|
||||
t.Assert(j.GetString("items"), "")
|
||||
t.Assert(j.GetBool("items"), false)
|
||||
t.Assert(j.GetArray("items"), nil)
|
||||
m := new(M)
|
||||
err = j.ToStructDeep(m)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.AssertNE(m.Me, nil)
|
||||
gtest.Assert(m.Me["day"], "20009")
|
||||
gtest.Assert(m.Items, nil)
|
||||
t.Assert(err, nil)
|
||||
t.AssertNE(m.Me, nil)
|
||||
t.Assert(m.Me["day"], "20009")
|
||||
t.Assert(m.Items, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -16,291 +16,291 @@ import (
|
||||
|
||||
func Test_New(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
v := j.Value().(g.Map)
|
||||
gtest.Assert(v["n"], 123456789)
|
||||
t.Assert(v["n"], 123456789)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_NewUnsafe(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Encode(t *testing.T) {
|
||||
value := g.Slice{1, 2, 3}
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b, err := gparser.VarToJson(value)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(b, []byte(`[1,2,3]`))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(b, []byte(`[1,2,3]`))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Decode(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_SplitChar(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
j.SetSplitChar(byte('#'))
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m#k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a#1"), 2)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m#k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a#1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ViolenceCheck(t *testing.T) {
|
||||
data := []byte(`{"m":{"a":[1,2,3], "v1.v2":"4"}}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("m.a.2"), 3)
|
||||
gtest.Assert(j.Get("m.v1.v2"), nil)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("m.a.2"), 3)
|
||||
t.Assert(j.Get("m.v1.v2"), nil)
|
||||
j.SetViolenceCheck(true)
|
||||
gtest.Assert(j.Get("m.v1.v2"), 4)
|
||||
t.Assert(j.Get("m.v1.v2"), 4)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetVar(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.GetVar("n").String(), "123456789")
|
||||
gtest.Assert(j.GetVar("m").Map(), g.Map{"k": "v"})
|
||||
gtest.Assert(j.GetVar("a").Interfaces(), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.GetVar("a").Slice(), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.GetMap("a"), g.Map{"1": "2", "3": nil})
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.GetVar("n").String(), "123456789")
|
||||
t.Assert(j.GetVar("m").Map(), g.Map{"k": "v"})
|
||||
t.Assert(j.GetVar("a").Interfaces(), g.Slice{1, 2, 3})
|
||||
t.Assert(j.GetVar("a").Slice(), g.Slice{1, 2, 3})
|
||||
t.Assert(j.GetMap("a"), g.Map{"1": "2", "3": nil})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetMap(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.GetMap("n"), nil)
|
||||
gtest.Assert(j.GetMap("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.GetMap("a"), g.Map{"1": "2", "3": nil})
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.GetMap("n"), nil)
|
||||
t.Assert(j.GetMap("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.GetMap("a"), g.Map{"1": "2", "3": nil})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetArray(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.GetArray("n"), g.Array{123456789})
|
||||
gtest.Assert(j.GetArray("m"), g.Array{g.Map{"k": "v"}})
|
||||
gtest.Assert(j.GetArray("a"), g.Array{1, 2, 3})
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.GetArray("n"), g.Array{123456789})
|
||||
t.Assert(j.GetArray("m"), g.Array{g.Map{"k": "v"}})
|
||||
t.Assert(j.GetArray("a"), g.Array{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetString(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.AssertEQ(j.GetString("n"), "123456789")
|
||||
gtest.AssertEQ(j.GetString("m"), `{"k":"v"}`)
|
||||
gtest.AssertEQ(j.GetString("a"), `[1,2,3]`)
|
||||
gtest.AssertEQ(j.GetString("i"), "")
|
||||
t.AssertNE(j, nil)
|
||||
t.AssertEQ(j.GetString("n"), "123456789")
|
||||
t.AssertEQ(j.GetString("m"), `{"k":"v"}`)
|
||||
t.AssertEQ(j.GetString("a"), `[1,2,3]`)
|
||||
t.AssertEQ(j.GetString("i"), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetStrings(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.AssertEQ(j.GetStrings("n"), g.SliceStr{"123456789"})
|
||||
gtest.AssertEQ(j.GetStrings("m"), g.SliceStr{`{"k":"v"}`})
|
||||
gtest.AssertEQ(j.GetStrings("a"), g.SliceStr{"1", "2", "3"})
|
||||
gtest.AssertEQ(j.GetStrings("i"), nil)
|
||||
t.AssertNE(j, nil)
|
||||
t.AssertEQ(j.GetStrings("n"), g.SliceStr{"123456789"})
|
||||
t.AssertEQ(j.GetStrings("m"), g.SliceStr{`{"k":"v"}`})
|
||||
t.AssertEQ(j.GetStrings("a"), g.SliceStr{"1", "2", "3"})
|
||||
t.AssertEQ(j.GetStrings("i"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetInterfaces(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.AssertEQ(j.GetInterfaces("n"), g.Array{123456789})
|
||||
gtest.AssertEQ(j.GetInterfaces("m"), g.Array{g.Map{"k": "v"}})
|
||||
gtest.AssertEQ(j.GetInterfaces("a"), g.Array{1, 2, 3})
|
||||
t.AssertNE(j, nil)
|
||||
t.AssertEQ(j.GetInterfaces("n"), g.Array{123456789})
|
||||
t.AssertEQ(j.GetInterfaces("m"), g.Array{g.Map{"k": "v"}})
|
||||
t.AssertEQ(j.GetInterfaces("a"), g.Array{1, 2, 3})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Len(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
p.Append("a", 1)
|
||||
p.Append("a", 2)
|
||||
gtest.Assert(p.Len("a"), 2)
|
||||
t.Assert(p.Len("a"), 2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
p.Append("a.b", 1)
|
||||
p.Append("a.c", 2)
|
||||
gtest.Assert(p.Len("a"), 2)
|
||||
t.Assert(p.Len("a"), 2)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
p.Set("a", 1)
|
||||
gtest.Assert(p.Len("a"), -1)
|
||||
t.Assert(p.Len("a"), -1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Append(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
p.Append("a", 1)
|
||||
p.Append("a", 2)
|
||||
gtest.Assert(p.Get("a"), g.Slice{1, 2})
|
||||
t.Assert(p.Get("a"), g.Slice{1, 2})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
p.Append("a.b", 1)
|
||||
p.Append("a.c", 2)
|
||||
gtest.Assert(p.Get("a"), g.Map{
|
||||
t.Assert(p.Get("a"), g.Map{
|
||||
"b": g.Slice{1},
|
||||
"c": g.Slice{2},
|
||||
})
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
p.Set("a", 1)
|
||||
err := p.Append("a", 2)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(p.Get("a"), 1)
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(p.Get("a"), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Convert(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(`{"name":"gf","bool":true,"int":1,"float":1,"ints":[1,2],"floats":[1,2],"time":"2019-06-12","person": {"name": "gf"}}`)
|
||||
gtest.Assert(p.GetVar("name").String(), "gf")
|
||||
gtest.Assert(p.GetString("name"), "gf")
|
||||
gtest.Assert(p.GetBool("bool"), true)
|
||||
gtest.Assert(p.GetInt("int"), 1)
|
||||
gtest.Assert(p.GetInt8("int"), 1)
|
||||
gtest.Assert(p.GetInt16("int"), 1)
|
||||
gtest.Assert(p.GetInt32("int"), 1)
|
||||
gtest.Assert(p.GetInt64("int"), 1)
|
||||
gtest.Assert(p.GetUint("int"), 1)
|
||||
gtest.Assert(p.GetUint8("int"), 1)
|
||||
gtest.Assert(p.GetUint16("int"), 1)
|
||||
gtest.Assert(p.GetUint32("int"), 1)
|
||||
gtest.Assert(p.GetUint64("int"), 1)
|
||||
gtest.Assert(p.GetInts("ints")[0], 1)
|
||||
gtest.Assert(p.GetFloat32("float"), 1)
|
||||
gtest.Assert(p.GetFloat64("float"), 1)
|
||||
gtest.Assert(p.GetFloats("floats")[0], 1)
|
||||
gtest.Assert(p.GetTime("time").Format("2006-01-02"), "2019-06-12")
|
||||
gtest.Assert(p.GetGTime("time").Format("Y-m-d"), "2019-06-12")
|
||||
gtest.Assert(p.GetDuration("time").String(), "0s")
|
||||
t.Assert(p.GetVar("name").String(), "gf")
|
||||
t.Assert(p.GetString("name"), "gf")
|
||||
t.Assert(p.GetBool("bool"), true)
|
||||
t.Assert(p.GetInt("int"), 1)
|
||||
t.Assert(p.GetInt8("int"), 1)
|
||||
t.Assert(p.GetInt16("int"), 1)
|
||||
t.Assert(p.GetInt32("int"), 1)
|
||||
t.Assert(p.GetInt64("int"), 1)
|
||||
t.Assert(p.GetUint("int"), 1)
|
||||
t.Assert(p.GetUint8("int"), 1)
|
||||
t.Assert(p.GetUint16("int"), 1)
|
||||
t.Assert(p.GetUint32("int"), 1)
|
||||
t.Assert(p.GetUint64("int"), 1)
|
||||
t.Assert(p.GetInts("ints")[0], 1)
|
||||
t.Assert(p.GetFloat32("float"), 1)
|
||||
t.Assert(p.GetFloat64("float"), 1)
|
||||
t.Assert(p.GetFloats("floats")[0], 1)
|
||||
t.Assert(p.GetTime("time").Format("2006-01-02"), "2019-06-12")
|
||||
t.Assert(p.GetGTime("time").Format("Y-m-d"), "2019-06-12")
|
||||
t.Assert(p.GetDuration("time").String(), "0s")
|
||||
name := struct {
|
||||
Name string
|
||||
}{}
|
||||
err := p.GetStruct("person", &name)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(name.Name, "gf")
|
||||
gtest.Assert(p.ToMap()["name"], "gf")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(name.Name, "gf")
|
||||
t.Assert(p.ToMap()["name"], "gf")
|
||||
err = p.ToStruct(&name)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(name.Name, "gf")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(name.Name, "gf")
|
||||
//p.Dump()
|
||||
|
||||
p = gparser.New(`[0,1,2]`)
|
||||
gtest.Assert(p.ToArray()[0], 0)
|
||||
t.Assert(p.ToArray()[0], 0)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Convert2(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
xmlArr := []byte{60, 114, 111, 111, 116, 47, 62}
|
||||
p := gparser.New(`<root></root>`)
|
||||
arr, err := p.ToXml("root")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, xmlArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, xmlArr)
|
||||
arr, err = gparser.VarToXml(`<root></root>`, "root")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, xmlArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, xmlArr)
|
||||
|
||||
arr, err = p.ToXmlIndent("root")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, xmlArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, xmlArr)
|
||||
arr, err = gparser.VarToXmlIndent(`<root></root>`, "root")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, xmlArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, xmlArr)
|
||||
|
||||
p = gparser.New(`{"name":"gf"}`)
|
||||
str, err := p.ToJsonString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(str, `{"name":"gf"}`)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(str, `{"name":"gf"}`)
|
||||
str, err = gparser.VarToJsonString(`{"name":"gf"}`)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(str, `{"name":"gf"}`)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(str, `{"name":"gf"}`)
|
||||
|
||||
jsonIndentArr := []byte{123, 10, 9, 34, 110, 97, 109, 101, 34, 58, 32, 34, 103, 102, 34, 10, 125}
|
||||
arr, err = p.ToJsonIndent()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, jsonIndentArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, jsonIndentArr)
|
||||
arr, err = gparser.VarToJsonIndent(`{"name":"gf"}`)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, jsonIndentArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, jsonIndentArr)
|
||||
|
||||
str, err = p.ToJsonIndentString()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(str, "{\n\t\"name\": \"gf\"\n}")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(str, "{\n\t\"name\": \"gf\"\n}")
|
||||
str, err = gparser.VarToJsonIndentString(`{"name":"gf"}`)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(str, "{\n\t\"name\": \"gf\"\n}")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(str, "{\n\t\"name\": \"gf\"\n}")
|
||||
|
||||
p = gparser.New(g.Map{"name": "gf"})
|
||||
arr, err = p.ToYaml()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, "name: gf\n")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, "name: gf\n")
|
||||
arr, err = gparser.VarToYaml(g.Map{"name": "gf"})
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, "name: gf\n")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, "name: gf\n")
|
||||
|
||||
tomlArr := []byte{110, 97, 109, 101, 32, 61, 32, 34, 103, 102, 34, 10}
|
||||
p = gparser.New(`
|
||||
name= "gf"
|
||||
`)
|
||||
arr, err = p.ToToml()
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, tomlArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, tomlArr)
|
||||
arr, err = gparser.VarToToml(`
|
||||
name= "gf"
|
||||
`)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(arr, tomlArr)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(arr, tomlArr)
|
||||
})
|
||||
}
|
||||
|
||||
@ -19,58 +19,58 @@ import (
|
||||
func Test_Load_JSON(t *testing.T) {
|
||||
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gparser.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.json"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gparser.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_XML(t *testing.T) {
|
||||
data := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n></doc>`)
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gparser.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("doc.n"), "123456789")
|
||||
gtest.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("doc.m.k"), "v")
|
||||
gtest.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("doc.a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("doc.n"), "123456789")
|
||||
t.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("doc.m.k"), "v")
|
||||
t.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("doc.a.1"), 2)
|
||||
})
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.xml"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gparser.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("doc.n"), "123456789")
|
||||
gtest.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("doc.m.k"), "v")
|
||||
gtest.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("doc.a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("doc.n"), "123456789")
|
||||
t.Assert(j.Get("doc.m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("doc.m.k"), "v")
|
||||
t.Assert(j.Get("doc.a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("doc.a.1"), 2)
|
||||
})
|
||||
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
xml := `<?xml version="1.0"?>
|
||||
|
||||
<Output type="o">
|
||||
@ -82,9 +82,9 @@ func Test_Load_XML(t *testing.T) {
|
||||
<nworkOrderFrontXML/>
|
||||
</Output>`
|
||||
j, err := gparser.LoadContent(xml)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("Output.ipageIndex"), "2")
|
||||
gtest.Assert(j.Get("Output.itotalRecords"), "GF框架")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("Output.ipageIndex"), "2")
|
||||
t.Assert(j.Get("Output.itotalRecords"), "GF框架")
|
||||
})
|
||||
}
|
||||
|
||||
@ -99,36 +99,36 @@ m:
|
||||
"n": 123456789
|
||||
`)
|
||||
// YAML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gparser.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
// YAML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.yaml"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gparser.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{1, 2, 3})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_YAML2(t *testing.T) {
|
||||
data := []byte("i : 123456789")
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gparser.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("i"), "123456789")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("i"), "123456789")
|
||||
})
|
||||
}
|
||||
|
||||
@ -141,50 +141,50 @@ n = "123456789"
|
||||
k = "v"
|
||||
`)
|
||||
// TOML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gparser.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
// TOML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
path := "test.toml"
|
||||
gfile.PutBytes(path, data)
|
||||
defer gfile.Remove(path)
|
||||
j, err := gparser.Load(path)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("n"), "123456789")
|
||||
gtest.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
gtest.Assert(j.Get("m.k"), "v")
|
||||
gtest.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
gtest.Assert(j.Get("a.1"), 2)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("n"), "123456789")
|
||||
t.Assert(j.Get("m"), g.Map{"k": "v"})
|
||||
t.Assert(j.Get("m.k"), "v")
|
||||
t.Assert(j.Get("a"), g.Slice{"1", "2", "3"})
|
||||
t.Assert(j.Get("a.1"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_TOML2(t *testing.T) {
|
||||
data := []byte("i=123456789")
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j, err := gparser.LoadContent(data)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(j.Get("i"), "123456789")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j.Get("i"), "123456789")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Load_Nil(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
p := gparser.New(nil)
|
||||
gtest.Assert(p.Value(), nil)
|
||||
t.Assert(p.Value(), nil)
|
||||
file := "test22222.json"
|
||||
filePath := gfile.Pwd() + gfile.Separator + file
|
||||
ioutil.WriteFile(filePath, []byte("{"), 0644)
|
||||
defer gfile.Remove(filePath)
|
||||
_, err := gparser.Load(file)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
_, err = gparser.LoadContent("{")
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -25,25 +25,25 @@ func Test_Load_NewWithTag(t *testing.T) {
|
||||
Addr: "chengdu",
|
||||
}
|
||||
// JSON
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.New(data)
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("age-xml"), nil)
|
||||
gtest.Assert(j.Get("age-json"), data.Age)
|
||||
gtest.Assert(j.Get("name-xml"), nil)
|
||||
gtest.Assert(j.Get("name-json"), data.Name)
|
||||
gtest.Assert(j.Get("addr-xml"), nil)
|
||||
gtest.Assert(j.Get("addr-json"), data.Addr)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("age-xml"), nil)
|
||||
t.Assert(j.Get("age-json"), data.Age)
|
||||
t.Assert(j.Get("name-xml"), nil)
|
||||
t.Assert(j.Get("name-json"), data.Name)
|
||||
t.Assert(j.Get("addr-xml"), nil)
|
||||
t.Assert(j.Get("addr-json"), data.Addr)
|
||||
})
|
||||
// XML
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
j := gparser.NewWithTag(data, "xml")
|
||||
gtest.AssertNE(j, nil)
|
||||
gtest.Assert(j.Get("age-xml"), data.Age)
|
||||
gtest.Assert(j.Get("age-json"), nil)
|
||||
gtest.Assert(j.Get("name-xml"), data.Name)
|
||||
gtest.Assert(j.Get("name-json"), nil)
|
||||
gtest.Assert(j.Get("addr-xml"), data.Addr)
|
||||
gtest.Assert(j.Get("addr-json"), nil)
|
||||
t.AssertNE(j, nil)
|
||||
t.Assert(j.Get("age-xml"), data.Age)
|
||||
t.Assert(j.Get("age-json"), nil)
|
||||
t.Assert(j.Get("name-xml"), data.Name)
|
||||
t.Assert(j.Get("name-json"), nil)
|
||||
t.Assert(j.Get("addr-xml"), data.Addr)
|
||||
t.Assert(j.Get("addr-json"), nil)
|
||||
})
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ dd = 11
|
||||
`
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := make(map[string]string)
|
||||
m["toml"] = tomlStr
|
||||
res, err := gtoml.Encode(m)
|
||||
@ -49,10 +49,10 @@ func TestEncode(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
gtest.Assert(p.GetString("toml"), tomlStr)
|
||||
t.Assert(p.GetString("toml"), tomlStr)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := gtoml.Encode(tomlErr)
|
||||
if err == nil {
|
||||
t.Errorf("encode should be failed. %v", err)
|
||||
@ -62,7 +62,7 @@ func TestEncode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := make(map[string]string)
|
||||
m["toml"] = tomlStr
|
||||
res, err := gtoml.Encode(m)
|
||||
@ -77,7 +77,7 @@ func TestDecode(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
gtest.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
|
||||
t.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
|
||||
|
||||
decodeStr1 := make(map[string]interface{})
|
||||
err = gtoml.DecodeTo(res, &decodeStr1)
|
||||
@ -85,10 +85,10 @@ func TestDecode(t *testing.T) {
|
||||
t.Errorf("decodeTo failed. %v", err)
|
||||
return
|
||||
}
|
||||
gtest.Assert(decodeStr1["toml"], tomlStr)
|
||||
t.Assert(decodeStr1["toml"], tomlStr)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := gtoml.Decode([]byte(tomlErr))
|
||||
if err == nil {
|
||||
t.Errorf("decode failed. %v", err)
|
||||
@ -105,7 +105,7 @@ func TestDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestToJson(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := make(map[string]string)
|
||||
m["toml"] = tomlStr
|
||||
res, err := gtoml.Encode(m)
|
||||
@ -130,10 +130,10 @@ func TestToJson(t *testing.T) {
|
||||
t.Errorf("parser ToJson failed. %v", err)
|
||||
return
|
||||
}
|
||||
gtest.Assert(jsonToml, expectJson)
|
||||
t.Assert(jsonToml, expectJson)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := gtoml.ToJson([]byte(tomlErr))
|
||||
if err == nil {
|
||||
t.Errorf("ToJson failed. %v", err)
|
||||
|
||||
@ -18,28 +18,28 @@ var urlEncode string = `https%3A%2F%2Fgolang.org%2Fx%2Fcrypto%3Fgo-get%3D1+%2B`
|
||||
var rawUrlEncode string = `https%3A%2F%2Fgolang.org%2Fx%2Fcrypto%3Fgo-get%3D1%20%2B`
|
||||
|
||||
func TestEncodeAndDecode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gurl.Encode(urlStr), urlEncode)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gurl.Encode(urlStr), urlEncode)
|
||||
|
||||
res, err := gurl.Decode(urlEncode)
|
||||
if err != nil {
|
||||
t.Errorf("decode failed. %v", err)
|
||||
return
|
||||
}
|
||||
gtest.Assert(res, urlStr)
|
||||
t.Assert(res, urlStr)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRowEncodeAndDecode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.Assert(gurl.RawEncode(urlStr), rawUrlEncode)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gurl.RawEncode(urlStr), rawUrlEncode)
|
||||
|
||||
res, err := gurl.RawDecode(rawUrlEncode)
|
||||
if err != nil {
|
||||
t.Errorf("decode failed. %v", err)
|
||||
return
|
||||
}
|
||||
gtest.Assert(res, urlStr)
|
||||
t.Assert(res, urlStr)
|
||||
})
|
||||
}
|
||||
|
||||
@ -51,7 +51,7 @@ func TestBuildQuery(t *testing.T) {
|
||||
}
|
||||
expect := "a=a2&a=a1&b=b2&b=b1&c=c1&c=c2"
|
||||
|
||||
gtest.Assert(gurl.BuildQuery(src), expect)
|
||||
t.Assert(gurl.BuildQuery(src), expect)
|
||||
}
|
||||
|
||||
func TestParseURL(t *testing.T) {
|
||||
@ -67,7 +67,7 @@ func TestParseURL(t *testing.T) {
|
||||
"fragment": "anchor",
|
||||
}
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
component := 0
|
||||
for k, v := range []string{"all", "scheme", "host", "port", "user", "pass", "path", "query", "fragment"} {
|
||||
if v == "all" {
|
||||
@ -83,9 +83,9 @@ func TestParseURL(t *testing.T) {
|
||||
}
|
||||
|
||||
if v == "all" {
|
||||
gtest.Assert(res, expect)
|
||||
t.Assert(res, expect)
|
||||
} else {
|
||||
gtest.Assert(res[v], expect[v])
|
||||
t.Assert(res[v], expect[v])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -107,28 +107,28 @@ func Test_Decode1(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Decode2(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
content := `
|
||||
<?xml version="1.0" encoding="UTF-8"?><doc><username>johngcn</username><password1>123456</password1><password2>123456</password2></doc>
|
||||
`
|
||||
m, err := gxml.Decode([]byte(content))
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m["doc"].(map[string]interface{})["username"], "johngcn")
|
||||
gtest.Assert(m["doc"].(map[string]interface{})["password1"], "123456")
|
||||
gtest.Assert(m["doc"].(map[string]interface{})["password2"], "123456")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m["doc"].(map[string]interface{})["username"], "johngcn")
|
||||
t.Assert(m["doc"].(map[string]interface{})["password1"], "123456")
|
||||
t.Assert(m["doc"].(map[string]interface{})["password2"], "123456")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_DecodeWitoutRoot(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
content := `
|
||||
<?xml version="1.0" encoding="UTF-8"?><doc><username>johngcn</username><password1>123456</password1><password2>123456</password2></doc>
|
||||
`
|
||||
m, err := gxml.DecodeWithoutRoot([]byte(content))
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(m["username"], "johngcn")
|
||||
gtest.Assert(m["password1"], "123456")
|
||||
gtest.Assert(m["password2"], "123456")
|
||||
t.Assert(err, nil)
|
||||
t.Assert(m["username"], "johngcn")
|
||||
t.Assert(m["password1"], "123456")
|
||||
t.Assert(m["password2"], "123456")
|
||||
})
|
||||
}
|
||||
|
||||
@ -190,7 +190,7 @@ func TestErrXml(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestErrCase(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
errXml := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string>`
|
||||
_, err := gxml.ToJson([]byte(errXml))
|
||||
if err == nil {
|
||||
@ -198,7 +198,7 @@ func TestErrCase(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
errXml := `<root><bool>true</bool><float>100.92</float><int>123</int><string>hello world</string>`
|
||||
_, err := gxml.Decode([]byte(errXml))
|
||||
if err == nil {
|
||||
|
||||
@ -39,13 +39,13 @@ dd = 11
|
||||
`
|
||||
|
||||
func Test_Decode(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := gyaml.Decode([]byte(yamlStr))
|
||||
gtest.Assert(err, nil)
|
||||
t.Assert(err, nil)
|
||||
|
||||
m, ok := result.(map[string]interface{})
|
||||
gtest.Assert(ok, true)
|
||||
gtest.Assert(m, map[string]interface{}{
|
||||
t.Assert(ok, true)
|
||||
t.Assert(m, map[string]interface{}{
|
||||
"url": "https://goframe.org",
|
||||
"server": g.Slice{"120.168.117.21", "120.168.117.22"},
|
||||
"pi": 3.14,
|
||||
@ -56,11 +56,11 @@ func Test_Decode(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DecodeTo(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result := make(map[string]interface{})
|
||||
err := gyaml.DecodeTo([]byte(yamlStr), &result)
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(result, map[string]interface{}{
|
||||
t.Assert(err, nil)
|
||||
t.Assert(result, map[string]interface{}{
|
||||
"url": "https://goframe.org",
|
||||
"server": g.Slice{"120.168.117.21", "120.168.117.22"},
|
||||
"pi": 3.14,
|
||||
@ -71,18 +71,18 @@ func Test_DecodeTo(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DecodeError(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := gyaml.Decode([]byte(yamlErr))
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
|
||||
result := make(map[string]interface{})
|
||||
err = gyaml.DecodeTo([]byte(yamlErr), &result)
|
||||
gtest.AssertNE(err, nil)
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_ToJson(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
m := make(map[string]string)
|
||||
m["yaml"] = yamlStr
|
||||
res, err := gyaml.Encode(m)
|
||||
@ -107,10 +107,10 @@ func Test_ToJson(t *testing.T) {
|
||||
t.Errorf("parser ToJson failed. %v", err)
|
||||
return
|
||||
}
|
||||
gtest.Assert(jsonyaml, expectJson)
|
||||
t.Assert(jsonyaml, expectJson)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := gyaml.ToJson([]byte(yamlErr))
|
||||
if err == nil {
|
||||
t.Errorf("ToJson failed. %v", err)
|
||||
|
||||
Reference in New Issue
Block a user