Skip to content

Commit d9cfcd7

Browse files
committed
feat: add contract guard to jsonrpc test
1 parent 1d0bb29 commit d9cfcd7

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package jsonrpc
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
type jsonrpcSchema struct {
15+
Methods []struct {
16+
Name string
17+
}
18+
}
19+
20+
func methodNameToFileName(methodName string) string {
21+
return "method_" + strings.TrimPrefix(methodName, "cartesi_") + "_test.go"
22+
}
23+
24+
// parse discover file and check if the files for method handlers exist
25+
func Test(t *testing.T) {
26+
data, err := discoverSpec.ReadFile("jsonrpc-discover.json")
27+
assert.Nil(t, err)
28+
29+
var schema jsonrpcSchema
30+
err = json.Unmarshal(data, &schema)
31+
assert.Nil(t, err)
32+
33+
for _, method := range schema.Methods {
34+
testFile := methodNameToFileName(method.Name)
35+
msg := fmt.Sprintf("file not found: %v (%v). assuming there is not test for it.",
36+
testFile, method.Name)
37+
38+
_, err := os.Stat(testFile)
39+
assert.False(t, errors.Is(err, os.ErrNotExist), msg)
40+
}
41+
}

0 commit comments

Comments
 (0)