Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: tests #994

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
458 changes: 167 additions & 291 deletions internal/bcd/ast/ast_test.go

Large diffs are not rendered by default.

24 changes: 9 additions & 15 deletions internal/bcd/ast/bytes_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ast

import (
"reflect"
"testing"

"github.com/baking-bad/bcdhub/internal/testsuite"
"github.com/stretchr/testify/require"
)

func TestBytes_ToMiguel(t *testing.T) {
Expand All @@ -20,30 +22,22 @@ func TestBytes_ToMiguel(t *testing.T) {
want: &MiguelNode{
Prim: "bytes",
Type: "bytes",
Name: getStringPtr("@bytes_1"),
Name: testsuite.Ptr("@bytes_1"),
Value: `{ Pair "ledger" "tz1b1L4P8P1ucwuqCEP1Hxs7KB68CX8prFCp" }`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tree, err := NewSettledTypedAst(tt.tree, tt.node)
if err != nil {
t.Errorf("NewSettledTypedAst error %v", err)
return
}
require.NoError(t, err)

got, err := tree.Nodes[0].ToMiguel()
if (err != nil) != tt.wantErr {
t.Errorf("Bytes.ToMiguel() error = %v, wantErr %v", err, tt.wantErr)
require.Equal(t, tt.wantErr, err != nil)
if err != nil {
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Bytes.ToMiguel() = %v, want %v", got, tt.want)
}
require.Equal(t, tt.want, got)
})
}
}

func getStringPtr(val string) *string {
return &val
}
31 changes: 10 additions & 21 deletions internal/bcd/ast/contract_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ast
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFindContractInterface(t *testing.T) {
Expand Down Expand Up @@ -78,20 +78,13 @@ func TestFindContractInterface(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tree UntypedAST
if err := json.UnmarshalFromString(tt.tree, &tree); err != nil {
t.Errorf("UnmarshalFromString() error = %v", err)
return
}
err := json.UnmarshalFromString(tt.tree, &tree)
require.NoError(t, err)

typedTree, err := tree.ToTypedAST()
if err != nil {
t.Errorf("ToTypedAST() error = %v", err)
return
}
require.NoError(t, err)

if got := FindContractInterface(typedTree, tt.interfaceName); got != tt.want {
t.Errorf("FindContractInterface() = %v, want %v", got, tt.want)
}
require.Equal(t, tt.want, FindContractInterface(typedTree, tt.interfaceName))
})
}
}
Expand All @@ -111,18 +104,14 @@ func TestFindContractInterfaces(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tree UntypedAST
if err := json.UnmarshalFromString(tt.tree, &tree); err != nil {
t.Errorf("UnmarshalFromString() error = %v", err)
return
}
err := json.UnmarshalFromString(tt.tree, &tree)
require.NoError(t, err)

typedTree, err := tree.ToTypedAST()
if err != nil {
t.Errorf("ToTypedAST() error = %v", err)
return
}
require.NoError(t, err)

got := FindContractInterfaces(typedTree)
assert.ElementsMatch(t, tt.want, got)
require.ElementsMatch(t, tt.want, got)
})
}
}
51 changes: 15 additions & 36 deletions internal/bcd/ast/ordered_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ast

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestOrderedMap_Add(t *testing.T) {
Expand Down Expand Up @@ -55,52 +57,29 @@ func TestOrderedMap_Add(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
m := NewOrderedMap()
for i := range tt.data {
if err := m.Add(tt.data[i].key, tt.data[i].value); (err != nil) != tt.wantErr {
t.Errorf("OrderedMap.Add() error = %v, wantErr %v", err, tt.wantErr)
return
}
err := m.Add(tt.data[i].key, tt.data[i].value)
require.NoError(t, err)
}

if len(tt.wantKeys) != m.Len() {
t.Errorf("OrderedMap.Len() len(tt.wantKeys) = %d, m.Len() = %d", len(tt.wantKeys), m.Len())
return
}
require.Equal(t, len(tt.wantKeys), m.Len())

for i := range tt.wantKeys {
res, err := tt.wantKeys[i].Compare(m.keys[i])
if err != nil {
t.Errorf("Compare err = %v", err)
return
}
if res != 0 {
t.Errorf("Compare res=%d, i=%d", res, i)
return
}
require.NoError(t, err)
require.Equal(t, res, 0)
}

receive, ok := m.Get(tt.getKey)
if !ok {
t.Errorf("Get ok = %v", ok)
return
}
require.True(t, ok)

res, err := receive.Compare(tt.wantForGet)
if err != nil {
t.Errorf("receive.Compare err = %v", err)
return
}
if res != 0 {
t.Errorf("receive.Compare res=%d", res)
return
}
require.NoError(t, err)
require.Equal(t, res, 0)

if _, ok := m.Remove(tt.remove); !ok {
t.Errorf("OrderedMap.Remove() ok=%v", ok)
return
}
if tt.lengthAfterRemove != m.Len() {
t.Errorf("lengthAfterRemove=%d, m.Len()=%d", tt.lengthAfterRemove, m.Len())
return
}
_, ok = m.Remove(tt.remove)
require.True(t, ok)

require.Equal(t, tt.lengthAfterRemove, m.Len())

_ = m.Range(func(key, value Comparable) (bool, error) {
return false, nil
Expand Down
17 changes: 6 additions & 11 deletions internal/bcd/ast/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/baking-bad/bcdhub/internal/bcd/base"
"github.com/stretchr/testify/require"
)

func TestBigMapKeyHash(t *testing.T) {
Expand Down Expand Up @@ -42,18 +43,12 @@ func TestBigMapKeyHash(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var node base.Node
if err := json.UnmarshalFromString(tt.input, &node); err != nil {
t.Errorf("UnmarshalFromString error: %v", err)
return
}
err := json.UnmarshalFromString(tt.input, &node)
require.NoError(t, err)

result, err := BigMapKeyHash(&node)
if err != nil {
t.Errorf("error in Key, error: %v", err)
return
}
if result != tt.expected {
t.Errorf("error in Key, got: %v, expected: %v", result, tt.expected)
}
require.NoError(t, err)
require.Equal(t, tt.expected, result)
})
}
}
14 changes: 8 additions & 6 deletions internal/bcd/ast/simple_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ast

import "testing"
import (
"testing"

"github.com/stretchr/testify/require"
)

func TestAddress_Compare(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -81,13 +85,11 @@ func TestAddress_Compare(t *testing.T) {
second.ValueKind = tt.secondType

got, err := first.Compare(second)
if (err != nil) != tt.wantErr {
t.Errorf("Address.Compare() error = %v, wantErr %v", err, tt.wantErr)
require.Equal(t, tt.wantErr, err != nil)
if err != nil {
return
}
if got != tt.want {
t.Errorf("Address.Compare() = %v, want %v", got, tt.want)
}
require.Equal(t, tt.want, got)
})
}
}
10 changes: 6 additions & 4 deletions internal/bcd/ast/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/baking-bad/bcdhub/internal/bcd/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTimestamp_ParseValue(t *testing.T) {
Expand Down Expand Up @@ -46,10 +46,12 @@ func TestTimestamp_ParseValue(t *testing.T) {
node := &base.Node{
StringValue: &tt.ts,
}
if err := ts.ParseValue(node); (err != nil) != tt.wantErr {
t.Errorf("Timestamp.ParseValue() error = %v, wantErr %v", err, tt.wantErr)
err := ts.ParseValue(node)
require.Equal(t, tt.wantErr, err != nil)
if err != nil {
return
}
assert.Equal(t, tt.want, ts.Value)
require.Equal(t, tt.want, ts.Value)
})
}
}
18 changes: 8 additions & 10 deletions internal/bcd/ast/untyped_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ast

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestUntypedAST_GetStrings(t *testing.T) {
Expand All @@ -23,18 +24,15 @@ func TestUntypedAST_GetStrings(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tree UntypedAST
if err := json.UnmarshalFromString(tt.tree, &tree); err != nil {
t.Errorf("UntypedAST.GetStrings() error = %v, wantErr %v", err, tt.wantErr)
return
}
err := json.UnmarshalFromString(tt.tree, &tree)
require.NoError(t, err)

got, err := tree.GetStrings(tt.tryUnpack)
if (err != nil) != tt.wantErr {
t.Errorf("UntypedAST.GetStrings() error = %v, wantErr %v", err, tt.wantErr)
require.Equal(t, tt.wantErr, err != nil)
if err != nil {
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UntypedAST.GetStrings() = %v, want %v", got, tt.want)
}
require.Equal(t, tt.want, got)
})
}
}
37 changes: 16 additions & 21 deletions internal/bcd/ast/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ast

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestAddressValidator(t *testing.T) {
Expand Down Expand Up @@ -34,9 +36,8 @@ func TestAddressValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := AddressValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("AddressValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := AddressValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Expand Down Expand Up @@ -67,9 +68,8 @@ func TestBakerHashValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := BakerHashValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("BakerHashValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := BakerHashValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Expand Down Expand Up @@ -100,9 +100,8 @@ func TestPublicKeyValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := PublicKeyValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("PublicKeyValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := PublicKeyValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Expand Down Expand Up @@ -133,9 +132,8 @@ func TestBytesValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := BytesValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("BytesValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := BytesValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Expand All @@ -162,9 +160,8 @@ func TestChainIDValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ChainIDValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("ChainIDValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := ChainIDValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Expand Down Expand Up @@ -199,9 +196,8 @@ func TestSignatureValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := SignatureValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("SignatureValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := SignatureValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Expand All @@ -220,9 +216,8 @@ func TestContractValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ContractValidator(tt.value); (err != nil) != tt.wantErr {
t.Errorf("ContractValidator() error = %v, wantErr %v", err, tt.wantErr)
}
err := ContractValidator(tt.value)
require.Equal(t, tt.wantErr, err != nil)
})
}
}
Loading
Loading