-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr_test.go
140 lines (113 loc) · 2.89 KB
/
ptr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package wrap_test
import (
"encoding/json"
"encoding/xml"
"testing"
"github.com/twoojoo/wrap"
"github.com/stretchr/testify/assert"
)
func TestNewPtr(t *testing.T) {
value := 42
ptr := wrap.NewPtr(&value)
assert.NotNil(t, ptr.Unwrap())
assert.Equal(t, 42, *ptr.Unwrap())
nilPtr := wrap.NewPtr[int](nil)
assert.Nil(t, nilPtr.Unwrap())
}
func TestUnwrap(t *testing.T) {
value := 42
ptr := wrap.NewPtr(&value)
assert.Equal(t, &value, ptr.Unwrap())
nilPtr := wrap.NewPtr[int](nil)
assert.Nil(t, nilPtr.Unwrap())
}
func TestGetValue(t *testing.T) {
value := 42
ptr := wrap.NewPtr(&value)
val, ok := ptr.GetValue()
assert.True(t, ok)
assert.Equal(t, 42, val)
nilPtr := wrap.NewPtr[int](nil)
val, ok = nilPtr.GetValue()
assert.False(t, ok)
assert.Equal(t, 0, val)
}
func TestSetValue(t *testing.T) {
ptr := wrap.NewPtr[int](nil)
ptr.SetValue(42)
val, ok := ptr.GetValue()
assert.True(t, ok)
assert.Equal(t, 42, val)
ptr.SetValue(100)
val, ok = ptr.GetValue()
assert.True(t, ok)
assert.Equal(t, 100, val)
}
func TestClear(t *testing.T) {
value := 42
ptr := wrap.NewPtr(&value)
assert.NotNil(t, ptr.Unwrap())
ptr.Clear()
assert.Nil(t, ptr.Unwrap())
ptr.Clear() // Clear again to make sure no error
assert.Nil(t, ptr.Unwrap())
}
func TestIsNil(t *testing.T) {
ptr := wrap.NewPtr[int](nil)
assert.True(t, ptr.IsNil())
value := 42
ptr = wrap.NewPtr(&value)
assert.False(t, ptr.IsNil())
}
func TestMarshalJSON(t *testing.T) {
value := 42
ptr := wrap.NewPtr(&value)
data, err := json.Marshal(ptr)
assert.NoError(t, err)
assert.Equal(t, "42", string(data))
nilPtr := wrap.NewPtr[int](nil)
data, err = json.Marshal(nilPtr)
assert.NoError(t, err)
assert.Equal(t, "null", string(data))
}
func TestUnmarshalJSON(t *testing.T) {
var ptr wrap.Ptr[int]
err := json.Unmarshal([]byte("42"), &ptr)
assert.NoError(t, err)
val, ok := ptr.GetValue()
assert.True(t, ok)
assert.Equal(t, 42, val)
err = json.Unmarshal([]byte("null"), &ptr)
assert.NoError(t, err)
assert.True(t, ptr.IsNil())
}
func TestMarshalXML(t *testing.T) {
type Test struct {
Value wrap.Ptr[int] `xml:"value"`
}
value := 42
ptr := wrap.NewPtr(&value)
testStruct := Test{Value: ptr}
data, err := xml.Marshal(testStruct)
assert.NoError(t, err)
assert.Contains(t, string(data), "<value>42</value>")
nilPtr := wrap.NewPtr[int](nil)
testStruct = Test{Value: nilPtr}
data, err = xml.Marshal(testStruct)
assert.NoError(t, err)
assert.Equal(t, string(data), "<Test></Test>")
}
func TestUnmarshalXML(t *testing.T) {
type Test struct {
Value wrap.Ptr[int] `xml:"value"`
}
var testStruct Test
err := xml.Unmarshal([]byte("<Test><value>42</value></Test>"), &testStruct)
assert.NoError(t, err)
val, ok := testStruct.Value.GetValue()
assert.True(t, ok)
assert.Equal(t, 42, val)
err = xml.Unmarshal([]byte("<Test><value></value></Test>"), &testStruct)
assert.NoError(t, err)
assert.True(t, testStruct.Value.IsNil())
}