-
Notifications
You must be signed in to change notification settings - Fork 135
/
structs_encoding_test.go
95 lines (78 loc) · 1.84 KB
/
structs_encoding_test.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
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
95
package ethgo
import (
"bytes"
"encoding/json"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func compactJSON(s string) string {
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, []byte(s)); err != nil {
panic(err)
}
return buffer.String()
}
func TestDecodeL2Block(t *testing.T) {
c := readTestsuite(t, "./testsuite/arbitrum-block-full.json")
block := new(Block)
require.NoError(t, block.UnmarshalJSON(c[0].content))
for _, txn := range block.Transactions {
require.NotEqual(t, txn.Type, 0)
}
}
func TestEncodingJSON_Block(t *testing.T) {
for _, c := range readTestsuite(t, "./testsuite/block-*.json") {
content := []byte(compactJSON(string(c.content)))
txn := new(Block)
// unmarshal
err := txn.UnmarshalJSON(content)
assert.NoError(t, err)
// marshal back
res2, err := txn.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, content, res2)
}
}
func TestEncodingJSON_Transaction(t *testing.T) {
for _, c := range readTestsuite(t, "./testsuite/transaction-*.json") {
content := []byte(compactJSON(string(c.content)))
txn := new(Transaction)
// unmarshal
err := txn.UnmarshalJSON(content)
assert.NoError(t, err)
if c.name == "testsuite/transaction-eip1559-notype.json" {
continue
}
// marshal back
res2, err := txn.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, content, res2)
}
}
type testFile struct {
name string
content []byte
}
func readTestsuite(t *testing.T, pattern string) (res []*testFile) {
files, err := filepath.Glob(pattern)
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("no test files found")
}
for _, f := range files {
data, err := ioutil.ReadFile(f)
if err != nil {
t.Fatal(err)
}
res = append(res, &testFile{
name: f,
content: data,
})
}
return
}