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