be

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Vendored copy of https://github.com/ducksify/be/tree/d3d9b39d71dd594af2ce96ba7fb599233e82377c

Example
package main

import (
	"errors"
	"fmt"
	"testing"

	"github.com/ducksify/requests/internal/be"
)

type mockingT struct{ *testing.T }

func (_ mockingT) Helper() {}

func (_ mockingT) Fatalf(format string, args ...any) {
	fmt.Printf(format+"\n", args...)
}

func main() {
	// mock *testing.T for example purposes
	var t mockingT

	be.Equal(t, "hello", "world")     // bad
	be.Equal(t, "goodbye", "goodbye") // good

	be.Unequal(t, "hello", "world")     // good
	be.Unequal(t, "goodbye", "goodbye") // bad

	s := []int{1, 2, 3}
	be.AllEqual(t, []int{1, 2, 3}, s) // good
	be.AllEqual(t, []int{3, 2, 1}, s) // bad

	var err error
	be.NilErr(t, err)  // good
	be.Nonzero(t, err) // bad
	err = errors.New("(O_o)")
	be.NilErr(t, err)  // bad
	be.Nonzero(t, err) // good

	type mytype string
	var mystring mytype = "hello, world"
	be.In(t, "world", mystring)                 // good
	be.In(t, "World", mystring)                 // bad
	be.NotIn(t, "\x01", []byte("\a\b\x00\r\t")) // good
	be.NotIn(t, "\x00", []byte("\a\b\x00\r\t")) // bad

}
Output:

want: hello; got: world
got: goodbye
want: [3 2 1]; got: [1 2 3]
got: <nil>
got: (O_o)
"World" not in "hello, world"
"\x00" in "\a\b\x00\r\t"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllEqual

func AllEqual[T comparable](t testing.TB, want, got []T)

AllEqual calls t.Fatalf if want != got.

func Debug

func Debug(t testing.TB, f func())

Debug takes a callback that will only be run after the test fails.

Example
package main

import (
	"fmt"
	"testing"

	"github.com/ducksify/requests/internal/be"
)

type mockDebug struct {
	testing.T
	failed   bool
	cleanups []func()
}

func (m *mockDebug) Run(name string, f func(t *testing.T)) {
	defer func() {
		for _, f := range m.cleanups {
			defer f()
		}
	}()
	f(&m.T)
}

func (m *mockDebug) Cleanup(f func()) {
	m.cleanups = append(m.cleanups, f)
}

func (_ *mockDebug) Log(args ...any) {
	fmt.Println(args...)
}

func (_ *mockDebug) Helper() {}

func (m *mockDebug) Fatalf(format string, args ...any) {
	m.failed = true
	fmt.Printf(format+"\n", args...)
}

func (m *mockDebug) Failed() bool { return m.failed }

func main() {
	t := &mockDebug{}
	// If a test fails, the callbacks will be replayed in LIFO order
	t.Run("logging-example", func(_ *testing.T) {
		x := 1
		x1 := x
		be.Debug(t, func() {
			// record some debug information about x1
			fmt.Println("x1:", x1)
		})
		x = 2
		x2 := x
		be.Debug(t, func() {
			// record some debug information about x2
			fmt.Println("x2:", x2)
		})
		be.Equal(t, x, 3)
	})
	t = &mockDebug{}
	// If a test succeeds, nothing will be replayed
	t.Run("silent-example", func(_ *testing.T) {
		y := 1
		y1 := y
		be.Debug(t, func() {
			// record some debug information about y1
			fmt.Println("y1:", y1)
		})
		y = 2
		y2 := y
		be.Debug(t, func() {
			// record some debug information about y2
			fmt.Println("y2:", y2)
		})
		be.Unequal(t, y, 3)
	})
}
Output:

want: 2; got: 3
x2: 2
x1: 1

func DebugLog

func DebugLog(t testing.TB, format string, args ...any)

DebugLog records a message that will only be logged after the test fails.

Example
package main

import (
	"fmt"
	"testing"

	"github.com/ducksify/requests/internal/be"
)

type mockDebug struct {
	testing.T
	failed   bool
	cleanups []func()
}

func (m *mockDebug) Run(name string, f func(t *testing.T)) {
	defer func() {
		for _, f := range m.cleanups {
			defer f()
		}
	}()
	f(&m.T)
}

func (m *mockDebug) Cleanup(f func()) {
	m.cleanups = append(m.cleanups, f)
}

func (_ *mockDebug) Log(args ...any) {
	fmt.Println(args...)
}

func (_ *mockDebug) Helper() {}

func (m *mockDebug) Fatalf(format string, args ...any) {
	m.failed = true
	fmt.Printf(format+"\n", args...)
}

func (m *mockDebug) Failed() bool { return m.failed }

func main() {
	t := &mockDebug{}
	// If a test fails, the logs will be replayed in LIFO order
	t.Run("logging-example", func(_ *testing.T) {
		x := 1
		be.DebugLog(t, "x: %d", x)
		x = 2
		be.DebugLog(t, "x: %d", x)
		be.Equal(t, x, 3)
	})
	t = &mockDebug{}
	// If a test succeeds, nothing will be replayed
	t.Run("silent-example", func(_ *testing.T) {
		y := 1
		be.DebugLog(t, "y: %d", y)
		y = 2
		be.DebugLog(t, "y: %d", y)
		be.Unequal(t, y, 3)
	})
}
Output:

want: 2; got: 3
x: 2
x: 1

func DeepEqual

func DeepEqual[T any](t testing.TB, want, got T)

DeepEqual calls t.Fatalf if want and got are different according to reflect.DeepEqual.

Example
var t mockingT

// good
m1 := map[int]bool{1: true, 2: false}
m2 := map[int]bool{1: true, 2: false}
be.DeepEqual(t, m1, m2)

// bad
var s1 []int
s2 := []int{}
be.DeepEqual(t, s1, s2) // DeepEqual is picky about nil vs. len 0
Output:

reflect.DeepEqual([]int(nil), []int{}) == false

func Equal

func Equal[T comparable](t testing.TB, want, got T)

Equal calls t.Fatalf if want != got.

func False

func False(t testing.TB, value bool)

False calls t.Fatalf if value is not false.

func In

func In[byteseq ~string | ~[]byte](t testing.TB, needle string, haystack byteseq)

In calls t.Fatalf if needle is not contained in the string or []byte haystack.

func NilErr

func NilErr(t testing.TB, err error)

NilErr calls t.Fatalf if err is not nil.

func Nonzero

func Nonzero[T any](t testing.TB, value T)

Nonzero calls t.Fatalf if value == the zero value for T.

func NotIn

func NotIn[byteseq ~string | ~[]byte](t testing.TB, needle string, haystack byteseq)

NotIn calls t.Fatalf if needle is contained in the string or []byte haystack.

func True

func True(t testing.TB, value bool)

True calls t.Fatalf if value is not true.

func Unequal

func Unequal[T comparable](t testing.TB, bad, got T)

Unequal calls t.Fatalf if got == bad.

func Zero

func Zero[T any](t testing.TB, value T)

Zero calls t.Fatalf if value != the zero value for T.

Types

This section is empty.

Jump to

Keyboard shortcuts

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