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 ¶
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.
Click to show internal directories.
Click to hide internal directories.