forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_rapid_test.go
51 lines (44 loc) · 1.34 KB
/
step_rapid_test.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
package main
import (
"log"
"os"
"path/filepath"
"testing"
"pgregory.net/rapid"
)
// TestReadAndWriteTrace uses rapid to do property based testing
// of reading and writing traces.
// It generates a random trace, writes it to a file, then reads it back.
// It then compares the original trace to the read trace.
// If the traces are not equal, rapid will generate a minimal example
// that causes the test to fail.
func TestReadAndWriteTrace(t *testing.T) {
parser := JSONParser{}
writer := JSONWriter{}
dir, err := os.MkdirTemp("", "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
rapid.Check(t, func(t *rapid.T) {
trace := GetTraceGen().Draw(t, "Trace")
filename := filepath.Join(dir, "trace.json")
err := WriteAndReadTrace(parser, writer, trace, filename)
if err != nil {
t.Fatalf("error writing and reading trace: %v", err)
}
})
}
// This can be used to test writing and parsing traces, but does not make much sense
// for testing trace execution, since the generated traces are almost guaranteed to be nonsensical.
func GetTraceGen() *rapid.Generator[[]Step] {
return rapid.SliceOf(GetStepGen())
}
func GetStepGen() *rapid.Generator[Step] {
return rapid.Custom(func(t *rapid.T) Step {
return Step{
Action: GetActionGen().Draw(t, "Action"),
State: GetStateGen().Draw(t, "State"),
}
})
}