github.com/elmarschill/hugo_sample@v0.47.1/hugolib/pages_related_test.go (about) 1 // Copyright 2017-present 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 "fmt" 18 "path/filepath" 19 "testing" 20 21 "github.com/gohugoio/hugo/common/types" 22 "github.com/gohugoio/hugo/deps" 23 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestRelated(t *testing.T) { 28 assert := require.New(t) 29 30 t.Parallel() 31 32 var ( 33 cfg, fs = newTestCfg() 34 //th = testHelper{cfg, fs, t} 35 ) 36 37 pageTmpl := `--- 38 title: Page %d 39 keywords: [%s] 40 date: %s 41 --- 42 43 Content 44 ` 45 46 writeSource(t, fs, filepath.Join("content", "page1.md"), fmt.Sprintf(pageTmpl, 1, "hugo, says", "2017-01-03")) 47 writeSource(t, fs, filepath.Join("content", "page2.md"), fmt.Sprintf(pageTmpl, 2, "hugo, rocks", "2017-01-02")) 48 writeSource(t, fs, filepath.Join("content", "page3.md"), fmt.Sprintf(pageTmpl, 3, "bep, says", "2017-01-01")) 49 50 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 51 assert.Len(s.RegularPages, 3) 52 53 result, err := s.RegularPages.RelatedTo(types.NewKeyValuesStrings("keywords", "hugo", "rocks")) 54 55 assert.NoError(err) 56 assert.Len(result, 2) 57 assert.Equal("Page 2", result[0].title) 58 assert.Equal("Page 1", result[1].title) 59 60 result, err = s.RegularPages.Related(s.RegularPages[0]) 61 assert.Len(result, 2) 62 assert.Equal("Page 2", result[0].title) 63 assert.Equal("Page 3", result[1].title) 64 65 result, err = s.RegularPages.RelatedIndices(s.RegularPages[0], "keywords") 66 assert.Len(result, 2) 67 assert.Equal("Page 2", result[0].title) 68 assert.Equal("Page 3", result[1].title) 69 70 result, err = s.RegularPages.RelatedTo(types.NewKeyValuesStrings("keywords", "bep", "rocks")) 71 assert.NoError(err) 72 assert.Len(result, 2) 73 assert.Equal("Page 2", result[0].title) 74 assert.Equal("Page 3", result[1].title) 75 }