-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec_registry_test.go
More file actions
138 lines (110 loc) · 4.2 KB
/
spec_registry_test.go
File metadata and controls
138 lines (110 loc) · 4.2 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
129
130
131
132
133
134
135
136
137
138
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"iter"
"testing"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
type specRegistryStubGroup struct {
name string
basePath string
}
func (g *specRegistryStubGroup) Name() string { return g.name }
func (g *specRegistryStubGroup) BasePath() string { return g.basePath }
func (g *specRegistryStubGroup) RegisterRoutes(rg *gin.RouterGroup) {}
func TestRegisterSpecGroups_Good_DeduplicatesByIdentity(t *testing.T) {
snapshot := api.RegisteredSpecGroups()
api.ResetSpecGroups()
t.Cleanup(func() {
api.ResetSpecGroups()
api.RegisterSpecGroups(snapshot...)
})
first := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
second := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
third := &specRegistryStubGroup{name: "beta", basePath: "/beta"}
api.RegisterSpecGroups(nil, first, second, third, first)
groups := api.RegisteredSpecGroups()
if len(groups) != 2 {
t.Fatalf("expected 2 unique groups, got %d", len(groups))
}
if groups[0].Name() != "alpha" || groups[0].BasePath() != "/alpha" {
t.Fatalf("expected first group to be alpha at /alpha, got %s at %s", groups[0].Name(), groups[0].BasePath())
}
if groups[1].Name() != "beta" || groups[1].BasePath() != "/beta" {
t.Fatalf("expected second group to be beta at /beta, got %s at %s", groups[1].Name(), groups[1].BasePath())
}
}
func TestRegisterSpecGroups_Good_IteratorReturnsSnapshot(t *testing.T) {
snapshot := api.RegisteredSpecGroups()
api.ResetSpecGroups()
t.Cleanup(func() {
api.ResetSpecGroups()
api.RegisterSpecGroups(snapshot...)
})
first := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
second := &specRegistryStubGroup{name: "beta", basePath: "/beta"}
api.RegisterSpecGroups(first)
iter := api.RegisteredSpecGroupsIter()
api.RegisterSpecGroups(second)
var groups []api.RouteGroup
for group := range iter {
groups = append(groups, group)
}
if len(groups) != 1 {
t.Fatalf("expected iterator snapshot to contain 1 group, got %d", len(groups))
}
if groups[0].Name() != "alpha" || groups[0].BasePath() != "/alpha" {
t.Fatalf("expected iterator snapshot to preserve alpha at /alpha, got %s at %s", groups[0].Name(), groups[0].BasePath())
}
}
func TestRegisterSpecGroupsIter_Good_DeduplicatesAndRegisters(t *testing.T) {
snapshot := api.RegisteredSpecGroups()
api.ResetSpecGroups()
t.Cleanup(func() {
api.ResetSpecGroups()
api.RegisterSpecGroups(snapshot...)
})
first := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
second := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
third := &specRegistryStubGroup{name: "gamma", basePath: "/gamma"}
groups := iter.Seq[api.RouteGroup](func(yield func(api.RouteGroup) bool) {
for _, group := range []api.RouteGroup{first, second, nil, third, first} {
if !yield(group) {
return
}
}
})
api.RegisterSpecGroupsIter(groups)
registered := api.RegisteredSpecGroups()
if len(registered) != 2 {
t.Fatalf("expected 2 unique groups, got %d", len(registered))
}
if registered[0].Name() != "alpha" || registered[0].BasePath() != "/alpha" {
t.Fatalf("expected first group to be alpha at /alpha, got %s at %s", registered[0].Name(), registered[0].BasePath())
}
if registered[1].Name() != "gamma" || registered[1].BasePath() != "/gamma" {
t.Fatalf("expected second group to be gamma at /gamma, got %s at %s", registered[1].Name(), registered[1].BasePath())
}
}
func TestSpecGroupsIter_Good_DeduplicatesExtraBridge(t *testing.T) {
snapshot := api.RegisteredSpecGroups()
api.ResetSpecGroups()
t.Cleanup(func() {
api.ResetSpecGroups()
api.RegisterSpecGroups(snapshot...)
})
first := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
extra := &specRegistryStubGroup{name: "alpha", basePath: "/alpha"}
api.RegisterSpecGroups(first)
var groups []api.RouteGroup
for group := range api.SpecGroupsIter(extra) {
groups = append(groups, group)
}
if len(groups) != 1 {
t.Fatalf("expected deduplicated iterator to return 1 group, got %d", len(groups))
}
if groups[0].Name() != "alpha" || groups[0].BasePath() != "/alpha" {
t.Fatalf("expected alpha at /alpha, got %s at %s", groups[0].Name(), groups[0].BasePath())
}
}