forked from kinbiko/jsonassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexports.go
More file actions
91 lines (83 loc) · 3.5 KB
/
exports.go
File metadata and controls
91 lines (83 loc) · 3.5 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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.
package jsonassert
import (
"fmt"
)
// Printer is any type that has a testing.T-like Errorf function.
// You probably want to pass in a *testing.T instance here if you are using
// this in your tests.
type Printer interface {
Errorf(msg string, args ...interface{})
}
// Asserter represents the main type within the jsonassert package.
// See Asserter.Assertf for the main use of this package.
type Asserter struct {
tt
partial bool
}
// 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)
func New(p Printer) *Asserter {
// Initially this package was written without the assumption that the
// provided Printer will implement testing.tt, which includes the Helper()
// function to get better stacktraces in your testing utility functions.
// This assumption was later added in order to get more accurate stackframe
// information in test failures. In most cases users will pass in a
// *testing.T to this function, which does adhere to that interface.
// However, in order to be backwards compatible we also permit the use of
// printers that do not implement Helper(). This is done by wrapping the
// provided Printer into another struct that implements a NOOP Helper
// method.
if t, ok := p.(tt); ok {
return &Asserter{tt: t}
}
return &Asserter{tt: &noopHelperTT{Printer: p}}
}
// 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")
//
// Additionally, 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.
func (a *Asserter) Assertf(actualJSON, expectedJSON string, fmtArgs ...interface{}) {
a.tt.Helper()
a.pathassertf("$", actualJSON, fmt.Sprintf(expectedJSON, fmtArgs...))
}
// Assert is identical to Assertf but without format args
func (a *Asserter) Assert(actualJSON, expectedJSON string) {
a.tt.Helper()
a.pathassertf("$", actualJSON, expectedJSON)
}
// AssertContainsf asserts that the JSON object only contains the expected elements
func (a *Asserter) AssertContainsf(actualJSON, expectedJSON string, fmtArgs ...interface{}) {
a.partial = true
a.Assertf(actualJSON, expectedJSON, fmtArgs...)
}
// AssertContains is identical to AssertContainsf but without format args
func (a *Asserter) AssertContains(actualJSON, expectedJSON string) {
a.partial = true
a.Assert(actualJSON, expectedJSON)
}