forked from TrueCloudLab/neoneo-go
util: make tests use require
This commit is contained in:
parent
ea9bc22510
commit
116d4c656e
3 changed files with 32 additions and 76 deletions
|
@ -1,26 +1,23 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestArrayEvenReverse(t *testing.T) {
|
func TestArrayEvenReverse(t *testing.T) {
|
||||||
arr := []byte{0x01, 0x02, 0x03, 0x04}
|
arr := []byte{0x01, 0x02, 0x03, 0x04}
|
||||||
have := ArrayReverse(arr)
|
have := ArrayReverse(arr)
|
||||||
want := []byte{0x04, 0x03, 0x02, 0x01}
|
want := []byte{0x04, 0x03, 0x02, 0x01}
|
||||||
if !bytes.Equal(have, want) {
|
require.Equal(t, want, have)
|
||||||
t.Fatalf("expected %v got %v", want, have)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayOddReverse(t *testing.T) {
|
func TestArrayOddReverse(t *testing.T) {
|
||||||
arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
|
arr := []byte{0x01, 0x02, 0x03, 0x04, 0x05}
|
||||||
have := ArrayReverse(arr)
|
have := ArrayReverse(arr)
|
||||||
want := []byte{0x05, 0x04, 0x03, 0x02, 0x01}
|
want := []byte{0x05, 0x04, 0x03, 0x02, 0x01}
|
||||||
if !bytes.Equal(have, want) {
|
require.Equal(t, want, have)
|
||||||
t.Fatalf("expected %v got %v", want, have)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This tests a bug that occurred with arrays of size 1
|
// This tests a bug that occurred with arrays of size 1
|
||||||
|
@ -28,7 +25,5 @@ func TestArrayReverseLen2(t *testing.T) {
|
||||||
arr := []byte{0x01}
|
arr := []byte{0x01}
|
||||||
have := ArrayReverse(arr)
|
have := ArrayReverse(arr)
|
||||||
want := []byte{0x01}
|
want := []byte{0x01}
|
||||||
if !bytes.Equal(have, want) {
|
require.Equal(t, want, have)
|
||||||
t.Fatalf("expected %v got %v", want, have)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,54 +5,42 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUint160UnmarshalJSON(t *testing.T) {
|
func TestUint160UnmarshalJSON(t *testing.T) {
|
||||||
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
str := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||||
expected, err := Uint160DecodeString(str)
|
expected, err := Uint160DecodeString(str)
|
||||||
if err != nil {
|
assert.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON decodes hex-strings
|
// UnmarshalJSON decodes hex-strings
|
||||||
var u1, u2 Uint160
|
var u1, u2 Uint160
|
||||||
|
|
||||||
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
|
assert.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.True(t, expected.Equals(u1))
|
assert.True(t, expected.Equals(u1))
|
||||||
|
|
||||||
s, err := expected.MarshalJSON()
|
s, err := expected.MarshalJSON()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
||||||
if err = u2.UnmarshalJSON(s); err != nil {
|
assert.NoError(t, u2.UnmarshalJSON(s))
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.True(t, expected.Equals(u1))
|
assert.True(t, expected.Equals(u1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUInt160DecodeString(t *testing.T) {
|
func TestUInt160DecodeString(t *testing.T) {
|
||||||
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||||
val, err := Uint160DecodeString(hexStr)
|
val, err := Uint160DecodeString(hexStr)
|
||||||
if err != nil {
|
assert.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, hexStr, val.String())
|
assert.Equal(t, hexStr, val.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUint160DecodeBytes(t *testing.T) {
|
func TestUint160DecodeBytes(t *testing.T) {
|
||||||
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||||
b, err := hex.DecodeString(hexStr)
|
b, err := hex.DecodeString(hexStr)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
val, err := Uint160DecodeBytes(b)
|
val, err := Uint160DecodeBytes(b)
|
||||||
if err != nil {
|
assert.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, hexStr, val.String())
|
assert.Equal(t, hexStr, val.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,19 +49,12 @@ func TestUInt160Equals(t *testing.T) {
|
||||||
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
|
b := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||||
|
|
||||||
ua, err := Uint160DecodeString(a)
|
ua, err := Uint160DecodeString(a)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
ub, err := Uint160DecodeString(b)
|
ub, err := Uint160DecodeString(b)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(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)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUInt160Less(t *testing.T) {
|
func TestUInt160Less(t *testing.T) {
|
||||||
|
|
|
@ -12,49 +12,36 @@ import (
|
||||||
func TestUint256UnmarshalJSON(t *testing.T) {
|
func TestUint256UnmarshalJSON(t *testing.T) {
|
||||||
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
str := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||||
expected, err := Uint256DecodeReverseString(str)
|
expected, err := Uint256DecodeReverseString(str)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON decodes hex-strings
|
// UnmarshalJSON decodes hex-strings
|
||||||
var u1, u2 Uint256
|
var u1, u2 Uint256
|
||||||
|
|
||||||
if err = u1.UnmarshalJSON([]byte(`"` + str + `"`)); err != nil {
|
require.NoError(t, u1.UnmarshalJSON([]byte(`"`+str+`"`)))
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.True(t, expected.Equals(u1))
|
assert.True(t, expected.Equals(u1))
|
||||||
|
|
||||||
s, err := expected.MarshalJSON()
|
s, err := expected.MarshalJSON()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
// UnmarshalJSON decodes hex-strings prefixed by 0x
|
||||||
if err = u2.UnmarshalJSON(s); err != nil {
|
require.NoError(t, u2.UnmarshalJSON(s))
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.True(t, expected.Equals(u1))
|
assert.True(t, expected.Equals(u1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUint256DecodeString(t *testing.T) {
|
func TestUint256DecodeString(t *testing.T) {
|
||||||
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||||
val, err := Uint256DecodeReverseString(hexStr)
|
val, err := Uint256DecodeReverseString(hexStr)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, hexStr, val.ReverseString())
|
assert.Equal(t, hexStr, val.ReverseString())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUint256DecodeBytes(t *testing.T) {
|
func TestUint256DecodeBytes(t *testing.T) {
|
||||||
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
hexStr := "f037308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d"
|
||||||
b, err := hex.DecodeString(hexStr)
|
b, err := hex.DecodeString(hexStr)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
val, err := Uint256DecodeReverseBytes(b)
|
val, err := Uint256DecodeReverseBytes(b)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, hexStr, val.ReverseString())
|
assert.Equal(t, hexStr, val.ReverseString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,19 +50,12 @@ func TestUInt256Equals(t *testing.T) {
|
||||||
b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5"
|
b := "e287c5b29a1b66092be6803c59c765308ac20287e1b4977fd399da5fc8f66ab5"
|
||||||
|
|
||||||
ua, err := Uint256DecodeReverseString(a)
|
ua, err := Uint256DecodeReverseString(a)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
ub, err := Uint256DecodeReverseString(b)
|
ub, err := Uint256DecodeReverseString(b)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(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)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUint256_Serializable(t *testing.T) {
|
func TestUint256_Serializable(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue