2020-09-15 23:30:59 +02:00
|
|
|
// Copyright (c) 2020 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcjson
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/txscript"
|
2020-09-15 23:30:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestGetAddressInfoResult ensures that custom unmarshalling of
|
|
|
|
// GetAddressInfoResult works as intended.
|
|
|
|
func TestGetAddressInfoResult(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// arbitrary script class to use in tests
|
|
|
|
nonStandard, _ := txscript.NewScriptClass("nonstandard")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
result string
|
|
|
|
want GetAddressInfoResult
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "GetAddressInfoResult - no ScriptType",
|
|
|
|
result: `{}`,
|
|
|
|
want: GetAddressInfoResult{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetAddressInfoResult - ScriptType",
|
|
|
|
result: `{"script":"nonstandard","address":"1abc"}`,
|
|
|
|
want: GetAddressInfoResult{
|
2022-07-06 05:11:29 +02:00
|
|
|
Address: "1abc",
|
|
|
|
ScriptType: nonStandard,
|
2020-09-15 23:30:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetAddressInfoResult - embedded ScriptType",
|
|
|
|
result: `{"embedded": {"script":"nonstandard","address":"121313"}}`,
|
|
|
|
want: GetAddressInfoResult{
|
|
|
|
Embedded: &embeddedAddressInfo{
|
|
|
|
Address: "121313",
|
|
|
|
ScriptType: nonStandard,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetAddressInfoResult - invalid ScriptType",
|
|
|
|
result: `{"embedded": {"script":"foo","address":"121313"}}`,
|
|
|
|
wantErr: txscript.ErrUnsupportedScriptType,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
var out GetAddressInfoResult
|
|
|
|
err := json.Unmarshal([]byte(test.result), &out)
|
|
|
|
if err != nil && !errors.Is(err, test.wantErr) {
|
|
|
|
t.Errorf("Test #%d (%s) unexpected error: %v, want: %v", i,
|
|
|
|
test.name, err, test.wantErr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(out, test.want) {
|
|
|
|
t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+
|
|
|
|
"got %v, want %v", i, test.name, spew.Sdump(out),
|
|
|
|
spew.Sdump(test.want))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 15:46:32 +02:00
|
|
|
|
|
|
|
// TestGetWalletInfoResult ensures that custom unmarshalling of
|
|
|
|
// GetWalletInfoResult works as intended.
|
|
|
|
func TestGetWalletInfoResult(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
result string
|
|
|
|
want GetWalletInfoResult
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "GetWalletInfoResult - not scanning",
|
|
|
|
result: `{"scanning":false}`,
|
|
|
|
want: GetWalletInfoResult{
|
|
|
|
Scanning: ScanningOrFalse{Value: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GetWalletInfoResult - scanning",
|
|
|
|
result: `{"scanning":{"duration":10,"progress":1.0}}`,
|
|
|
|
want: GetWalletInfoResult{
|
|
|
|
Scanning: ScanningOrFalse{
|
|
|
|
Value: ScanProgress{Duration: 10, Progress: 1.0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
var out GetWalletInfoResult
|
|
|
|
err := json.Unmarshal([]byte(test.result), &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test #%d (%s) unexpected error: %v", i,
|
|
|
|
test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(out, test.want) {
|
|
|
|
t.Errorf("Test #%d (%s) unexpected unmarshalled data - "+
|
|
|
|
"got %v, want %v", i, test.name, spew.Sdump(out),
|
|
|
|
spew.Sdump(test.want))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|