github.com/btcsuite/btcd@v0.24.0/btcjson/helpers_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  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/btcsuite/btcd/btcjson"
    12  )
    13  
    14  // TestHelpers tests the various helper functions which create pointers to
    15  // primitive types.
    16  func TestHelpers(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	tests := []struct {
    20  		name     string
    21  		f        func() interface{}
    22  		expected interface{}
    23  	}{
    24  		{
    25  			name: "bool",
    26  			f: func() interface{} {
    27  				return btcjson.Bool(true)
    28  			},
    29  			expected: func() interface{} {
    30  				val := true
    31  				return &val
    32  			}(),
    33  		},
    34  		{
    35  			name: "int",
    36  			f: func() interface{} {
    37  				return btcjson.Int(5)
    38  			},
    39  			expected: func() interface{} {
    40  				val := int(5)
    41  				return &val
    42  			}(),
    43  		},
    44  		{
    45  			name: "uint",
    46  			f: func() interface{} {
    47  				return btcjson.Uint(5)
    48  			},
    49  			expected: func() interface{} {
    50  				val := uint(5)
    51  				return &val
    52  			}(),
    53  		},
    54  		{
    55  			name: "int32",
    56  			f: func() interface{} {
    57  				return btcjson.Int32(5)
    58  			},
    59  			expected: func() interface{} {
    60  				val := int32(5)
    61  				return &val
    62  			}(),
    63  		},
    64  		{
    65  			name: "uint32",
    66  			f: func() interface{} {
    67  				return btcjson.Uint32(5)
    68  			},
    69  			expected: func() interface{} {
    70  				val := uint32(5)
    71  				return &val
    72  			}(),
    73  		},
    74  		{
    75  			name: "int64",
    76  			f: func() interface{} {
    77  				return btcjson.Int64(5)
    78  			},
    79  			expected: func() interface{} {
    80  				val := int64(5)
    81  				return &val
    82  			}(),
    83  		},
    84  		{
    85  			name: "uint64",
    86  			f: func() interface{} {
    87  				return btcjson.Uint64(5)
    88  			},
    89  			expected: func() interface{} {
    90  				val := uint64(5)
    91  				return &val
    92  			}(),
    93  		},
    94  		{
    95  			name: "string",
    96  			f: func() interface{} {
    97  				return btcjson.String("abc")
    98  			},
    99  			expected: func() interface{} {
   100  				val := "abc"
   101  				return &val
   102  			}(),
   103  		},
   104  	}
   105  
   106  	t.Logf("Running %d tests", len(tests))
   107  	for i, test := range tests {
   108  		result := test.f()
   109  		if !reflect.DeepEqual(result, test.expected) {
   110  			t.Errorf("Test #%d (%s) unexpected value - got %v, "+
   111  				"want %v", i, test.name, result, test.expected)
   112  			continue
   113  		}
   114  	}
   115  }