2017-02-20 03:33:35 -05:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
2015-12-10 17:19:38 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2014-11-03 10:14:10 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
"fmt"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2016-08-09 14:06:15 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-10-18 14:25:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-20 03:33:35 -05:00
|
|
|
menuPageTemplate = `---
|
|
|
|
title: %q
|
|
|
|
weight: %d
|
2016-03-10 04:31:12 -05:00
|
|
|
menu:
|
2017-02-20 03:33:35 -05:00
|
|
|
%s:
|
|
|
|
weight: %d
|
2016-03-10 04:31:12 -05:00
|
|
|
---
|
2017-02-20 03:33:35 -05:00
|
|
|
# Doc Menu
|
|
|
|
`
|
|
|
|
)
|
2014-12-16 06:08:16 -05:00
|
|
|
|
2015-05-09 14:54:11 -04:00
|
|
|
func TestSectionPagesMenu(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2015-05-28 17:05:13 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
siteConfig := `
|
|
|
|
baseurl = "http://example.com/"
|
|
|
|
title = "Section Menu"
|
|
|
|
sectionPagesMenu = "sect"
|
|
|
|
`
|
|
|
|
|
|
|
|
th, h := newTestSitesFromConfig(t, siteConfig,
|
|
|
|
"layouts/partials/menu.html", `{{- $p := .page -}}
|
|
|
|
{{- $m := .menu -}}
|
|
|
|
{{ range (index $p.Site.Menus $m) -}}
|
|
|
|
{{- .URL }}|{{ .Name }}|{{ .Weight -}}|
|
|
|
|
{{- if $p.IsMenuCurrent $m . }}IsMenuCurrent{{ else }}-{{ end -}}|
|
|
|
|
{{- if $p.HasMenuCurrent $m . }}HasMenuCurrent{{ else }}-{{ end -}}|
|
|
|
|
{{- end -}}
|
|
|
|
`,
|
|
|
|
"layouts/_default/single.html",
|
|
|
|
`Single|{{ .Title }}
|
|
|
|
Menu Sect: {{ partial "menu.html" (dict "page" . "menu" "sect") }}
|
|
|
|
Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}`,
|
|
|
|
"layouts/_default/list.html", "List|{{ .Title }}|{{ .Content }}",
|
2017-02-04 22:20:06 -05:00
|
|
|
)
|
2017-02-20 03:33:35 -05:00
|
|
|
require.Len(t, h.Sites, 1)
|
2015-05-09 14:54:11 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
fs := th.Fs
|
2015-05-09 14:54:11 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
writeSource(t, fs, "content/sect1/p1.md", fmt.Sprintf(menuPageTemplate, "p1", 1, "main", 40))
|
|
|
|
writeSource(t, fs, "content/sect1/p2.md", fmt.Sprintf(menuPageTemplate, "p2", 2, "main", 30))
|
|
|
|
writeSource(t, fs, "content/sect2/p3.md", fmt.Sprintf(menuPageTemplate, "p3", 3, "main", 20))
|
|
|
|
writeSource(t, fs, "content/sect2/p4.md", fmt.Sprintf(menuPageTemplate, "p4", 4, "main", 10))
|
|
|
|
writeSource(t, fs, "content/sect3/p5.md", fmt.Sprintf(menuPageTemplate, "p5", 5, "main", 5))
|
2015-05-09 14:54:11 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
writeNewContentFile(t, fs, "Section One", "2017-01-01", "content/sect1/_index.md", 100)
|
|
|
|
writeNewContentFile(t, fs, "Section Five", "2017-01-01", "content/sect5/_index.md", 10)
|
2015-05-28 17:05:13 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
err := h.Build(BuildCfg{})
|
2016-02-07 06:34:43 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
require.NoError(t, err)
|
2016-07-28 03:30:58 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
s := h.Sites[0]
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
require.Len(t, s.Menus, 2)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
p1 := s.RegularPages[0].Menus()
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
// There is only one menu in the page, but it is "member of" 2
|
|
|
|
require.Len(t, p1, 1)
|
2016-07-28 03:30:58 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
th.assertFileContent("public/sect1/p1/index.html", "Single",
|
|
|
|
"Menu Sect: /sect5/|Section Five|10|-|-|/sect1/|Section One|100|-|HasMenuCurrent|/sect2/|Sect2s|0|-|-|/sect3/|Sect3s|0|-|-|",
|
|
|
|
"Menu Main: /sect3/p5/|p5|5|-|-|/sect2/p4/|p4|10|-|-|/sect2/p3/|p3|20|-|-|/sect1/p2/|p2|30|-|-|/sect1/p1/|p1|40|IsMenuCurrent|-|",
|
|
|
|
)
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2017-02-20 03:33:35 -05:00
|
|
|
th.assertFileContent("public/sect2/p3/index.html", "Single",
|
|
|
|
"Menu Sect: /sect5/|Section Five|10|-|-|/sect1/|Section One|100|-|-|/sect2/|Sect2s|0|-|HasMenuCurrent|/sect3/|Sect3s|0|-|-|")
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
}
|