cmpjson

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 4 Imported by: 0

README

cmpjson

A simple package for Go that compares JSON for testing purposes.

To determine whether json is equal,github.com/evanphx/json-patch/v5 is utilized. If the json is not equal, a diff is created utilizing github.com/wI2L/jsondiff.

Usage

package main

import (
	"testing"

	"github.com/chanced/cmpjson"
)

func TestSomething(t *testing.T) {
	dataA := []byte(`{
		"fieldA": ["1", "2", "3"],
		"fieldB": "str"
	}`)
	dataB := []byte(`{
		"fieldB": "str",
		"fieldA": ["2","3"]
	}`)
	if ok, diff, err := cmpjson.Equal(dataA, dataB); !ok {
		t.Error(diff, err)
	}
	// alternatively:
	if ok, diff := cmpjson.MustEqual(dataA, dataB); !ok {
		t.Error("dataA and dataB were not equal:", diff)
	}
}


Prints:


=== RUN   TestSomething
    /cmpjson/examples_test.go:84: [
          {
            "op": "remove",
            "path": "/fieldA/2"
          },
          {
            "op": "replace",
            "path": "/fieldA/0",
            "value": "2"
          },
          {
            "op": "replace",
            "path": "/fieldA/1",
            "value": "3"
          }
        ] <nil>
    /cmpjson/examples_test.go:88: dataA and dataB were not equal: [
          {
            "op": "remove",
            "path": "/fieldA/2"
          },
          {
            "op": "replace",
            "path": "/fieldA/0",
            "value": "2"
          },
          {
            "op": "replace",
            "path": "/fieldA/1",
            "value": "3"
          }
        ]
--- FAIL: TestSomething (0.00s)
FAIL
FAIL	github.com/chanced/cmpjson	0.245s

https://play.golang.org/p/VMrggnGiydw

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(a interface{}, b interface{}) ([]byte, error)

func Equal

func Equal(a interface{}, b interface{}) (bool, string, error)

Equal checks the equality of json.

The intended purpose is for unit testing equality of json. It is not intended to be used outside of that purpose.

Example
package main

import (
	"fmt"

	"github.com/chanced/cmpjson"
)

func main() {
	dataA := []byte(`{
		"fieldA": ["1", "2", "3"],
		"fieldB": "str"
	}`)
	dataB := []byte(`{
		"fieldB": "str",
		"fieldA": ["2","3"]
	}`)
	if ok, diff, err := cmpjson.Equal(dataA, dataB); !ok {
		fmt.Println(diff)

		if err != nil {
			fmt.Println(err)
		}
	}
}
Output:

[
  {
    "op": "remove",
    "path": "/fieldA/2"
  },
  {
    "op": "replace",
    "path": "/fieldA/0",
    "value": "2"
  },
  {
    "op": "replace",
    "path": "/fieldA/1",
    "value": "3"
  }
]

func MustDiff

func MustDiff(a interface{}, b interface{}) []byte

MustDiff returns a JSON patch of the differences between of a and b.

MustDiff panics if there is an error marshaling a or b (if necessary) or performing the diff

func MustEqual

func MustEqual(a interface{}, b interface{}) (bool, string)

MustEqual checks the equality of json.

It panics if there is an error marshaling or unmarshaling to keep the interface clean & simple.

The intended purpose is for unit testing equality of json. It is not intended to be used outside of that purpose.

Example
package main

import (
	"fmt"

	"github.com/chanced/cmpjson"
)

func main() {
	dataA := map[string]interface{}{
		"fieldA": []interface{}{"1", "2", "3"},
		"fieldB": "initial",
	}
	dataB := map[string]interface{}{
		"fieldA": []interface{}{"1", "2"},
		"fieldB": "newval",
	}

	if ok, diff := cmpjson.MustEqual(dataA, dataB); !ok {
		fmt.Println(diff)
	} else {
		fmt.Println("dataA and dataB were equal")
	}
}
Output:

[
  {
    "op": "remove",
    "path": "/fieldA/2"
  },
  {
    "op": "replace",
    "path": "/fieldB",
    "value": "newval"
  }
]

Types

This section is empty.

Jump to

Keyboard shortcuts

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