github.com/palcoin-project/palcd@v1.0.0/btcjson/error_test.go (about)

     1  // Copyright (c) 2014 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_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/palcoin-project/palcd/btcjson"
    11  )
    12  
    13  // TestErrorCodeStringer tests the stringized output for the ErrorCode type.
    14  func TestErrorCodeStringer(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	tests := []struct {
    18  		in   btcjson.ErrorCode
    19  		want string
    20  	}{
    21  		{btcjson.ErrDuplicateMethod, "ErrDuplicateMethod"},
    22  		{btcjson.ErrInvalidUsageFlags, "ErrInvalidUsageFlags"},
    23  		{btcjson.ErrInvalidType, "ErrInvalidType"},
    24  		{btcjson.ErrEmbeddedType, "ErrEmbeddedType"},
    25  		{btcjson.ErrUnexportedField, "ErrUnexportedField"},
    26  		{btcjson.ErrUnsupportedFieldType, "ErrUnsupportedFieldType"},
    27  		{btcjson.ErrNonOptionalField, "ErrNonOptionalField"},
    28  		{btcjson.ErrNonOptionalDefault, "ErrNonOptionalDefault"},
    29  		{btcjson.ErrMismatchedDefault, "ErrMismatchedDefault"},
    30  		{btcjson.ErrUnregisteredMethod, "ErrUnregisteredMethod"},
    31  		{btcjson.ErrNumParams, "ErrNumParams"},
    32  		{btcjson.ErrMissingDescription, "ErrMissingDescription"},
    33  		{0xffff, "Unknown ErrorCode (65535)"},
    34  	}
    35  
    36  	// Detect additional error codes that don't have the stringer added.
    37  	if len(tests)-1 != int(btcjson.TstNumErrorCodes) {
    38  		t.Errorf("It appears an error code was added without adding an " +
    39  			"associated stringer test")
    40  	}
    41  
    42  	t.Logf("Running %d tests", len(tests))
    43  	for i, test := range tests {
    44  		result := test.in.String()
    45  		if result != test.want {
    46  			t.Errorf("String #%d\n got: %s want: %s", i, result,
    47  				test.want)
    48  			continue
    49  		}
    50  	}
    51  }
    52  
    53  // TestError tests the error output for the Error type.
    54  func TestError(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	tests := []struct {
    58  		in   btcjson.Error
    59  		want string
    60  	}{
    61  		{
    62  			btcjson.Error{Description: "some error"},
    63  			"some error",
    64  		},
    65  		{
    66  			btcjson.Error{Description: "human-readable error"},
    67  			"human-readable error",
    68  		},
    69  	}
    70  
    71  	t.Logf("Running %d tests", len(tests))
    72  	for i, test := range tests {
    73  		result := test.in.Error()
    74  		if result != test.want {
    75  			t.Errorf("Error #%d\n got: %s want: %s", i, result,
    76  				test.want)
    77  			continue
    78  		}
    79  	}
    80  }