2019-03-21 00:14:23 +08:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
// go test *.go -bench=".*"
|
|
|
|
|
|
|
|
package gstr_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
2019-03-21 00:14:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Parse(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Case(t, func() {
|
2019-09-18 23:20:45 +08:00
|
|
|
// name overwrite
|
|
|
|
m, err := gstr.Parse("a=1&a=2")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(m, g.Map{
|
|
|
|
"a": 2,
|
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
// slice
|
2019-09-18 23:20:45 +08:00
|
|
|
m, err = gstr.Parse("a[]=1&a[]=2")
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(m, g.Map{
|
|
|
|
"a": g.Slice{"1", "2"},
|
|
|
|
})
|
|
|
|
// map
|
|
|
|
m, err = gstr.Parse("a=1&b=2&c=3")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(m, g.Map{
|
|
|
|
"a": "1",
|
|
|
|
"b": "2",
|
|
|
|
"c": "3",
|
|
|
|
})
|
|
|
|
// map
|
|
|
|
m, err = gstr.Parse("m[a]=1&m[b]=2&m[c]=3")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(m, g.Map{
|
|
|
|
"m": g.Map{
|
|
|
|
"a": "1",
|
|
|
|
"b": "2",
|
|
|
|
"c": "3",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// map - slice
|
|
|
|
m, err = gstr.Parse("m[a][]=1&m[a][]=2")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(m, g.Map{
|
|
|
|
"m": g.Map{
|
|
|
|
"a": g.Slice{"1", "2"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// map - complicated
|
|
|
|
m, err = gstr.Parse("m[a1][b1][c1][d1]=1&m[a2][b2]=2&m[a3][b3][c3]=3")
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.Assert(m, g.Map{
|
|
|
|
"m": g.Map{
|
|
|
|
"a1": g.Map{
|
|
|
|
"b1": g.Map{
|
|
|
|
"c1": g.Map{
|
|
|
|
"d1": "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"a2": g.Map{
|
|
|
|
"b2": "2",
|
|
|
|
},
|
|
|
|
"a3": g.Map{
|
|
|
|
"b3": g.Map{
|
|
|
|
"c3": "3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2019-03-21 00:14:23 +08:00
|
|
|
}
|