forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 22
/
testing.go
68 lines (54 loc) · 1.21 KB
/
testing.go
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
package precompiled
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"github.com/dogechain-lab/dogechain/helper/hex"
)
type TestCase struct {
Name string
Input []byte
Expected []byte
Gas uint64
}
// decodeHex is a helper function for decoding a hex string
func decodeHex(t *testing.T, input string) []byte {
t.Helper()
inputDecode, decodeErr := hex.DecodeHex(input)
if decodeErr != nil {
t.Fatalf("unable to decode hex, %v", decodeErr)
}
return inputDecode
}
func ReadTestCase(t *testing.T, path string, f func(t *testing.T, c *TestCase)) {
t.Helper()
data, err := ioutil.ReadFile(filepath.Join("./fixtures", path))
if err != nil {
t.Fatal(err)
}
type testCase struct {
Name string
Input string
Expected string
Gas uint64
}
var cases []*testCase
if err := json.Unmarshal(data, &cases); err != nil {
t.Fatal(err)
}
for _, i := range cases {
inputDecode := decodeHex(t, fmt.Sprintf("0x%s", i.Input))
expectedDecode := decodeHex(t, fmt.Sprintf("0x%s", i.Expected))
c := &TestCase{
Name: i.Name,
Gas: i.Gas,
Input: inputDecode,
Expected: expectedDecode,
}
t.Run(i.Name, func(t *testing.T) {
f(t, c)
})
}
}