jsonassert

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2019 License: MIT Imports: 6 Imported by: 6

README

jsonassert

Mentioned in Awesome Go Build Status Go Report Card Coverage Status Latest version Go Documentation License

jsonassert is a Go test assertion library for verifying that two representations of JSON are semantically equal.

Usage

Create a new *jsonassert.Asserter in your test and use this to make assertions against your JSON payloads:

func TestWhatever(t *testing.T) {
    ja := jsonassert.New(t)
    // find some sort of payload
    ja.Assertf(payload, `
    {
        "name": "%s",
        "age": %d,
        "skills": [
            { "name": "martial arts", "level": 99 },
            { "name": "intelligence", "level": 100 },
            { "name": "mental fortitude", "level": 4 }
        ]
    }`, "River Tam", 16)
}

You may pass in fmt.Sprintf arguments after the expected JSON structure.

ja.Assertf() currently supports assertions against strings only.

Check for presence only

Some properties of a JSON payload may be difficult to know in advance. E.g. timestamps, UUIDs, or other randomly assigned values.

For these types of values, place the string "<<PRESENCE>>" as the expected value, and jsonassert will only verify that this key exists (i.e. the actual JSON has the expected key, and its value is not null), but this does not check its value.

For example:

func TestWhatever(t *testing.T) {
    ja := jsonassert.New(t)
    ja.Assertf(`
    {
        "time": "2019-01-28T21:19:42",
        "uuid": "94ae1a31-63b2-4a55-a478-47764b60c56b"
    }`, `
    {
        "time": "<<PRESENCE>>",
        "uuid": "<<PRESENCE>>"

    }`)
}

The above will pass your test, but:

func TestWhatever(t *testing.T) {
    ja := jsonassert.New(t)
    ja.Assertf(`
    {
        "date": "2019-01-28T21:19:42",
        "uuid": null
    }`, `
    {
        "time": "<<PRESENCE>>",
        "uuid": "<<PRESENCE>>"
    }`)
}

The above will fail your tests because the time key was not present in the actual JSON, and the uuid was null.

Docs

You can find the GoDocs for this package here.

Contributing

Contributions are welcome. Please discuss feature requests in an issue before opening a PR.

Documentation

Overview

Package jsonassert is a Go test assertion library for verifying that two representations of JSON are semantically equal. Create a new `*jsonassert.Asserter` in your test and use this to make assertions against your JSON payloads.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Asserter

type Asserter struct {
	Printer Printer
}

Asserter represents the main type within the jsonassert package. See Asserter.Assertf for the main use of this package.

func New

func New(p Printer) *Asserter

New creates a new *jsonassert.Asserter for making assertions against JSON payloads. This type can be reused. I.e. if you are using jsonassert as part of your tests, you only need one *jsonassert.Asseter per (sub)test. In most cases, this will look something like

ja := jsonassert.New(t)

Example
package main

import (
	"fmt"

	"github.com/kinbiko/jsonassert"
)

type printer struct{}

func (p *printer) Errorf(format string, args ...interface{}) {
	fmt.Println(fmt.Sprintf(format, args...))
}

// using the varible name 't' to mimic a *testing.T variable
var t *printer

func main() {
	ja := jsonassert.New(t)
	ja.Assertf(`{"hello":"world"}`, `
		{
			"hello": "world"
		}`)
}
Output:

func (*Asserter) Assertf added in v0.2.0

func (a *Asserter) Assertf(actualJSON, expectedJSON string, fmtArgs ...interface{})

Assertf takes two strings, the first being the 'actual' JSON that you wish to make assertions against. The second string is the 'expected' JSON, which can be treated as a template for additional format arguments. If any discrepancies are found, these will be given to the Errorf function in the printer. E.g. for the JSON

{"hello": "world"}

you may use an expected JSON of

{"hello": "%s"}

along with the "world" format argument. For example:

ja.Assertf(`{"hello": "world"}`, `{"hello":"%s"}`, "world")

Additionally, you may wish to make assertions against the *presence* of a value, but not against its value. For example:

ja.Assertf(`{"uuid": "94ae1a31-63b2-4a55-a478-47764b60c56b"}`, `{"uuid":"<<PRESENCE>>"}`)

will verify that the UUID field is present, but does not check its actual value. You may use "<<PRESENCE>>" against any type of value. The only exception is null, which will result in an assertion failure.

Example (FormatArguments)
package main

import (
	"fmt"

	"github.com/kinbiko/jsonassert"
)

type printer struct{}

func (p *printer) Errorf(format string, args ...interface{}) {
	fmt.Println(fmt.Sprintf(format, args...))
}

// using the varible name 't' to mimic a *testing.T variable
var t *printer

func main() {
	ja := jsonassert.New(t)
	ja.Assertf(`{"hello":"世界"}`, `
		{
			"hello": "%s"
		}`, "world")
}
Output:

expected string at '$.hello' to be 'world' but was '世界'
Example (PresenceOnly)
package main

import (
	"fmt"

	"github.com/kinbiko/jsonassert"
)

type printer struct{}

func (p *printer) Errorf(format string, args ...interface{}) {
	fmt.Println(fmt.Sprintf(format, args...))
}

// using the varible name 't' to mimic a *testing.T variable
var t *printer

func main() {
	ja := jsonassert.New(t)
	ja.Assertf(`{"hi":"not the right key name"}`, `
		{
			"hello": "<<PRESENCE>>"
		}`)
}
Output:

unexpected object key(s) ["hi"] found at '$'
expected object key(s) ["hello"] missing at '$'

type Printer

type Printer interface {
	Errorf(msg string, args ...interface{})
}

Printer is any type that has a testing.T-like Errorf function. You probably want to pass in a *testing.T instance here if you are using this in your tests.

Jump to

Keyboard shortcuts

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