2017-03-06 10:20:30 +00:00
|
|
|
// Copyright 2017-present 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 output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-03-07 16:26:22 +00:00
|
|
|
"github.com/spf13/hugo/media"
|
|
|
|
|
2017-03-06 10:20:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-03-16 07:32:14 +00:00
|
|
|
var ampType = Format{
|
2017-03-07 16:26:22 +00:00
|
|
|
Name: "AMP",
|
|
|
|
MediaType: media.HTMLType,
|
|
|
|
BaseName: "index",
|
|
|
|
}
|
|
|
|
|
2017-03-06 10:20:30 +00:00
|
|
|
func TestLayout(t *testing.T) {
|
|
|
|
|
2017-03-07 16:26:22 +00:00
|
|
|
for _, this := range []struct {
|
|
|
|
name string
|
2017-03-16 07:58:50 +00:00
|
|
|
d LayoutDescriptor
|
2017-03-06 18:54:46 +00:00
|
|
|
hasTheme bool
|
2017-03-06 12:18:33 +00:00
|
|
|
layoutOverride string
|
2017-03-16 07:32:14 +00:00
|
|
|
tp Format
|
2017-03-06 12:18:33 +00:00
|
|
|
expect []string
|
2017-03-06 10:20:30 +00:00
|
|
|
}{
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Home", LayoutDescriptor{Kind: "home"}, true, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"index.amp.html", "index.html", "_default/list.amp.html", "_default/list.html", "theme/index.amp.html", "theme/index.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Section", LayoutDescriptor{Kind: "section", Section: "sect1"}, false, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"section/sect1.amp.html", "section/sect1.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Taxonomy", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, false, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"taxonomy/tag.amp.html", "taxonomy/tag.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Taxonomy term", LayoutDescriptor{Kind: "taxonomyTerm", Section: "categories"}, false, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"taxonomy/categories.terms.amp.html", "taxonomy/categories.terms.html", "_default/terms.amp.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Page", LayoutDescriptor{Kind: "page"}, true, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"_default/single.amp.html", "_default/single.html", "theme/_default/single.amp.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Page with layout", LayoutDescriptor{Kind: "page", Layout: "mylayout"}, false, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"_default/mylayout.amp.html", "_default/mylayout.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Page with layout and type", LayoutDescriptor{Kind: "page", Layout: "mylayout", Type: "myttype"}, false, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"myttype/mylayout.amp.html", "myttype/mylayout.html", "_default/mylayout.amp.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Page with layout and type with subtype", LayoutDescriptor{Kind: "page", Layout: "mylayout", Type: "myttype/mysubtype"}, false, "", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"myttype/mysubtype/mylayout.amp.html", "myttype/mysubtype/mylayout.html", "myttype/mylayout.amp.html"}},
|
2017-03-16 07:58:50 +00:00
|
|
|
{"Page with overridden layout", LayoutDescriptor{Kind: "page", Layout: "mylayout", Type: "myttype"}, false, "myotherlayout", ampType,
|
2017-03-07 16:26:22 +00:00
|
|
|
[]string{"myttype/myotherlayout.amp.html", "myttype/myotherlayout.html"}},
|
2017-03-06 10:20:30 +00:00
|
|
|
} {
|
2017-03-07 16:26:22 +00:00
|
|
|
t.Run(this.name, func(t *testing.T) {
|
|
|
|
l := NewLayoutHandler(this.hasTheme)
|
|
|
|
|
2017-03-16 07:58:50 +00:00
|
|
|
layouts := l.For(this.d, this.layoutOverride, this.tp)
|
2017-03-07 16:26:22 +00:00
|
|
|
|
|
|
|
require.NotNil(t, layouts)
|
|
|
|
require.True(t, len(layouts) >= len(this.expect))
|
|
|
|
// Not checking the complete list for now ...
|
|
|
|
require.Equal(t, this.expect, layouts[:len(this.expect)])
|
2017-03-06 18:54:46 +00:00
|
|
|
|
2017-03-07 16:26:22 +00:00
|
|
|
if !this.hasTheme {
|
|
|
|
for _, layout := range layouts {
|
|
|
|
require.NotContains(t, layout, "theme")
|
|
|
|
}
|
2017-03-06 18:54:46 +00:00
|
|
|
}
|
2017-03-07 16:26:22 +00:00
|
|
|
})
|
2017-03-06 10:20:30 +00:00
|
|
|
}
|
2017-03-18 15:46:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLayout(b *testing.B) {
|
|
|
|
descriptor := LayoutDescriptor{Kind: "taxonomyTerm", Section: "categories"}
|
|
|
|
l := NewLayoutHandler(false)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
layouts := l.For(descriptor, "", HTMLType)
|
|
|
|
require.NotEmpty(b, layouts)
|
|
|
|
}
|
2017-03-06 10:20:30 +00:00
|
|
|
}
|