github.com/palcoin-project/palcd@v1.0.0/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/palcoin-project/palcd/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  				embeddedAddressInfo: embeddedAddressInfo{
    41  					Address:    "1abc",
    42  					ScriptType: nonStandard,
    43  				},
    44  			},
    45  		},
    46  		{
    47  			name:   "GetAddressInfoResult - embedded ScriptType",
    48  			result: `{"embedded": {"script":"nonstandard","address":"121313"}}`,
    49  			want: GetAddressInfoResult{
    50  				Embedded: &embeddedAddressInfo{
    51  					Address:    "121313",
    52  					ScriptType: nonStandard,
    53  				},
    54  			},
    55  		},
    56  		{
    57  			name:    "GetAddressInfoResult - invalid ScriptType",
    58  			result:  `{"embedded": {"script":"foo","address":"121313"}}`,
    59  			wantErr: txscript.ErrUnsupportedScriptType,
    60  		},
    61  	}
    62  
    63  	t.Logf("Running %d tests", len(tests))
    64  	for i, test := range tests {
    65  		var out GetAddressInfoResult
    66  		err := json.Unmarshal([]byte(test.result), &out)
    67  		if err != nil && !errors.Is(err, test.wantErr) {
    68  			t.Errorf("Test #%d (%s) unexpected error: %v, want: %v", i,
    69  				test.name, err, test.wantErr)
    70  			continue
    71  		}
    72  
    73  		if !reflect.DeepEqual(out, test.want) {
    74  			t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+
    75  				"got %v, want %v", i, test.name, spew.Sdump(out),
    76  				spew.Sdump(test.want))
    77  			continue
    78  		}
    79  	}
    80  }
    81  
    82  // TestGetWalletInfoResult ensures that custom unmarshalling of
    83  // GetWalletInfoResult works as intended.
    84  func TestGetWalletInfoResult(t *testing.T) {
    85  	t.Parallel()
    86  
    87  	tests := []struct {
    88  		name   string
    89  		result string
    90  		want   GetWalletInfoResult
    91  	}{
    92  		{
    93  			name:   "GetWalletInfoResult - not scanning",
    94  			result: `{"scanning":false}`,
    95  			want: GetWalletInfoResult{
    96  				Scanning: ScanningOrFalse{Value: false},
    97  			},
    98  		},
    99  		{
   100  			name:   "GetWalletInfoResult - scanning",
   101  			result: `{"scanning":{"duration":10,"progress":1.0}}`,
   102  			want: GetWalletInfoResult{
   103  				Scanning: ScanningOrFalse{
   104  					Value: ScanProgress{Duration: 10, Progress: 1.0},
   105  				},
   106  			},
   107  		},
   108  	}
   109  
   110  	t.Logf("Running %d tests", len(tests))
   111  	for i, test := range tests {
   112  		var out GetWalletInfoResult
   113  		err := json.Unmarshal([]byte(test.result), &out)
   114  		if err != nil {
   115  			t.Errorf("Test #%d (%s) unexpected error: %v", i,
   116  				test.name, err)
   117  			continue
   118  		}
   119  
   120  		if !reflect.DeepEqual(out, test.want) {
   121  			t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+
   122  				"got %v, want %v", i, test.name, spew.Sdump(out),
   123  				spew.Sdump(test.want))
   124  			continue
   125  		}
   126  	}
   127  }