Newer
Older
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
83
84
85
86
87
88
89
90
91
92
93
94
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
}
if d.IsDir() {
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)
currentPair := &TestPair{
Request: nil,
}
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) {
if currentPair.Request != nil {
t.Pairs = append(t.Pairs, currentPair)
}
currentPair = &TestPair{
Request: nil,
}
currentPair.Request = bytes.TrimSpace(bytes.TrimPrefix(txt, arrowRight))
continue
}
if bytes.HasPrefix(txt, arrowLeft) {
xs := bytes.TrimSpace(bytes.TrimPrefix(txt, arrowLeft))
currentPair.Responses = append(currentPair.Responses, xs)
continue
}
}
if currentPair.Request != nil {
t.Pairs = append(t.Pairs, currentPair)
}
return nil
}
type TestData struct {
Files []*TestFile
}
type TestFile struct {
Name string
Pairs []*TestPair
}
type TestPair struct {
Request json.RawMessage
Responses []json.RawMessage
}