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