Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions alias_typed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gunit_test

import (
"github.com/gogunit/gunit"
"github.com/gogunit/gunit/eye"
"testing"
)

func Test_string_NotContains_success(t *testing.T) {
gunit.String(t, "Hello").NotContains("bye")
}

func Test_string_NotContains_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.String(aSpy, "Hello").NotContains("ell")
aSpy.HadErrorContaining(t, "not to contain")
}

func Test_string_NotEmpty_success(t *testing.T) {
gunit.String(t, "Hello").NotEmpty()
}

func Test_string_NotEmpty_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.String(aSpy, "").NotEmpty()
aSpy.HadErrorContaining(t, "was not")
}

func Test_number_NotEqual_success(t *testing.T) {
gunit.Number(t, 1).NotEqual(2)
}

func Test_number_NotEqual_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.Number(aSpy, 1).NotEqual(1)
aSpy.HadErrorContaining(t, "not equal")
}
95 changes: 95 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gunit

import (
"errors"
"regexp"
"strings"
)

func NilError(t T, err error) {
t.Helper()
if err != nil {
t.Errorf("got <%v>, want nil error", err)
}
}

func Error(t T, err error) {
t.Helper()
if err == nil {
t.Errorf("got nil, want error")
}
}

func EqualError(t T, err error, expected string) {
t.Helper()
if err == nil {
t.Errorf("got nil error, want error message <%s>", expected)
return
}
if err.Error() != expected {
t.Errorf("got error message <%s>, want <%s>", err.Error(), expected)
}
}

func ErrorContains(t T, err error, expected string) {
t.Helper()
if err == nil {
t.Errorf("got nil error, want error containing <%s>", expected)
return
}
if !strings.Contains(err.Error(), expected) {
t.Errorf("got error message <%s>, want containing <%s>", err.Error(), expected)
}
}

func ErrorMatchesRegexp(t T, err error, pattern string) {
t.Helper()
if err == nil {
t.Errorf("got nil error, want error matching regexp <%s>", pattern)
return
}
re, compileErr := regexp.Compile(pattern)
if compileErr != nil {
t.Errorf("invalid regexp <%s>: %v", pattern, compileErr)
return
}
if !re.MatchString(err.Error()) {
t.Errorf("got error message <%s>, want regexp <%s>", err.Error(), pattern)
}
}

func ErrorIs(t T, err error, target error) {
t.Helper()
if !errors.Is(err, target) {
t.Errorf("got <%v>, want error matching <%v>", err, target)
}
}

func NotErrorIs(t T, err error, target error) {
t.Helper()
if errors.Is(err, target) {
t.Errorf("got <%v>, want error not matching <%v>", err, target)
}
}

func ErrorAs[E any](t T, err error, target *E) {
t.Helper()
if !errors.As(err, target) {
t.Errorf("got <%v>, want error assignable to <%T>", err, target)
}
}

func NotErrorAs[E any](t T, err error, target *E) {
t.Helper()
if errors.As(err, target) {
t.Errorf("got <%v>, want error not assignable to <%T>", err, target)
}
}

func ErrorType[E error](t T, err error) {
t.Helper()
var target E
if !errors.As(err, &target) {
t.Errorf("got <%v>, want error assignable to <%T>", err, target)
}
}
147 changes: 147 additions & 0 deletions error_typed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package gunit_test

import (
"errors"
"fmt"
"github.com/gogunit/gunit"
"github.com/gogunit/gunit/eye"
"testing"
)

func Test_error_NilError_success(t *testing.T) {
gunit.NilError(t, nil)
}

func Test_error_NilError_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.NilError(aSpy, errors.New("sentinel"))
aSpy.HadErrorContaining(t, "want nil error")
}

func Test_error_Error_success(t *testing.T) {
gunit.Error(t, errors.New("sentinel"))
}

func Test_error_Error_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.Error(aSpy, nil)
aSpy.HadErrorContaining(t, "want error")
}

func Test_error_EqualError_success(t *testing.T) {
gunit.EqualError(t, errors.New("sentinel"), "sentinel")
}

func Test_error_EqualError_failure_for_nil(t *testing.T) {
aSpy := eye.Spy()
gunit.EqualError(aSpy, nil, "sentinel")
aSpy.HadErrorContaining(t, "got nil error")
}

func Test_error_EqualError_failure_for_mismatch(t *testing.T) {
aSpy := eye.Spy()
gunit.EqualError(aSpy, errors.New("wrapped sentinel"), "sentinel")
aSpy.HadErrorContaining(t, "want <sentinel>")
}

func Test_error_ErrorContains_success(t *testing.T) {
gunit.ErrorContains(t, errors.New("wrapped sentinel"), "wrapped")
}

func Test_error_ErrorContains_failure_for_nil(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorContains(aSpy, nil, "wrapped")
aSpy.HadErrorContaining(t, "got nil error")
}

func Test_error_ErrorContains_failure_for_mismatch(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorContains(aSpy, errors.New("wrapped sentinel"), "missing")
aSpy.HadErrorContaining(t, "want containing")
}

func Test_error_ErrorMatchesRegexp_success(t *testing.T) {
gunit.ErrorMatchesRegexp(t, errors.New("wrapped sentinel"), "sentinel")
}

func Test_error_ErrorMatchesRegexp_failure_for_nil(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorMatchesRegexp(aSpy, nil, "wrapped")
aSpy.HadErrorContaining(t, "got nil error")
}

func Test_error_ErrorMatchesRegexp_failure_for_invalid_regexp(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorMatchesRegexp(aSpy, errors.New("wrapped sentinel"), "[")
aSpy.HadErrorContaining(t, "invalid regexp")
}

func Test_error_ErrorMatchesRegexp_failure_for_mismatch(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorMatchesRegexp(aSpy, errors.New("wrapped sentinel"), "missing")
aSpy.HadErrorContaining(t, "want regexp")
}

func Test_error_ErrorIs_success(t *testing.T) {
sentinel := errors.New("sentinel")
gunit.ErrorIs(t, fmt.Errorf("wrapped: %w", sentinel), sentinel)
}

func Test_error_ErrorIs_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorIs(aSpy, errors.New("wrapped"), errors.New("sentinel"))
aSpy.HadErrorContaining(t, "want error matching")
}

func Test_error_NotErrorIs_success(t *testing.T) {
gunit.NotErrorIs(t, errors.New("wrapped"), errors.New("sentinel"))
}

func Test_error_NotErrorIs_failure(t *testing.T) {
sentinel := errors.New("sentinel")
aSpy := eye.Spy()
gunit.NotErrorIs(aSpy, sentinel, sentinel)
aSpy.HadErrorContaining(t, "want error not matching")
}

func Test_error_ErrorAs_success(t *testing.T) {
var target *typedTestError
gunit.ErrorAs(t, &typedTestError{msg: "wrapped sentinel"}, &target)
}

func Test_error_ErrorAs_failure(t *testing.T) {
var target *typedTestError
aSpy := eye.Spy()
gunit.ErrorAs(aSpy, errors.New("sentinel"), &target)
aSpy.HadErrorContaining(t, "want error assignable")
}

func Test_error_NotErrorAs_success(t *testing.T) {
var target *otherTypedTestError
gunit.NotErrorAs(t, &typedTestError{msg: "wrapped sentinel"}, &target)
}

func Test_error_NotErrorAs_failure(t *testing.T) {
var target *typedTestError
aSpy := eye.Spy()
gunit.NotErrorAs(aSpy, &typedTestError{msg: "wrapped sentinel"}, &target)
aSpy.HadErrorContaining(t, "want error not assignable")
}

func Test_error_ErrorType_success(t *testing.T) {
gunit.ErrorType[*typedTestError](t, &typedTestError{msg: "wrapped sentinel"})
}

func Test_error_ErrorType_failure(t *testing.T) {
aSpy := eye.Spy()
gunit.ErrorType[*typedTestError](aSpy, errors.New("sentinel"))
aSpy.HadErrorContaining(t, "want error assignable")
}

type typedTestError struct{ msg string }

func (e *typedTestError) Error() string { return e.msg }

type otherTypedTestError struct{ msg string }

func (e *otherTypedTestError) Error() string { return e.msg }
60 changes: 60 additions & 0 deletions filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package gunit

import (
"errors"
"os"
)

func FileExists(t T, path string) {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Errorf("got path <%s> unavailable: %v, wanted existing file", path, err)
return
}
if info.IsDir() {
t.Errorf("got directory <%s>, wanted existing file", path)
}
}

func NoFileExists(t T, path string) {
t.Helper()
info, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return
}
if err != nil {
t.Errorf("got path <%s> unavailable: %v, wanted no existing file", path, err)
return
}
if !info.IsDir() {
t.Errorf("got existing file <%s>, wanted no file", path)
}
}

func DirExists(t T, path string) {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Errorf("got path <%s> unavailable: %v, wanted existing directory", path, err)
return
}
if !info.IsDir() {
t.Errorf("got file <%s>, wanted existing directory", path)
}
}

func NoDirExists(t T, path string) {
t.Helper()
info, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return
}
if err != nil {
t.Errorf("got path <%s> unavailable: %v, wanted no existing directory", path, err)
return
}
if info.IsDir() {
t.Errorf("got existing directory <%s>, wanted no directory", path)
}
}
Loading
Loading