github.com/Finschia/finschia-sdk@v0.48.1/testutil/encoding_test.go (about) 1 package testutil_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/Finschia/finschia-sdk/testutil" 11 sdk "github.com/Finschia/finschia-sdk/types" 12 ) 13 14 func TestMustJSONMarshal(t *testing.T) { 15 type tc struct { 16 Name string `json:"myName"` 17 Order string `json:"myOrder"` 18 } 19 20 a := tc{ 21 Name: "test", 22 Order: "first", 23 } 24 b := new(tc) 25 26 marshaled := testutil.MustJSONMarshal(a) 27 err := json.Unmarshal(marshaled, b) 28 require.NoError(t, err) 29 require.Equal(t, a, *b) 30 require.Panics(t, func() { testutil.MustJSONMarshal(make(chan int)) }) 31 } 32 33 func TestW(t *testing.T) { 34 testCases := map[string]struct { 35 acceptedType any 36 }{ 37 "string": { 38 acceptedType: "test", 39 }, 40 "sdk.AccAddress": { 41 acceptedType: sdk.AccAddress("address"), 42 }, 43 "sdk.Coin": { 44 acceptedType: sdk.NewInt(1), 45 }, 46 "should panic for unsupported types": { 47 acceptedType: 1, 48 }, 49 } 50 51 for name, tc := range testCases { 52 t.Run(name, func(t *testing.T) { 53 switch v := tc.acceptedType.(type) { 54 case string, fmt.Stringer: 55 require.Equal(t, []byte(fmt.Sprintf(`"%s"`, v)), testutil.W(v)) 56 default: 57 require.Panics(t, func() { 58 testutil.W(tc.acceptedType) 59 }) 60 } 61 }) 62 } 63 }