MCPcopy
hub / github.com/tidwall/gjson / TestModifier

Function TestModifier

gjson_test.go:1380–1416  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1378}
1379
1380func TestModifier(t *testing.T) {
1381 json := `{"other":{"hello":"world"},"arr":[1,2,3,4,5,6]}`
1382 opts := *pretty.DefaultOptions
1383 opts.SortKeys = true
1384 exp := string(pretty.PrettyOptions([]byte(json), &opts))
1385 res := Get(json, `@pretty:{"sortKeys":true}`).String()
1386 if res != exp {
1387 t.Fatalf("expected '%v', got '%v'", exp, res)
1388 }
1389 res = Get(res, "@pretty|@reverse|@ugly").String()
1390 if res != json {
1391 t.Fatalf("expected '%v', got '%v'", json, res)
1392 }
1393 if res := Get(res, "@this").String(); res != json {
1394 t.Fatalf("expected '%v', got '%v'", json, res)
1395 }
1396 if res := Get(res, "other.@this").String(); res != `{"hello":"world"}` {
1397 t.Fatalf("expected '%v', got '%v'", json, res)
1398 }
1399 res = Get(res, "@pretty|@reverse|arr|@reverse|2").String()
1400 if res != "4" {
1401 t.Fatalf("expected '%v', got '%v'", "4", res)
1402 }
1403 AddModifier("case", func(json, arg string) string {
1404 if arg == "upper" {
1405 return strings.ToUpper(json)
1406 }
1407 if arg == "lower" {
1408 return strings.ToLower(json)
1409 }
1410 return json
1411 })
1412 res = Get(json, "other|@case:upper").String()
1413 if res != `{"HELLO":"WORLD"}` {
1414 t.Fatalf("expected '%v', got '%v'", `{"HELLO":"WORLD"}`, res)
1415 }
1416}
1417
1418func TestChaining(t *testing.T) {
1419 json := `{

Callers

nothing calls this directly

Calls 3

GetFunction · 0.85
AddModifierFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…