gojas

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2016 License: MIT Imports: 4 Imported by: 1

README

Gojas: Golang JSON Assertions

Values:

Compatible, Fast, Hackable, Usable, Native, Minimal, Readable.

Importing

To add to your project, import:

import "github.com/flowfaction/gojas"

There are no dependencies on 3rd party components, only native Go packages.

Pull Requests

Your PRs are welcome! The goal is to evolve this package toward more testing scenarios and reliability. Accepting issue posts as well, thank you.

Example

For most routine testing, just use the assertions in the package. For example

func TestSomething(t *testing.T) {

//...

    jsonDocumentAsString := LoadTheJsonFromSomewhere()

    // The path string represents a hierarchy within the json document.

    passed := gojas.AssertNumberAtPath(t, jsonDocumentAsString, "/user/properties/age", 42.0)

    // If the assertion does not pass, the 't' pointer will be used to set the test to Fail, with an error string.

    // There are several other assertions available.
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertBoolAtPath

func AssertBoolAtPath(t *testing.T, data, path string, asserted bool) (ok bool)

func AssertFloatArrayAtPath

func AssertFloatArrayAtPath(t *testing.T, data, path string, asserted []interface{}) (ok bool)

func AssertNumberAtPath

func AssertNumberAtPath(t *testing.T, data, path string, asserted float64) (ok bool)

func AssertObjectAtPath

func AssertObjectAtPath(t *testing.T, data, path string) (ok bool)

func AssertObjectAtPathWithKeys

func AssertObjectAtPathWithKeys(t *testing.T, data, path string, keys []string) (ok bool)

AssertObjectAtPathWithKeys attempts to locate an object (a JSON doc) at a given path. Given that there is an object there, it will assert that ALL the given property keys are found in the object, ignoring their value.

func AssertStringArrayAtPath

func AssertStringArrayAtPath(t *testing.T, data, path string, asserted []interface{}) (ok bool)

func AssertStringAtPath

func AssertStringAtPath(t *testing.T, data, path, asserted string) (ok bool)

Types

type JsonAssertion

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

JsonAssertion is the struct we use to organize our walking of the JSON doc. The decoder is created by the Maker only. At the moment, the assertions are walking the JSON doc each time. Consider an extended method set that can reuse a single JsonAssertion, in cases of large numbers of asserts.

func MakeJsonAssertion

func MakeJsonAssertion(data string) (jas *JsonAssertion, err error)

MakeJsonAssertion creates and initializes a JsonAssertion, with decoder instance, or returns an error.

func (*JsonAssertion) IsBoolAt

func (jas *JsonAssertion) IsBoolAt(path string, val bool) (ok bool)

func (*JsonAssertion) IsIdenticalFloatSliceAt

func (jas *JsonAssertion) IsIdenticalFloatSliceAt(path string, val []interface{}) (ok bool)

func (*JsonAssertion) IsIdenticalStringSliceAt

func (jas *JsonAssertion) IsIdenticalStringSliceAt(path string, val []interface{}) (ok bool)

func (*JsonAssertion) IsMatchingFloatSliceAt

func (jas *JsonAssertion) IsMatchingFloatSliceAt(path string, val []interface{}) (ok bool)

IsMatchingFloatSliceAt looks for a float slice at the given path, and if it finds one, compares them without regard to their ordering.

func (*JsonAssertion) IsMatchingStringSliceAt

func (jas *JsonAssertion) IsMatchingStringSliceAt(path string, val []interface{}) (ok bool)

IsMatchingStringArrayAt Assert that a string array is found at the given path, which is 'similar' as in: same length, same elements but not in the same order.

func (*JsonAssertion) IsNumberAt

func (jas *JsonAssertion) IsNumberAt(path string, val float64) (ok bool)

func (*JsonAssertion) IsObjectAt

func (jas *JsonAssertion) IsObjectAt(path string) (ok bool)

func (*JsonAssertion) IsStringAt

func (jas *JsonAssertion) IsStringAt(path string, val string) (ok bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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