-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_ptr_test.go
69 lines (61 loc) · 1.24 KB
/
kind_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
package dapper_test
import "testing"
func TestPrinter_Ptr(t *testing.T) {
type ptr struct {
Value any
}
value := 100
test(t, "nil pointer", (*int)(nil), "*int(nil)")
test(t, "non-nil pointer", &value, "*int(100)")
test(t, "package path", &ptr{}, "*github.com/dogmatiq/dapper_test.ptr{<zero>}")
test(
t,
"nil pointer inside interface includes element type",
ptr{
(*int)(nil),
},
"github.com/dogmatiq/dapper_test.ptr{",
" Value: *int(nil)",
"}",
)
test(
t,
"non-nil pointer inside interface includes element type",
ptr{
&value,
},
"github.com/dogmatiq/dapper_test.ptr{",
" Value: *int(100)",
"}",
)
}
// This test verifies that recursive structures are detected, and do not produce
// an infinite loop or stack overflow.
func TestPrinter_PtrRecursion(t *testing.T) {
type recursive struct {
Name string
Child *recursive
}
r := recursive{
Name: "one",
Child: &recursive{
Name: "two",
},
}
r.Child.Child = &r
test(
t,
"recursive structure",
r,
"github.com/dogmatiq/dapper_test.recursive{",
` Name: "one"`,
" Child: {",
` Name: "two"`,
" Child: {",
` Name: "one"`,
" Child: <recursion>",
" }",
" }",
"}",
)
}