Skip to content

RageCage64/go-assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-assert

Assertion library for Go built with generics!

This library is mostly meant to keep assertion patterns very minimal, and geared towards reducing the need to handroll t.Fatalf messages constantly by providing sensible default messages (that can be overriden if needed).

Usage

Install the module in your project:

go get github.com/RageCage64/go-assert

Assert

A default Assert function is provided, which you can pass a condition and your own failure strings to.

package main_test

import (
    "testing"

    "github.com/RageCage64/go-assert/assert"
)

func TestAssert(t *testing.T) {
    a := 1
    b := 2
    assert.Assert(t, a == b, "didn't work")
}

NilErr

Checks if the error passed in is nil.

package main_test

import (
    "testing"

    "github.com/RageCage64/go-assert/assert"
)

func TestAssert(t *testing.T) {
    err := errors.New("not nil")
    assert.NilErr(t, err)
}

Equal

Using Equal you can check if two comparable types are equal.

package main_test

import (
    "testing"

    "github.com/RageCage64/go-assert/assert"
)

type x struct {
    num int
}

func TestEqual(t *testing.T) {
    expected := x{num: 1}
    got := x{num: 2}
    assert.Equal(t, expected, got)
}

DereferenceEqual

Using DereferenceEqual you can check if the value at two pointers of comparable type are the same.

package main_test

import (
    "testing"

    "github.com/RageCage64/go-assert/assert"
)

type x struct {
    num int
}

func TestEqual(t *testing.T) {
    expected := &x{num: 1}
    got := &x{num: 2}
    assert.DereferenceEqual(t, expected, got)
}

SliceEqual

Using SliceEqual you can check if two slices of comparable types are equal. Different messages will be used if the slices are different length or if they are mismatched.

package main_test

import (
    "testing"

    "github.com/RageCage64/go-assert/assert"
)

func TestEqual(t *testing.T) {
    expected := []int{1, 2}
    got := []int{1, 3}
    assert.SliceEqual(t, expected, got)
}

About

A package with common assertion patterns

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages