This package provides a simple and systematic way to test
functions: we merely check that on given inputs, functions indeed
compute the expected outputs; tests are still managed via the
standard testing
package.
Tests can then be wrapped in a simple struct{}
:
type Test struct {
Name string // Test name
Fun any // Function to test
Args []any // Inputs
Expected []any // Expected outputs
}
Arrays of tests may then be tested using ftests.Run()
:
import (
"testing"
"github.com/mbivert/ftests"
)
func TestSomething(t *testing.T) {
ftests.Run(t, []ftests.Test{
{
"(x -> x)(42) = 42",
func(x any) any { return x },
[]interface{}{42},
[]interface{}{42},
},
{
...
},
...
})
}
"Weird" cases (e.g. environment alteration) can be managed by wrapping the code with a few extra functions. See this blog post for some examples.
Note: Because of the regularity of the tests format, it's easy to move towards or away from this pattern, to programatically alter the tests, to run the same tests in different settings, etc. In particular, it's a great way to progressively add tests to an existing codebase with almost no overhead.
Implementations for other languages are also provided. The main requirements are rather standard nowadays:
- tuples;
- deep-comparison mechanism.
go(1)
:perl(1)
:sh(1)
/cc(1)
:- JavaScript:
nix(1)
: