From 116d4c656e07ffcf151a0123cd9537a92bcdd102 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sun, 24 Nov 2019 17:55:50 +0300 Subject: [PATCH 1/4] util: make tests use require --- pkg/util/array_test.go | 15 +++++-------- pkg/util/uint160_test.go | 47 ++++++++++++---------------------------- pkg/util/uint256_test.go | 46 +++++++++++---------------------------- 3 files changed, 32 insertions(+), 76 deletions(-) diff --git a/pkg/util/array_test.go b/pkg/util/array_test.go index 13ab01330..da0ef9448 100644 --- a/pkg/util/array_test.go +++ b/pkg/util/array_test.go @@ -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) } diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index 20a4ff541..f2a37a74d 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -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) { diff --git a/pkg/util/uint256_test.go b/pkg/util/uint256_test.go index 975edec9f..1c6e58305 100644 --- a/pkg/util/uint256_test.go +++ b/pkg/util/uint256_test.go @@ -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) { From 18075bc5c89b7af2d529050eeb8b88d475f529b0 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sun, 24 Nov 2019 17:37:44 +0300 Subject: [PATCH 2/4] util: add missing tests for Fixed8 --- pkg/util/fixed8_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pkg/util/fixed8_test.go b/pkg/util/fixed8_test.go index aea003130..5841da3f2 100644 --- a/pkg/util/fixed8_test.go +++ b/pkg/util/fixed8_test.go @@ -64,6 +64,15 @@ func TestFixed8FromString(t *testing.T) { n, err = Fixed8FromString(val) assert.Nil(t, err) assert.Equal(t, Fixed8(90123410000), n) + + // Fixed8FromString with errors + val = "90n1" + _, err = Fixed8FromString(val) + assert.Error(t, err) + + val = "90.1s" + _, err = Fixed8FromString(val) + assert.Error(t, err) } func TestSatoshi(t *testing.T) { @@ -93,4 +102,35 @@ func TestFixed8UnmarshalJSON(t *testing.T) { assert.Nil(t, json.Unmarshal(s, &u2)) assert.Equal(t, expected, u2) } + + errorCases := []string{ + `"123.u"`, + "13.j", + } + + for _, tc := range errorCases { + var u Fixed8 + assert.Error(t, u.UnmarshalJSON([]byte(tc))) + } +} + +func TestFixed8_MarshalJSON(t *testing.T) { + u, err := Fixed8FromString("123.4") + assert.NoError(t, err) + + s, err := json.Marshal(u) + assert.NoError(t, err) + assert.Equal(t, []byte(`"123.4"`), s) +} + +func TestFixed8_Arith(t *testing.T) { + u1 := Fixed8FromInt64(3) + u2 := Fixed8FromInt64(8) + + assert.True(t, u1.LessThan(u2)) + assert.True(t, u2.GreaterThan(u1)) + assert.True(t, u1.Equal(u1)) + assert.NotZero(t, u1.CompareTo(u2)) + assert.Zero(t, u1.CompareTo(u1)) + assert.EqualValues(t, Fixed8(2), u2.Div(3)) } From d018dc4e569a29cc64e601c29c750e5eee89c1b1 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 25 Nov 2019 11:43:13 +0300 Subject: [PATCH 3/4] util: add missing tests for Uint160 --- pkg/util/uint160_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index f2a37a74d..f8e37d297 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -25,6 +25,8 @@ func TestUint160UnmarshalJSON(t *testing.T) { // UnmarshalJSON decodes hex-strings prefixed by 0x assert.NoError(t, u2.UnmarshalJSON(s)) assert.True(t, expected.Equals(u1)) + + assert.Error(t, u2.UnmarshalJSON([]byte(`123`))) } func TestUInt160DecodeString(t *testing.T) { @@ -32,6 +34,13 @@ func TestUInt160DecodeString(t *testing.T) { val, err := Uint160DecodeString(hexStr) assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) + + _, err = Uint160DecodeString(hexStr[1:]) + assert.Error(t, err) + + hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302" + _, err = Uint160DecodeString(hexStr) + assert.Error(t, err) } func TestUint160DecodeBytes(t *testing.T) { @@ -42,6 +51,9 @@ func TestUint160DecodeBytes(t *testing.T) { val, err := Uint160DecodeBytes(b) assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) + + _, err = Uint160DecodeBytes(b[1:]) + assert.Error(t, err) } func TestUInt160Equals(t *testing.T) { From ab922c181036e98eaa9bcf64de672af593329458 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 25 Nov 2019 11:51:34 +0300 Subject: [PATCH 4/4] util: add missing tests for Uint256 --- pkg/util/uint256_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/util/uint256_test.go b/pkg/util/uint256_test.go index 1c6e58305..6b6caaec7 100644 --- a/pkg/util/uint256_test.go +++ b/pkg/util/uint256_test.go @@ -26,6 +26,9 @@ func TestUint256UnmarshalJSON(t *testing.T) { // UnmarshalJSON decodes hex-strings prefixed by 0x require.NoError(t, u2.UnmarshalJSON(s)) assert.True(t, expected.Equals(u1)) + + // UnmarshalJSON does not accepts numbers + assert.Error(t, u2.UnmarshalJSON([]byte("123"))) } func TestUint256DecodeString(t *testing.T) { @@ -33,6 +36,21 @@ func TestUint256DecodeString(t *testing.T) { val, err := Uint256DecodeReverseString(hexStr) require.NoError(t, err) assert.Equal(t, hexStr, val.ReverseString()) + + bs, err := hex.DecodeString(hexStr) + require.NoError(t, err) + + val1, err := Uint256DecodeBytes(bs) + assert.NoError(t, err) + assert.Equal(t, hexStr, val1.String()) + assert.Equal(t, val, val1.Reverse()) + + _, err = Uint256DecodeReverseString(hexStr[1:]) + assert.Error(t, err) + + hexStr = "zzz7308fa0ab18155bccfc08485468c112409ea5064595699e98c545f245f32d" + _, err = Uint256DecodeReverseString(hexStr) + assert.Error(t, err) } func TestUint256DecodeBytes(t *testing.T) { @@ -43,6 +61,9 @@ func TestUint256DecodeBytes(t *testing.T) { val, err := Uint256DecodeReverseBytes(b) require.NoError(t, err) assert.Equal(t, hexStr, val.ReverseString()) + + _, err = Uint256DecodeBytes(b[1:]) + assert.Error(t, err) } func TestUInt256Equals(t *testing.T) { @@ -56,6 +77,7 @@ func TestUInt256Equals(t *testing.T) { 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) + assert.Zero(t, ua.CompareTo(ua), "%s and %s must be equal", ua, ua) } func TestUint256_Serializable(t *testing.T) {