-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_test.go
More file actions
81 lines (67 loc) · 1.58 KB
/
pointer_test.go
File metadata and controls
81 lines (67 loc) · 1.58 KB
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
package pointer_test
import (
"testing"
"github.com/tiny-go/pointer/v2"
)
func Test_New(t *testing.T) {
t.Run("Pointer", func(t *testing.T) {
value := "foo"
if *pointer.New(value) != value {
t.Errorf("unexpected value: %v", value)
}
})
}
func Test_Value(t *testing.T) {
t.Run("Scalar type (nil)", func(t *testing.T) {
var ptr *float64
value, ok := pointer.Value(ptr)
if ok {
t.Errorf("`ok` was expected to be false")
}
if value != 0 {
t.Errorf("unexpected value: %v", value)
}
})
t.Run("Scalar type (non nil)", func(t *testing.T) {
var v float64 = 3.14
value, ok := pointer.Value(&v)
if !ok {
t.Errorf("`ok` was expected to be true")
}
if value != 3.14 {
t.Errorf("unexpected value: %v", value)
}
})
t.Run("Interface (nil)", func(t *testing.T) {
var ptr *interface{}
value, ok := pointer.Value(ptr)
if ok {
t.Errorf("`ok` was expected to be false")
}
if value != nil {
t.Errorf("value was expected to be nil")
}
})
t.Run("Interface (non nil)", func(t *testing.T) {
var v interface{} = "foo"
value, ok := pointer.Value(&v)
if !ok {
t.Errorf("`ok` was expected to be true")
}
if value != "foo" {
t.Errorf("unexpected value: %v", value)
}
})
}
func Test_Coalesce(t *testing.T) {
t.Run("From collection", func(t *testing.T) {
if value := pointer.Coalesce(1, nil, nil, pointer.New(42)); value != 42 {
t.Errorf("unexpected value: %v", value)
}
})
t.Run("Fallback", func(t *testing.T) {
if value := pointer.Coalesce(1, nil, nil, nil); value != 1 {
t.Errorf("unexpected value: %v", value)
}
})
}