github.com/zhongdalu/gf@v1.0.0/g/text/gstr/gstr_z_unit_parse_test.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package gstr_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g"
    13  	"github.com/zhongdalu/gf/g/test/gtest"
    14  	"github.com/zhongdalu/gf/g/text/gstr"
    15  	"testing"
    16  )
    17  
    18  func Test_Parse(t *testing.T) {
    19  	gtest.Case(t, func() {
    20  		// slice
    21  		m, err := gstr.Parse("a[]=1&a[]=2")
    22  		gtest.Assert(err, nil)
    23  		gtest.Assert(m, g.Map{
    24  			"a": g.Slice{"1", "2"},
    25  		})
    26  		// map
    27  		m, err = gstr.Parse("a=1&b=2&c=3")
    28  		gtest.Assert(err, nil)
    29  		gtest.Assert(m, g.Map{
    30  			"a": "1",
    31  			"b": "2",
    32  			"c": "3",
    33  		})
    34  		// map
    35  		m, err = gstr.Parse("m[a]=1&m[b]=2&m[c]=3")
    36  		gtest.Assert(err, nil)
    37  		gtest.Assert(m, g.Map{
    38  			"m": g.Map{
    39  				"a": "1",
    40  				"b": "2",
    41  				"c": "3",
    42  			},
    43  		})
    44  		// map - slice
    45  		m, err = gstr.Parse("m[a][]=1&m[a][]=2")
    46  		gtest.Assert(err, nil)
    47  		gtest.Assert(m, g.Map{
    48  			"m": g.Map{
    49  				"a": g.Slice{"1", "2"},
    50  			},
    51  		})
    52  		// map - complicated
    53  		m, err = gstr.Parse("m[a1][b1][c1][d1]=1&m[a2][b2]=2&m[a3][b3][c3]=3")
    54  		gtest.Assert(err, nil)
    55  		gtest.Assert(m, g.Map{
    56  			"m": g.Map{
    57  				"a1": g.Map{
    58  					"b1": g.Map{
    59  						"c1": g.Map{
    60  							"d1": "1",
    61  						},
    62  					},
    63  				},
    64  				"a2": g.Map{
    65  					"b2": "2",
    66  				},
    67  				"a3": g.Map{
    68  					"b3": g.Map{
    69  						"c3": "3",
    70  					},
    71  				},
    72  			},
    73  		})
    74  	})
    75  }