// Copyright 2019 The Hugo Authors. All rights reserved.
//
// 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.
package hugolib
import (
"fmt"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/gohugoio/hugo/resources/page"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
)
func TestTaxonomiesCountOrder(t *testing.T) {
t.Parallel()
c := qt.New(t)
taxonomies := make(map[string]string)
taxonomies["tag"] = "tags"
taxonomies["category"] = "categories"
cfg, fs := newTestCfg()
cfg.Set("titleCaseStyle", "none")
cfg.Set("taxonomies", taxonomies)
configs, err := loadTestConfigFromProvider(cfg)
c.Assert(err, qt.IsNil)
const pageContent = `---
tags: ['a', 'B', 'c']
categories: 'd'
---
YAML frontmatter with tags and categories taxonomy.`
writeSource(t, fs, filepath.Join("content", "page.md"), pageContent)
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
st := make([]string, 0)
for _, t := range s.Taxonomies()["tags"].ByCount() {
st = append(st, t.Page().Title()+":"+t.Name)
}
expect := []string{"a:a", "B:b", "c:c"}
if !reflect.DeepEqual(st, expect) {
t.Fatalf("ordered taxonomies mismatch, expected\n%v\ngot\n%q", expect, st)
}
}
func TestTaxonomiesWithAndWithoutContentFile(t *testing.T) {
for _, uglyURLs := range []bool{false, true} {
uglyURLs := uglyURLs
t.Run(fmt.Sprintf("uglyURLs=%t", uglyURLs), func(t *testing.T) {
t.Parallel()
doTestTaxonomiesWithAndWithoutContentFile(t, uglyURLs)
})
}
}
func doTestTaxonomiesWithAndWithoutContentFile(t *testing.T, uglyURLs bool) {
siteConfig := `
baseURL = "http://example.com/blog"
titleCaseStyle = "firstupper"
uglyURLs = %t
paginate = 1
defaultContentLanguage = "en"
[Taxonomies]
tag = "tags"
category = "categories"
other = "others"
empty = "empties"
permalinked = "permalinkeds"
[permalinks]
permalinkeds = "/perma/:slug/"
`
pageTemplate := `---
title: "%s"
tags:
%s
categories:
%s
others:
%s
permalinkeds:
%s
---
# Doc
`
siteConfig = fmt.Sprintf(siteConfig, uglyURLs)
b := newTestSitesBuilder(t).WithConfigFile("toml", siteConfig)
b.WithContent(
"p1.md", fmt.Sprintf(pageTemplate, "t1/c1", "- Tag1", "- cAt1", "- o1", "- Pl1"),
"p2.md", fmt.Sprintf(pageTemplate, "t2/c1", "- tag2",<