Help us shape the future of Go! Take the 2026 Go Developer Survey

fun

package module
v0.0.0-...-ba0a654 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package fun is a library for quickly testing functions.

Example:

package example

import (
	"errors"
	"testing"

	"github.com/joelterry/fun"
)

func foo(s string) (string, error) {
	if len(s) == 0 {
		panic("expected non-empty string")
	}
	if s[0:3] == "foo" {
		return "", errors.New("you can't foo a foo")
	}
	return s + " foo", nil
}

func TestAdd(t *testing.T) {

	f := fun.Test(t, foo)
	f.In("bar").Out("bar foo", nil)
	f.In("foo").Err()
	f.In("").Panic()

	// You can optionally chain the test cases, leave off the last return
	// value if you expect a nil error, and test for specific error/panic values.

	fun.Test(t, foo).
		In("bar").Out("bar foo").
		In("foo").Err(errors.New("you can't foo a foo")).
		In("").Panic("expected non-empty string")

	// As shorthand you can call Err(nil) to check that an error wasn't returned.
	// You can't do the same for Panic, since nil panic values are possible.

	fun.Test(t, foo).In("bar").Err(nil)
	fun.Test(t, foo).In("bar").Panic(nil) // fails
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Case

type Case struct {
	// contains filtered or unexported fields
}

Case contains the Out/Err/Panic methods, and can be ignored as a type.

func (Case) Err

func (c Case) Err(v ...interface{}) (ret *FunTest)

Err should be called instead of Out if you just want to check for an error. This is only valid if the tested function's final return value is an error.

You can optionally pass in an error if you're expecting something specific.

func (Case) Out

func (c Case) Out(results ...interface{}) (ret *FunTest)

Out is where you pass in the return variables that you expect.

It should follow In() in a call chain.

If the last return type is an error, and you expect it to be nil, you may leave it out.

func (Case) Panic

func (c Case) Panic(v ...interface{}) (ret *FunTest)

Panic should be called instead of Out if you want to check that a panic occured.

You can optionally pass in a value if you're expecting something specific.

type FunTest

type FunTest struct {
	// contains filtered or unexported fields
}

FunTest contains the In method, and can be ignored as a type.

func Test

func Test(t *testing.T, fun interface{}) *FunTest

Test is the entry point for testing a function. fun must be a func.

func (*FunTest) In

func (ft *FunTest) In(args ...interface{}) Case

In is where you pass in the arguments you want to test.

It can either be called from the value returned by Test, or after Out/Err/Panic in a chain.

Jump to

Keyboard shortcuts

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