error_test.go raw

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