github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/resources/page/testhelpers_test.go (about)

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