-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rc/v0.48.0-rcx' into bump/ostracon
- Loading branch information
Showing
7 changed files
with
832 additions
and
681 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package testutil | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func MustJSONMarshal(v any) []byte { | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return b | ||
} | ||
|
||
// W wraps input with double quotes if it is a string or fmt.Stringer. | ||
func W(input any) []byte { | ||
switch input.(type) { | ||
case string, fmt.Stringer: | ||
return []byte(fmt.Sprintf("\"%s\"", input)) | ||
default: | ||
panic("unsupported type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package testutil_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/Finschia/finschia-sdk/testutil" | ||
sdk "github.com/Finschia/finschia-sdk/types" | ||
) | ||
|
||
func TestMustJSONMarshal(t *testing.T) { | ||
type tc struct { | ||
Name string `json:"myName"` | ||
Order string `json:"myOrder"` | ||
} | ||
|
||
a := tc{ | ||
Name: "test", | ||
Order: "first", | ||
} | ||
b := new(tc) | ||
|
||
marshaled := testutil.MustJSONMarshal(a) | ||
err := json.Unmarshal(marshaled, b) | ||
require.NoError(t, err) | ||
require.Equal(t, a, *b) | ||
require.Panics(t, func() { testutil.MustJSONMarshal(make(chan int)) }) | ||
} | ||
|
||
func TestW(t *testing.T) { | ||
testCases := map[string]struct { | ||
acceptedType any | ||
}{ | ||
"string": { | ||
acceptedType: "test", | ||
}, | ||
"sdk.AccAddress": { | ||
acceptedType: sdk.AccAddress("address"), | ||
}, | ||
"sdk.Coin": { | ||
acceptedType: sdk.NewInt(1), | ||
}, | ||
"should panic for unsupported types": { | ||
acceptedType: 1, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
switch v := tc.acceptedType.(type) { | ||
case string, fmt.Stringer: | ||
require.Equal(t, []byte(fmt.Sprintf(`"%s"`, v)), testutil.W(v)) | ||
default: | ||
require.Panics(t, func() { | ||
testutil.W(tc.acceptedType) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.