improve unit testing cases coverage to 80%+ (#2480)

This commit is contained in:
HaiLaz 2023-03-07 21:27:23 +08:00 committed by GitHub
parent 13f6fb1929
commit 55690f3738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 3 deletions

View File

@ -5,7 +5,7 @@
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf)
[![GoFrame CI](https://github.com/gogf/gf/actions/workflows/gf.yml/badge.svg)](https://github.com/gogf/gf/actions/workflows/gf.yml)
[![Go Report](https://goreportcard.com/badge/github.com/gogf/gf?v=1)](https://goreportcard.com/report/github.com/gogf/gf)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf/branch/master)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf)
[![Production Ready](https://img.shields.io/badge/production-ready-blue.svg)](https://github.com/gogf/gf)
[![License](https://img.shields.io/github/license/gogf/gf.svg?style=flat)](https://github.com/gogf/gf)
</div>

View File

@ -40,9 +40,11 @@ func Parse(s string) (result map[string]interface{}, err error) {
err = gerror.Wrapf(err, `url.QueryUnescape failed for string "%s"`, part[:pos])
return nil, err
}
for key[0] == ' ' {
for len(key) > 0 && key[0] == ' ' {
key = key[1:]
}
if key == "" || key[0] == '[' {
continue
}
@ -129,7 +131,6 @@ func build(result map[string]interface{}, keys []string, value interface{}) erro
result[key] = append(children, value)
return nil
}
// The end is slice + map. like v[][a]
if keys[1] == "" && length > 2 && keys[2] != "" {
val, ok := result[key]

View File

@ -18,6 +18,74 @@ import (
)
func Test_Parse(t *testing.T) {
// cover test
gtest.C(t, func(t *gtest.T) {
// empty
m, err := gstr.Parse("")
t.AssertNil(err)
t.Assert(m, nil)
// invalid
m, err = gstr.Parse("a&b")
t.AssertNil(err)
t.Assert(m, make(map[string]interface{}))
// special key
m, err = gstr.Parse(" =1& b=2& c =3")
t.AssertNil(err)
t.Assert(m, map[string]interface{}{"b": "2", "c_": "3"})
m, err = gstr.Parse("c[=3")
t.AssertNil(err)
t.Assert(m, map[string]interface{}{"c_": "3"})
m, err = gstr.Parse("v[a][a]a=m")
t.AssertNil(err)
t.Assert(m, g.Map{
"v": g.Map{
"a": g.Map{
"a": "m",
},
},
})
// v[][a]=m&v[][b]=b => map["v"]:[{"a":"m","b":"b"}]
m, err = gstr.Parse("v[][a]=m&v[][b]=b")
t.AssertNil(err)
t.Assert(m, g.Map{
"v": g.Slice{
g.Map{
"a": "m",
"b": "b",
},
},
})
// v[][a]=m&v[][a]=b => map["v"]:[{"a":"m"},{"a":"b"}]
m, err = gstr.Parse("v[][a]=m&v[][a]=b")
t.AssertNil(err)
t.Assert(m, g.Map{
"v": g.Slice{
g.Map{
"a": "m",
},
g.Map{
"a": "b",
},
},
})
// error
m, err = gstr.Parse("v=111&v[]=m&v[]=a&v[]=b")
t.Log(err)
t.AssertNE(err, nil)
m, err = gstr.Parse("v=111&v[a]=m&v[a]=a")
t.Log(err)
t.AssertNE(err, nil)
_, err = gstr.Parse("%Q=%Q&b")
t.Log(err)
t.AssertNE(err, nil)
_, err = gstr.Parse("a=%Q&b")
t.Log(err)
t.AssertNE(err, nil)
_, err = gstr.Parse("v[a][a]=m&v[][a]=b")
t.Log(err)
t.AssertNE(err, nil)
})
// url
gtest.C(t, func(t *gtest.T) {
s := "goframe.org/index?name=john&score=100"