-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid_chain_test.go
More file actions
128 lines (115 loc) · 3.16 KB
/
Copy pathid_chain_test.go
File metadata and controls
128 lines (115 loc) · 3.16 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
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
package structpages
import (
"context"
"strings"
"testing"
)
// Chain form parallels URLFor's []any{Parent{}, Leaf{}, ...} shape.
// Leading typed values are chain steps; the trailing element is the
// method spec — either a string method name or a method expression
// whose receiver type IS the leaf.
type chainAdmin struct {
Dashboard chainDashboard `route:"/dashboard Admin Dashboard"`
}
type chainUser struct {
Dashboard chainDashboard `route:"/dashboard User Dashboard"`
}
type chainDashboard struct{}
func (chainDashboard) Header() component { return testComponent{"Header"} }
type chainRoot struct {
Admin chainAdmin `route:"/admin Admin"`
User chainUser `route:"/user User"`
}
func TestID_ChainForm(t *testing.T) {
pc, err := parsePageTree("/", &chainRoot{})
if err != nil {
t.Fatalf("parsePageTree: %v", err)
}
ctx := pcCtx.WithValue(context.Background(), pc)
cases := []struct {
name string
input []any
want string
}{
{
name: "chain with string method name",
input: []any{chainAdmin{}, chainDashboard{}, "Header"},
want: "#admin-dashboard-header",
},
{
name: "chain with method expression terminal",
input: []any{chainAdmin{}, chainDashboard.Header},
want: "#admin-dashboard-header",
},
{
name: "chain targeting user's dashboard (different parent, explicit descent)",
input: []any{chainUser{}, chainDashboard{}, "Header"},
want: "#user-dashboard-header",
},
{
name: "chain leaf type matches method expr receiver (collapse duplicate descend)",
input: []any{chainAdmin{}, chainDashboard{}, chainDashboard.Header},
want: "#admin-dashboard-header",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := IDTarget(ctx, tc.input)
if err != nil {
t.Fatalf("IDTarget: %v", err)
}
if got != tc.want {
t.Errorf("IDTarget = %q, want %q", got, tc.want)
}
})
}
}
func TestID_ChainFormErrors(t *testing.T) {
pc, err := parsePageTree("/", &chainRoot{})
if err != nil {
t.Fatalf("parsePageTree: %v", err)
}
ctx := pcCtx.WithValue(context.Background(), pc)
cases := []struct {
name string
input []any
wantInErr string
}{
{
name: "empty chain",
input: []any{},
wantInErr: "empty",
},
{
name: "chain with no method spec (only typed values)",
input: []any{chainAdmin{}, chainDashboard{}},
wantInErr: "method",
},
{
name: "chain step does not descend correctly",
input: []any{chainAdmin{}, chainUser{}, "Header"},
wantInErr: "child",
},
{
name: "method expression receiver type conflicts with prior chain step",
input: []any{chainAdmin{}, chainUser{}, chainDashboard.Header},
wantInErr: "child",
},
{
name: "method name not on leaf type",
input: []any{chainAdmin{}, chainDashboard{}, "Nonexistent"},
wantInErr: "method",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := IDTarget(ctx, tc.input)
if err == nil {
t.Fatalf("expected error containing %q, got nil", tc.wantInErr)
}
if !strings.Contains(err.Error(), tc.wantInErr) {
t.Errorf("error %q does not contain %q", err.Error(), tc.wantInErr)
}
})
}
}