-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptr_test.go
65 lines (55 loc) · 1.6 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
package ptr_test
import (
"fmt"
"testing"
"github.com/koron-go/ptr"
)
func checkTV[T any](t *testing.T, p *T, want string) {
t.Helper()
got := fmt.Sprintf("%T %#v", p, *p)
if got != want {
t.Errorf("unmatch want=%q got=%q", want, got)
}
}
type Item0 struct{}
func TestP(t *testing.T) {
checkTV(t, ptr.P(1), "*int 1")
checkTV(t, ptr.P(-123), "*int -123")
checkTV(t, ptr.P(float64(1)), "*float64 1")
checkTV(t, ptr.P("abc"), "*string \"abc\"")
checkTV(t, ptr.P(Item0{}), "*ptr_test.Item0 ptr_test.Item0{}")
}
func checkT[T any](t *testing.T, p *T, want string) {
t.Helper()
got := fmt.Sprintf("%T", p)
if got != want {
t.Errorf("unmatch want=%q got=%q", want, got)
}
}
func TestPP(t *testing.T) {
checkT(t, ptr.P(ptr.P(1)), "**int")
checkT(t, ptr.P(ptr.P(-123)), "**int")
checkT(t, ptr.P(ptr.P(float64(1))), "**float64")
checkT(t, ptr.P(ptr.P("abc")), "**string")
checkT(t, ptr.P(ptr.P(Item0{})), "**ptr_test.Item0")
}
func checkV[T any](t *testing.T, v T, want string) {
t.Helper()
got := fmt.Sprintf("%[1]T %#[1]v", v)
if got != want {
t.Errorf("unmatch want=%q got=%q", want, got)
}
}
func TestV(t *testing.T) {
checkV(t, ptr.V(ptr.P(1)), "int 1")
checkV(t, ptr.V(ptr.P(-123)), "int -123")
checkV(t, ptr.V(ptr.P(float64(1))), "float64 1")
checkV(t, ptr.V(ptr.P("abc")), "string \"abc\"")
checkV(t, ptr.V(ptr.P(Item0{})), "ptr_test.Item0 ptr_test.Item0{}")
}
func TestVNil(t *testing.T) {
checkV(t, ptr.V((*int)(nil)), "int 0")
checkV(t, ptr.V((*float64)(nil)), "float64 0")
checkV(t, ptr.V((*string)(nil)), "string \"\"")
checkV(t, ptr.V((*Item0)(nil)), "ptr_test.Item0 ptr_test.Item0{}")
}