Newer
Older
package jrpctest
import (
"bufio"
"bytes"
"embed"
"encoding/json"
"io"
"io/fs"
)
//go:embed testdata
var originalTestDataFS embed.FS
var OriginalTestData = &TestData{}
func init() {
err := fs.WalkDir(originalTestDataFS, "testdata", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
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
return nil
}
file, err := originalTestDataFS.Open(path)
if err != nil {
return err
}
OriginalTestData.AddTestData(d.Name(), file)
return nil
})
if err != nil {
panic(err)
}
}
func (td *TestData) AddTestData(name string, rd io.Reader) *TestData {
s := bufio.NewScanner(rd)
t := &TestFile{
Name: name,
}
td.Files = append(td.Files, t)
var (
arrowRight = []byte("-->")
arrowLeft = []byte("<--")
comment = []byte("//")
)
for s.Scan() {
txt := bytes.TrimSpace(s.Bytes())
if string(txt) == "" {
continue
}
// ignore comments
if bytes.HasPrefix(txt, comment) {
continue
}
if bytes.HasPrefix(txt, arrowRight) {
currentPair.Data = bytes.TrimSpace(bytes.TrimPrefix(txt, arrowRight))
t.Action = append(t.Action, currentPair)
currentPair := &TestAction{
Direction: DirectionRecv,
}
currentPair.Data = bytes.TrimSpace(bytes.TrimPrefix(txt, arrowLeft))
t.Action = append(t.Action, currentPair)
continue
}
}
return nil
}
type TestData struct {
Files []*TestFile
}
type TestFile struct {
type TestDirection string
const (
DirectionSend = TestDirection("send")
DirectionRecv = TestDirection("recv")
)
type TestAction struct {
Direction TestDirection
Data json.RawMessage