test(32-06): framework unit matrices for form extensions, record events and descriptors
- form extensions: placement across three plugins, required plugins' tabs and fields, Hide with neighbours and whole tabs, spans, grouping only of extended forms; every remaining boot error with plugin, controller and offending name - record events: the pure snapshot/diff (create, update, pointers, times as instants, timestamps and key omitted, per-locale translations) and the value helpers - descriptors: MatchPattern edge cases, MatchPath ordering, types without Content, duplicate Model/Controller across plugins
This commit is contained in:
@@ -263,3 +263,164 @@ func TestContentMatchPath(t *testing.T) {
|
||||
t.Fatalf("Match error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestContentMatchPatternEdgeCases: trailing slashes, encoded and empty
|
||||
// segments, case and segment counts. Segments are compared as given; the
|
||||
// caller decodes the path and strips the query.
|
||||
func TestContentMatchPatternEdgeCases(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, pattern, path string
|
||||
want map[string]string
|
||||
ok bool
|
||||
}{
|
||||
{"a trailing slash on the path is an extra empty segment", "/blog/:slug", "/blog/hello/", nil, false},
|
||||
{"a trailing slash on both matches", "/blog/:slug/", "/blog/hello/", map[string]string{"slug": "hello"}, true},
|
||||
{"a trailing slash on the pattern only", "/blog/:slug/", "/blog/hello", nil, false},
|
||||
{"an encoded space stays encoded", "/blog/:slug", "/blog/hello%20world", map[string]string{"slug": "hello%20world"}, true},
|
||||
{"an encoded slash is one segment", "/blog/:slug", "/blog/a%2Fb", map[string]string{"slug": "a%2Fb"}, true},
|
||||
{"a decoded slash is two segments", "/blog/:slug", "/blog/a/b", nil, false},
|
||||
{"literal segments are case-sensitive", "/blog/:slug", "/Blog/hello", nil, false},
|
||||
{"an empty segment in the middle", "/blog/:a/:b", "/blog//hello", nil, false},
|
||||
{"a missing segment", "/blog/:a/:b", "/blog/x", nil, false},
|
||||
{"an extra segment", "/blog/:a", "/blog/x/y", nil, false},
|
||||
{"a pattern without a leading slash", "blog/:slug", "/blog/x", nil, false},
|
||||
{"an empty path", "/blog/:slug", "", nil, false},
|
||||
{"a lone colon is a literal segment", "/a/:", "/a/:", map[string]string{}, true},
|
||||
{"a lone colon does not match a value", "/a/:", "/a/x", nil, false},
|
||||
{"the root does not match a segment", "/", "/x", nil, false},
|
||||
{"a placeholder matches a unicode segment", "/:slug", "/grzyby-leśne", map[string]string{"slug": "grzyby-leśne"}, true},
|
||||
{"three different placeholders", "/:a/:b/:c", "/1/2/3", map[string]string{"a": "1", "b": "2", "c": "3"}, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, ok := signpost.MatchPattern(tc.pattern, tc.path)
|
||||
if ok != tc.ok || (tc.ok && !reflect.DeepEqual(got, tc.want)) {
|
||||
t.Fatalf("MatchPattern(%q, %q) = %v %v, want %v %v", tc.pattern, tc.path, got, ok, tc.want, tc.ok)
|
||||
}
|
||||
if !ok && got != nil {
|
||||
t.Fatalf("a failed match returned values %v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestContentMatchPathOrdering: an exact match of a later type beats a
|
||||
// non-exact match of an earlier one, the first of two non-exact matches
|
||||
// wins, types without Match are never asked, and a nil registry or context
|
||||
// is safe.
|
||||
func TestContentMatchPathOrdering(t *testing.T) {
|
||||
first, second, third := &contentCalls{}, &contentCalls{}, &contentCalls{}
|
||||
plain := contentType("acme-plain", "/plain/:slug", "acme_plain", "", &contentCalls{}, nil)
|
||||
reg := contentRegistry(t, "",
|
||||
contentType("acme-post", "/blog/:slug", "acme_posts", "", first, map[string]bool{"/x": false, "/y": false}),
|
||||
plain,
|
||||
contentType("acme-page", "/:slug", "acme_pages", "", second, map[string]bool{"/x": true, "/y": false}),
|
||||
contentType("acme-tag", "/tag/:slug", "acme_tags", "", third, map[string]bool{"/x": true}),
|
||||
)
|
||||
ctx := context.Background()
|
||||
if key, res, err := reg.MatchPath(ctx, "/x", ""); err != nil || key != "acme-page" || !res.Exact {
|
||||
t.Fatalf("/x = %q %+v %v, want the later exact acme-page", key, res, err)
|
||||
}
|
||||
if len(third.match) != 0 {
|
||||
t.Fatalf("the search went on after an exact match: %+v", third.match)
|
||||
}
|
||||
if key, res, err := reg.MatchPath(ctx, "/y", ""); err != nil || key != "acme-post" || res.Exact {
|
||||
t.Fatalf("/y = %q %+v %v, want the first non-exact acme-post", key, res, err)
|
||||
}
|
||||
if len(third.match) != 1 {
|
||||
t.Fatalf("without an exact match every type is asked: %+v", third.match)
|
||||
}
|
||||
//nolint:staticcheck // a nil context is part of the contract
|
||||
if key, _, err := reg.MatchPath(nil, "/x", ""); err != nil || key != "acme-page" {
|
||||
t.Fatalf("nil context: %q %v", key, err)
|
||||
}
|
||||
var none *signpost.Registry
|
||||
if key, res, err := none.MatchPath(ctx, "/x", ""); key != "" || res != nil || err != nil {
|
||||
t.Fatalf("nil registry = %q %+v %v", key, res, err)
|
||||
}
|
||||
if got := none.ContentTypes(); got == nil || len(got) != 0 {
|
||||
t.Fatalf("nil registry ContentTypes = %#v", got)
|
||||
}
|
||||
if _, ok := none.ContentTypeForModel("acme_posts"); ok {
|
||||
t.Fatal("a nil registry names a model")
|
||||
}
|
||||
if _, ok := none.ContentTypeForController("acme.blog.posts"); ok {
|
||||
t.Fatal("a nil registry names a controller")
|
||||
}
|
||||
}
|
||||
|
||||
// TestContentTypesWithoutContent: types without a content descriptor keep
|
||||
// resolving as before and stay out of every content lookup.
|
||||
func TestContentTypesWithoutContent(t *testing.T) {
|
||||
reg := contentRegistry(t, "patterns:\n acme-page: /p/:slug\n",
|
||||
pact.MenuItemType{Key: "acme-page", DefaultPattern: "/:slug", Resolve: echo},
|
||||
pact.MenuItemType{Key: "acme-home", DefaultPattern: "/", Resolve: echo},
|
||||
)
|
||||
ctx := context.Background()
|
||||
if got := reg.ContentTypes(); len(got) != 0 {
|
||||
t.Fatalf("ContentTypes = %v", keys(got))
|
||||
}
|
||||
if item, err := reg.Resolve(ctx, "acme-page", "", "", false, ""); err != nil || item.URL != "/p/:slug" {
|
||||
t.Fatalf("Resolve = %+v %v, want the layered pattern", item, err)
|
||||
}
|
||||
if _, err := reg.Describe(ctx, "acme-page", "1", ""); err == nil || !strings.Contains(err.Error(), "no content descriptor") {
|
||||
t.Fatalf("Describe = %v", err)
|
||||
}
|
||||
if _, err := reg.Describe(ctx, "acme-missing", "1", ""); err == nil || !strings.Contains(err.Error(), "unknown menu item type") {
|
||||
t.Fatalf("Describe of an unknown key = %v", err)
|
||||
}
|
||||
if _, err := reg.Records(ctx, "acme-missing", "", 10, ""); err == nil {
|
||||
t.Fatal("Records of an unknown key succeeded")
|
||||
}
|
||||
if key, res, err := reg.MatchPath(ctx, "/anything", ""); key != "" || res != nil || err != nil {
|
||||
t.Fatalf("MatchPath = %q %+v %v", key, res, err)
|
||||
}
|
||||
|
||||
mixed := contentRegistry(t, "",
|
||||
pact.MenuItemType{Key: "acme-page", DefaultPattern: "/:slug", Resolve: echo},
|
||||
contentType("acme-post", "/blog/:slug", "acme_posts", "acme.blog.posts", &contentCalls{}, nil),
|
||||
)
|
||||
got := mixed.ContentTypes()
|
||||
got[0].Key = "changed"
|
||||
if again := keys(mixed.ContentTypes()); !reflect.DeepEqual(again, []string{"acme-post"}) {
|
||||
t.Fatalf("ContentTypes is not a copy: %v", again)
|
||||
}
|
||||
if typ, ok := mixed.Type("acme-page"); !ok || typ.Content != nil {
|
||||
t.Fatalf("acme-page = %+v %v", typ, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestContentBootErrorsAcrossPlugins: duplicate Model and Controller
|
||||
// declarations are refused across plugins too and name both plugins; a
|
||||
// blank Model is a missing one.
|
||||
func TestContentBootErrorsAcrossPlugins(t *testing.T) {
|
||||
calls := &contentCalls{}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
plugins []typesPlugin
|
||||
want []string
|
||||
}{
|
||||
{"same Model in two plugins", []typesPlugin{
|
||||
{id: "acme.blog", types: []pact.MenuItemType{contentType("acme-post", "/a/:slug", "acme_posts", "", calls, nil)}},
|
||||
{id: "acme.news", types: []pact.MenuItemType{contentType("acme-article", "/b/:slug", "acme_posts", "", calls, nil)}},
|
||||
}, []string{`"acme-article"`, "acme.news", `"acme-post"`, "acme.blog", `"acme_posts"`}},
|
||||
{"same Controller in two plugins", []typesPlugin{
|
||||
{id: "acme.blog", types: []pact.MenuItemType{contentType("acme-post", "/a/:slug", "acme_posts", "acme.blog.posts", calls, nil)}},
|
||||
{id: "acme.news", types: []pact.MenuItemType{contentType("acme-article", "/b/:slug", "acme_articles", "acme.blog.posts", calls, nil)}},
|
||||
}, []string{`"acme-article"`, "acme.news", `"acme-post"`, "acme.blog", `"acme.blog.posts"`}},
|
||||
{"a blank Model", []typesPlugin{
|
||||
{id: "acme.blog", types: []pact.MenuItemType{contentType("acme-post", "/a/:slug", " ", "", calls, nil)}},
|
||||
}, []string{`"acme-post"`, "acme.blog", "without Model"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := signpost.Activate(smokeApp(t, ""), tc.plugins)
|
||||
if err == nil {
|
||||
t.Fatal("Activate accepted the descriptors")
|
||||
}
|
||||
for _, part := range tc.want {
|
||||
if !strings.Contains(err.Error(), part) {
|
||||
t.Fatalf("error = %q, want %q", err, part)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user