jsonassert

package module
v0.0.0-...-4818cf6 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2016 License: Apache-2.0 Imports: 4 Imported by: 0

README

JSONAssert for GO

Write JSON unit tests in less code. Great for testing REST interfaces.

This is a port from the Java library https://github.com/skyscreamer/JSONassert

Usage

package test

import (
    "testing"
	. "github.com/matpimenta/go-jsonassert"
)

func TestLenientAssertion(t *testing.T) {
    AssertJSONEquals(t, `{ "name": "John" }`, `{ "name": "John", "surname": "Smith" }`, false)
}

func TestStrictAssertion(t *testing.T) {
    AssertJSONEquals(t, `{ "name": "John" }`, `{ "name": "John" }`, true)
}

Gomega Matcher

package test

import (
    "testing"
	. "github.com/onsi/gomega"
)

func TestLenientlyMatchJSON(t *testing.T) {
    RegisterTestingT(t)
    Expect(`{"url": "URL 1", "name": "John"}`).To(LenientlyMatchJSON(`{"url": "URL 1"}`))
}

func TestStrictlyMatchJSON(t *testing.T) {
    RegisterTestingT(t)
    Expect(`{"url": "URL 1"}`).To(StrictlyMatchJSON(`{"url": "URL 1"}`))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertJSONEquals

func AssertJSONEquals(t Testing, expected, actual string, strict bool)

func AssertJSONNotEquals

func AssertJSONNotEquals(t Testing, expected, actual string, strict bool)

Types

type DefaultComparator

type DefaultComparator struct {
	// contains filtered or unexported fields
}

func (*DefaultComparator) CheckJsonObjectKeysActualInExpected

func (comp *DefaultComparator) CheckJsonObjectKeysActualInExpected(prefix string, expected, actual JSONNode, result *JSONCompareResult)

func (*DefaultComparator) CheckJsonObjectKeysExpectedInActual

func (comp *DefaultComparator) CheckJsonObjectKeysExpectedInActual(prefix string, expected, actual JSONNode, result *JSONCompareResult)

func (*DefaultComparator) CompareJSONArray

func (comp *DefaultComparator) CompareJSONArray(expected, actual JSONNode) *JSONCompareResult

func (*DefaultComparator) CompareJSONArrayWithPrefix

func (comp *DefaultComparator) CompareJSONArrayWithPrefix(prefix string, expected, actual JSONNode, result *JSONCompareResult)

func (*DefaultComparator) CompareJSONArrayWithStrictOrder

func (comp *DefaultComparator) CompareJSONArrayWithStrictOrder(key string, expected, actual JSONNode, result *JSONCompareResult)

func (*DefaultComparator) CompareJSONObject

func (comp *DefaultComparator) CompareJSONObject(expected, actual JSONNode) *JSONCompareResult

func (*DefaultComparator) CompareJSONObjectWithPrefix

func (comp *DefaultComparator) CompareJSONObjectWithPrefix(prefix string, expected, actual JSONNode, result *JSONCompareResult)

func (*DefaultComparator) CompareValues

func (comp *DefaultComparator) CompareValues(prefix string, expected, actual interface{}, result *JSONCompareResult)

func (*DefaultComparator) CompareValuesJSONNode

func (comp *DefaultComparator) CompareValuesJSONNode(prefix string, expected, actual JSONNode, result *JSONCompareResult)

func (*DefaultComparator) RecursivelyCompareJSONArray

func (comp *DefaultComparator) RecursivelyCompareJSONArray(key string, expected, actual JSONNode, result *JSONCompareResult)

type JSONAssertMatcher

type JSONAssertMatcher struct {
	// contains filtered or unexported fields
}

func LenientlyMatchJSON

func LenientlyMatchJSON(expected interface{}) *JSONAssertMatcher

func StrictlyMatchJSON

func StrictlyMatchJSON(expected interface{}) *JSONAssertMatcher

func (*JSONAssertMatcher) FailureMessage

func (m *JSONAssertMatcher) FailureMessage(actual interface{}) (message string)

func (*JSONAssertMatcher) Match

func (m *JSONAssertMatcher) Match(actual interface{}) (success bool, err error)

func (*JSONAssertMatcher) NegatedFailureMessage

func (m *JSONAssertMatcher) NegatedFailureMessage(actual interface{}) (message string)

type JSONComparator

type JSONComparator interface {
	CompareJSONObject(expected, actual JSONNode) *JSONCompareResult
	CompareJSONArray(expected, actual JSONNode) *JSONCompareResult
	CompareJSONObjectWithPrefix(prefix string, expected, actual JSONNode, result *JSONCompareResult)
	CompareJSONArrayWithPrefix(prefix string, expected, actual JSONNode, result *JSONCompareResult)
	CompareValues(prefix string, expected interface{}, actual interface{}, result *JSONCompareResult)
}

type JSONCompare

type JSONCompare struct {
}

func NewJSONCompare

func NewJSONCompare() JSONCompare

func (JSONCompare) CompareJSON

func (comp JSONCompare) CompareJSON(expectedStr string, actualStr string, compareMode JSONCompareMode) *JSONCompareResult

type JSONCompareMode

type JSONCompareMode int
const (
	STRICT JSONCompareMode = iota
	LENIENT
	NON_EXTENSIBLE
	STRICT_ORDER
)

type JSONCompareResult

type JSONCompareResult struct {
	// contains filtered or unexported fields
}

func NewJSONCompareResult

func NewJSONCompareResult() *JSONCompareResult

func (*JSONCompareResult) Fail

func (res *JSONCompareResult) Fail(prefix string, expected interface{}, actual interface{})

func (*JSONCompareResult) FailWithMessage

func (res *JSONCompareResult) FailWithMessage(message string)

func (*JSONCompareResult) Failed

func (res *JSONCompareResult) Failed() bool

func (*JSONCompareResult) GetMessage

func (res *JSONCompareResult) GetMessage() string

func (*JSONCompareResult) Missing

func (res *JSONCompareResult) Missing(field string, object interface{})

func (*JSONCompareResult) Passed

func (res *JSONCompareResult) Passed() bool

func (*JSONCompareResult) Unexpected

func (res *JSONCompareResult) Unexpected(message string, object interface{})

type JSONNode

type JSONNode interface {
	IsMap() bool
	IsArray() bool
	GetData() interface{}
	SetData(data interface{})
	SetArray(array []interface{})
	GetArray() []interface{}
	GetSize() int
	GetMap() map[string]interface{}
	CheckGet(key string) (JSONNode, bool)
	Set(key string, value interface{})
	String() string
}

func NewJSONNode

func NewJSONNode() JSONNode

func NewJSONNodeFromArray

func NewJSONNodeFromArray(array []interface{}) JSONNode

func NewJSONNodeFromMap

func NewJSONNodeFromMap(mymap map[string]interface{}) JSONNode

func NewJSONNodeFromString

func NewJSONNodeFromString(jsonStr string) (JSONNode, error)

type SimpleJSONNode

type SimpleJSONNode struct {
	// contains filtered or unexported fields
}

func (*SimpleJSONNode) CheckGet

func (s *SimpleJSONNode) CheckGet(key string) (JSONNode, bool)

func (*SimpleJSONNode) GetArray

func (s *SimpleJSONNode) GetArray() []interface{}

func (*SimpleJSONNode) GetData

func (s *SimpleJSONNode) GetData() interface{}

func (*SimpleJSONNode) GetMap

func (s *SimpleJSONNode) GetMap() map[string]interface{}

func (*SimpleJSONNode) GetSize

func (s *SimpleJSONNode) GetSize() int

func (*SimpleJSONNode) IsArray

func (s *SimpleJSONNode) IsArray() bool

func (*SimpleJSONNode) IsMap

func (s *SimpleJSONNode) IsMap() bool

func (*SimpleJSONNode) Set

func (s *SimpleJSONNode) Set(key string, value interface{})

func (*SimpleJSONNode) SetArray

func (s *SimpleJSONNode) SetArray(array []interface{})

func (*SimpleJSONNode) SetData

func (s *SimpleJSONNode) SetData(data interface{})

func (*SimpleJSONNode) String

func (s *SimpleJSONNode) String() string

type Testing

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

Jump to

Keyboard shortcuts

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