Skip to content

sku0x20/assertgo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

286 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI

AssertGo

A simple assertion library for Go.

Installation

go get github.com/sku0x20/assertgo

Usage

import . "github.com/sku0x20/assertgo"

func TestSomething(t *testing.T) {
    T(t).Assert("hello").EqualTo("hello")
    T(t).Assert("hello").Not().EqualTo("world")

    T(t).AssertBool(true).IsTrue()
    T(t).AssertBool(false).Not().IsTrue()

    T(t).AssertInt(10).GreaterThan(5)
    T(t).AssertInt(10).InRange(1, 20)
    T(t).AssertFloat(3.14).LessThan(4.0)

    T(t).AssertString("hello").Contains("ell")
    T(t).AssertString("hello").HasPrefix("hel")
    T(t).AssertString("hello").HasSuffix("llo")
    T(t).AssertString("hello123").MatchesRegex(`^\w+\d+$`)

    T(t).AssertSlice([]int{1, 2, 3}).HasLength(3)
    T(t).AssertSlice([]int{1, 2, 3}).ContainsAll([]int{1, 3})
    T(t).AssertSlice([]int{1, 2, 3}).Not().ContainsAll([]int{4, 5})
    T(t).AssertSlice([]int{1, 2, 3}).IsUnique()

    T(t).AssertMap(map[string]int{"a": 1}).HasLength(1)
    T(t).AssertMap(map[string]int{"a": 1, "b": 2}).ContainsAllEntries(map[string]int{"a": 1})

    T(t).AssertType(42).IsType(reflect.TypeOf(0))
    T(t).AssertType(42).Is[int]()
}

Custom Comparator

T(t).Assert(myStruct).WithComparator(myComparator).EqualTo(expected)

Custom Matcher

import "github.com/sku0x20/assertgo/pkg/matcher"

T(t).Assert("hello").Matches(matcher.NewNotMatcher[any](matcher.NewNilMatcher[any]()))

About

A simple, type-safe assertion library for Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages