github.com/schumacherfm/hugo@v0.47.1/hugolib/menu_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 "testing" 18 19 "fmt" 20 21 "github.com/spf13/afero" 22 23 "github.com/stretchr/testify/require" 24 ) 25 26 const ( 27 menuPageTemplate = `--- 28 title: %q 29 weight: %d 30 menu: 31 %s: 32 title: %s 33 weight: %d 34 --- 35 # Doc Menu 36 ` 37 ) 38 39 func TestSectionPagesMenu(t *testing.T) { 40 t.Parallel() 41 42 siteConfig := ` 43 baseurl = "http://example.com/" 44 title = "Section Menu" 45 sectionPagesMenu = "sect" 46 ` 47 48 th, h := newTestSitesFromConfig( 49 t, 50 afero.NewMemMapFs(), 51 siteConfig, 52 "layouts/partials/menu.html", 53 `{{- $p := .page -}} 54 {{- $m := .menu -}} 55 {{ range (index $p.Site.Menus $m) -}} 56 {{- .URL }}|{{ .Name }}|{{ .Title }}|{{ .Weight -}}| 57 {{- if $p.IsMenuCurrent $m . }}IsMenuCurrent{{ else }}-{{ end -}}| 58 {{- if $p.HasMenuCurrent $m . }}HasMenuCurrent{{ else }}-{{ end -}}| 59 {{- end -}} 60 `, 61 "layouts/_default/single.html", 62 `Single|{{ .Title }} 63 Menu Sect: {{ partial "menu.html" (dict "page" . "menu" "sect") }} 64 Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}`, 65 "layouts/_default/list.html", "List|{{ .Title }}|{{ .Content }}", 66 ) 67 require.Len(t, h.Sites, 1) 68 69 fs := th.Fs 70 71 writeSource(t, fs, "content/sect1/p1.md", fmt.Sprintf(menuPageTemplate, "p1", 1, "main", "atitle1", 40)) 72 writeSource(t, fs, "content/sect1/p2.md", fmt.Sprintf(menuPageTemplate, "p2", 2, "main", "atitle2", 30)) 73 writeSource(t, fs, "content/sect2/p3.md", fmt.Sprintf(menuPageTemplate, "p3", 3, "main", "atitle3", 20)) 74 writeSource(t, fs, "content/sect2/p4.md", fmt.Sprintf(menuPageTemplate, "p4", 4, "main", "atitle4", 10)) 75 writeSource(t, fs, "content/sect3/p5.md", fmt.Sprintf(menuPageTemplate, "p5", 5, "main", "atitle5", 5)) 76 77 writeNewContentFile(t, fs.Source, "Section One", "2017-01-01", "content/sect1/_index.md", 100) 78 writeNewContentFile(t, fs.Source, "Section Five", "2017-01-01", "content/sect5/_index.md", 10) 79 80 err := h.Build(BuildCfg{}) 81 82 require.NoError(t, err) 83 84 s := h.Sites[0] 85 86 require.Len(t, s.Menus, 2) 87 88 p1 := s.RegularPages[0].Menus() 89 90 // There is only one menu in the page, but it is "member of" 2 91 require.Len(t, p1, 1) 92 93 th.assertFileContent("public/sect1/p1/index.html", "Single", 94 "Menu Sect: "+ 95 "/sect5/|Section Five||10|-|-|"+ 96 "/sect1/|Section One||100|-|HasMenuCurrent|"+ 97 "/sect2/|Sect2s||0|-|-|"+ 98 "/sect3/|Sect3s||0|-|-|", 99 "Menu Main: "+ 100 "/sect3/p5/|p5|atitle5|5|-|-|"+ 101 "/sect2/p4/|p4|atitle4|10|-|-|"+ 102 "/sect2/p3/|p3|atitle3|20|-|-|"+ 103 "/sect1/p2/|p2|atitle2|30|-|-|"+ 104 "/sect1/p1/|p1|atitle1|40|IsMenuCurrent|-|", 105 ) 106 107 th.assertFileContent("public/sect2/p3/index.html", "Single", 108 "Menu Sect: "+ 109 "/sect5/|Section Five||10|-|-|"+ 110 "/sect1/|Section One||100|-|-|"+ 111 "/sect2/|Sect2s||0|-|HasMenuCurrent|"+ 112 "/sect3/|Sect3s||0|-|-|") 113 114 }