assert

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 2 Imported by: 1

README

Package assert - assert macro for Go

Build Status GoDoc

Package assert provides developer a way to assert expression and print expression source code in test cases. It works like C macro assert. When assertion fails, source code of the expression in assert function is printed.

For example, if we write Assert(t, a > b) when a = 1 and b = 2, we can read Assertion failed: a > b in the failure message. The a > b is the expression evaluated in Assert.

Without this package, developers must use negate logic to test expressions and call t.Fatalf to print meaningful failure message for debugging. Just like following.

func TestSomething(t *testing.T) {
    str := "actual"

    // We expect `str` to be "expected". To verify it, we need to use negate logic.
    // It's not straight forward.
    if str != "expected" {
        // We have to write some messages to let us know what's called and why it fails.
        t.Fatalf("invalid str. [str:%v] [expected:%v]", str, "expected")
    }
}

With this package, we can significantly simplify test code which works similar as above.

import . "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    str := "actual"
    Assert(t, str == "expected")

    // This case fails with following message.
    //
    //     Assertion failed: str == "expected"
    
    // If we're aware of the value of str, use AssertEqual.
    AssertEqual(t, str, "expected")
    
    // This case fails with following message.
    //
    //     Assertion failed: str == "expected"
    //         v1 = actual
    //         v2 = expected
}

Import

Use go get to install this package.

go get github.com/huandu/go-assert

Current stable version is v1.*. Old versions tagged by v0.* are obsoleted.

Usage

Use it in a test file.

import . "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a, b := 1, 2
    Assert(t, a > b)
    
    // This case fails with message:
    //     Assertion failed: a > b
}

func TestAssertEquality(t *testing.T) {
    AssertEqual(t, map[string]int{
        "foo": 1,
        "bar": -2,
    }, map[string]int{
        "bar": -2,
        "foo": 10000,
    })
    
    // This case fails with message:
    //     Assertion failed: map[string]int{
    //         "foo": 1,
    //         "bar": -2,
    //     } == map[string]int{
    //         "bar": -2,
    //         "foo": 10000,
    //     }
    //         v1 = map[foo:1 bar:-2]
    //         v2 = map[bar:-2 foo:10000]
}

We can wrap t in an Assertion to validate results returned by a function.

import "github.com/huandu/go-assert"

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

    f := func(bool, int) (int, string, error) {
        return 0, "", errors.New("an error")
    }
    a.NilError(f(true, 42))
    
    // This case fails with message:
    //     Assertion failed: f(true, 42) should return nil error.
    //         err = an error
}

Documentation

Overview

Package assert provides API to implement C-like assert macro.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(t *testing.T, expr interface{})

Assert tests expr and call `t.Fatalf` to terminate test case if expr is false-equivalent value. `false`, 0, nil and empty string are false-equivalent values.

Usage:

import . "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a, b := 1, 2
    Assert(t, a > b) // This case fails with message "Assertion failed: a > b".
}

func AssertEqual

func AssertEqual(t *testing.T, v1, v2 interface{})

AssertEqual uses `reflect.DeepEqual` to test v1 and v2 equality.

Usage:

import . "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    AssertEqual(t, []int{1,2}, []int{1})

    // This case fails with message:
    //     Assertion failed: []int{1,2} == []int{1}
    //         v1 = [1,2]
    //         v2 = [1]
}

func AssertNotEqual

func AssertNotEqual(t *testing.T, v1, v2 interface{})

AssertNotEqual uses `reflect.DeepEqual` to test v1 and v2 equality.

Usage:

import . "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    AssertNotEqual(t, []int{1}, []int{1})

    // This case fails with message:
    //     Assertion failed: []int{1} != []int{1}
}

Types

type A added in v1.0.0

type A struct {
	*testing.T
}

The A is a wrapper of testing.T with some extra help methods.

func New added in v1.0.0

func New(t *testing.T) *A

New creates an assertion object wraps t.

func (*A) Assert added in v1.0.0

func (a *A) Assert(expr interface{})

Assert tests expr and call `t.Fatalf` to terminate test case if expr is false-equivalent value. `false`, 0, nil and empty string are false-equivalent values. Usage:

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a := assert.New(t)
    x, y := 1, 2
    a.Assert(t, x > y) // This case fails with message "Assertion failed: x > y".
}

func (*A) Equal added in v1.0.0

func (a *A) Equal(v1, v2 interface{})

Equal uses `reflect.DeepEqual` to test v1 and v2 equality.

Usage:

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a := assert.New(t)
    a.Equal([]int{1,2}, []int{1})

    // This case fails with message:
    //     Assertion failed: []int{1,2} == []int{1}
    //         v1 = [1 2]
    //         v2 = [1]
}

func (*A) NilError added in v1.0.0

func (a *A) NilError(result ...interface{})

NilError expects a function return a nil error. Otherwise, it will terminate the test case using `t.Fatalf`.

Usage:

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a := assert.New(t)
    a.NilError(os.Open("path/to/a/file")) // This case fails if os.Open returns error.
}

func (*A) NonNilError added in v1.0.0

func (a *A) NonNilError(result ...interface{})

NonNilError expects a function return a non-nil error. Otherwise, it will terminate the test case using `t.Fatalf`.

Usage:

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a := assert.New(t)
    f := func() (int, error) { return 0, errors.New("expected") }
    a.NilError(f()) // This case fails.
}

func (*A) NotEqual added in v1.0.0

func (a *A) NotEqual(v1, v2 interface{})

NotEqual uses `reflect.DeepEqual` to test v1 and v2 equality.

Usage:

import "github.com/huandu/go-assert"

func TestSomething(t *testing.T) {
    a := assert.New(t)
    a.NotEqual(t, []int{1}, []int{1})

    // This case fails with message:
    //     Assertion failed: []int{1} != []int{1}
}

Directories

Path Synopsis
internal
assertion
Package assertion is the implementation detail of package assert.
Package assertion is the implementation detail of package assert.

Jump to

Keyboard shortcuts

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