From ea10abb42a987f09a2afb6f41100154e9007bc53 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 2 May 2023 11:58:29 +0300 Subject: [PATCH] [#296] morph: Add parser unit tests Signed-off-by: Dmitrii Stepanov --- pkg/morph/event/utils_test.go | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 pkg/morph/event/utils_test.go diff --git a/pkg/morph/event/utils_test.go b/pkg/morph/event/utils_test.go new file mode 100644 index 000000000..83facc653 --- /dev/null +++ b/pkg/morph/event/utils_test.go @@ -0,0 +1,48 @@ +package event + +import ( + "math/big" + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/stretchr/testify/require" +) + +func TestParseStackArray(t *testing.T) { + t.Run("success", func(t *testing.T) { + arr := &stackitem.Array{} + arr.Append(stackitem.NewBigInteger(big.NewInt(1))) + arr.Append(stackitem.NewBigInteger(big.NewInt(2))) + ev := &state.ContainedNotificationEvent{ + Container: util.Uint256{67}, + NotificationEvent: state.NotificationEvent{ + ScriptHash: util.Uint160{69}, + Name: "name", + Item: arr, + }, + } + + items, err := ParseStackArray(ev) + require.NoError(t, err, "failed to parse event items") + require.Equal(t, 2, len(items), "invalid length") + require.Equal(t, stackitem.NewBigInteger(big.NewInt(1)), items[0], "invalid item 0") + require.Equal(t, stackitem.NewBigInteger(big.NewInt(2)), items[1], "invalid item 0") + }) + t.Run("empty stack error", func(t *testing.T) { + arr := &stackitem.Array{} + ev := &state.ContainedNotificationEvent{ + Container: util.Uint256{67}, + NotificationEvent: state.NotificationEvent{ + ScriptHash: util.Uint160{69}, + Name: "name", + Item: arr, + }, + } + + items, err := ParseStackArray(ev) + require.ErrorIs(t, err, errEmptyStackArray, "invalid empty array error") + require.Equal(t, 0, len(items), "items was returned") + }) +}