Skip to content

Commit 729a024

Browse files
committed
feat(jsonrpc): add contract guard
1 parent ba1badc commit 729a024

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)