Skip to content

Commit

Permalink
test(logic): add tests for TermToBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Oct 12, 2023
1 parent e4fdc8f commit e046dbf
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
4 changes: 2 additions & 2 deletions starship/tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"testing"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

func TestE2ETestSuite(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions starship/tests/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"os"
"time"

"github.com/cosmos/cosmos-sdk/types/module"

starship "github.com/cosmology-tech/starship/clients/go/client"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
lens "github.com/strangelove-ventures/lens/client"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"gopkg.in/yaml.v3"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
)

var configFile = "../configs/devnet.yaml"
Expand Down
107 changes: 107 additions & 0 deletions x/logic/predicate/util_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:lll
package predicate

import (
Expand Down Expand Up @@ -175,3 +176,109 @@ func TestOptionsContains(t *testing.T) {
}
})
}

func TestTermToBytes(t *testing.T) {
Convey("Given a test cases", t, func() {
cases := []struct {
term engine.Term
options engine.Term
result []byte
wantSuccess bool
wantError error
}{
{ // If no option, by default, given term is in hexadecimal format.
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: nil,
result: []byte{72, 101, 121, 32, 33, 32, 89, 111, 117, 32, 119, 97, 110, 116, 32, 116, 111, 32, 115, 101, 101, 32, 116, 104, 105, 115, 32, 116, 101, 120, 116, 44, 32, 119, 111, 110, 100, 101, 114, 102, 117, 108, 33},
wantSuccess: true,
},
{
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("hex")),
result: []byte{72, 101, 121, 32, 33, 32, 89, 111, 117, 32, 119, 97, 110, 116, 32, 116, 111, 32, 115, 101, 101, 32, 116, 104, 105, 115, 32, 116, 101, 120, 116, 44, 32, 119, 111, 110, 100, 101, 114, 102, 117, 108, 33},
wantSuccess: true,
},
{
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("octet")),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("invalid term type: engine.Atom, should be a List"),
},
{
term: engine.List(engine.Integer(72), engine.Integer(101), engine.Integer(121), engine.Integer(32), engine.Integer(33), engine.Integer(32), engine.Integer(89), engine.Integer(111), engine.Integer(117), engine.Integer(32), engine.Integer(119), engine.Integer(97), engine.Integer(110), engine.Integer(116), engine.Integer(32), engine.Integer(116), engine.Integer(111), engine.Integer(32), engine.Integer(115), engine.Integer(101), engine.Integer(101), engine.Integer(32), engine.Integer(116), engine.Integer(104), engine.Integer(105), engine.Integer(115), engine.Integer(32), engine.Integer(116), engine.Integer(101), engine.Integer(120), engine.Integer(116), engine.Integer(44), engine.Integer(32), engine.Integer(119), engine.Integer(111), engine.Integer(110), engine.Integer(100), engine.Integer(101), engine.Integer(114), engine.Integer(102), engine.Integer(117), engine.Integer(108), engine.Integer(33)),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("octet")),
result: []byte{72, 101, 121, 32, 33, 32, 89, 111, 117, 32, 119, 97, 110, 116, 32, 116, 111, 32, 115, 101, 101, 32, 116, 104, 105, 115, 32, 116, 101, 120, 116, 44, 32, 119, 111, 110, 100, 101, 114, 102, 117, 108, 33},
wantSuccess: true,
},
{
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("foo")),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("invalid encoding option: foo, valid value are 'hex' or 'octet'"),
},
{
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("foo"), engine.NewAtom("bar")),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("invalid arity for encoding option, should be 1"),
},
{
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: engine.NewAtom("encoding").Apply(engine.NewVariable()),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("invalid given options"),
},
{
term: engine.NewAtom("foo").Apply(engine.NewAtom("bar")),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("octet")),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("term should be a List, give *engine.compound"),
},
{
term: engine.NewAtom("foo").Apply(engine.NewAtom("bar")),
options: engine.NewAtom("encoding").Apply(engine.NewAtom("hex")),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("invalid term type: *engine.compound, should be String"),
},
{
term: engine.NewAtom("486579202120596f752077616e7420746f20736565207468697320746578742c20776f6e64657266756c21"),
options: engine.NewAtom("foo"),
result: nil,
wantSuccess: false,
wantError: fmt.Errorf("invalid options term, should be compound, give engine.Atom"),
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the term #%d: %s", nc, tc.term), func() {
Convey("when check try convert", func() {
env := engine.Env{}
result, err := TermToBytes(tc.term, tc.options, &env)

if tc.wantSuccess {
Convey("then no error should be thrown", func() {
So(err, ShouldBeNil)

Convey("and result should be as expected", func() {
So(result, ShouldResemble, tc.result)
})
})
} else {
Convey("then error should occurs", func() {
So(err, ShouldNotBeNil)

Convey("and should be as expected", func() {
So(err, ShouldResemble, tc.wantError)
})
})
}
})
})
}
})
}

0 comments on commit e046dbf

Please sign in to comment.