Debian Codegraph @

hugolib/content_map_page_contentnodeshifter_test.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hugolib
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
21 )
22
23 type testContentNodeForSite struct {
24 vec sitesmatrix.Vector
25 }
26
27 func (n *testContentNodeForSite) Path() string { return "/test" }
28 func (n *testContentNodeForSite) nodeCategorySingle() {}
29 func (n *testContentNodeForSite) siteVector() sitesmatrix.Vector { return n.vec }
30 func (n *testContentNodeForSite) forEeachContentNode(f func(sitesmatrix.Vector, contentNode) bool) bool {
31 return f(n.vec, n)
32 }
33
34 // See issue 15207.
35 func TestContentNodeShifterDeleteMultipleFromNodes(t *testing.T) {
36 c := qt.New(t)
37
38 s := &contentNodeShifter{}
39 vec := sitesmatrix.Vector{2, 0, 0}
40 other := sitesmatrix.Vector{1, 0, 0}
41
42 newNodes := func() contentNodes {
43 return contentNodes{
44 &testContentNodeForSite{vec: other},
45 &testContentNodeForSite{vec: vec},
46 &testContentNodeForSite{vec: other},
47 &testContentNodeForSite{vec: other},
48 &testContentNodeForSite{vec: vec},
49 }
50 }
51
52 updated, deleted, wasDeleted, isEmpty := s.Delete(newNodes(), vec)
53 c.Assert(wasDeleted, qt.IsTrue)
54 c.Assert(isEmpty, qt.IsFalse)
55 c.Assert(deleted.(contentNodes), qt.HasLen, 2)
56 remaining := updated.(contentNodes)
57 c.Assert(remaining, qt.HasLen, 3)
58 for _, n := range remaining {
59 c.Assert(n.(contentNodeForSite).siteVector(), qt.Equals, other)
60 }
61
62 // Delete all.
63 updated, deleted, wasDeleted, isEmpty = s.Delete(contentNodes{
64 &testContentNodeForSite{vec: vec},
65 &testContentNodeForSite{vec: vec},
66 }, vec)
67 c.Assert(wasDeleted, qt.IsTrue)
68 c.Assert(isEmpty, qt.IsTrue)
69 c.Assert(deleted.(contentNodes), qt.HasLen, 2)
70 c.Assert(updated, qt.IsNil)
71
72 // Delete none.
73 nodes := contentNodes{&testContentNodeForSite{vec: other}}
74 updated, deleted, wasDeleted, isEmpty = s.Delete(nodes, vec)
75 c.Assert(wasDeleted, qt.IsFalse)
76 c.Assert(isEmpty, qt.IsFalse)
77 c.Assert(deleted, qt.IsNil)
78 c.Assert(updated.(contentNodes), qt.HasLen, 1)
79 }