assertjson

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 5 Imported by: 20

README

JSON assertions

Build Status Coverage Status GoDoc

This library extends awesome github.com/stretchr/testify/assert with nice JSON equality assertions built with github.com/yudai/gojsondiff.

Usage

Default comparer is set up to ignore difference against "<ignore-diff>" values. It is accessible with package function Equal.

package my_test

import (
	"testing"

	"github.com/swaggest/assertjson"
)

func Test(t *testing.T) {
	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:	....
	//	Error:      	Not equal:
	//	            	 {
	//	            	   "a": [
	//	            	     1,
	//	            	     {
	//	            	       "val": "<ignore-diff>"
	//	            	     },
	//	            	     3
	//	            	   ],
	//	            	-  "b": 2,
	//	            	+  "b": 3,
	//	            	-  "c": 3
	//	            	+  "c": 2
	//	            	 }
}

Custom Comparer can be created and used to control ignore behavior and formatter options.

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:90
	            				equal.go:33
	            				example_test.go:14
	Error:      	Not equal:
	            	 {
	            	   "a": [
	            	     1,
	            	     {
	            	       "val": "<ignore-diff>"
	            	     },
	            	     3
	            	   ],
	            	-  "b": 2,
	            	+  "b": 3,
	            	-  "c": 3
	            	+  "c": 2
	            	 }

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool

Equal compares two JSON documents ignoring string values "<ignore-diff>".

Types

type Comparer

type Comparer struct {
	// IgnoreDiff is a value in expected document to ignore difference with actual document.
	IgnoreDiff string

	// FormatterConfig controls diff formatter configuration.
	FormatterConfig formatter.AsciiFormatterConfig
}

Comparer compares JSON documents.

func (Comparer) Equal

func (c Comparer) Equal(t TestingT, expected, actual []byte, msgAndArgs ...interface{}) bool

Equal compares two JSON payloads.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL