JSONPointer

func JSONPointer(pointer string, expectedValue interface{}) TestDeep

JSONPointer is a smuggler operator. It takes the JSON representation of data, gets the value corresponding to the JSON pointer pointer (as RFC 6901 specifies it) and compares it to expectedValue.

JSONPointer does its best to convert back the JSON pointed data to the type of expectedValue or to the type behind the expectedValue operator, if it is an operator. Allowing to do things like:

type Item struct {
  Val  int   `json:"val"`
  Next *Item `json:"next"`
}
got := Item{Val: 1, Next: &Item{Val: 2, Next: &Item{Val: 3}}}

td.Cmp(t, got, td.JSONPointer("/next/next", Item{Val: 3}))
td.Cmp(t, got, td.JSONPointer("/next/next", &Item{Val: 3}))
td.Cmp(t,
  got,
  td.JSONPointer("/next/next",
    td.Struct(Item{}, td.StructFields{"Val": td.Gte(3)})),
)

It does this conversion only if the expected type is a struct, a struct pointer or implements the encoding/json.Unmarshaler interface. In the case the conversion does not occur, the Lax mode is automatically enabled to simplify numeric tests.

got := map[string]int64{"zzz": 42} // 42 is int64 here
td.Cmp(t, got, td.JSONPointer("/zzz", 42))
td.Cmp(t, got, td.JSONPointer("/zzz", td.Between(40, 45)))

Note that as any TestDeep operator can be used as expectedValue, JSON operator works out of the box:

got := json.RawMessage(`{"foo":{"bar": {"zip": true}}}`)
td.Cmp(t, got, td.JSONPointer("/foo/bar", td.JSON(`{"zip": true}`)))

It can be used with structs lacking json tags. In this case, fields names have to be used in JSON pointer:

type Item struct {
  Val  int
  Next *Item
}
got := Item{Val: 1, Next: &Item{Val: 2, Next: &Item{Val: 3}}}

td.Cmp(t, got, td.JSONPointer("/Next/Next", Item{Val: 3}))

Contrary to Smuggle operator and its field-path feature, only public fields can be followed, as private ones are never (un)marshalled.

There is no JSONHas nor JSONHasnt operators to only check a JSON pointer exists or not, but they can easily be emulated:

JSONHas := func(pointer string) td.TestDeep {
  return td.JSONPointer(pointer, td.Ignore())
}

JSONHasnt := func(pointer string) td.TestDeep {
  return td.Not(td.JSONPointer(pointer, td.Ignore()))
}

TypeBehind method always returns nil as the expected type cannot be guessed from a JSON pointer.

See also JSONPointer godoc.

Examples

Rfc6901 example
Struct example
Has_hasnt example

CmpJSONPointer shortcut

func CmpJSONPointer(t TestingT, got interface{}, pointer string, expectedValue interface{}, args ...interface{}) bool

CmpJSONPointer is a shortcut for:

td.Cmp(t, got, td.JSONPointer(pointer, expectedValue), args...)

See above for details.

Returns true if the test is OK, false if it fails.

args… are optional and allow to name the test. This name is used in case of failure to qualify the test. If len(args) > 1 and the first item of args is a string and contains a ‘%’ rune then fmt.Fprintf is used to compose the name, else args are passed to fmt.Fprint. Do not forget it is the name of the test, not the reason of a potential failure.

See also CmpJSONPointer godoc.

Examples

Rfc6901 example
Struct example
Has_hasnt example

T.JSONPointer shortcut

func (t *T) JSONPointer(got interface{}, pointer string, expectedValue interface{}, args ...interface{}) bool

JSONPointer is a shortcut for:

t.Cmp(got, td.JSONPointer(pointer, expectedValue), args...)

See above for details.

Returns true if the test is OK, false if it fails.

args… are optional and allow to name the test. This name is used in case of failure to qualify the test. If len(args) > 1 and the first item of args is a string and contains a ‘%’ rune then fmt.Fprintf is used to compose the name, else args are passed to fmt.Fprint. Do not forget it is the name of the test, not the reason of a potential failure.

See also T.JSONPointer godoc.

Examples

Rfc6901 example
Struct example
Has_hasnt example