github.com/zhongdalu/gf@v1.0.0/g/encoding/gjson/gjson_z_unit_set_test.go (about)

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