-
Notifications
You must be signed in to change notification settings - Fork 16
/
examples_test.go
59 lines (50 loc) · 1.39 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package jsonassert_test
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 ExampleNew() {
ja := jsonassert.New(t)
ja.Assertf(`{"hello":"world"}`, `
{
"hello": "world"
}`)
}
func ExampleAsserter_Assertf_formatArguments() {
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%'
}
func ExampleAsserter_Assertf_presenceOnly() {
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 '$'
}
func ExampleAsserter_Assertf_unorderedArray() {
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
}