github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/hugolib/page_collections_test.go (about) 1 // Copyright 2017 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 "math/rand" 19 "path" 20 "path/filepath" 21 "testing" 22 "time" 23 24 "github.com/gohugoio/hugo/deps" 25 "github.com/stretchr/testify/require" 26 ) 27 28 const pageCollectionsPageTemplate = `--- 29 title: "%s" 30 categories: 31 - Hugo 32 --- 33 # Doc 34 ` 35 36 func BenchmarkGetPage(b *testing.B) { 37 var ( 38 cfg, fs = newTestCfg() 39 r = rand.New(rand.NewSource(time.Now().UnixNano())) 40 ) 41 42 for i := 0; i < 10; i++ { 43 for j := 0; j < 100; j++ { 44 writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), "CONTENT") 45 } 46 } 47 48 s := buildSingleSite(b, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 49 50 pagePaths := make([]string, b.N) 51 52 for i := 0; i < b.N; i++ { 53 pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10)) 54 } 55 56 b.ResetTimer() 57 for i := 0; i < b.N; i++ { 58 home := s.getPage(KindHome) 59 if home == nil { 60 b.Fatal("Home is nil") 61 } 62 63 p := s.getPage(KindSection, pagePaths[i]) 64 if p == nil { 65 b.Fatal("Section is nil") 66 } 67 68 } 69 } 70 71 func BenchmarkGetPageRegular(b *testing.B) { 72 var ( 73 cfg, fs = newTestCfg() 74 r = rand.New(rand.NewSource(time.Now().UnixNano())) 75 ) 76 77 for i := 0; i < 10; i++ { 78 for j := 0; j < 100; j++ { 79 content := fmt.Sprintf(pageCollectionsPageTemplate, fmt.Sprintf("Title%d_%d", i, j)) 80 writeSource(b, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content) 81 } 82 } 83 84 s := buildSingleSite(b, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 85 86 pagePaths := make([]string, b.N) 87 88 for i := 0; i < b.N; i++ { 89 pagePaths[i] = path.Join(fmt.Sprintf("sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", r.Intn(100))) 90 } 91 92 b.ResetTimer() 93 for i := 0; i < b.N; i++ { 94 page := s.getPage(KindPage, pagePaths[i]) 95 require.NotNil(b, page) 96 } 97 } 98 99 func TestGetPage(t *testing.T) { 100 101 var ( 102 assert = require.New(t) 103 cfg, fs = newTestCfg() 104 ) 105 106 for i := 0; i < 10; i++ { 107 for j := 0; j < 10; j++ { 108 content := fmt.Sprintf(pageCollectionsPageTemplate, fmt.Sprintf("Title%d_%d", i, j)) 109 writeSource(t, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", j)), content) 110 } 111 } 112 113 content := fmt.Sprintf(pageCollectionsPageTemplate, "UniqueBase") 114 writeSource(t, fs, filepath.Join("content", "sect3", "unique.md"), content) 115 116 s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true}) 117 118 tests := []struct { 119 kind string 120 path []string 121 expectedTitle string 122 }{ 123 {KindHome, []string{}, ""}, 124 {KindSection, []string{"sect3"}, "Sect3s"}, 125 {KindPage, []string{"sect3", "page1.md"}, "Title3_1"}, 126 {KindPage, []string{"sect4/page2.md"}, "Title4_2"}, 127 {KindPage, []string{filepath.FromSlash("sect5/page3.md")}, "Title5_3"}, 128 // Ref/Relref supports this potentially ambiguous lookup. 129 {KindPage, []string{"unique.md"}, "UniqueBase"}, 130 } 131 132 for i, test := range tests { 133 errorMsg := fmt.Sprintf("Test %d", i) 134 page := s.getPage(test.kind, test.path...) 135 assert.NotNil(page, errorMsg) 136 assert.Equal(test.kind, page.Kind, errorMsg) 137 assert.Equal(test.expectedTitle, page.title) 138 } 139 140 }