chainsvrresults_test.go raw

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