Skip to content

Instantly share code, notes, and snippets.

@nfx
Last active October 7, 2026 13:20
Show Gist options
  • Select an option

  • Save nfx/78978244750d0746919238de9229d36e to your computer and use it in GitHub Desktop.

Select an option

Save nfx/78978244750d0746919238de9229d36e to your computer and use it in GitHub Desktop.
zerodep go assertions

Testify is a great library, but it has more functionality and dependencies than the most of libraries need. And it leaks its dependencies to go.mod. JVM build tooling ecosystem has the concept of a "test scope", but Go team explicitly decided against test scopes in go.mod - see golang/go#26913 (comment)

This tiny library is meant for copy-pasting into library's internal/assert/assert.go and extended as-necessary. Inspired by https://github.com/shadcn-ui/ui widgets. It's just a hundred lines of code or so - doesn't deserve a dedicated repository.

// Copyright 2026 Serge Smertin
// SPDX-License-Identifier: MIT
// Package assert provides compact, zero-dependency, and vendorable assertion library.
package assert
import (
"errors"
"reflect"
"strings"
"testing"
)
// Equal compares two values for equality.
func Equal(t *testing.T, expected, actual any) {
t.Helper()
expected, actual, res := deepEqual(expected, actual)
if !res {
t.Fatalf("results differ:\nexpected: %#v\n actual: %#v", expected, actual)
}
}
func deepEqual(expected, actual any) (want, got any, ok bool) {
ev, av := reflect.ValueOf(expected), reflect.ValueOf(actual)
if !ev.IsValid() || !av.IsValid() {
return expected, actual, expected == nil && actual == nil
}
if ev.Type().ConvertibleTo(av.Type()) {
expected = ev.Convert(av.Type()).Interface()
} else if av.Type().ConvertibleTo(ev.Type()) {
actual = av.Convert(ev.Type()).Interface()
}
res := reflect.DeepEqual(expected, actual)
return expected, actual, res
}
// NotNil asserts that the value is not nil.
func NotNil(t *testing.T, value any) {
t.Helper()
var fail bool
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Invalid:
fail = true
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
fail = v.IsNil()
}
if fail {
t.Fatal("expected not nil, got nil")
}
}
// True asserts that the value is true.
func True(t *testing.T, value bool) { //nolint:revive // ignore
t.Helper()
if !value {
t.Fatal("expected true, got false")
}
}
// Contains asserts that the string contains the substring.
func Contains(t *testing.T, s, substr string) {
t.Helper()
if !strings.Contains(s, substr) {
t.Fatalf("expected %s to contain: %s", s, substr)
}
}
// NotContains asserts that the string does not contain the substring.
func NotContains(t *testing.T, s, substr string) {
t.Helper()
if strings.Contains(s, substr) {
t.Fatalf("expected %s not to contain: %s", s, substr)
}
}
// Error asserts that the error is not nil.
func Error(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Fatal("expected error, got: nil")
}
}
// ErrorIs asserts that the error is of the expected type.
func ErrorIs(t *testing.T, err, expected error) {
t.Helper()
if !errors.Is(err, expected) {
t.Fatalf("expected error %v, got: %v", expected, err)
}
}
// NoError asserts that the error is nil.
func NoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
MIT License
Copyright (c) 2026 Serge Smertin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment