github.com/Finschia/finschia-sdk@v0.48.1/client/keys/codec_test.go (about) 1 package keys_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/Finschia/finschia-sdk/client/keys" 10 "github.com/Finschia/finschia-sdk/crypto/keyring" 11 ) 12 13 type testCases struct { 14 Keys []keyring.KeyOutput 15 Answers []keyring.KeyOutput 16 JSON [][]byte 17 } 18 19 func getTestCases() testCases { 20 return testCases{ 21 // nolint:govet 22 []keyring.KeyOutput{ 23 {"A", "B", "C", "D", "E"}, 24 {"A", "B", "C", "D", ""}, 25 {"", "B", "C", "D", ""}, 26 {"", "", "", "", ""}, 27 }, 28 make([]keyring.KeyOutput, 4), 29 [][]byte{ 30 []byte(`{"name":"A","type":"B","address":"C","pubkey":"D","mnemonic":"E"}`), 31 []byte(`{"name":"A","type":"B","address":"C","pubkey":"D"}`), 32 []byte(`{"name":"","type":"B","address":"C","pubkey":"D"}`), 33 []byte(`{"name":"","type":"","address":"","pubkey":""}`), 34 }, 35 } 36 } 37 38 func TestMarshalJSON(t *testing.T) { 39 type args struct { 40 o keyring.KeyOutput 41 } 42 43 data := getTestCases() 44 45 tests := []struct { 46 name string 47 args args 48 want []byte 49 wantErr bool 50 }{ 51 {"basic", args{data.Keys[0]}, data.JSON[0], false}, 52 {"mnemonic is optional", args{data.Keys[1]}, data.JSON[1], false}, 53 54 // REVIEW: Are the next results expected?? 55 {"empty name", args{data.Keys[2]}, data.JSON[2], false}, 56 {"empty object", args{data.Keys[3]}, data.JSON[3], false}, 57 } 58 for _, tt := range tests { 59 tt := tt 60 t.Run(tt.name, func(t *testing.T) { 61 got, err := keys.MarshalJSON(tt.args.o) 62 require.Equal(t, tt.wantErr, err != nil) 63 require.True(t, bytes.Equal(got, tt.want)) 64 }) 65 } 66 } 67 68 func TestUnmarshalJSON(t *testing.T) { 69 type args struct { 70 bz []byte 71 ptr interface{} 72 } 73 74 data := getTestCases() 75 76 tests := []struct { 77 name string 78 args args 79 wantErr bool 80 }{ 81 {"basic", args{data.JSON[0], &data.Answers[0]}, false}, 82 {"mnemonic is optional", args{data.JSON[1], &data.Answers[1]}, false}, 83 84 // REVIEW: Are the next results expected?? 85 {"empty name", args{data.JSON[2], &data.Answers[2]}, false}, 86 {"empty object", args{data.JSON[3], &data.Answers[3]}, false}, 87 } 88 for idx, tt := range tests { 89 idx, tt := idx, tt 90 t.Run(tt.name, func(t *testing.T) { 91 err := keys.UnmarshalJSON(tt.args.bz, tt.args.ptr) 92 require.Equal(t, tt.wantErr, err != nil) 93 // Confirm deserialized objects are the same 94 require.Equal(t, data.Keys[idx], data.Answers[idx]) 95 }) 96 } 97 }