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

Fix parsing arguments, use typehint #162

Merged
merged 15 commits into from
Jun 18, 2024
2 changes: 1 addition & 1 deletion contracts/Debug.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NonFungibleToken from "NonFungibleToken.cdc"
import "NonFungibleToken"

access(all) contract Debug {

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/bjartek/underflow v1.3.0
github.com/bjartek/underflow v1.5.0
github.com/enescakir/emoji v1.0.0
github.com/fatih/color v1.16.0
github.com/hexops/autogold v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,8 @@ github.com/bits-and-blooms/bitset v1.5.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edY
github.com/bits-and-blooms/bitset v1.7.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bitset v1.10.0 h1:ePXTeiPEazB5+opbv5fr8umg2R/1NlzgDsyepwsSr88=
github.com/bits-and-blooms/bitset v1.10.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bjartek/underflow v1.3.0 h1:HOE1ZEuAMkFUXCsXwV+gCFUHjyJA9d9nTIJu+RHI7SI=
github.com/bjartek/underflow v1.3.0/go.mod h1:JO6QNXSxgqr1CeegDk3DXbNhffRP1K/+DPBvB8Q+/N8=
github.com/bjartek/underflow v1.5.0 h1:0EJK6DmueoO+ffKzH8uhXkuVPL6FHiAQdzBsfrQ9s5Q=
github.com/bjartek/underflow v1.5.0/go.mod h1:M+rSteYN7KtCDUIjILxxUJRFyhpgOM1WFujJzyTDIP4=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
Expand Down
148 changes: 148 additions & 0 deletions parse_parameters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package overflow

import (
"fmt"
"testing"

"github.com/hexops/autogold"
"github.com/onflow/cadence"
"github.com/stretchr/testify/require"
)

func TestParseArguments(t *testing.T) {
o, err := OverflowTesting()
require.NoError(t, err)
require.NotNil(t, o)

type Args struct {
inputArgs map[string]interface{}
code string
}

type TestInput struct {
want autogold.Value
want1 autogold.Value
wantErr autogold.Value
name string
args Args
}

tests := []TestInput{
{
name: "64bit integers",
args: Args{
code: "u:UInt64, i:Int64",
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
inputArgs: map[string]interface{}{"u": 42, "i": 42},
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
},
want1: autogold.Want("map", CadenceArguments{"i": cadence.Int64(42), "u": cadence.UInt64(42)}),
want: autogold.Want("slice", []cadence.Value{cadence.UInt64(42), cadence.Int64(42)}),
wantErr: autogold.Want("err", nil),
},
{
name: "strings",
args: Args{
code: "s:String",
inputArgs: map[string]interface{}{"s": "foobar"},
},
want1: autogold.Want("map2", CadenceArguments{"s": cadence.String("foobar")}),
want: autogold.Want("slice2", []cadence.Value{cadence.String("foobar")}),
wantErr: autogold.Want("err2", nil),
},

{
name: "address",
args: Args{
code: "adr:Address",
inputArgs: map[string]interface{}{"adr": "first"},
},
want1: autogold.Want("map3", CadenceArguments{"adr": cadence.Address{
23,
155,
107,
28,
182,
117,
94,
49,
}}),
want: autogold.Want("slice3", []cadence.Value{cadence.Address{
23,
155,
107,
28,
182,
117,
94,
49,
}}),
wantErr: autogold.Want("err3", nil),
},
{
name: "missing argument",
args: Args{
code: "adr:Address",
inputArgs: map[string]interface{}{},
},
want1: autogold.Want("map4", CadenceArguments{}),
want: autogold.Want("slice4", []cadence.Value{}),
wantErr: autogold.Want("err4", "extracting arguments: the interaction 'somefile' is missing [adr]"),
},
{
name: "redundant argument",
args: Args{
code: "",
inputArgs: map[string]interface{}{"foo": "bar"},
},
want1: autogold.Want("map5", CadenceArguments{}),
want: autogold.Want("slice5", []cadence.Value{}),
wantErr: autogold.Want("err5", "extracting arguments: the interaction 'somefile' has the following extra arguments [foo]"),
},
{
name: "array of addresses",
args: Args{
code: "adr:[Address]",
inputArgs: map[string]interface{}{"adr": []string{"second"}},
},
want1: autogold.Want("map6", CadenceArguments{"adr": cadence.Array{
Values: []cadence.Value{cadence.Address{
243,
252,
210,
193,
167,
143,
94,
238,
}},
}}),
want: autogold.Want("slice6", []cadence.Value{cadence.Array{
Values: []cadence.Value{cadence.Address{
243,
252,
210,
193,
167,
143,
94,
238,
}},
}}),
wantErr: autogold.Want("err6", nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fmt.Println("testing")
code := []byte(fmt.Sprintf("transaction(%s){}", tt.args.code))
got, got1, err := o.parseArguments("somefile", code, tt.args.inputArgs)

tt.want.Equal(t, got)
tt.want1.Equal(t, got1)
if err != nil {
tt.wantErr.Equal(t, err.Error())
} else {
tt.wantErr.Equal(t, nil)
}
})
}
}
18 changes: 15 additions & 3 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,21 @@ func (o *OverflowBuilder) StartResult() *OverflowState {
if o.InputResolver != nil {
overflow.InputResolver = *o.InputResolver
} else {
overflow.InputResolver = func(name string) (string, error) {
return overflow.QualifiedIdentifierFromSnakeCase(name)
overflow.InputResolver = func(name string, resolveType underflow.ResolveType) (string, error) {
if resolveType == underflow.Identifier {
return overflow.QualifiedIdentifierFromSnakeCase(name)
}

adr, err2 := hexToAddress(name)
if err2 == nil {
return adr.String(), nil
}

address, err2 := overflow.FlowAddressE(name)
if err2 != nil {
return "", errors.Wrapf(err2, "could not parse %s into an address", name)
}
return address.HexWithPrefix(), nil
gabrielseibel1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
network, err := state.Networks().ByName(o.Network)
Expand Down Expand Up @@ -240,7 +253,6 @@ func (o *OverflowBuilder) StartResult() *OverflowState {
return overflow
}
}

if o.DeployContracts {
overflow = overflow.InitializeContracts(o.Ctx)
if overflow.Error != nil {
Expand Down
Loading
Loading