github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/cmdinfo_test.go (about)

     1  // Copyright (c) 2015 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package btcjson_test
     7  
     8  import (
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/dashpay/godash/btcjson"
    13  )
    14  
    15  // TestCmdMethod tests the CmdMethod function to ensure it retunrs the expected
    16  // methods and errors.
    17  func TestCmdMethod(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	tests := []struct {
    21  		name   string
    22  		cmd    interface{}
    23  		method string
    24  		err    error
    25  	}{
    26  		{
    27  			name: "unregistered type",
    28  			cmd:  (*int)(nil),
    29  			err:  btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
    30  		},
    31  		{
    32  			name:   "nil pointer of registered type",
    33  			cmd:    (*btcjson.GetBlockCmd)(nil),
    34  			method: "getblock",
    35  		},
    36  		{
    37  			name:   "nil instance of registered type",
    38  			cmd:    &btcjson.GetBlockCountCmd{},
    39  			method: "getblockcount",
    40  		},
    41  	}
    42  
    43  	t.Logf("Running %d tests", len(tests))
    44  	for i, test := range tests {
    45  		method, err := btcjson.CmdMethod(test.cmd)
    46  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
    47  			t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+
    48  				"want %T", i, test.name, err, test.err)
    49  			continue
    50  		}
    51  		if err != nil {
    52  			gotErrorCode := err.(btcjson.Error).ErrorCode
    53  			if gotErrorCode != test.err.(btcjson.Error).ErrorCode {
    54  				t.Errorf("Test #%d (%s) mismatched error code "+
    55  					"- got %v (%v), want %v", i, test.name,
    56  					gotErrorCode, err,
    57  					test.err.(btcjson.Error).ErrorCode)
    58  				continue
    59  			}
    60  
    61  			continue
    62  		}
    63  
    64  		// Ensure method matches the expected value.
    65  		if method != test.method {
    66  			t.Errorf("Test #%d (%s) mismatched method - got %v, "+
    67  				"want %v", i, test.name, method, test.method)
    68  			continue
    69  		}
    70  	}
    71  }
    72  
    73  // TestMethodUsageFlags tests the MethodUsage function ensure it returns the
    74  // expected flags and errors.
    75  func TestMethodUsageFlags(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	tests := []struct {
    79  		name   string
    80  		method string
    81  		err    error
    82  		flags  btcjson.UsageFlag
    83  	}{
    84  		{
    85  			name:   "unregistered type",
    86  			method: "bogusmethod",
    87  			err:    btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
    88  		},
    89  		{
    90  			name:   "getblock",
    91  			method: "getblock",
    92  			flags:  0,
    93  		},
    94  		{
    95  			name:   "walletpassphrase",
    96  			method: "walletpassphrase",
    97  			flags:  btcjson.UFWalletOnly,
    98  		},
    99  	}
   100  
   101  	t.Logf("Running %d tests", len(tests))
   102  	for i, test := range tests {
   103  		flags, err := btcjson.MethodUsageFlags(test.method)
   104  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
   105  			t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+
   106  				"want %T", i, test.name, err, test.err)
   107  			continue
   108  		}
   109  		if err != nil {
   110  			gotErrorCode := err.(btcjson.Error).ErrorCode
   111  			if gotErrorCode != test.err.(btcjson.Error).ErrorCode {
   112  				t.Errorf("Test #%d (%s) mismatched error code "+
   113  					"- got %v (%v), want %v", i, test.name,
   114  					gotErrorCode, err,
   115  					test.err.(btcjson.Error).ErrorCode)
   116  				continue
   117  			}
   118  
   119  			continue
   120  		}
   121  
   122  		// Ensure flags match the expected value.
   123  		if flags != test.flags {
   124  			t.Errorf("Test #%d (%s) mismatched flags - got %v, "+
   125  				"want %v", i, test.name, flags, test.flags)
   126  			continue
   127  		}
   128  	}
   129  }
   130  
   131  // TestMethodUsageText tests the MethodUsageText function ensure it returns the
   132  // expected text.
   133  func TestMethodUsageText(t *testing.T) {
   134  	t.Parallel()
   135  
   136  	tests := []struct {
   137  		name     string
   138  		method   string
   139  		err      error
   140  		expected string
   141  	}{
   142  		{
   143  			name:   "unregistered type",
   144  			method: "bogusmethod",
   145  			err:    btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod},
   146  		},
   147  		{
   148  			name:     "getblockcount",
   149  			method:   "getblockcount",
   150  			expected: "getblockcount",
   151  		},
   152  		{
   153  			name:     "getblock",
   154  			method:   "getblock",
   155  			expected: `getblock "hash" (verbose=true verbosetx=false)`,
   156  		},
   157  	}
   158  
   159  	t.Logf("Running %d tests", len(tests))
   160  	for i, test := range tests {
   161  		usage, err := btcjson.MethodUsageText(test.method)
   162  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
   163  			t.Errorf("Test #%d (%s) wrong error - got %T (%[3]v), "+
   164  				"want %T", i, test.name, err, test.err)
   165  			continue
   166  		}
   167  		if err != nil {
   168  			gotErrorCode := err.(btcjson.Error).ErrorCode
   169  			if gotErrorCode != test.err.(btcjson.Error).ErrorCode {
   170  				t.Errorf("Test #%d (%s) mismatched error code "+
   171  					"- got %v (%v), want %v", i, test.name,
   172  					gotErrorCode, err,
   173  					test.err.(btcjson.Error).ErrorCode)
   174  				continue
   175  			}
   176  
   177  			continue
   178  		}
   179  
   180  		// Ensure usage matches the expected value.
   181  		if usage != test.expected {
   182  			t.Errorf("Test #%d (%s) mismatched usage - got %v, "+
   183  				"want %v", i, test.name, usage, test.expected)
   184  			continue
   185  		}
   186  
   187  		// Get the usage again to excerise caching.
   188  		usage, err = btcjson.MethodUsageText(test.method)
   189  		if err != nil {
   190  			t.Errorf("Test #%d (%s) unexpected error: %v", i,
   191  				test.name, err)
   192  			continue
   193  		}
   194  
   195  		// Ensure usage still matches the expected value.
   196  		if usage != test.expected {
   197  			t.Errorf("Test #%d (%s) mismatched usage - got %v, "+
   198  				"want %v", i, test.name, usage, test.expected)
   199  			continue
   200  		}
   201  	}
   202  }
   203  
   204  // TestFieldUsage tests the internal fieldUsage function ensure it returns the
   205  // expected text.
   206  func TestFieldUsage(t *testing.T) {
   207  	t.Parallel()
   208  
   209  	tests := []struct {
   210  		name     string
   211  		field    reflect.StructField
   212  		defValue *reflect.Value
   213  		expected string
   214  	}{
   215  		{
   216  			name: "jsonrpcusage tag override",
   217  			field: func() reflect.StructField {
   218  				type s struct {
   219  					Test int `jsonrpcusage:"testvalue"`
   220  				}
   221  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   222  			}(),
   223  			defValue: nil,
   224  			expected: "testvalue",
   225  		},
   226  		{
   227  			name: "generic interface",
   228  			field: func() reflect.StructField {
   229  				type s struct {
   230  					Test interface{}
   231  				}
   232  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   233  			}(),
   234  			defValue: nil,
   235  			expected: `test`,
   236  		},
   237  		{
   238  			name: "string without default value",
   239  			field: func() reflect.StructField {
   240  				type s struct {
   241  					Test string
   242  				}
   243  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   244  			}(),
   245  			defValue: nil,
   246  			expected: `"test"`,
   247  		},
   248  		{
   249  			name: "string with default value",
   250  			field: func() reflect.StructField {
   251  				type s struct {
   252  					Test string
   253  				}
   254  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   255  			}(),
   256  			defValue: func() *reflect.Value {
   257  				value := "default"
   258  				rv := reflect.ValueOf(&value)
   259  				return &rv
   260  			}(),
   261  			expected: `test="default"`,
   262  		},
   263  		{
   264  			name: "array of strings",
   265  			field: func() reflect.StructField {
   266  				type s struct {
   267  					Test []string
   268  				}
   269  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   270  			}(),
   271  			defValue: nil,
   272  			expected: `["test",...]`,
   273  		},
   274  		{
   275  			name: "array of strings with plural field name 1",
   276  			field: func() reflect.StructField {
   277  				type s struct {
   278  					Keys []string
   279  				}
   280  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   281  			}(),
   282  			defValue: nil,
   283  			expected: `["key",...]`,
   284  		},
   285  		{
   286  			name: "array of strings with plural field name 2",
   287  			field: func() reflect.StructField {
   288  				type s struct {
   289  					Addresses []string
   290  				}
   291  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   292  			}(),
   293  			defValue: nil,
   294  			expected: `["address",...]`,
   295  		},
   296  		{
   297  			name: "array of strings with plural field name 3",
   298  			field: func() reflect.StructField {
   299  				type s struct {
   300  					Capabilities []string
   301  				}
   302  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   303  			}(),
   304  			defValue: nil,
   305  			expected: `["capability",...]`,
   306  		},
   307  		{
   308  			name: "array of structs",
   309  			field: func() reflect.StructField {
   310  				type s2 struct {
   311  					Txid string
   312  				}
   313  				type s struct {
   314  					Capabilities []s2
   315  				}
   316  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   317  			}(),
   318  			defValue: nil,
   319  			expected: `[{"txid":"value"},...]`,
   320  		},
   321  		{
   322  			name: "array of ints",
   323  			field: func() reflect.StructField {
   324  				type s struct {
   325  					Test []int
   326  				}
   327  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   328  			}(),
   329  			defValue: nil,
   330  			expected: `[test,...]`,
   331  		},
   332  		{
   333  			name: "sub struct with jsonrpcusage tag override",
   334  			field: func() reflect.StructField {
   335  				type s2 struct {
   336  					Test string `jsonrpcusage:"testusage"`
   337  				}
   338  				type s struct {
   339  					Test s2
   340  				}
   341  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   342  			}(),
   343  			defValue: nil,
   344  			expected: `{testusage}`,
   345  		},
   346  		{
   347  			name: "sub struct with string",
   348  			field: func() reflect.StructField {
   349  				type s2 struct {
   350  					Txid string
   351  				}
   352  				type s struct {
   353  					Test s2
   354  				}
   355  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   356  			}(),
   357  			defValue: nil,
   358  			expected: `{"txid":"value"}`,
   359  		},
   360  		{
   361  			name: "sub struct with int",
   362  			field: func() reflect.StructField {
   363  				type s2 struct {
   364  					Vout int
   365  				}
   366  				type s struct {
   367  					Test s2
   368  				}
   369  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   370  			}(),
   371  			defValue: nil,
   372  			expected: `{"vout":n}`,
   373  		},
   374  		{
   375  			name: "sub struct with float",
   376  			field: func() reflect.StructField {
   377  				type s2 struct {
   378  					Amount float64
   379  				}
   380  				type s struct {
   381  					Test s2
   382  				}
   383  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   384  			}(),
   385  			defValue: nil,
   386  			expected: `{"amount":n.nnn}`,
   387  		},
   388  		{
   389  			name: "sub struct with sub struct",
   390  			field: func() reflect.StructField {
   391  				type s3 struct {
   392  					Amount float64
   393  				}
   394  				type s2 struct {
   395  					Template s3
   396  				}
   397  				type s struct {
   398  					Test s2
   399  				}
   400  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   401  			}(),
   402  			defValue: nil,
   403  			expected: `{"template":{"amount":n.nnn}}`,
   404  		},
   405  		{
   406  			name: "sub struct with slice",
   407  			field: func() reflect.StructField {
   408  				type s2 struct {
   409  					Capabilities []string
   410  				}
   411  				type s struct {
   412  					Test s2
   413  				}
   414  				return reflect.TypeOf((*s)(nil)).Elem().Field(0)
   415  			}(),
   416  			defValue: nil,
   417  			expected: `{"capabilities":["capability",...]}`,
   418  		},
   419  	}
   420  
   421  	t.Logf("Running %d tests", len(tests))
   422  	for i, test := range tests {
   423  		// Ensure usage matches the expected value.
   424  		usage := btcjson.TstFieldUsage(test.field, test.defValue)
   425  		if usage != test.expected {
   426  			t.Errorf("Test #%d (%s) mismatched usage - got %v, "+
   427  				"want %v", i, test.name, usage, test.expected)
   428  			continue
   429  		}
   430  	}
   431  }