forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs_encoding_test.go
79 lines (66 loc) · 1.47 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
package ethgo
import (
"bytes"
"encoding/json"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func compactJSON(s string) string {
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, []byte(s)); err != nil {
panic(err)
}
return buffer.String()
}
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)
// 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
}