2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-01-26 16:36:07 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-01-26 16:36:07 +08:00
|
|
|
|
|
|
|
package gparser_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"bytes"
|
|
|
|
"testing"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/encoding/gparser"
|
2018-01-26 16:36:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Set1(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"k1":{"k11":[1,2,3]},"k2":"v2"}`)
|
|
|
|
p := gparser.New(map[string]string{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
|
|
|
})
|
|
|
|
p.Set("k1.k11", []int{1, 2, 3})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, []byte(`{"k1":{"k11":[1,2,3]},"k2":"v2"}`)) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set2(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`[[null,1]]`)
|
|
|
|
p := gparser.New([]string{"a"})
|
|
|
|
p.Set("0.1", 1)
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set3(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"kv":{"k1":"v1"}}`)
|
|
|
|
p := gparser.New([]string{"a"})
|
|
|
|
p.Set("kv", map[string]string{
|
|
|
|
"k1": "v1",
|
|
|
|
})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set4(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`["a",[{"k1":"v1"}]]`)
|
|
|
|
p := gparser.New([]string{"a"})
|
|
|
|
p.Set("1.0", map[string]string{
|
|
|
|
"k1": "v1",
|
|
|
|
})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set5(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`[[[[[[[[[[[[[[[[[[[[[1,2,3]]]]]]]]]]]]]]]]]]]]]`)
|
|
|
|
p := gparser.New([]string{"a"})
|
|
|
|
p.Set("0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", []int{1, 2, 3})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set6(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`["a",[1,2,3]]`)
|
|
|
|
p := gparser.New([]string{"a"})
|
|
|
|
p.Set("1", []int{1, 2, 3})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set7(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"0":[null,[1,2,3]],"k1":"v1","k2":"v2"}`)
|
|
|
|
p := gparser.New(map[string]string{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
|
|
|
})
|
|
|
|
p.Set("0.1", []int{1, 2, 3})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set8(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"0":[[[[[[null,[1,2,3]]]]]]],"k1":"v1","k2":"v2"}`)
|
|
|
|
p := gparser.New(map[string]string{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
|
|
|
})
|
|
|
|
p.Set("0.0.0.0.0.0.1", []int{1, 2, 3})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set9(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"k1":[null,[1,2,3]],"k2":"v2"}`)
|
|
|
|
p := gparser.New(map[string]string{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
|
|
|
})
|
|
|
|
p.Set("k1.1", []int{1, 2, 3})
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
}
|
|
|
|
|
2018-04-12 14:09:33 +08:00
|
|
|
func Test_Set10(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"a":{"b":{"c":1}}}`)
|
|
|
|
p := gparser.New(nil)
|
|
|
|
p.Set("a.b.c", 1)
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-04-12 14:09:33 +08:00
|
|
|
}
|
2018-01-26 16:36:07 +08:00
|
|
|
|
2018-04-12 17:34:03 +08:00
|
|
|
func Test_Set11(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"a":{"b":{}}}`)
|
|
|
|
p, _ := gparser.LoadContent([]byte(`{"a":{"b":{"c":1}}}`))
|
|
|
|
p.Remove("a.b.c")
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-04-12 17:34:03 +08:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:02:07 +08:00
|
|
|
func Test_Set12(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`[0,1]`)
|
|
|
|
p := gparser.New(nil)
|
|
|
|
p.Set("0", 0)
|
|
|
|
p.Set("1", 1)
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-09-14 17:02:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Set13(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"array":[0,1]}`)
|
|
|
|
p := gparser.New(nil)
|
|
|
|
p.Set("array.0", 0)
|
|
|
|
p.Set("array.1", 1)
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-09-14 17:02:07 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 23:33:27 +08:00
|
|
|
func Test_Set14(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
e := []byte(`{"f":{"a":1}}`)
|
|
|
|
p := gparser.New(nil)
|
|
|
|
p.Set("f", "m")
|
|
|
|
p.Set("f.a", 1)
|
|
|
|
if c, err := p.ToJson(); err == nil {
|
|
|
|
if bytes.Compare(c, e) != 0 {
|
|
|
|
t.Error("expect:", string(e))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-04-02 23:33:27 +08:00
|
|
|
}
|