github.com/lbryio/lbcd@v0.22.119/btcjson/walletsvrresults_test.go (about) 1 // Copyright (c) 2020 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package btcjson 6 7 import ( 8 "encoding/json" 9 "errors" 10 "reflect" 11 "testing" 12 13 "github.com/davecgh/go-spew/spew" 14 "github.com/lbryio/lbcd/txscript" 15 ) 16 17 // TestGetAddressInfoResult ensures that custom unmarshalling of 18 // GetAddressInfoResult works as intended. 19 func TestGetAddressInfoResult(t *testing.T) { 20 t.Parallel() 21 22 // arbitrary script class to use in tests 23 nonStandard, _ := txscript.NewScriptClass("nonstandard") 24 25 tests := []struct { 26 name string 27 result string 28 want GetAddressInfoResult 29 wantErr error 30 }{ 31 { 32 name: "GetAddressInfoResult - no ScriptType", 33 result: `{}`, 34 want: GetAddressInfoResult{}, 35 }, 36 { 37 name: "GetAddressInfoResult - ScriptType", 38 result: `{"script":"nonstandard","address":"1abc"}`, 39 want: GetAddressInfoResult{ 40 Address: "1abc", 41 ScriptType: nonStandard, 42 }, 43 }, 44 { 45 name: "GetAddressInfoResult - embedded ScriptType", 46 result: `{"embedded": {"script":"nonstandard","address":"121313"}}`, 47 want: GetAddressInfoResult{ 48 Embedded: &embeddedAddressInfo{ 49 Address: "121313", 50 ScriptType: nonStandard, 51 }, 52 }, 53 }, 54 { 55 name: "GetAddressInfoResult - invalid ScriptType", 56 result: `{"embedded": {"script":"foo","address":"121313"}}`, 57 wantErr: txscript.ErrUnsupportedScriptType, 58 }, 59 } 60 61 t.Logf("Running %d tests", len(tests)) 62 for i, test := range tests { 63 var out GetAddressInfoResult 64 err := json.Unmarshal([]byte(test.result), &out) 65 if err != nil && !errors.Is(err, test.wantErr) { 66 t.Errorf("Test #%d (%s) unexpected error: %v, want: %v", i, 67 test.name, err, test.wantErr) 68 continue 69 } 70 71 if !reflect.DeepEqual(out, test.want) { 72 t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+ 73 "got %v, want %v", i, test.name, spew.Sdump(out), 74 spew.Sdump(test.want)) 75 continue 76 } 77 } 78 } 79 80 // TestGetWalletInfoResult ensures that custom unmarshalling of 81 // GetWalletInfoResult works as intended. 82 func TestGetWalletInfoResult(t *testing.T) { 83 t.Parallel() 84 85 tests := []struct { 86 name string 87 result string 88 want GetWalletInfoResult 89 }{ 90 { 91 name: "GetWalletInfoResult - not scanning", 92 result: `{"scanning":false}`, 93 want: GetWalletInfoResult{ 94 Scanning: ScanningOrFalse{Value: false}, 95 }, 96 }, 97 { 98 name: "GetWalletInfoResult - scanning", 99 result: `{"scanning":{"duration":10,"progress":1.0}}`, 100 want: GetWalletInfoResult{ 101 Scanning: ScanningOrFalse{ 102 Value: ScanProgress{Duration: 10, Progress: 1.0}, 103 }, 104 }, 105 }, 106 } 107 108 t.Logf("Running %d tests", len(tests)) 109 for i, test := range tests { 110 var out GetWalletInfoResult 111 err := json.Unmarshal([]byte(test.result), &out) 112 if err != nil { 113 t.Errorf("Test #%d (%s) unexpected error: %v", i, 114 test.name, err) 115 continue 116 } 117 118 if !reflect.DeepEqual(out, test.want) { 119 t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+ 120 "got %v, want %v", i, test.name, spew.Sdump(out), 121 spew.Sdump(test.want)) 122 continue 123 } 124 } 125 }