util: make tests use require

This commit is contained in:
Evgenii Stratonikov 2019-11-24 17:55:50 +03:00
parent ea9bc22510
commit 116d4c656e
3 changed files with 32 additions and 76 deletions

View file

@ -1,26 +1,23 @@
package util
import (
"bytes"
"testing"
"github.com/stretchr/testify/require"
)
func TestArrayEvenReverse(t *testing.T) {
arr := []byte{0x01, 0x02, 0x03, 0x04}
have := ArrayReverse(arr)
want := []byte{0x04, 0x03, 0x02, 0x01}
if !bytes.Equal(have, want) {
t.Fatalf("expected %v got %v", want, have)
}
require.Equal(t, want, have)
}
func TestArrayOddReverse(t *testing.T) {
arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
have := ArrayReverse(arr)
want := []byte{0x05, 0x04, 0x03, 0x02, 0x01}
if !bytes.Equal(have, want) {
t.Fatalf("expected %v got %v", want, have)
}
require.Equal(t, want, have)
}
// This tests a bug that occurred with arrays of size 1
@ -28,7 +25,5 @@ func TestArrayReverseLen2(t *testing.T) {
arr := []byte{0x01}
have := ArrayReverse(arr)
want := []byte{0x01}
if !bytes.Equal(have, want) {
t.Fatalf("expected %v got %v", want, have)
}
require.Equal(t, want, have)
}

View file

@ -5,54 +5,42 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUint160UnmarshalJSON(t *testing.T) {
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
expected, err := Uint160DecodeString(str)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
// UnmarshalJSON decodes hex-strings
var u1, u2 Uint160
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
t.Fatal(err)
}
assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
assert.True(t, expected.Equals(u1))
s, err := expected.MarshalJSON()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings prefixed by 0x
if err = u2.UnmarshalJSON(s); err != nil {
t.Fatal(err)
}
assert.NoError(t, u2.UnmarshalJSON(s))
assert.True(t, expected.Equals(u1))
}
func TestUInt160DecodeString(t *testing.T) {
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
val, err := Uint160DecodeString(hexStr)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
assert.Equal(t, hexStr, val.String())
}
func TestUint160DecodeBytes(t *testing.T) {
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
b, err := hex.DecodeString(hexStr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
val, err := Uint160DecodeBytes(b)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)
assert.Equal(t, hexStr, val.String())
}
@ -61,19 +49,12 @@ func TestUInt160Equals(t *testing.T) {
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
ua, err := Uint160DecodeString(a)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
ub, err := Uint160DecodeString(b)
if err != nil {
t.Fatal(err)
}
if ua.Equals(ub) {
t.Fatalf("%s and %s cannot be equal", ua, ub)
}
if !ua.Equals(ua) {
t.Fatalf("%s and %s must be equal", ua, ua)
}
require.NoError(t, err)
assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub)
assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua)
}
func TestUInt160Less(t *testing.T) {

View file

@ -12,49 +12,36 @@ import (
func TestUint256UnmarshalJSON(t *testing.T) {
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
expected, err := Uint256DecodeReverseString(str)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings
var u1, u2 Uint256
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
t.Fatal(err)
}
require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
assert.True(t, expected.Equals(u1))
s, err := expected.MarshalJSON()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
// UnmarshalJSON decodes hex-strings prefixed by 0x
if err = u2.UnmarshalJSON(s); err != nil {
t.Fatal(err)
}
require.NoError(t, u2.UnmarshalJSON(s))
assert.True(t, expected.Equals(u1))
}
func TestUint256DecodeString(t *testing.T) {
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
val, err := Uint256DecodeReverseString(hexStr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, hexStr, val.ReverseString())
}
func TestUint256DecodeBytes(t *testing.T) {
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
b, err := hex.DecodeString(hexStr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
val, err := Uint256DecodeReverseBytes(b)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
assert.Equal(t, hexStr, val.ReverseString())
}
@ -63,19 +50,12 @@ func TestUInt256Equals(t *testing.T) {
b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5"
ua, err := Uint256DecodeReverseString(a)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
ub, err := Uint256DecodeReverseString(b)
if err != nil {
t.Fatal(err)
}
if ua.Equals(ub) {
t.Fatalf("%s and %s cannot be equal", ua, ub)
}
if !ua.Equals(ua) {
t.Fatalf("%s and %s must be equal", ua, ua)
}
require.NoError(t, err)
assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub)
assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua)
}
func TestUint256_Serializable(t *testing.T) {