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