1
0
mirror of https://github.com/fluencelabs/tendermint synced 2025-06-28 20:21:47 +00:00
Files
autofile
cli
clist
common
LICENSE
array.go
async.go
bit_array.go
bit_array_test.go
bytes.go
bytes_test.go
byteslice.go
cmap.go
cmap_test.go
colors.go
date.go
date_test.go
errors.go
heap.go
int.go
int_test.go
io.go
kvpair.go
math.go
net.go
net_test.go
os.go
os_test.go
random.go
random_test.go
repeat_timer.go
repeat_timer_test.go
service.go
service_test.go
string.go
string_test.go
throttle_timer.go
throttle_timer_test.go
types.pb.go
types.proto
word.go
db
events
flowrate
log
merkle
pubsub
test
version
.editorconfig
.gitignore
CHANGELOG.md
CODEOWNERS
LICENSE
Makefile
README.md
circle.yml
glide.lock
glide.yaml
merge.sh
test.sh
tendermint/common/bytes_test.go

66 lines
1.3 KiB
Go
Raw Normal View History

2017-12-27 13:52:32 -08:00
package common
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// This is a trivial test for protobuf compatibility.
func TestMarshal(t *testing.T) {
bz := []byte("hello world")
dataB := HexBytes(bz)
bz2, err := dataB.Marshal()
assert.Nil(t, err)
assert.Equal(t, bz, bz2)
var dataB2 HexBytes
err = (&dataB2).Unmarshal(bz)
assert.Nil(t, err)
assert.Equal(t, dataB, dataB2)
}
// Test that the hex encoding works.
func TestJSONMarshal(t *testing.T) {
type TestStruct struct {
B1 []byte
B2 HexBytes
}
cases := []struct {
input []byte
expected string
}{
{[]byte(``), `{"B1":"","B2":""}`},
{[]byte(``), `{"B1":"","B2":""}`},
{[]byte(``), `{"B1":"","B2":""}`},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
ts := TestStruct{B1: tc.input, B2: tc.input}
// Test that it marshals correctly to JSON.
jsonBytes, err := json.Marshal(ts)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(jsonBytes), tc.expected)
// TODO do fuzz testing to ensure that unmarshal fails
// Test that unmarshaling works correctly.
ts2 := TestStruct{}
err = json.Unmarshal(jsonBytes, &ts2)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, ts2.B1, tc.input)
assert.Equal(t, ts2.B2, HexBytes(tc.input))
})
}
}