Documentation
¶
Overview ¶
Package jsonassert is a Go test assertion library for verifying that two representations of JSON are semantically equal. Create a new
*jsonassert.Asserter
in your test and use this to make assertions against your JSON payloads:
ja := jsonassert.New(t)
E.g. for the JSON
{"hello": "world"}
you may use an expected JSON of
{"hello": "%s"}
along with the "world" format argument. For example:
ja.Assertf(`{"hello": "world"}`, `{"hello":"%s"}`, "world")
You may wish to make assertions against the *presence* of a value, but not against its value. For example:
ja.Assertf(`{"uuid": "94ae1a31-63b2-4a55-a478-47764b60c56b"}`, `{"uuid":"<<PRESENCE>>"}`)
will verify that the UUID field is present, but does not check its actual value. You may use "<<PRESENCE>>" against any type of value. The only exception is null, which will result in an assertion failure.
If you don't know / care about the order of the elements in an array in your payload, you can ignore the ordering:
payload := `["bar", "foo", "baz"]` ja.Assertf(payload, `["<<UNORDERED>>", "foo", "bar", "baz"]`)
The above will verify that "foo", "bar", and "baz" are exactly the elements in the payload, but will ignore the order in which they appear.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Asserter ¶
type Asserter struct {
// contains filtered or unexported fields
}
Asserter represents the main type within the jsonassert package. See Asserter.Assertf for the main use of this package.
func New ¶
New creates a new *jsonassert.Asserter for making assertions against JSON payloads. This type can be reused. I.e. if you are using jsonassert as part of your tests, you only need one *jsonassert.Asseter per (sub)test. In most cases, this will look something like
ja := jsonassert.New(t)
Example ¶
package main import ( "fmt" "github.com/kinbiko/jsonassert" ) type printer struct{} func (p *printer) Errorf(format string, args ...interface{}) { fmt.Println(fmt.Sprintf(format, args...)) } // using the varible name 't' to mimic a *testing.T variable // //nolint:gochecknoglobals // this is global to make the examples look like valid test code var t *printer func main() { ja := jsonassert.New(t) ja.Assertf(`{"hello":"world"}`, ` { "hello": "world" }`) }
Output:
func (*Asserter) Assert ¶
Assert works like Assertf, but does not accept fmt.Sprintf directives. See Assert for details.
func (*Asserter) Assertf ¶ added in v0.2.0
Assertf takes two strings, the first being the 'actual' JSON that you wish to make assertions against. The second string is the 'expected' JSON, which can be treated as a template for additional format arguments. If any discrepancies are found, these will be given to the Errorf function in the Printer. E.g. for the JSON
{"hello": "world"}
you may use an expected JSON of
{"hello": "%s"}
along with the "world" format argument. For example:
ja.Assertf(`{"hello": "world"}`, `{"hello":"%s"}`, "world")
You may also use format arguments in the case when your expected JSON contains a percent character, which would otherwise be interpreted as a format-directive.
ja.Assertf(`{"averageTestScore": "99%"}`, `{"averageTestScore":"%s"}`, "99%")
You may wish to make assertions against the *presence* of a value, but not against its value. For example:
ja.Assertf(`{"uuid": "94ae1a31-63b2-4a55-a478-47764b60c56b"}`, `{"uuid":"<<PRESENCE>>"}`)
will verify that the UUID field is present, but does not check its actual value. You may use "<<PRESENCE>>" against any type of value. The only exception is null, which will result in an assertion failure.
If you don't know / care about the order of the elements in an array in your payload, you can ignore the ordering:
payload := `["bar", "foo", "baz"]` ja.Assertf(payload, `["<<UNORDERED>>", "foo", "bar", "baz"]`)
The above will verify that "foo", "bar", and "baz" are exactly the elements in the payload, but will ignore the order in which they appear.
Example (FormatArguments) ¶
package main import ( "fmt" "github.com/kinbiko/jsonassert" ) type printer struct{} func (p *printer) Errorf(format string, args ...interface{}) { fmt.Println(fmt.Sprintf(format, args...)) } // using the varible name 't' to mimic a *testing.T variable // //nolint:gochecknoglobals // this is global to make the examples look like valid test code var t *printer func main() { ja := jsonassert.New(t) expTestScore := "28%" ja.Assertf( `{ "name": "Jayne Cobb", "age": 36, "averageTestScore": "88%" }`, `{ "name": "Jayne Cobb", "age": 36, "averageTestScore": "%s" }`, expTestScore, ) }
Output: expected string at '$.averageTestScore' to be '28%' but was '88%'
Example (PresenceOnly) ¶
package main import ( "fmt" "github.com/kinbiko/jsonassert" ) type printer struct{} func (p *printer) Errorf(format string, args ...interface{}) { fmt.Println(fmt.Sprintf(format, args...)) } // using the varible name 't' to mimic a *testing.T variable // //nolint:gochecknoglobals // this is global to make the examples look like valid test code var t *printer func main() { ja := jsonassert.New(t) ja.Assertf(`{"hi":"not the right key name"}`, ` { "hello": "<<PRESENCE>>" }`) }
Output: unexpected object key(s) ["hi"] found at '$' expected object key(s) ["hello"] missing at '$'
Example (UnorderedArray) ¶
package main import ( "fmt" "github.com/kinbiko/jsonassert" ) type printer struct{} func (p *printer) Errorf(format string, args ...interface{}) { fmt.Println(fmt.Sprintf(format, args...)) } // using the varible name 't' to mimic a *testing.T variable // //nolint:gochecknoglobals // this is global to make the examples look like valid test code var t *printer func main() { ja := jsonassert.New(t) ja.Assertf( `["zero", "one", "two"]`, `["<<UNORDERED>>", "one", "two", "three"]`, ) }
Output: actual JSON at '$[0]' contained an unexpected element: "zero" expected JSON at '$[2]': "three" was missing from actual payload