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