Repository navigation
Expand file tree
/
Copy pathnull_test.go
More file actions
67 lines (60 loc) · 1.78 KB
/
Copy pathnull_test.go
File metadata and controls
67 lines (60 loc) · 1.78 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
package pg
import "testing"
// TestPtr verifies that Ptr returns a non-nil pointer to a copy of v, for both a zero and a
// non-zero value, and that dereferencing it yields v back.
func TestPtr(t *testing.T) {
t.Run("string", func(t *testing.T) {
for _, v := range []string{"", "hello"} {
p := Ptr(v)
if p == nil {
t.Fatalf("Ptr(%q): expected a non-nil pointer", v)
}
if *p != v {
t.Fatalf("Ptr(%q): expected *p == %q, got %q", v, v, *p)
}
}
})
t.Run("int", func(t *testing.T) {
for _, v := range []int{0, 42} {
p := Ptr(v)
if p == nil {
t.Fatalf("Ptr(%d): expected a non-nil pointer", v)
}
if *p != v {
t.Fatalf("Ptr(%d): expected *p == %d, got %d", v, v, *p)
}
}
})
}
// TestNullIfZero verifies that NullIfZero returns nil for the zero value of T and a pointer to
// v (dereferencing back to v) otherwise, for both string and int.
func TestNullIfZero(t *testing.T) {
t.Run("string zero value returns nil", func(t *testing.T) {
if p := NullIfZero(""); p != nil {
t.Fatalf(`NullIfZero(""): expected nil, got a pointer to %q`, *p)
}
})
t.Run("string non-zero value returns a pointer to it", func(t *testing.T) {
p := NullIfZero("hello")
if p == nil {
t.Fatal(`NullIfZero("hello"): expected a non-nil pointer`)
}
if *p != "hello" {
t.Fatalf(`NullIfZero("hello"): expected *p == "hello", got %q`, *p)
}
})
t.Run("int zero value returns nil", func(t *testing.T) {
if p := NullIfZero(0); p != nil {
t.Fatalf("NullIfZero(0): expected nil, got a pointer to %d", *p)
}
})
t.Run("int non-zero value returns a pointer to it", func(t *testing.T) {
p := NullIfZero(42)
if p == nil {
t.Fatal("NullIfZero(42): expected a non-nil pointer")
}
if *p != 42 {
t.Fatalf("NullIfZero(42): expected *p == 42, got %d", *p)
}
})
}