Categorygithub.com/podhmo/or
repositorypackage
0.0.3
Repository: https://github.com/podhmo/or.git
Documentation: pkg.go.dev

# README

or

shorthand for testing in go

required

go 1.18 (for generics)

how to use

Fatal() function is a shorthand for the factory function returning with error value (e.g. func(...) (T, error))

Use it like this.

package main

import (
	"fmt"
	"testing"

	"github.com/podhmo/or"
)

type Foo struct{}

func getFoo() (*Foo, error) { return &Foo{}, nil }

type Bar struct{ Foo *Foo }

func getBar(foo *Foo) (*Bar, error) { return nil, fmt.Errorf("hmm") }

type Boo struct{ Foo *Foo }

func getBoo(foo *Foo) (*Boo, func(), error) { return nil, nil, fmt.Errorf("hmm") }

func TestFatal(t *testing.T) {
	foo := or.Fatal(getFoo())(t)
	bar := or.Fatal(getBar(foo))(t)

	_ = bar // doSomething
}

func TestFatalWithCleanup(t *testing.T) {
	foo := or.Fatal(getFoo())(t)
	boo := or.FatalWithCleanup(getBoo(foo))(t)

	_ = boo // doSomething
}

The result is here.

$ go test
--- FAIL: TestFatal (0.00s)
    example_test.go:24: accessing fail (*main.Bar): hmm
--- FAIL: TestFatalWithCleanup (0.00s)
    example_test.go:31: accessing fail (*main.Boo): hmm
FAIL
exit status 1
FAIL	github.com/podhmo/or/testdata	0.003s

why not or.Fatal(t, getFoo() ?

There are two reasons.

  • go's multiple values specification
  • go's generics specification