Debian Codegraph @

resources/page/testhelpers_test.go

raw
1 // Copyright 2024 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 page
15
16 import (
17 "context"
18 "fmt"
19 "html/template"
20 "path"
21 "path/filepath"
22 "time"
23
24 "github.com/gohugoio/hugo/markup/tableofcontents"
25
26 "github.com/gohugoio/hugo/resources/resource"
27
28 "github.com/gohugoio/hugo/navigation"
29
30 "github.com/gohugoio/hugo/common/hmaps"
31 "github.com/gohugoio/hugo/common/hstore"
32 "github.com/gohugoio/hugo/common/loggers"
33 "github.com/gohugoio/hugo/common/paths"
34 "github.com/gohugoio/hugo/config"
35 "github.com/gohugoio/hugo/hugofs"
36 "github.com/gohugoio/hugo/langs"
37 "github.com/gohugoio/hugo/media"
38 "github.com/gohugoio/hugo/related"
39
40 "github.com/gohugoio/hugo/source"
41 )
42
43 var (
44 _ resource.LengthProvider = (*testPage)(nil)
45 _ Page = (*testPage)(nil)
46 )
47
48 var relatedDocsHandler = NewRelatedDocsHandler(related.DefaultConfig)
49
50 func newTestPage() *testPage {
51 return newTestPageWithFile("/a/b/c.md")
52 }
53
54 func newTestPageWithFile(filename string) *testPage {
55 filename = filepath.FromSlash(filename)
56 file := source.NewContentFileInfoFrom(filename, filename)
57
58 l, err := langs.NewLanguage(
59 "en",
60 "en",
61 "UTC",
62 langs.LanguageConfig{
63 Label: "English",
64 },
65 loggers.NewDefault(),
66 )
67 if err != nil {
68 panic(err)
69 }
70
71 return &testPage{
72 params: make(map[string]any),
73 data: make(map[string]any),
74 file: file,
75 pathInfo: file.FileInfo().Meta().PathInfo,
76 currentSection: &testPage{
77 sectionEntries: []string{"a", "b", "c"},
78 },
79 site: &testSite{l: l},
80 }
81 }
82
83 type testPage struct {
84 kind string
85 description string
86 title string
87 linkTitle string
88 lang string
89 section string
90 site *testSite
91
92 content string
93
94 fuzzyWordCount int
95
96 path string
97 pathInfo *paths.Path
98
99 slug string
100
101 // Dates
102 date time.Time
103 lastMod time.Time
104 expiryDate time.Time
105 pubDate time.Time
106
107 weight int
108
109 params map[string]any
110 data map[string]any
111
112 file *source.File
113
114 currentSection *testPage
115 sectionEntries []string
116 ancestors Pages
117 }
118
119 func (p *testPage) Aliases() []string {
120 panic("testpage: not implemented")
121 }
122
123 func (p *testPage) AllTranslations() Pages {
124 panic("testpage: not implemented")
125 }
126
127 func (p *testPage) AlternativeOutputFormats() OutputFormats {
128 panic("testpage: not implemented")
129 }
130
131 func (p *testPage) BaseFileName() string {
132 panic("testpage: not implemented")
133 }
134
135 func (p *testPage) BundleType() string {
136 panic("testpage: not implemented")
137 }
138
139 func (p *testPage) Content(context.Context) (any, error) {
140 panic("testpage: not implemented")
141 }
142
143 func (p *testPage) Markup(...any) Markup {
144 panic("testpage: not implemented")
145 }
146
147 func (p *testPage) ContentBaseName() string {
148 panic("testpage: not implemented")
149 }
150
151 func (p *testPage) CurrentSection() Page {
152 return p.currentSection
153 }
154
155 func (p *testPage) Data() any {
156 return p.data
157 }
158
159 func (p *testPage) Sitemap() config.SitemapConfig {
160 return config.SitemapConfig{}
161 }
162
163 func (p *testPage) Layout() string {
164 return ""
165 }
166
167 func (p *testPage) Date() time.Time {
168 return p.date
169 }
170
171 func (p *testPage) Description() string {
172 return ""
173 }
174
175 func (p *testPage) ContentWithoutSummary(ctx context.Context) (template.HTML, error) {
176 return "", nil
177 }
178
179 func (p *testPage) Dir() string {
180 panic("testpage: not implemented")
181 }
182
183 func (p *testPage) Draft() bool {
184 panic("testpage: not implemented")
185 }
186
187 func (p *testPage) Eq(other any) bool {
188 return p == other
189 }
190
191 func (p *testPage) ExpiryDate() time.Time {
192 return p.expiryDate
193 }
194
195 func (p *testPage) File() *source.File {
196 return p.file
197 }
198
199 func (p *testPage) FileInfo() hugofs.FileMetaInfo {
200 panic("testpage: not implemented")
201 }
202
203 func (p *testPage) Filename() string {
204 panic("testpage: not implemented")
205 }
206
207 func (p *testPage) FirstSection() Page {
208 // Return the current section for regular pages
209 // For section pages, this would be the section itself
210 if p.currentSection != nil {
211 return p.currentSection
212 }
213 return p // If no current section, assume this page is the section
214 }
215
216 func (p *testPage) FuzzyWordCount(context.Context) int {
217 return p.fuzzyWordCount
218 }
219
220 func (p *testPage) GetPage(ref string) (Page, error) {
221 panic("testpage: not implemented")
222 }
223
224 func (p *testPage) GetParam(key string) any {
225 panic("testpage: not implemented")
226 }
227
228 func (p *testPage) GetTerms(taxonomy string) Pages {
229 panic("testpage: not implemented")
230 }
231
232 func (p *testPage) GetInternalRelatedDocsHandler() *RelatedDocsHandler {
233 return relatedDocsHandler
234 }
235
236 func (p *testPage) GitInfo() *source.GitInfo {
237 return nil
238 }
239
240 func (p *testPage) CodeOwners() []string {
241 return nil
242 }
243
244 func (p *testPage) HasMenuCurrent(menuID string, me *navigation.MenuEntry) bool {
245 panic("testpage: not implemented")
246 }
247
248 func (p *testPage) HasShortcode(name string) bool {
249 panic("testpage: not implemented")
250 }
251
252 func (p *testPage) Hugo() HugoInfo {
253 panic("testpage: not implemented")
254 }
255
256 func (p *testPage) InSection(other any) bool {
257 panic("testpage: not implemented")
258 }
259
260 func (p *testPage) IsAncestor(other any) bool {
261 panic("testpage: not implemented")
262 }
263
264 func (p *testPage) IsDescendant(other any) bool {
265 panic("testpage: not implemented")
266 }
267
268 func (p *testPage) IsDraft() bool {
269 return false
270 }
271
272 func (p *testPage) IsHome() bool {
273 return p.kind == "home"
274 }
275
276 func (p *testPage) IsMenuCurrent(menuID string, inme *navigation.MenuEntry) bool {
277 panic("testpage: not implemented")
278 }
279
280 func (p *testPage) IsNode() bool {
281 panic("testpage: not implemented")
282 }
283
284 func (p *testPage) IsBranch() bool {
285 panic("testpage: not implemented")
286 }
287
288 func (p *testPage) IsPage() bool {
289 panic("testpage: not implemented")
290 }
291
292 func (p *testPage) IsSection() bool {
293 return p.kind == "section"
294 }
295
296 func (p *testPage) IsTranslated() bool {
297 panic("testpage: not implemented")
298 }
299
300 func (p *testPage) Ancestors() Pages {
301 return p.ancestors
302 }
303
304 func (p *testPage) Keywords() []string {
305 return nil
306 }
307
308 func (p *testPage) Kind() string {
309 return p.kind
310 }
311
312 func (p *testPage) Lang() string {
313 return p.lang
314 }
315
316 func (p *testPage) Language() *langs.Language {
317 panic("testpage: not implemented")
318 }
319
320 func (p *testPage) LanguagePrefix() string {
321 return ""
322 }
323
324 func (p *testPage) Fragments(context.Context) *tableofcontents.Fragments {
325 return nil
326 }
327
328 func (p *testPage) HeadingsFiltered(context.Context) tableofcontents.Headings {
329 return nil
330 }
331
332 func (p *testPage) Lastmod() time.Time {
333 return p.lastMod
334 }
335
336 func (p *testPage) Len(context.Context) int {
337 return len(p.content)
338 }
339
340 func (p *testPage) LinkTitle() string {
341 if p.linkTitle == "" {
342 if p.title == "" {
343 return p.path
344 }
345 return p.title
346 }
347 return p.linkTitle
348 }
349
350 func (p *testPage) LogicalName() string {
351 panic("testpage: not implemented")
352 }
353
354 func (p *testPage) MediaType() media.Type {
355 panic("testpage: not implemented")
356 }
357
358 func (p *testPage) Menus() navigation.PageMenus {
359 return navigation.PageMenus{}
360 }
361
362 func (p *testPage) Name() string {
363 panic("testpage: not implemented")
364 }
365
366 func (p *testPage) Next() Page {
367 panic("testpage: not implemented")
368 }
369
370 func (p *testPage) NextInSection() Page {
371 return nil
372 }
373
374 func (p *testPage) NextPage() Page {
375 return nil
376 }
377
378 func (p *testPage) OutputFormats() OutputFormats {
379 panic("testpage: not implemented")
380 }
381
382 func (p *testPage) Pages() Pages {
383 panic("testpage: not implemented")
384 }
385
386 func (p *testPage) RegularPages() Pages {
387 panic("testpage: not implemented")
388 }
389
390 func (p *testPage) RegularPagesRecursive() Pages {
391 panic("testpage: not implemented")
392 }
393
394 func (p *testPage) Paginate(seq any, options ...any) (*Pager, error) {
395 return nil, nil
396 }
397
398 func (p *testPage) Paginator(options ...any) (*Pager, error) {
399 return nil, nil
400 }
401
402 func (p *testPage) Param(key any) (any, error) {
403 return resource.Param(p, nil, key)
404 }
405
406 func (p *testPage) Params() hmaps.Params {
407 return p.params
408 }
409
410 func (p *testPage) Page() Page {
411 return p
412 }
413
414 func (p *testPage) Parent() Page {
415 panic("testpage: not implemented")
416 }
417
418 func (p *testPage) Path() string {
419 return p.path
420 }
421
422 func (p *testPage) PathInfo() *paths.Path {
423 return p.pathInfo
424 }
425
426 func (p *testPage) Permalink() string {
427 panic("testpage: not implemented")
428 }
429
430 func (p *testPage) Plain(context.Context) string {
431 panic("testpage: not implemented")
432 }
433
434 func (p *testPage) PlainWords(context.Context) []string {
435 panic("testpage: not implemented")
436 }
437
438 func (p *testPage) Prev() Page {
439 panic("testpage: not implemented")
440 }
441
442 func (p *testPage) PrevInSection() Page {
443 return nil
444 }
445
446 func (p *testPage) PrevPage() Page {
447 return nil
448 }
449
450 func (p *testPage) PublishDate() time.Time {
451 return p.pubDate
452 }
453
454 func (p *testPage) RawContent() string {
455 panic("testpage: not implemented")
456 }
457
458 func (p *testPage) RenderShortcodes(context.Context) (template.HTML, error) {
459 panic("testpage: not implemented")
460 }
461
462 func (p *testPage) ReadingTime(context.Context) int {
463 panic("testpage: not implemented")
464 }
465
466 func (p *testPage) Ref(argsm map[string]any) (string, error) {
467 panic("testpage: not implemented")
468 }
469
470 func (p *testPage) RefFrom(argsm map[string]any, source any) (string, error) {
471 return "", nil
472 }
473
474 func (p *testPage) RelPermalink() string {
475 panic("testpage: not implemented")
476 }
477
478 func (p *testPage) RelRef(argsm map[string]any) (string, error) {
479 panic("testpage: not implemented")
480 }
481
482 func (p *testPage) RelRefFrom(argsm map[string]any, source any) (string, error) {
483 return "", nil
484 }
485
486 func (p *testPage) Render(ctx context.Context, args ...any) (template.HTML, error) {
487 panic("testpage: not implemented")
488 }
489
490 func (p *testPage) RenderString(ctx context.Context, args ...any) (template.HTML, error) {
491 panic("testpage: not implemented")
492 }
493
494 func (p *testPage) ResourceType() string {
495 panic("testpage: not implemented")
496 }
497
498 func (p *testPage) Resources() resource.Resources {
499 panic("testpage: not implemented")
500 }
501
502 func (p *testPage) Scratch() *hstore.Scratch {
503 panic("testpage: not implemented")
504 }
505
506 func (p *testPage) Store() *hstore.Scratch {
507 panic("testpage: not implemented")
508 }
509
510 func (p *testPage) RelatedKeywords(cfg related.IndexConfig) ([]string, error) {
511 v, err := p.Param(cfg.Name)
512 if err != nil {
513 return nil, err
514 }
515
516 return cfg.ToKeywords(v)
517 }
518
519 func (p *testPage) Section() string {
520 return p.section
521 }
522
523 func (p *testPage) Sections() Pages {
524 panic("testpage: not implemented")
525 }
526
527 func (p *testPage) SectionsEntries() []string {
528 return p.sectionEntries
529 }
530
531 func (p *testPage) SectionsPath() string {
532 return path.Join(p.sectionEntries...)
533 }
534
535 func (p *testPage) Site() Site {
536 if p.site == nil {
537 panic(fmt.Sprintf("testpage: site is nil for %q", p.path))
538 }
539 return p.site
540 }
541
542 func (p *testPage) Sites() Sites {
543 panic("testpage: not implemented")
544 }
545
546 func (p *testPage) Slug() string {
547 return p.slug
548 }
549
550 func (p *testPage) String() string {
551 return p.path
552 }
553
554 func (p *testPage) Summary(context.Context) template.HTML {
555 panic("testpage: not implemented")
556 }
557
558 func (p *testPage) TableOfContents(context.Context) template.HTML {
559 panic("testpage: not implemented")
560 }
561
562 func (p *testPage) Title() string {
563 return p.title
564 }
565
566 func (p *testPage) TranslationBaseName() string {
567 panic("testpage: not implemented")
568 }
569
570 func (p *testPage) TranslationKey() string {
571 return p.path
572 }
573
574 func (p *testPage) Translations() Pages {
575 panic("testpage: not implemented")
576 }
577
578 func (p *testPage) Truncated(context.Context) bool {
579 panic("testpage: not implemented")
580 }
581
582 func (p *testPage) Type() string {
583 return p.section
584 }
585
586 func (p *testPage) URL() string {
587 return ""
588 }
589
590 func (p *testPage) UniqueID() string {
591 panic("testpage: not implemented")
592 }
593
594 func (p *testPage) Weight() int {
595 return p.weight
596 }
597
598 func (p *testPage) WordCount(context.Context) int {
599 panic("testpage: not implemented")
600 }
601
602 func createTestPages(num int) Pages {
603 pages := make(Pages, num)
604
605 for i := range num {
606 m := &testPage{
607 path: fmt.Sprintf("/x/y/z/p%d.md", i),
608 weight: 5,
609 fuzzyWordCount: i + 2, // magic
610 }
611
612 if i%2 == 0 {
613 m.weight = 10
614 }
615 pages[i] = m
616
617 }
618
619 return pages
620 }