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

Function TestQueries

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

Source from the content-addressed store, hash-verified

1646}
1647
1648func TestQueries(t *testing.T) {
1649 json := `{
1650 "info": {
1651 "friends": [
1652 {
1653 "first": "Dale", "last": "Murphy", "kind": "Person",
1654 "cust1": true,
1655 "extra": [10,20,30],
1656 "details": {
1657 "city": "Tempe",
1658 "state": "Arizona"
1659 }
1660 },
1661 {
1662 "first": "Roger", "last": "Craig", "kind": "Person",
1663 "cust2": false,
1664 "extra": [40,50,60],
1665 "details": {
1666 "city": "Phoenix",
1667 "state": "Arizona"
1668 }
1669 }
1670 ]
1671 }
1672 }`
1673
1674 // numbers
1675 assert(t, Get(json, "i*.f*.#[extra.0<11].first").Exists())
1676 assert(t, Get(json, "i*.f*.#[extra.0<=11].first").Exists())
1677 assert(t, !Get(json, "i*.f*.#[extra.0<10].first").Exists())
1678 assert(t, Get(json, "i*.f*.#[extra.0<=10].first").Exists())
1679 assert(t, Get(json, "i*.f*.#[extra.0=10].first").Exists())
1680 assert(t, !Get(json, "i*.f*.#[extra.0=11].first").Exists())
1681 assert(t, Get(json, "i*.f*.#[extra.0!=10].first").String() == "Roger")
1682 assert(t, Get(json, "i*.f*.#[extra.0>10].first").String() == "Roger")
1683 assert(t, Get(json, "i*.f*.#[extra.0>=10].first").String() == "Dale")
1684
1685 // strings
1686 assert(t, Get(json, `i*.f*.#[extra.0<"11"].first`).Exists())
1687 assert(t, Get(json, `i*.f*.#[first>"Dale"].last`).String() == "Craig")
1688 assert(t, Get(json, `i*.f*.#[first>="Dale"].last`).String() == "Murphy")
1689 assert(t, Get(json, `i*.f*.#[first="Dale"].last`).String() == "Murphy")
1690 assert(t, Get(json, `i*.f*.#[first!="Dale"].last`).String() == "Craig")
1691 assert(t, !Get(json, `i*.f*.#[first<"Dale"].last`).Exists())
1692 assert(t, Get(json, `i*.f*.#[first<="Dale"].last`).Exists())
1693 assert(t, Get(json, `i*.f*.#[first%"Da*"].last`).Exists())
1694 assert(t, Get(json, `i*.f*.#[first%"Dale"].last`).Exists())
1695 assert(t, Get(json, `i*.f*.#[first%"*a*"]#|#`).String() == "1")
1696 assert(t, Get(json, `i*.f*.#[first%"*e*"]#|#`).String() == "2")
1697 assert(t, Get(json, `i*.f*.#[first!%"*e*"]#|#`).String() == "0")
1698
1699 // trues
1700 assert(t, Get(json, `i*.f*.#[cust1=true].first`).String() == "Dale")
1701 assert(t, Get(json, `i*.f*.#[cust2=false].first`).String() == "Roger")
1702 assert(t, Get(json, `i*.f*.#[cust1!=false].first`).String() == "Dale")
1703 assert(t, Get(json, `i*.f*.#[cust2!=true].first`).String() == "Roger")
1704 assert(t, !Get(json, `i*.f*.#[cust1>true].first`).Exists())
1705 assert(t, Get(json, `i*.f*.#[cust1>=true].first`).Exists())

Callers

nothing calls this directly

Calls 4

assertFunction · 0.85
GetFunction · 0.85
ExistsMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…