-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_table_test.go
More file actions
57 lines (49 loc) · 1.85 KB
/
Copy pathexample_table_test.go
File metadata and controls
57 lines (49 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package testx_test
import (
"errors"
"testing"
"github.com/drykit-go/testx"
"github.com/drykit-go/testx/check"
"github.com/drykit-go/testx/checkconv"
)
func ExampleTable_monadic() {
t := &testing.T{} // ignore: emulating a testing context
// double is the func to be tested.
double := func(x float64) float64 { return 2 * x }
// func double has 1 parameter and 1 return value,
// hence no config is needed
testx.Table(double).Cases([]testx.Case{
{In: 0.0, Exp: 0.0},
{In: -2.0, Pass: checkconv.AssertMany(check.Float64.InRange(-5, -3))},
}).Run(t)
}
func ExampleTable_dyadic() {
t := &testing.T{} // ignore: emulating a testing context
// divide is the func to be tested.
// It returns x/y or a non-nil error if y == 0
divide := func(x, y float64) (float64, error) {
if y == 0 {
return 0, errors.New("division by 0")
}
return x / y, nil
}
// func divide has several parameters and return values,
// so we specify a config to determinate:
// - at which param position Case.In is injected
// - the values used for the other arguments (fixed for all cases)
// - which return value we want to compare Case.Exp with
//
// In this example, we check the error value of divide (return value
// at position 1).
// We inject Case.In at position 1 (param y) and use a fixed value
// of 42.0 at position 0 (param x) for all cases.
testx.Table(divide).Config(testx.TableConfig{
// Positions start at 0
InPos: 1, // Case.In injected in param position 1 (y)
OutPos: 1, // Case.Exp compared to return value position 1 (error value)
FixedArgs: []interface{}{0: 42.0}, // param 0 (x) set to 42.0 for all cases
}).Cases([]testx.Case{
{In: 1.0, Exp: testx.ExpNil}, // divide(42.0, 1.0) -> (_, nil)
{In: 0.0, Exp: errors.New("division by 0")}, // divide(42.0, 0.0) -> (_, err)
}).Run(t)
}