github.com/gogf/gf@v1.16.9/encoding/gparser/gparser_unit_set_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  package gparser_test
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/encoding/gparser"
    14  )
    15  
    16  func Test_Set1(t *testing.T) {
    17  	e := []byte(`{"k1":{"k11":[1,2,3]},"k2":"v2"}`)
    18  	p := gparser.New(map[string]string{
    19  		"k1": "v1",
    20  		"k2": "v2",
    21  	})
    22  	p.Set("k1.k11", []int{1, 2, 3})
    23  	if c, err := p.ToJson(); err == nil {
    24  		if bytes.Compare(c, []byte(`{"k1":{"k11":[1,2,3]},"k2":"v2"}`)) != 0 {
    25  			t.Error("expect:", string(e))
    26  		}
    27  	} else {
    28  		t.Error(err)
    29  	}
    30  }
    31  
    32  func Test_Set2(t *testing.T) {
    33  	e := []byte(`[[null,1]]`)
    34  	p := gparser.New([]string{"a"})
    35  	p.Set("0.1", 1)
    36  	if c, err := p.ToJson(); err == nil {
    37  		if bytes.Compare(c, e) != 0 {
    38  			t.Error("expect:", string(e))
    39  		}
    40  	} else {
    41  		t.Error(err)
    42  	}
    43  }
    44  
    45  func Test_Set3(t *testing.T) {
    46  	e := []byte(`{"kv":{"k1":"v1"}}`)
    47  	p := gparser.New([]string{"a"})
    48  	p.Set("kv", map[string]string{
    49  		"k1": "v1",
    50  	})
    51  	if c, err := p.ToJson(); err == nil {
    52  		if bytes.Compare(c, e) != 0 {
    53  			t.Error("expect:", string(e))
    54  		}
    55  	} else {
    56  		t.Error(err)
    57  	}
    58  }
    59  
    60  func Test_Set4(t *testing.T) {
    61  	e := []byte(`["a",[{"k1":"v1"}]]`)
    62  	p := gparser.New([]string{"a"})
    63  	p.Set("1.0", map[string]string{
    64  		"k1": "v1",
    65  	})
    66  	if c, err := p.ToJson(); err == nil {
    67  		if bytes.Compare(c, e) != 0 {
    68  			t.Error("expect:", string(e))
    69  		}
    70  	} else {
    71  		t.Error(err)
    72  	}
    73  }
    74  
    75  func Test_Set5(t *testing.T) {
    76  	e := []byte(`[[[[[[[[[[[[[[[[[[[[[1,2,3]]]]]]]]]]]]]]]]]]]]]`)
    77  	p := gparser.New([]string{"a"})
    78  	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})
    79  	if c, err := p.ToJson(); err == nil {
    80  		if bytes.Compare(c, e) != 0 {
    81  			t.Error("expect:", string(e))
    82  		}
    83  	} else {
    84  		t.Error(err)
    85  	}
    86  }
    87  
    88  func Test_Set6(t *testing.T) {
    89  	e := []byte(`["a",[1,2,3]]`)
    90  	p := gparser.New([]string{"a"})
    91  	p.Set("1", []int{1, 2, 3})
    92  	if c, err := p.ToJson(); err == nil {
    93  		if bytes.Compare(c, e) != 0 {
    94  			t.Error("expect:", string(e))
    95  		}
    96  	} else {
    97  		t.Error(err)
    98  	}
    99  }
   100  
   101  func Test_Set7(t *testing.T) {
   102  	e := []byte(`{"0":[null,[1,2,3]],"k1":"v1","k2":"v2"}`)
   103  	p := gparser.New(map[string]string{
   104  		"k1": "v1",
   105  		"k2": "v2",
   106  	})
   107  	p.Set("0.1", []int{1, 2, 3})
   108  	if c, err := p.ToJson(); err == nil {
   109  		if bytes.Compare(c, e) != 0 {
   110  			t.Error("expect:", string(e))
   111  		}
   112  	} else {
   113  		t.Error(err)
   114  	}
   115  }
   116  
   117  func Test_Set8(t *testing.T) {
   118  	e := []byte(`{"0":[[[[[[null,[1,2,3]]]]]]],"k1":"v1","k2":"v2"}`)
   119  	p := gparser.New(map[string]string{
   120  		"k1": "v1",
   121  		"k2": "v2",
   122  	})
   123  	p.Set("0.0.0.0.0.0.1", []int{1, 2, 3})
   124  	if c, err := p.ToJson(); err == nil {
   125  		if bytes.Compare(c, e) != 0 {
   126  			t.Error("expect:", string(e))
   127  		}
   128  	} else {
   129  		t.Error(err)
   130  	}
   131  }
   132  
   133  func Test_Set9(t *testing.T) {
   134  	e := []byte(`{"k1":[null,[1,2,3]],"k2":"v2"}`)
   135  	p := gparser.New(map[string]string{
   136  		"k1": "v1",
   137  		"k2": "v2",
   138  	})
   139  	p.Set("k1.1", []int{1, 2, 3})
   140  	if c, err := p.ToJson(); err == nil {
   141  		if bytes.Compare(c, e) != 0 {
   142  			t.Error("expect:", string(e))
   143  		}
   144  	} else {
   145  		t.Error(err)
   146  	}
   147  }
   148  
   149  func Test_Set10(t *testing.T) {
   150  	e := []byte(`{"a":{"b":{"c":1}}}`)
   151  	p := gparser.New(nil)
   152  	p.Set("a.b.c", 1)
   153  	if c, err := p.ToJson(); err == nil {
   154  		if bytes.Compare(c, e) != 0 {
   155  			t.Error("expect:", string(e))
   156  		}
   157  	} else {
   158  		t.Error(err)
   159  	}
   160  }
   161  
   162  func Test_Set11(t *testing.T) {
   163  	e := []byte(`{"a":{"b":{}}}`)
   164  	p, _ := gparser.LoadContent([]byte(`{"a":{"b":{"c":1}}}`))
   165  	p.Remove("a.b.c")
   166  	if c, err := p.ToJson(); err == nil {
   167  		if bytes.Compare(c, e) != 0 {
   168  			t.Error("expect:", string(e))
   169  		}
   170  	} else {
   171  		t.Error(err)
   172  	}
   173  }
   174  
   175  func Test_Set12(t *testing.T) {
   176  	e := []byte(`[0,1]`)
   177  	p := gparser.New(nil)
   178  	p.Set("0", 0)
   179  	p.Set("1", 1)
   180  	if c, err := p.ToJson(); err == nil {
   181  		if bytes.Compare(c, e) != 0 {
   182  			t.Error("expect:", string(e))
   183  		}
   184  	} else {
   185  		t.Error(err)
   186  	}
   187  }
   188  
   189  func Test_Set13(t *testing.T) {
   190  	e := []byte(`{"array":[0,1]}`)
   191  	p := gparser.New(nil)
   192  	p.Set("array.0", 0)
   193  	p.Set("array.1", 1)
   194  	if c, err := p.ToJson(); err == nil {
   195  		if bytes.Compare(c, e) != 0 {
   196  			t.Error("expect:", string(e))
   197  		}
   198  	} else {
   199  		t.Error(err)
   200  	}
   201  }
   202  
   203  func Test_Set14(t *testing.T) {
   204  	e := []byte(`{"f":{"a":1}}`)
   205  	p := gparser.New(nil)
   206  	p.Set("f", "m")
   207  	p.Set("f.a", 1)
   208  	if c, err := p.ToJson(); err == nil {
   209  		if bytes.Compare(c, e) != 0 {
   210  			t.Error("expect:", string(e))
   211  		}
   212  	} else {
   213  		t.Error(err)
   214  	}
   215  }