github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/output/layout_test.go (about)

     1  // Copyright 2017-present 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 output
    15  
    16  import (
    17  	"fmt"
    18  	"reflect"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/gohugoio/hugo/media"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestLayout(t *testing.T) {
    28  
    29  	noExtNoDelimMediaType := media.TextType
    30  	noExtNoDelimMediaType.Suffix = ""
    31  	noExtNoDelimMediaType.Delimiter = ""
    32  
    33  	noExtMediaType := media.TextType
    34  	noExtMediaType.Suffix = ""
    35  
    36  	var (
    37  		ampType = Format{
    38  			Name:      "AMP",
    39  			MediaType: media.HTMLType,
    40  			BaseName:  "index",
    41  		}
    42  
    43  		htmlFormat = HTMLFormat
    44  
    45  		noExtDelimFormat = Format{
    46  			Name:      "NEM",
    47  			MediaType: noExtNoDelimMediaType,
    48  			BaseName:  "_redirects",
    49  		}
    50  		noExt = Format{
    51  			Name:      "NEX",
    52  			MediaType: noExtMediaType,
    53  			BaseName:  "next",
    54  		}
    55  	)
    56  
    57  	for _, this := range []struct {
    58  		name           string
    59  		d              LayoutDescriptor
    60  		layoutOverride string
    61  		tp             Format
    62  		expect         []string
    63  		expectCount    int
    64  	}{
    65  		{"Home", LayoutDescriptor{Kind: "home"}, "", ampType,
    66  			[]string{"index.amp.html", "home.amp.html", "list.amp.html", "index.html", "home.html", "list.html", "_default/index.amp.html"}, 12},
    67  		{"Home, HTML", LayoutDescriptor{Kind: "home"}, "", htmlFormat,
    68  			// We will eventually get to index.html. This looks stuttery, but makes the lookup logic easy to understand.
    69  			[]string{"index.html.html", "home.html.html"}, 12},
    70  		{"Home, french language", LayoutDescriptor{Kind: "home", Lang: "fr"}, "", ampType,
    71  			[]string{"index.fr.amp.html"},
    72  			24},
    73  		{"Home, no ext or delim", LayoutDescriptor{Kind: "home"}, "", noExtDelimFormat,
    74  			[]string{"index.nem", "home.nem", "list.nem"}, 6},
    75  		{"Home, no ext", LayoutDescriptor{Kind: "home"}, "", noExt,
    76  			[]string{"index.nex", "home.nex", "list.nex"}, 6},
    77  		{"Page, no ext or delim", LayoutDescriptor{Kind: "page"}, "", noExtDelimFormat,
    78  			[]string{"_default/single.nem"}, 1},
    79  		{"Section", LayoutDescriptor{Kind: "section", Section: "sect1"}, "", ampType,
    80  			[]string{"sect1/sect1.amp.html", "sect1/section.amp.html", "sect1/list.amp.html", "sect1/sect1.html", "sect1/section.html", "sect1/list.html", "section/sect1.amp.html", "section/section.amp.html"}, 18},
    81  		{"Section with layout", LayoutDescriptor{Kind: "section", Section: "sect1", Layout: "mylayout"}, "", ampType,
    82  			[]string{"sect1/mylayout.amp.html", "sect1/sect1.amp.html", "sect1/section.amp.html", "sect1/list.amp.html", "sect1/mylayout.html", "sect1/sect1.html"}, 24},
    83  		{"Taxonomy", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, "", ampType,
    84  			[]string{"taxonomy/tag.amp.html", "taxonomy/taxonomy.amp.html", "taxonomy/list.amp.html", "taxonomy/tag.html", "taxonomy/taxonomy.html"}, 18},
    85  		{"Taxonomy term", LayoutDescriptor{Kind: "taxonomyTerm", Section: "categories"}, "", ampType,
    86  			[]string{"taxonomy/categories.terms.amp.html", "taxonomy/terms.amp.html", "taxonomy/list.amp.html", "taxonomy/categories.terms.html", "taxonomy/terms.html"}, 18},
    87  		{"Page", LayoutDescriptor{Kind: "page"}, "", ampType,
    88  			[]string{"_default/single.amp.html", "_default/single.html"}, 2},
    89  		{"Page with layout", LayoutDescriptor{Kind: "page", Layout: "mylayout"}, "", ampType,
    90  			[]string{"_default/mylayout.amp.html", "_default/single.amp.html", "_default/mylayout.html", "_default/single.html"}, 4},
    91  		{"Page with layout and type", LayoutDescriptor{Kind: "page", Layout: "mylayout", Type: "myttype"}, "", ampType,
    92  			[]string{"myttype/mylayout.amp.html", "myttype/single.amp.html", "myttype/mylayout.html"}, 8},
    93  		{"Page with layout and type with subtype", LayoutDescriptor{Kind: "page", Layout: "mylayout", Type: "myttype/mysubtype"}, "", ampType,
    94  			[]string{"myttype/mysubtype/mylayout.amp.html", "myttype/mysubtype/single.amp.html", "myttype/mysubtype/mylayout.html"}, 8},
    95  		// RSS
    96  		{"RSS Home", LayoutDescriptor{Kind: "home"}, "", RSSFormat,
    97  			[]string{"index.rss.xml", "home.rss.xml", "rss.xml"}, 15},
    98  		{"RSS Section", LayoutDescriptor{Kind: "section", Section: "sect1"}, "", RSSFormat,
    99  			[]string{"sect1/sect1.rss.xml", "sect1/section.rss.xml", "sect1/rss.xml", "sect1/list.rss.xml", "sect1/sect1.xml", "sect1/section.xml"}, 22},
   100  		{"RSS Taxonomy", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, "", RSSFormat,
   101  			[]string{"taxonomy/tag.rss.xml", "taxonomy/taxonomy.rss.xml", "taxonomy/rss.xml", "taxonomy/list.rss.xml", "taxonomy/tag.xml", "taxonomy/taxonomy.xml"}, 22},
   102  		{"RSS Taxonomy term", LayoutDescriptor{Kind: "taxonomyTerm", Section: "tag"}, "", RSSFormat,
   103  			[]string{"taxonomy/tag.terms.rss.xml", "taxonomy/terms.rss.xml", "taxonomy/rss.xml", "taxonomy/list.rss.xml", "taxonomy/tag.terms.xml"}, 22},
   104  		{"Home plain text", LayoutDescriptor{Kind: "home"}, "", JSONFormat,
   105  			[]string{"_text/index.json.json", "_text/home.json.json"}, 12},
   106  		{"Page plain text", LayoutDescriptor{Kind: "page"}, "", JSONFormat,
   107  			[]string{"_text/_default/single.json.json", "_text/_default/single.json"}, 2},
   108  		{"Reserved section, shortcodes", LayoutDescriptor{Kind: "section", Section: "shortcodes", Type: "shortcodes"}, "", ampType,
   109  			[]string{"section/shortcodes.amp.html"}, 12},
   110  		{"Reserved section, partials", LayoutDescriptor{Kind: "section", Section: "partials", Type: "partials"}, "", ampType,
   111  			[]string{"section/partials.amp.html"}, 12},
   112  	} {
   113  		t.Run(this.name, func(t *testing.T) {
   114  			l := NewLayoutHandler()
   115  
   116  			layouts, err := l.For(this.d, this.tp)
   117  
   118  			require.NoError(t, err)
   119  			require.NotNil(t, layouts)
   120  			require.True(t, len(layouts) >= len(this.expect), fmt.Sprint(layouts))
   121  			// Not checking the complete list for now ...
   122  			got := layouts[:len(this.expect)]
   123  			if len(layouts) != this.expectCount || !reflect.DeepEqual(got, this.expect) {
   124  				formatted := strings.Replace(fmt.Sprintf("%v", layouts), "[", "\"", 1)
   125  				formatted = strings.Replace(formatted, "]", "\"", 1)
   126  				formatted = strings.Replace(formatted, " ", "\", \"", -1)
   127  
   128  				t.Fatalf("Got %d/%d:\n%v\nExpected:\n%v\nAll:\n%v\nFormatted:\n%s", len(layouts), this.expectCount, got, this.expect, layouts, formatted)
   129  
   130  			}
   131  
   132  		})
   133  	}
   134  
   135  }
   136  
   137  func BenchmarkLayout(b *testing.B) {
   138  	descriptor := LayoutDescriptor{Kind: "taxonomyTerm", Section: "categories"}
   139  	l := NewLayoutHandler()
   140  
   141  	for i := 0; i < b.N; i++ {
   142  		layouts, err := l.For(descriptor, HTMLFormat)
   143  		require.NoError(b, err)
   144  		require.NotEmpty(b, layouts)
   145  	}
   146  }