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 ¶
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 ¶
var DefaultComparator = NewComparator(MatchStrict)
DefaultComparator is a Comparator that strictly compares needle and haystack for equality.
Functions ¶
func Compare ¶
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.