github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/btcjson/chainsvrresults_test.go (about) 1 // Copyright (c) 2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package btcjson_test 7 8 import ( 9 "encoding/json" 10 "testing" 11 12 "github.com/BlockABC/godash/btcjson" 13 ) 14 15 // TestChainSvrCustomResults ensures any results that have custom marshalling 16 // work as inteded. 17 // and unmarshal code of results are as expected. 18 func TestChainSvrCustomResults(t *testing.T) { 19 t.Parallel() 20 21 tests := []struct { 22 name string 23 result interface{} 24 expected string 25 }{ 26 { 27 name: "custom vin marshal with coinbase", 28 result: &btcjson.Vin{ 29 Coinbase: "021234", 30 Sequence: 4294967295, 31 }, 32 expected: `{"coinbase":"021234","sequence":4294967295}`, 33 }, 34 { 35 name: "custom vin marshal without coinbase", 36 result: &btcjson.Vin{ 37 Txid: "123", 38 Vout: 1, 39 ScriptSig: &btcjson.ScriptSig{ 40 Asm: "0", 41 Hex: "00", 42 }, 43 Sequence: 4294967295, 44 }, 45 expected: `{"txid":"123","vout":1,"scriptSig":{"asm":"0","hex":"00"},"sequence":4294967295}`, 46 }, 47 { 48 name: "custom vinprevout marshal with coinbase", 49 result: &btcjson.VinPrevOut{ 50 Coinbase: "021234", 51 Sequence: 4294967295, 52 }, 53 expected: `{"coinbase":"021234","sequence":4294967295}`, 54 }, 55 { 56 name: "custom vinprevout marshal without coinbase", 57 result: &btcjson.VinPrevOut{ 58 Txid: "123", 59 Vout: 1, 60 ScriptSig: &btcjson.ScriptSig{ 61 Asm: "0", 62 Hex: "00", 63 }, 64 PrevOut: &btcjson.PrevOut{ 65 Addresses: []string{"addr1"}, 66 Value: 0, 67 }, 68 Sequence: 4294967295, 69 }, 70 expected: `{"txid":"123","vout":1,"scriptSig":{"asm":"0","hex":"00"},"prevOut":{"addresses":["addr1"],"value":0},"sequence":4294967295}`, 71 }, 72 } 73 74 t.Logf("Running %d tests", len(tests)) 75 for i, test := range tests { 76 marshalled, err := json.Marshal(test.result) 77 if err != nil { 78 t.Errorf("Test #%d (%s) unexpected error: %v", i, 79 test.name, err) 80 continue 81 } 82 if string(marshalled) != test.expected { 83 t.Errorf("Test #%d (%s) unexpected marhsalled data - "+ 84 "got %s, want %s", i, test.name, marshalled, 85 test.expected) 86 continue 87 } 88 } 89 }