assert

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

assert

Go Report Card GoDoc

The assert package provides some helpful methods that allow you to write better test code in Go.

  • Prints friendly for read
  • Readable code

Installation

$ go get github.com/subchen/go-stack/assert

Usage

import (
    "testing"
    "github.com/subchen/go-stack/assert"
)

func TestToString(t *testing.T) {
    assert.Equal(t, ToString(nil), "")
    assert.Equal(t, ToString(true), "true")
    assert.Equal(t, ToString(0), "0")
}

if you assert many times, use the below:

import (
    "testing"
    "github.com/subchen/go-stack/assert"
)

func TestToString(t *testing.T) {
    assert := assert.New(t)

    assert.Equal(ToString(nil), "")
    assert.Equal(ToString(true), "true")
    assert.Equal(ToString(0), "0")
}

result on failure

$ go test
--- FAIL: TestToString (0.00s)
        to_string_test.go:12
                Expected: "true"
                Actual  : "false"
FAIL
exit status 1
API on godoc.org

https://godoc.org/github.com/subchen/go-stack/assert

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t *testing.T, list interface{}, element interface{})

Support array, slice, map, chan, string

func Empty

func Empty(t *testing.T, object interface{})

Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.

assert.Empty(t, obj)

func Equal

func Equal(t *testing.T, actual, expected interface{})

Equal asserts that two objects are equal.

assert.Equal(t, 123, 456)

func EqualVal

func EqualVal(t *testing.T, actual, expected interface{})

EqualVal asserts that two objects are equal (convertable to the same types and equal).

assert.EqualVal(t, uint32(123), int32(123))

func Error

func Error(t *testing.T, err interface{})

Error asserts that the specified error is not nil.

assert.Error(t, err)

func False

func False(t *testing.T, value bool)

False asserts that the specified value is false.

assert.False(t, myBool)

func HasNotPrefix

func HasNotPrefix(t *testing.T, str string, prefix string)

func HasNotSuffix

func HasNotSuffix(t *testing.T, str string, prefix string)

func HasPrefix

func HasPrefix(t *testing.T, str string, prefix string)

func HasSuffix

func HasSuffix(t *testing.T, str string, prefix string)

func Implements

func Implements(t *testing.T, object interface{}, interfaceObject interface{})

Implements asserts that an object is implemented by the specified interface.

assert.Implements(t, new(MyObject), (*MyInterface)(nil))

func Len

func Len(t *testing.T, object interface{}, length int)

Support array, slice, map, chan, string

func Nil

func Nil(t *testing.T, object interface{})

Nil asserts that the specified object is nil.

assert.Nil(t, err)

func NoError

func NoError(t *testing.T, err interface{})

NoError asserts that the specified error is nil.

assert.NoError(t, err)

func NoPanic

func NoPanic(t *testing.T, action func())

func NotContains

func NotContains(t *testing.T, list interface{}, element interface{})

Support array, slice, map, chan, string

func NotEmpty

func NotEmpty(t *testing.T, object interface{})

array, slice, map, channel, string with len == 0.

assert.Equal(t, "two", obj[1])

func NotEqual

func NotEqual(t *testing.T, actual, expected interface{})

NotEqual asserts that the specified values are NOT equal.

assert.NotEqual(t, obj1, obj2)

func NotEqualVal

func NotEqualVal(t *testing.T, actual, expected interface{})

NotEqualVal asserts that two objects are NOT equal(convertable to the same types and equal).

assert.NotEqualVal(t, uint32(123), int32(123))

func NotNil

func NotNil(t *testing.T, object interface{})

NotNil asserts that the specified object is not nil.

assert.NotNil(t, err)

func NotZero

func NotZero(t *testing.T, object interface{})

NotZero asserts that object is not the zero value for its type and returns the truth.

func Panic

func Panic(t *testing.T, action func())

func SameType

func SameType(t *testing.T, object interface{}, anotherObject interface{})

SameType asserts that the specified objects are of the same type.

assert.SameType(t, "1", "1")

func True

func True(t *testing.T, value bool)

True asserts that the specified value is true.

assert.True(t, myBool)

func Zero

func Zero(t *testing.T, object interface{})

Zero asserts that object is the zero value for its type and returns the truth.

Types

type Assert

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

func New

func New(t *testing.T) *Assert

func (*Assert) Contains

func (a *Assert) Contains(list interface{}, element interface{})

func (*Assert) Empty

func (a *Assert) Empty(object interface{})

func (*Assert) Equal

func (a *Assert) Equal(actual, expected interface{})

func (*Assert) EqualVal

func (a *Assert) EqualVal(actual, expected interface{})

func (*Assert) Error

func (a *Assert) Error(error interface{})

func (*Assert) False

func (a *Assert) False(value bool)

func (*Assert) HasNotPrefix

func (a *Assert) HasNotPrefix(str, prefix string)

func (*Assert) HasNotSuffix

func (a *Assert) HasNotSuffix(str, suffix string)

func (*Assert) HasPrefix

func (a *Assert) HasPrefix(str, prefix string)

func (*Assert) HasSuffix

func (a *Assert) HasSuffix(str, suffix string)

func (*Assert) Implements

func (a *Assert) Implements(object interface{}, interfaceObject interface{})

func (*Assert) Len

func (a *Assert) Len(object interface{}, length int)

func (*Assert) Nil

func (a *Assert) Nil(object interface{})

func (*Assert) NoError

func (a *Assert) NoError(error interface{})

func (*Assert) NoPanic

func (a *Assert) NoPanic(action func())

func (*Assert) NotContains

func (a *Assert) NotContains(list interface{}, element interface{})

func (*Assert) NotEmpty

func (a *Assert) NotEmpty(object interface{})

func (*Assert) NotEqual

func (a *Assert) NotEqual(actual, expected interface{})

func (*Assert) NotEqualVal

func (a *Assert) NotEqualVal(actual, expected interface{})

func (*Assert) NotNil

func (a *Assert) NotNil(object interface{})

func (*Assert) NotZero

func (a *Assert) NotZero(object interface{})

func (*Assert) Panic

func (a *Assert) Panic(action func())

func (*Assert) SameType

func (a *Assert) SameType(object interface{}, anotherObject interface{})

func (*Assert) True

func (a *Assert) True(value bool)

func (*Assert) Zero

func (a *Assert) Zero(object interface{})

Jump to

Keyboard shortcuts

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