github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/client/keys/codec_test.go (about) 1 package keys 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/keys" 11 ) 12 13 type testCases struct { 14 Keys []keys.KeyOutput 15 Answers []keys.KeyOutput 16 JSON [][]byte 17 } 18 19 func getTestCases() testCases { 20 return testCases{ 21 // nolint:govet 22 []keys.KeyOutput{ 23 {"A", "B", "C", "D", "G", "E", "F", 0, nil}, 24 {"A", "B", "C", "D", "G", "E", "", 0, nil}, 25 {"", "B", "C", "D", "G", "E", "", 0, nil}, 26 {"", "", "", "", "", "", "", 0, nil}, 27 }, 28 make([]keys.KeyOutput, 4), 29 [][]byte{ 30 []byte(`{"name":"A","type":"B","address":"C","eth_address":"D","oper_address":"G","pubkey":"E","mnemonic":"F"}`), 31 []byte(`{"name":"A","type":"B","address":"C","eth_address":"D","oper_address":"G","pubkey":"E"}`), 32 []byte(`{"name":"","type":"B","address":"C","eth_address":"D","oper_address":"G","pubkey":"E"}`), 33 []byte(`{"name":"","type":"","address":"","eth_address":"","oper_address":"","pubkey":""}`), 34 }, 35 } 36 } 37 38 func TestMarshalJSON(t *testing.T) { 39 type args struct { 40 o keys.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 := MarshalJSON(tt.args.o) 62 if (err != nil) != tt.wantErr { 63 t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 64 return 65 } 66 fmt.Printf("%s\n", got) 67 if !reflect.DeepEqual(got, tt.want) { 68 t.Errorf("MarshalJSON() = %v, want %v", got, tt.want) 69 } 70 }) 71 } 72 } 73 74 func TestUnmarshalJSON(t *testing.T) { 75 type args struct { 76 bz []byte 77 ptr interface{} 78 } 79 80 data := getTestCases() 81 82 tests := []struct { 83 name string 84 args args 85 wantErr bool 86 }{ 87 {"basic", args{data.JSON[0], &data.Answers[0]}, false}, 88 {"mnemonic is optional", args{data.JSON[1], &data.Answers[1]}, false}, 89 90 // REVIEW: Are the next results expected?? 91 {"empty name", args{data.JSON[2], &data.Answers[2]}, false}, 92 {"empty object", args{data.JSON[3], &data.Answers[3]}, false}, 93 } 94 for idx, tt := range tests { 95 idx, tt := idx, tt 96 t.Run(tt.name, func(t *testing.T) { 97 if err := UnmarshalJSON(tt.args.bz, tt.args.ptr); (err != nil) != tt.wantErr { 98 t.Errorf("unmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 99 } 100 101 // Confirm deserialized objects are the same 102 require.Equal(t, data.Keys[idx], data.Answers[idx]) 103 }) 104 } 105 }