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

refactor: simplify error messages in test cases #795

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 4 additions & 6 deletions x/logic/predicate/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
)

func TestBank(t *testing.T) {
bench32DecodingFail := strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should simplify this. The idea proposed in the GitHub issue #771, as flagged by Dependabot, was to remove string processing and directly insert the final string result. While this may reduce readability, I tend to agree with the approach because it directly represents the content we get, simplifying maintenance.

For instance:

Suggested change
bench32DecodingFail := strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")
bench32DecodingFail := "d,e,c,o,d,i,n,g, ,b,e,c,h,3,2, ,f,a,i,l,e,d,:, ,i,n,v,a,l,i,d, ,b,e,c,h,3,2, ,s,t,r,i,n,g, ,l,e,n,g,t,h, ,3"

So basically, wherever you see strings.Join + strings.Split on a constant string in the tests, just replace it with the final result.

Convey("Under a mocked environment", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down Expand Up @@ -173,8 +174,7 @@ func TestBank(t *testing.T) {
balances: []bank.Balance{},
query: `bank_balances('foo', X).`,
wantResult: []testutil.TermResults{{"X": "[uaxone-100]"}},
wantError: fmt.Errorf("error(domain_error(encoding(bech32),foo),[%s],bank_balances/2)",
strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")),
wantError: fmt.Errorf("error(domain_error(encoding(bech32),foo),[%s],bank_balances/2)", bench32DecodingFail),
},
{
ctx: context.Background(),
Expand Down Expand Up @@ -308,8 +308,7 @@ func TestBank(t *testing.T) {
spendableCoins: []bank.Balance{},
query: `bank_spendable_balances('foo', X).`,
wantResult: []testutil.TermResults{{"X": "[uaxone-100]"}},
wantError: fmt.Errorf("error(domain_error(encoding(bech32),foo),[%s],bank_spendable_balances/2)",
strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")),
wantError: fmt.Errorf("error(domain_error(encoding(bech32),foo),[%s],bank_spendable_balances/2)", bench32DecodingFail),
},

{
Expand Down Expand Up @@ -453,8 +452,7 @@ func TestBank(t *testing.T) {
lockedCoins: []bank.Balance{},
query: `bank_locked_balances('foo', X).`,
wantResult: []testutil.TermResults{{"X": "[uaxone-100]"}},
wantError: fmt.Errorf("error(domain_error(encoding(bech32),foo),[%s],bank_locked_balances/2)",
strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")),
wantError: fmt.Errorf("error(domain_error(encoding(bech32),foo),[%s],bank_locked_balances/2)", bench32DecodingFail),
},
}
for nc, tc := range cases {
Expand Down
9 changes: 5 additions & 4 deletions x/logic/predicate/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func TestCryptoOperations(t *testing.T) {
}

func TestXVerify(t *testing.T) {
badPublicKeyLength := strings.Join(strings.Split("ed25519: bad public key length: 33", ""), ",")
failedToParsePublicKey := strings.Join(strings.Split("failed to parse compressed public key (first 10 bytes): 0213c8426be471e55506", ""), ",")

Convey("Given a test cases", t, func() {
cases := []struct {
program string
Expand Down Expand Up @@ -204,8 +207,7 @@ func TestXVerify(t *testing.T) {
eddsa_verify(PubKey, Msg, Sig, encoding(octet)).`,
query: `verify.`,
wantSuccess: false,
wantError: fmt.Errorf("error(syntax_error([%s]),eddsa_verify/4)",
strings.Join(strings.Split("ed25519: bad public key length: 33", ""), ",")),
wantError: fmt.Errorf("error(syntax_error([%s]),eddsa_verify/4)", badPublicKeyLength),
},
{ // Wrong signature
program: `verify :-
Expand Down Expand Up @@ -256,8 +258,7 @@ func TestXVerify(t *testing.T) {
ecdsa_verify(PubKey, Msg, Sig, encoding(octet)).`,
query: `verify.`,
wantSuccess: false,
wantError: fmt.Errorf("error(syntax_error([%s]),ecdsa_verify/4)",
strings.Join(strings.Split("failed to parse compressed public key (first 10 bytes): 0213c8426be471e55506", ""), ",")),
wantError: fmt.Errorf("error(syntax_error([%s]),ecdsa_verify/4)", failedToParsePublicKey),
},
{ // Unsupported algo
program: `verify :-
Expand Down
4 changes: 2 additions & 2 deletions x/logic/predicate/did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func TestDID(t *testing.T) {
invalidDID := strings.Join(strings.Split("invalid DID", ""), ",")
Convey("Given a test cases", t, func() {
cases := []struct {
program string
Expand Down Expand Up @@ -79,8 +80,7 @@ func TestDID(t *testing.T) {
{
query: `did_components('foo',X).`,
wantResult: []testutil.TermResults{},
wantError: fmt.Errorf("error(domain_error(encoding(did),foo),[%s],did_components/2)",
strings.Join(strings.Split("invalid DID", ""), ",")),
wantError: fmt.Errorf("error(domain_error(encoding(did),foo),[%s],did_components/2)", invalidDID),
},
{
query: `did_components(123,X).`,
Expand Down
5 changes: 3 additions & 2 deletions x/logic/predicate/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
)

func TestHexBytesPredicate(t *testing.T) {
hexEncodingErrorFmt := strings.Join(strings.Split("encoding/hex: invalid byte: U+0069 'i'", ""), ",")

Convey("Given a test cases", t, func() {
cases := []struct {
program string
Expand Down Expand Up @@ -58,8 +60,7 @@ func TestHexBytesPredicate(t *testing.T) {
{
query: `hex_bytes('fail',
[44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]).`,
wantError: fmt.Errorf("error(domain_error(encoding(hex),fail),[%s],hex_bytes/2)",
strings.Join(strings.Split("encoding/hex: invalid byte: U+0069 'i'", ""), ",")),
wantError: fmt.Errorf("error(domain_error(encoding(hex),fail),[%s],hex_bytes/2)", hexEncodingErrorFmt),
wantSuccess: false,
},
{
Expand Down
4 changes: 2 additions & 2 deletions x/logic/predicate/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func TestURIEncoded(t *testing.T) {
invalidURLEscape := strings.Join(strings.Split("invalid URL escape \"%%3\"", ""), ",")
Convey("Given a test cases", t, func() {
cases := []struct {
program string
Expand Down Expand Up @@ -167,8 +168,7 @@ func TestURIEncoded(t *testing.T) {
{
query: "uri_encoded(path, Decoded, 'bar%%3foo').",
wantSuccess: false,
wantError: fmt.Errorf("error(domain_error(encoding(uri),bar%%%%3foo),[%s],uri_encoded/3)",
strings.Join(strings.Split("invalid URL escape \"%%3\"", ""), ",")),
wantError: fmt.Errorf("error(domain_error(encoding(uri),bar%%%%3foo),[%s],uri_encoded/3)", invalidURLEscape),
},
}
for nc, tc := range cases {
Expand Down
Loading