jsoncompare

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2019 License: MIT Imports: 4 Imported by: 1

README

jsoncompare

Build Status codecov Go Report Card GoDoc

jsoncompare is a helper utility to compare two byte slices containing json with each other. The matching behaviour is configurable. This is mostly useful for assertions in tests where we want to validate that a given haystack contains a needle but we do not mind if the haystack contains additional data not present in needle.

Installation

go get -u github.com/martinohmann/jsoncompare

Usage

See example_test.go for usage examples.

License

The source code of jsoncompare is released under the MIT License. See the bundled LICENSE file for details.

Documentation

Overview

Package jsoncompare is a helper utility to compare two byte slices containing json with each other. The matching behaviour is configurable. This is mostly useful for assertions in tests where we want to validate that a given haystack contains a needle but we do not mind if the haystack contains additional data not present in needle.

Index

Examples

Constants

View Source
const (
	// MatchSubtree will just match that the given needle is present, ignoring
	// any excess slice and map elements in haystack. Also the order of slice
	// elements is ignored.
	MatchSubtree MatchMode = 0

	// MatchMapLen will assert that maps in needle and haystack have the same
	// length.
	// MatchSliceLen works the same as MatchMapLen, only for slices.
	// MatchSliceOrder will make sure that slice ordering is that same in
	// needle and haystack.
	MatchMapLen MatchMode = 1 << iota
	MatchSliceLen
	MatchSliceOrder

	// MatchStrict will enforce exact matches of needle and haystack.
	MatchStrict = MatchMapLen | MatchSliceLen | MatchSliceOrder

	// MatchLenStrict will enforce that map and slice lengths are identical in
	// needle and haystack.
	MatchLenStrict = MatchMapLen | MatchSliceLen

	// MatchSliceStrict will enforce that slices are of the same lengths and
	// have the same order of elements.
	MatchSliceStrict = MatchSliceLen | MatchSliceOrder
)

Variables

View Source
var DefaultComparator = NewComparator(MatchStrict)

DefaultComparator is a Comparator that strictly compares needle and haystack for equality.

Functions

func Compare

func Compare(haystack, needle []byte) error

Compare checks if haystack contains needle. It uses the DefaultComparator.

Example
package main

import (
	"fmt"

	"github.com/martinohmann/jsoncompare"
)

func main() {
	needle := []byte(`{"foo": [1, 2]}`)
	haystack := []byte(`{"foo": ["1", "2"]}`)

	if err := jsoncompare.Compare(haystack, needle); err != nil {
		fmt.Println(err)
	}

}
Output:
$.foo[0]: type mismatch, expected float64, got string

Types

type Comparator

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

Comparator compares two json byte slices.

func NewComparator

func NewComparator(m MatchMode) *Comparator

NewComparator creates a new Comparator with MatchMode.

Example (MatchLenStrict)
package main

import (
	"fmt"

	"github.com/martinohmann/jsoncompare"
)

func main() {
	needle := []byte(`{"foo": [2, 1]}`)
	haystack := []byte(`{"foo": [1, 2, 3]}`)
	comparator := jsoncompare.NewComparator(jsoncompare.MatchLenStrict)

	if err := comparator.Compare(haystack, needle); err != nil {
		fmt.Println(err)
	}

}
Output:
$.foo: length mismatch, expected 2, got 3
Example (MatchSliceLen)
package main

import (
	"fmt"

	"github.com/martinohmann/jsoncompare"
)

func main() {
	needle := []byte(`{"foo": [3, 2, 1]}`)
	haystack := []byte(`{"foo": [1, 2, 3]}`)
	comparator := jsoncompare.NewComparator(jsoncompare.MatchSliceLen)

	if err := comparator.Compare(haystack, needle); err == nil {
		fmt.Println("haystack contains needle")
	}

}
Output:
haystack contains needle
Example (MatchSliceStrict)
package main

import (
	"fmt"

	"github.com/martinohmann/jsoncompare"
)

func main() {
	needle := []byte(`{"foo": [3, 2, 1]}`)
	haystack := []byte(`{"bar": "baz", "foo": [1, 2, 3]}`)
	comparator := jsoncompare.NewComparator(jsoncompare.MatchSliceStrict)

	if err := comparator.Compare(haystack, needle); err != nil {
		fmt.Println(err)
	}

}
Output:
$.foo[0]: value mismatch, expected 3, got 1
Example (MatchSubtree)
package main

import (
	"fmt"

	"github.com/martinohmann/jsoncompare"
)

func main() {
	needle := []byte(`{"foo": [2, 1]}`)
	haystack := []byte(`{"foo": [1, 2, 3]}`)
	comparator := jsoncompare.NewComparator(jsoncompare.MatchSubtree)

	if err := comparator.Compare(haystack, needle); err == nil {
		fmt.Println("haystack contains needle")
	}

}
Output:
haystack contains needle

func (*Comparator) Compare

func (c *Comparator) Compare(haystack, needle []byte) error

Compare checks if haystack contains needle. It takes the Comparator options into account when making assertions.

type ComparatorError

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

ComparatorError defines an error that can happen during Compare.

func (ComparatorError) Error

func (e ComparatorError) Error() string

Error implements the error interface.

type MatchMode

type MatchMode uint

MatchMode defines the type of flags to configure the matching behaviour.

Jump to

Keyboard shortcuts

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