efft

package
v0.260131.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: 0BSD Imports: 10 Imported by: 0

Documentation

Overview

Package efft from efftesting checks expectations and optionally rewrites them if the EFFUP=1 envvar is set. Just write `efft.Effect(any arg here)`, efftesting deterministically stringifies the arguments and maintains the expectation for the result.

Example usage:

func TestSplit(t *testing.T) {
  efft.Init(t)

  // simple cut, omits the success flag to keep things short
  efft.Effect(strings.CutPrefix("hello world", "hello")).Equals(" world")

  // failing cut, stringifies the success flag too on failure
  efft.Effect(strings.CutPrefix("hello world", "world")).Equals(`
    [
      "hello world",
      false
    ]`)
}

You only need to write `efft.Expect(some-expression)` and `EFFUP=1 go test ./...` does the rest. I.e. write only this and the runner will rewrite it to the above:

func TestSplit(t *testing.T) {
  efft.Init(t)

  // simple cut
  efft.Effect(strings.CutPrefix("hello world", "hello"))

  // failing cut
  efft.Effect(strings.CutPrefix("hello world", "world"))
}

Note that if the function's last arg is a nil error or true boolean then it's automatically omitted.

Index

Constants

This section is empty.

Variables

View Source
var Context = 2

Context is the number of lines to display before and after the diff starts and ends.

View Source
var Diff = dummydiff

Diff is the function to diff the expectation against the got value. Defaults to a very simple diff treats all lines changed from the first until the last change.

View Source
var Note string

Note to include in the error message when there's a diff between the effect's got and wanted value.

Functions

func Effect added in v0.260131.0

func Effect(args ...any) result

Effect sets up an expectation. Effect accepts a list of any args so it can be used with functions that return multiple values. This is why the expectation has to be given in a separate function. See the package comment how to use this.

func FatalEffect added in v0.260131.0

func FatalEffect(args ...any) result

FatalEffect is same as Effect but aborts the test if the expectation doesn't match.

func Init

func Init(tt *testing.T)

Init setup efft for this testcase. Note that efft doesn't support sub- or parallel tests.

func Must

func Must(err any)

Must fails the current test if err is `false` or is a non-nil error. This is a convenience helper.

func Must1

func Must1[T any](v T, err any) T

Must1 fails the current test if err is `false` or is a non-nil error. The other return values are returned otherwise. This is a convenience helper.

func Must2

func Must2[A, B any](a A, b B, err any) (A, B)

Must2 fails the current test if err is `false` or is a non-nil error. The other return values are returned otherwise. This is a convenience helper.

func Override

func Override[T any](p *T, v T)

Override overrides `p` for the duration of the test. Its value is reset when the test ends. This is a convenience helper.

func Stringify added in v0.251225.1

func Stringify(args ...any) string

Stringify stringifies an expression. Can be used to stringify a single value or even as "efft.Stringify(somefunc())". If there are multiple args and the last one indicates an error then only that part is stringified. Otherwise the rest of the args are stringified into a reasonable format. This is a convenience helper.

Types

This section is empty.

Directories

Path Synopsis
Package internal contains internal helper functions.
Package internal contains internal helper functions.

Jump to

Keyboard shortcuts

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