Skip to content

Latest commit

 

History

History
127 lines (92 loc) · 3.44 KB

File metadata and controls

127 lines (92 loc) · 3.44 KB

Assert

Latest Stable Version Build Status Coverage Status Go Report Card Go Dev Reference Software License

Simple and lightweight testing assertion library for Go.

Installation

go get github.com/gravitton/assert

Usage

package main

import (
	"testing"

	"github.com/gravitton/assert"
)

func Test(t *testing.T) {
	// assert equality
	assert.Equal(t, 123, 123, "Custom message: ")
	// assert inequality
	assert.NotEqual(t, 123, 456)
	// assert object contains element
	assert.Contains(t, []int{1, 2, 3}, 2)
	// assert object can be marshall to given JSON string
	assert.JSON(t, &obj, `{"data":1}`)
}

All assertions return false if assertion is not successful and accepts a custom error message(s) as the last argument which are prefixed to the assert error message.

You can use this to build your own complex assertions.

package main

import (
	"image"
	"testing"

	"github.com/gravitton/assert"
)

func Test(t *testing.T) {
	assertRect(t, image.Rect(1, 2, 3, 4), image.Rect(1, 2, 3, 4))
	// test.go:11: Min.Y: Should be equal:
	//       actual: 2
	//     expected: 3
	// test.go:11: Max.X: Should be equal:
	//       actual: 3
	//     expected: 2
}

func assertRect(t *testing.T, actual image.Rectangle, expected image.Rectangle) bool {
	t.Helper()

	ok := true
	
	if !assertPoint(t, actual.Min, expected.Min, "Min.") {
		ok = false
	}
	if !assertPoint(t, actual.Max, expected.Max, "Max.") {
		ok = false
	}
	
	return ok
}

func assertPoint(t *testing.T, actual image.Point, expected image.Point, messages ...string) bool {
	t.Helper()

	ok := true
	
	if !assert.Equal(t, actual.X, expected.X, append(messages, "X: ")...) {
		ok = false
	}
	if !assert.Equal(t, actual.Y, expected.Y, append(messages, "Y: ")...) {
		ok = false
	}

	return ok
}

Credits

License

The MIT License (MIT). Please see License File for more information.