Documentation
¶
Overview ¶
Package assertjson implements JSON equality assertion for tests.
Example ¶
package main import ( "fmt" "github.com/swaggest/assertjson" ) var t = testingT(func(format string, args ...interface{}) { fmt.Printf(format, args...) }) func main() { assertjson.Equal(t, []byte(`{"a": [1, {"val": "<ignore-diff>"}, 3], "b": 2, "c": 3}`), []byte(`{"a": [1, {"val": 123}, 3], "c": 2, "b": 3}`), ) }
Output: Error Trace: equal.go:88 equal.go:63 example_test.go:14 Error: Not equal: { "a": [ 1, { "val": "<ignore-diff>" }, 3 ], - "b": 2, + "b": 3, - "c": 3 + "c": 2 }
Index ¶
- Constants
- func EqMarshal(t TestingT, expected string, actualValue interface{}, ...) bool
- func Equal(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool
- func EqualMarshal(t TestingT, expected []byte, actualValue interface{}, ...) bool
- func FailMismatch(expected, actual []byte) error
- func FailMismatchMarshal(expected []byte, actualValue interface{}) error
- func FailNotEqual(expected, actual []byte) error
- func FailNotEqualMarshal(expected []byte, actualValue interface{}) error
- func MarshalIndentCompact(v interface{}, prefix, indent string, lineLen int) ([]byte, error)
- func Matches(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool
- func MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, ...) bool
- type Comparer
- func (c Comparer) Equal(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool
- func (c Comparer) EqualMarshal(t TestingT, expected []byte, actualValue interface{}, ...) bool
- func (c Comparer) FailMismatch(expected, actual []byte) error
- func (c Comparer) FailMismatchMarshal(expected []byte, actualValue interface{}) error
- func (c Comparer) FailNotEqual(expected, actual []byte) error
- func (c Comparer) FailNotEqualMarshal(expected []byte, actualValue interface{}) error
- func (c Comparer) Matches(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool
- func (c Comparer) MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, ...) bool
- type TestingT
Examples ¶
Constants ¶
const IgnoreDiff = "<ignore-diff>"
IgnoreDiff is a marker to ignore difference in JSON.
Variables ¶
This section is empty.
Functions ¶
func EqMarshal ¶ added in v1.9.0
func EqMarshal(t TestingT, expected string, actualValue interface{}, msgAndArgs ...interface{}) bool
EqMarshal marshals actual value and compares two JSON documents ignoring string values "<ignore-diff>".
func EqualMarshal ¶ added in v1.4.0
func EqualMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool
EqualMarshal marshals actual value and compares two JSON documents ignoring string values "<ignore-diff>".
func FailMismatch ¶ added in v1.8.0
FailMismatch returns error if expected JSON payload does not match actual JSON payload, nil otherwise. It ignores added fields in actual JSON payload.
func FailMismatchMarshal ¶ added in v1.8.0
FailMismatchMarshal returns error if expected JSON payload does not match marshaled actual value, nil otherwise. It ignores added fields in actual JSON payload.
func FailNotEqual ¶ added in v1.1.0
FailNotEqual returns error if JSON payloads are different, nil otherwise.
func FailNotEqualMarshal ¶ added in v1.7.0
FailNotEqualMarshal returns error if expected JSON payload is not equal to marshaled actual value.
func MarshalIndentCompact ¶ added in v1.2.0
MarshalIndentCompact applies indentation for large chunks of JSON and uses compact format for smaller ones.
Line length limits indented width of JSON structure, does not apply to long distinct scalars. This function is not optimized for performance, so it might be not a good fit for high load scenarios.
func Matches ¶ added in v1.8.0
Matches compares two JSON documents ignoring string values "<ignore-diff>". It ignores added fields in actual JSON payload.
func MatchesMarshal ¶ added in v1.8.0
func MatchesMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool
MatchesMarshal marshals actual value and compares two JSON documents ignoring string values "<ignore-diff>". It ignores added fields in actual JSON payload.
Types ¶
type Comparer ¶
type Comparer struct { // IgnoreDiff is a value in expected document to ignore difference with actual document. IgnoreDiff string // Vars keeps state of found variables. Vars *shared.Vars // FormatterConfig controls diff formatter configuration. FormatterConfig formatter.AsciiFormatterConfig // KeepFullDiff shows full diff in error message. KeepFullDiff bool // FullDiffMaxLines is a maximum number of lines to show without reductions, default 50. // Ignored if KeepFullDiff is true. FullDiffMaxLines int // DiffSurroundingLines is a number of lines to add before and after diff line, default 5. // Ignored if KeepFullDiff is true. DiffSurroundingLines int }
Comparer compares JSON documents.
func (Comparer) EqualMarshal ¶ added in v1.4.0
func (c Comparer) EqualMarshal(t TestingT, expected []byte, actualValue interface{}, msgAndArgs ...interface{}) bool
EqualMarshal marshals actual JSON payload and compares it with expected payload.
func (Comparer) FailMismatch ¶ added in v1.8.0
FailMismatch returns error if expected JSON payload does not match actual JSON payload, nil otherwise. It ignores added fields in actual JSON payload.
func (Comparer) FailMismatchMarshal ¶ added in v1.8.0
FailMismatchMarshal returns error if expected JSON payload does not match marshaled actual value, nil otherwise. It ignores added fields in actual JSON payload.
func (Comparer) FailNotEqual ¶ added in v1.1.0
FailNotEqual returns error if JSON payloads are different, nil otherwise.
func (Comparer) FailNotEqualMarshal ¶ added in v1.7.0
FailNotEqualMarshal returns error if expected JSON payload is not equal to marshaled actual value.
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
jsoncompact
Package main provides a tool to compact JSON.
|
Package main provides a tool to compact JSON. |
Package json5 provides JSON5 decoder.
|
Package json5 provides JSON5 decoder. |