-
Notifications
You must be signed in to change notification settings - Fork 20
/
testing.go
82 lines (67 loc) · 1.44 KB
/
testing.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package overflow
import (
"context"
"encoding/json"
"testing"
"github.com/onflow/flow-go/utils/io"
"github.com/stretchr/testify/require"
)
type OverflowTest struct {
O *OverflowState
height uint64
}
func (ot *OverflowTest) Reset() error {
block, err := ot.O.GetLatestBlock(context.Background())
if err != nil {
return err
}
height := block.Height
if ot.height != height {
return ot.O.RollbackToBlockHeight(ot.height)
}
return nil
}
func (ot *OverflowTest) Run(t *testing.T, name string, f func(t *testing.T)) {
t.Helper()
err := ot.Reset()
require.NoError(t, err)
t.Run(name, f)
err = ot.Reset()
require.NoError(t, err)
}
func (ot *OverflowTest) Teardown() {
report := ot.O.GetCoverageReport()
if report == nil {
return
}
bytes, err := json.MarshalIndent(report, "", " ")
if err != nil {
panic(err)
}
err = io.WriteFile("coverage-report.json", bytes)
if err != nil {
panic(err)
}
}
func SetupTest(opts []OverflowOption, setup func(o *OverflowState) error) (*OverflowTest, error) {
allOpts := []OverflowOption{WithNetwork("testing")}
allOpts = append(allOpts, opts...)
o := Overflow(allOpts...)
if o.Error != nil {
return nil, o.Error
}
err := setup(o)
if err != nil {
return nil, err
}
if o.Error != nil {
return nil, err
}
block, err := o.GetLatestBlock(context.Background())
if err != nil {
return nil, err
}
height := block.Height
ot := &OverflowTest{O: o, height: height}
return ot, nil
}