hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

outputFormat_test.go (7683B)

    1 // Copyright 2019 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 	"sort"
   18 	"testing"
   19 
   20 	qt "github.com/frankban/quicktest"
   21 	"github.com/gohugoio/hugo/media"
   22 )
   23 
   24 func TestDefaultTypes(t *testing.T) {
   25 	c := qt.New(t)
   26 	c.Assert(CalendarFormat.Name, qt.Equals, "Calendar")
   27 	c.Assert(CalendarFormat.MediaType, qt.Equals, media.CalendarType)
   28 	c.Assert(CalendarFormat.Protocol, qt.Equals, "webcal://")
   29 	c.Assert(CalendarFormat.Path, qt.HasLen, 0)
   30 	c.Assert(CalendarFormat.IsPlainText, qt.Equals, true)
   31 	c.Assert(CalendarFormat.IsHTML, qt.Equals, false)
   32 
   33 	c.Assert(CSSFormat.Name, qt.Equals, "CSS")
   34 	c.Assert(CSSFormat.MediaType, qt.Equals, media.CSSType)
   35 	c.Assert(CSSFormat.Path, qt.HasLen, 0)
   36 	c.Assert(CSSFormat.Protocol, qt.HasLen, 0) // Will inherit the BaseURL protocol.
   37 	c.Assert(CSSFormat.IsPlainText, qt.Equals, true)
   38 	c.Assert(CSSFormat.IsHTML, qt.Equals, false)
   39 
   40 	c.Assert(CSVFormat.Name, qt.Equals, "CSV")
   41 	c.Assert(CSVFormat.MediaType, qt.Equals, media.CSVType)
   42 	c.Assert(CSVFormat.Path, qt.HasLen, 0)
   43 	c.Assert(CSVFormat.Protocol, qt.HasLen, 0)
   44 	c.Assert(CSVFormat.IsPlainText, qt.Equals, true)
   45 	c.Assert(CSVFormat.IsHTML, qt.Equals, false)
   46 	c.Assert(CSVFormat.Permalinkable, qt.Equals, false)
   47 
   48 	c.Assert(HTMLFormat.Name, qt.Equals, "HTML")
   49 	c.Assert(HTMLFormat.MediaType, qt.Equals, media.HTMLType)
   50 	c.Assert(HTMLFormat.Path, qt.HasLen, 0)
   51 	c.Assert(HTMLFormat.Protocol, qt.HasLen, 0)
   52 	c.Assert(HTMLFormat.IsPlainText, qt.Equals, false)
   53 	c.Assert(HTMLFormat.IsHTML, qt.Equals, true)
   54 	c.Assert(AMPFormat.Permalinkable, qt.Equals, true)
   55 
   56 	c.Assert(AMPFormat.Name, qt.Equals, "AMP")
   57 	c.Assert(AMPFormat.MediaType, qt.Equals, media.HTMLType)
   58 	c.Assert(AMPFormat.Path, qt.Equals, "amp")
   59 	c.Assert(AMPFormat.Protocol, qt.HasLen, 0)
   60 	c.Assert(AMPFormat.IsPlainText, qt.Equals, false)
   61 	c.Assert(AMPFormat.IsHTML, qt.Equals, true)
   62 	c.Assert(AMPFormat.Permalinkable, qt.Equals, true)
   63 
   64 	c.Assert(RSSFormat.Name, qt.Equals, "RSS")
   65 	c.Assert(RSSFormat.MediaType, qt.Equals, media.RSSType)
   66 	c.Assert(RSSFormat.Path, qt.HasLen, 0)
   67 	c.Assert(RSSFormat.IsPlainText, qt.Equals, false)
   68 	c.Assert(RSSFormat.NoUgly, qt.Equals, true)
   69 	c.Assert(CalendarFormat.IsHTML, qt.Equals, false)
   70 
   71 	c.Assert(len(DefaultFormats), qt.Equals, 11)
   72 
   73 }
   74 
   75 func TestGetFormatByName(t *testing.T) {
   76 	c := qt.New(t)
   77 	formats := Formats{AMPFormat, CalendarFormat}
   78 	tp, _ := formats.GetByName("AMp")
   79 	c.Assert(tp, qt.Equals, AMPFormat)
   80 	_, found := formats.GetByName("HTML")
   81 	c.Assert(found, qt.Equals, false)
   82 	_, found = formats.GetByName("FOO")
   83 	c.Assert(found, qt.Equals, false)
   84 }
   85 
   86 func TestGetFormatByExt(t *testing.T) {
   87 	c := qt.New(t)
   88 	formats1 := Formats{AMPFormat, CalendarFormat}
   89 	formats2 := Formats{AMPFormat, HTMLFormat, CalendarFormat}
   90 	tp, _ := formats1.GetBySuffix("html")
   91 	c.Assert(tp, qt.Equals, AMPFormat)
   92 	tp, _ = formats1.GetBySuffix("ics")
   93 	c.Assert(tp, qt.Equals, CalendarFormat)
   94 	_, found := formats1.GetBySuffix("not")
   95 	c.Assert(found, qt.Equals, false)
   96 
   97 	// ambiguous
   98 	_, found = formats2.GetBySuffix("html")
   99 	c.Assert(found, qt.Equals, false)
  100 }
  101 
  102 func TestGetFormatByFilename(t *testing.T) {
  103 	c := qt.New(t)
  104 	noExtNoDelimMediaType := media.TextType
  105 	noExtNoDelimMediaType.Delimiter = ""
  106 
  107 	noExtMediaType := media.TextType
  108 
  109 	var (
  110 		noExtDelimFormat = Format{
  111 			Name:      "NEM",
  112 			MediaType: noExtNoDelimMediaType,
  113 			BaseName:  "_redirects",
  114 		}
  115 		noExt = Format{
  116 			Name:      "NEX",
  117 			MediaType: noExtMediaType,
  118 			BaseName:  "next",
  119 		}
  120 	)
  121 
  122 	formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat}
  123 	f, found := formats.FromFilename("my.amp.html")
  124 	c.Assert(found, qt.Equals, true)
  125 	c.Assert(f, qt.Equals, AMPFormat)
  126 	_, found = formats.FromFilename("my.ics")
  127 	c.Assert(found, qt.Equals, true)
  128 	f, found = formats.FromFilename("my.html")
  129 	c.Assert(found, qt.Equals, true)
  130 	c.Assert(f, qt.Equals, HTMLFormat)
  131 	f, found = formats.FromFilename("my.nem")
  132 	c.Assert(found, qt.Equals, true)
  133 	c.Assert(f, qt.Equals, noExtDelimFormat)
  134 	f, found = formats.FromFilename("my.nex")
  135 	c.Assert(found, qt.Equals, true)
  136 	c.Assert(f, qt.Equals, noExt)
  137 	_, found = formats.FromFilename("my.css")
  138 	c.Assert(found, qt.Equals, false)
  139 }
  140 
  141 func TestDecodeFormats(t *testing.T) {
  142 	c := qt.New(t)
  143 
  144 	mediaTypes := media.Types{media.JSONType, media.XMLType}
  145 
  146 	tests := []struct {
  147 		name        string
  148 		maps        []map[string]any
  149 		shouldError bool
  150 		assert      func(t *testing.T, name string, f Formats)
  151 	}{
  152 		{
  153 			"Redefine JSON",
  154 			[]map[string]any{
  155 				{
  156 					"JsON": map[string]any{
  157 						"baseName":    "myindex",
  158 						"isPlainText": "false",
  159 					},
  160 				},
  161 			},
  162 			false,
  163 			func(t *testing.T, name string, f Formats) {
  164 				msg := qt.Commentf(name)
  165 				c.Assert(len(f), qt.Equals, len(DefaultFormats), msg)
  166 				json, _ := f.GetByName("JSON")
  167 				c.Assert(json.BaseName, qt.Equals, "myindex")
  168 				c.Assert(json.MediaType, qt.Equals, media.JSONType)
  169 				c.Assert(json.IsPlainText, qt.Equals, false)
  170 			},
  171 		},
  172 		{
  173 			"Add XML format with string as mediatype",
  174 			[]map[string]any{
  175 				{
  176 					"MYXMLFORMAT": map[string]any{
  177 						"baseName":  "myxml",
  178 						"mediaType": "application/xml",
  179 					},
  180 				},
  181 			},
  182 			false,
  183 			func(t *testing.T, name string, f Formats) {
  184 				c.Assert(len(f), qt.Equals, len(DefaultFormats)+1)
  185 				xml, found := f.GetByName("MYXMLFORMAT")
  186 				c.Assert(found, qt.Equals, true)
  187 				c.Assert(xml.BaseName, qt.Equals, "myxml")
  188 				c.Assert(xml.MediaType, qt.Equals, media.XMLType)
  189 
  190 				// Verify that we haven't changed the DefaultFormats slice.
  191 				json, _ := f.GetByName("JSON")
  192 				c.Assert(json.BaseName, qt.Equals, "index")
  193 			},
  194 		},
  195 		{
  196 			"Add format unknown mediatype",
  197 			[]map[string]any{
  198 				{
  199 					"MYINVALID": map[string]any{
  200 						"baseName":  "mymy",
  201 						"mediaType": "application/hugo",
  202 					},
  203 				},
  204 			},
  205 			true,
  206 			func(t *testing.T, name string, f Formats) {
  207 			},
  208 		},
  209 		{
  210 			"Add and redefine XML format",
  211 			[]map[string]any{
  212 				{
  213 					"MYOTHERXMLFORMAT": map[string]any{
  214 						"baseName":  "myotherxml",
  215 						"mediaType": media.XMLType,
  216 					},
  217 				},
  218 				{
  219 					"MYOTHERXMLFORMAT": map[string]any{
  220 						"baseName": "myredefined",
  221 					},
  222 				},
  223 			},
  224 			false,
  225 			func(t *testing.T, name string, f Formats) {
  226 				c.Assert(len(f), qt.Equals, len(DefaultFormats)+1)
  227 				xml, found := f.GetByName("MYOTHERXMLFORMAT")
  228 				c.Assert(found, qt.Equals, true)
  229 				c.Assert(xml.BaseName, qt.Equals, "myredefined")
  230 				c.Assert(xml.MediaType, qt.Equals, media.XMLType)
  231 			},
  232 		},
  233 	}
  234 
  235 	for _, test := range tests {
  236 		result, err := DecodeFormats(mediaTypes, test.maps...)
  237 		msg := qt.Commentf(test.name)
  238 
  239 		if test.shouldError {
  240 			c.Assert(err, qt.Not(qt.IsNil), msg)
  241 		} else {
  242 			c.Assert(err, qt.IsNil, msg)
  243 			test.assert(t, test.name, result)
  244 		}
  245 	}
  246 }
  247 
  248 func TestSort(t *testing.T) {
  249 	c := qt.New(t)
  250 	c.Assert(DefaultFormats[0].Name, qt.Equals, "HTML")
  251 	c.Assert(DefaultFormats[1].Name, qt.Equals, "AMP")
  252 
  253 	json := JSONFormat
  254 	json.Weight = 1
  255 
  256 	formats := Formats{
  257 		AMPFormat,
  258 		HTMLFormat,
  259 		json,
  260 	}
  261 
  262 	sort.Sort(formats)
  263 
  264 	c.Assert(formats[0].Name, qt.Equals, "JSON")
  265 	c.Assert(formats[1].Name, qt.Equals, "HTML")
  266 	c.Assert(formats[2].Name, qt.Equals, "AMP")
  267 }