good morning!!!!

Skip to content
Snippets Groups Projects
Commit fb3bfe7a authored by a's avatar a
Browse files

tests

parent bf640797
No related branches found
No related tags found
2 merge requests!15Draft: V2,!13Draft: Move Openrpc Package
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
}
package jrpctest_test
import (
"fmt"
"log"
"testing"
"gfx.cafe/open/jrpc/pkg/jrpctest"
)
func TestLoadTestData(t *testing.T) {
log.Println(jrpctest.OriginalTestData)
for _, file := range jrpctest.OriginalTestData.Files {
fmt.Printf("file %s:\n", file.Name)
for idx, pair := range file.Pairs {
fmt.Printf(" %d --> %s\n", idx, string(pair.Request))
for _, v := range pair.Responses {
fmt.Printf(" <-- %s\n", string(v))
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment