| 145 | }` |
| 146 | |
| 147 | func TestPath(t *testing.T) { |
| 148 | json := basicJSON |
| 149 | r := Get(json, "@this") |
| 150 | path := r.Path(json) |
| 151 | if path != "@this" { |
| 152 | t.FailNow() |
| 153 | } |
| 154 | |
| 155 | r = Parse(json) |
| 156 | path = r.Path(json) |
| 157 | if path != "@this" { |
| 158 | t.FailNow() |
| 159 | } |
| 160 | |
| 161 | obj := Parse(json) |
| 162 | obj.ForEach(func(key, val Result) bool { |
| 163 | kp := key.Path(json) |
| 164 | assert(t, kp == "") |
| 165 | vp := val.Path(json) |
| 166 | if vp == "name" { |
| 167 | // there are two "name" keys |
| 168 | return true |
| 169 | } |
| 170 | val2 := obj.Get(vp) |
| 171 | assert(t, val2.Raw == val.Raw) |
| 172 | return true |
| 173 | }) |
| 174 | arr := obj.Get("loggy.programmers") |
| 175 | arr.ForEach(func(_, val Result) bool { |
| 176 | vp := val.Path(json) |
| 177 | val2 := Get(json, vp) |
| 178 | assert(t, val2.Raw == val.Raw) |
| 179 | return true |
| 180 | }) |
| 181 | get := func(path string) { |
| 182 | r1 := Get(json, path) |
| 183 | path2 := r1.Path(json) |
| 184 | r2 := Get(json, path2) |
| 185 | assert(t, r1.Raw == r2.Raw) |
| 186 | } |
| 187 | get("age") |
| 188 | get("name") |
| 189 | get("name.here") |
| 190 | get("noop") |
| 191 | get("noop.what is a wren?") |
| 192 | get("arr.0") |
| 193 | get("arr.1") |
| 194 | get("arr.2") |
| 195 | get("arr.3") |
| 196 | get("arr.3.hello") |
| 197 | get("arr.4") |
| 198 | get("arr.5") |
| 199 | get("loggy.programmers.2.email") |
| 200 | get("lastly.end\\.\\.\\.ing") |
| 201 | get("lastly.yay") |
| 202 | |
| 203 | } |
| 204 | |