2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-03-05 12:23:00 -05:00
|
|
|
//
|
|
|
|
// 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 (
|
2019-05-02 03:54:26 -04:00
|
|
|
"sort"
|
2017-03-05 12:23:00 -05:00
|
|
|
"testing"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2017-03-05 12:23:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultTypes(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
|
|
|
c.Assert(CalendarFormat.Name, qt.Equals, "Calendar")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(CalendarFormat.MediaType, qt.Equals, media.CalendarType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(CalendarFormat.Protocol, qt.Equals, "webcal://")
|
|
|
|
c.Assert(CalendarFormat.Path, qt.HasLen, 0)
|
|
|
|
c.Assert(CalendarFormat.IsPlainText, qt.Equals, true)
|
|
|
|
c.Assert(CalendarFormat.IsHTML, qt.Equals, false)
|
|
|
|
|
|
|
|
c.Assert(CSSFormat.Name, qt.Equals, "CSS")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(CSSFormat.MediaType, qt.Equals, media.CSSType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(CSSFormat.Path, qt.HasLen, 0)
|
|
|
|
c.Assert(CSSFormat.Protocol, qt.HasLen, 0) // Will inherit the BaseURL protocol.
|
|
|
|
c.Assert(CSSFormat.IsPlainText, qt.Equals, true)
|
|
|
|
c.Assert(CSSFormat.IsHTML, qt.Equals, false)
|
|
|
|
|
|
|
|
c.Assert(CSVFormat.Name, qt.Equals, "CSV")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(CSVFormat.MediaType, qt.Equals, media.CSVType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(CSVFormat.Path, qt.HasLen, 0)
|
|
|
|
c.Assert(CSVFormat.Protocol, qt.HasLen, 0)
|
|
|
|
c.Assert(CSVFormat.IsPlainText, qt.Equals, true)
|
|
|
|
c.Assert(CSVFormat.IsHTML, qt.Equals, false)
|
|
|
|
c.Assert(CSVFormat.Permalinkable, qt.Equals, false)
|
|
|
|
|
|
|
|
c.Assert(HTMLFormat.Name, qt.Equals, "HTML")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(HTMLFormat.MediaType, qt.Equals, media.HTMLType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(HTMLFormat.Path, qt.HasLen, 0)
|
|
|
|
c.Assert(HTMLFormat.Protocol, qt.HasLen, 0)
|
|
|
|
c.Assert(HTMLFormat.IsPlainText, qt.Equals, false)
|
|
|
|
c.Assert(HTMLFormat.IsHTML, qt.Equals, true)
|
|
|
|
c.Assert(AMPFormat.Permalinkable, qt.Equals, true)
|
|
|
|
|
|
|
|
c.Assert(AMPFormat.Name, qt.Equals, "AMP")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(AMPFormat.MediaType, qt.Equals, media.HTMLType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(AMPFormat.Path, qt.Equals, "amp")
|
|
|
|
c.Assert(AMPFormat.Protocol, qt.HasLen, 0)
|
|
|
|
c.Assert(AMPFormat.IsPlainText, qt.Equals, false)
|
|
|
|
c.Assert(AMPFormat.IsHTML, qt.Equals, true)
|
|
|
|
c.Assert(AMPFormat.Permalinkable, qt.Equals, true)
|
|
|
|
|
|
|
|
c.Assert(RSSFormat.Name, qt.Equals, "RSS")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(RSSFormat.MediaType, qt.Equals, media.RSSType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(RSSFormat.Path, qt.HasLen, 0)
|
|
|
|
c.Assert(RSSFormat.IsPlainText, qt.Equals, false)
|
|
|
|
c.Assert(RSSFormat.NoUgly, qt.Equals, true)
|
|
|
|
c.Assert(CalendarFormat.IsHTML, qt.Equals, false)
|
2021-06-18 07:15:00 -04:00
|
|
|
|
|
|
|
c.Assert(len(DefaultFormats), qt.Equals, 10)
|
|
|
|
|
2017-03-05 12:23:00 -05:00
|
|
|
}
|
2017-03-08 07:45:33 -05:00
|
|
|
|
2017-04-03 11:00:23 -04:00
|
|
|
func TestGetFormatByName(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-03-27 14:43:49 -04:00
|
|
|
formats := Formats{AMPFormat, CalendarFormat}
|
2017-04-03 11:00:23 -04:00
|
|
|
tp, _ := formats.GetByName("AMp")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(tp, qt.Equals, AMPFormat)
|
2017-03-27 14:43:49 -04:00
|
|
|
_, found := formats.GetByName("HTML")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, false)
|
2017-03-27 14:43:49 -04:00
|
|
|
_, found = formats.GetByName("FOO")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, false)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2017-04-03 11:00:23 -04:00
|
|
|
func TestGetFormatByExt(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-03-27 14:43:49 -04:00
|
|
|
formats1 := Formats{AMPFormat, CalendarFormat}
|
|
|
|
formats2 := Formats{AMPFormat, HTMLFormat, CalendarFormat}
|
|
|
|
tp, _ := formats1.GetBySuffix("html")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(tp, qt.Equals, AMPFormat)
|
2017-03-27 14:43:49 -04:00
|
|
|
tp, _ = formats1.GetBySuffix("ics")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(tp, qt.Equals, CalendarFormat)
|
2017-03-27 14:43:49 -04:00
|
|
|
_, found := formats1.GetBySuffix("not")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, false)
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
// ambiguous
|
2017-04-03 11:00:23 -04:00
|
|
|
_, found = formats2.GetBySuffix("html")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, false)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-04-03 11:00:23 -04:00
|
|
|
|
2017-06-20 11:20:08 -04:00
|
|
|
func TestGetFormatByFilename(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-06-20 11:20:08 -04:00
|
|
|
noExtNoDelimMediaType := media.TextType
|
|
|
|
noExtNoDelimMediaType.Delimiter = ""
|
|
|
|
|
|
|
|
noExtMediaType := media.TextType
|
|
|
|
|
|
|
|
var (
|
|
|
|
noExtDelimFormat = Format{
|
|
|
|
Name: "NEM",
|
|
|
|
MediaType: noExtNoDelimMediaType,
|
|
|
|
BaseName: "_redirects",
|
|
|
|
}
|
|
|
|
noExt = Format{
|
|
|
|
Name: "NEX",
|
|
|
|
MediaType: noExtMediaType,
|
|
|
|
BaseName: "next",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat}
|
|
|
|
f, found := formats.FromFilename("my.amp.html")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(f, qt.Equals, AMPFormat)
|
2019-08-02 11:28:43 -04:00
|
|
|
_, found = formats.FromFilename("my.ics")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
2017-06-20 11:20:08 -04:00
|
|
|
f, found = formats.FromFilename("my.html")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(f, qt.Equals, HTMLFormat)
|
2017-06-20 11:20:08 -04:00
|
|
|
f, found = formats.FromFilename("my.nem")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(f, qt.Equals, noExtDelimFormat)
|
2017-06-20 11:20:08 -04:00
|
|
|
f, found = formats.FromFilename("my.nex")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(f, qt.Equals, noExt)
|
2017-08-07 14:19:24 -04:00
|
|
|
_, found = formats.FromFilename("my.css")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, false)
|
2017-06-20 11:20:08 -04:00
|
|
|
}
|
|
|
|
|
2017-04-03 11:00:23 -04:00
|
|
|
func TestDecodeFormats(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-04-03 11:00:23 -04:00
|
|
|
|
|
|
|
mediaTypes := media.Types{media.JSONType, media.XMLType}
|
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
tests := []struct {
|
2017-04-03 11:00:23 -04:00
|
|
|
name string
|
|
|
|
maps []map[string]interface{}
|
|
|
|
shouldError bool
|
|
|
|
assert func(t *testing.T, name string, f Formats)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Redefine JSON",
|
|
|
|
[]map[string]interface{}{
|
2017-08-07 14:03:15 -04:00
|
|
|
{
|
2017-04-03 11:00:23 -04:00
|
|
|
"JsON": map[string]interface{}{
|
|
|
|
"baseName": "myindex",
|
2020-12-02 07:23:25 -05:00
|
|
|
"isPlainText": "false",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
false,
|
|
|
|
func(t *testing.T, name string, f Formats) {
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf(name)
|
|
|
|
c.Assert(len(f), qt.Equals, len(DefaultFormats), msg)
|
2017-04-03 11:00:23 -04:00
|
|
|
json, _ := f.GetByName("JSON")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(json.BaseName, qt.Equals, "myindex")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(json.MediaType, qt.Equals, media.JSONType)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(json.IsPlainText, qt.Equals, false)
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
{
|
|
|
|
"Add XML format with string as mediatype",
|
|
|
|
[]map[string]interface{}{
|
2017-08-07 14:03:15 -04:00
|
|
|
{
|
2017-04-03 11:00:23 -04:00
|
|
|
"MYXMLFORMAT": map[string]interface{}{
|
|
|
|
"baseName": "myxml",
|
|
|
|
"mediaType": "application/xml",
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
false,
|
|
|
|
func(t *testing.T, name string, f Formats) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(f), qt.Equals, len(DefaultFormats)+1)
|
2017-04-03 11:00:23 -04:00
|
|
|
xml, found := f.GetByName("MYXMLFORMAT")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
|
|
|
c.Assert(xml.BaseName, qt.Equals, "myxml")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(xml.MediaType, qt.Equals, media.XMLType)
|
2017-04-03 11:00:23 -04:00
|
|
|
|
|
|
|
// Verify that we haven't changed the DefaultFormats slice.
|
|
|
|
json, _ := f.GetByName("JSON")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(json.BaseName, qt.Equals, "index")
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
{
|
|
|
|
"Add format unknown mediatype",
|
|
|
|
[]map[string]interface{}{
|
2017-08-07 14:03:15 -04:00
|
|
|
{
|
2017-04-03 11:00:23 -04:00
|
|
|
"MYINVALID": map[string]interface{}{
|
|
|
|
"baseName": "mymy",
|
|
|
|
"mediaType": "application/hugo",
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
true,
|
|
|
|
func(t *testing.T, name string, f Formats) {
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
{
|
|
|
|
"Add and redefine XML format",
|
|
|
|
[]map[string]interface{}{
|
2017-08-07 14:03:15 -04:00
|
|
|
{
|
2017-04-03 11:00:23 -04:00
|
|
|
"MYOTHERXMLFORMAT": map[string]interface{}{
|
|
|
|
"baseName": "myotherxml",
|
|
|
|
"mediaType": media.XMLType,
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
2017-08-07 14:03:15 -04:00
|
|
|
{
|
2017-04-03 11:00:23 -04:00
|
|
|
"MYOTHERXMLFORMAT": map[string]interface{}{
|
|
|
|
"baseName": "myredefined",
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
func(t *testing.T, name string, f Formats) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(f), qt.Equals, len(DefaultFormats)+1)
|
2017-04-03 11:00:23 -04:00
|
|
|
xml, found := f.GetByName("MYOTHERXMLFORMAT")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(found, qt.Equals, true)
|
|
|
|
c.Assert(xml.BaseName, qt.Equals, "myredefined")
|
2021-03-11 03:18:01 -05:00
|
|
|
c.Assert(xml.MediaType, qt.Equals, media.XMLType)
|
2020-12-02 07:23:25 -05:00
|
|
|
},
|
|
|
|
},
|
2017-04-03 11:00:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-04-03 16:39:37 -04:00
|
|
|
result, err := DecodeFormats(mediaTypes, test.maps...)
|
2019-08-10 15:05:17 -04:00
|
|
|
msg := qt.Commentf(test.name)
|
|
|
|
|
2017-04-03 11:00:23 -04:00
|
|
|
if test.shouldError {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil), msg)
|
2017-04-03 11:00:23 -04:00
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil, msg)
|
2017-04-03 11:00:23 -04:00
|
|
|
test.assert(t, test.name, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 03:54:26 -04:00
|
|
|
|
|
|
|
func TestSort(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
|
|
|
c.Assert(DefaultFormats[0].Name, qt.Equals, "HTML")
|
|
|
|
c.Assert(DefaultFormats[1].Name, qt.Equals, "AMP")
|
2019-05-02 03:54:26 -04:00
|
|
|
|
|
|
|
json := JSONFormat
|
|
|
|
json.Weight = 1
|
|
|
|
|
|
|
|
formats := Formats{
|
|
|
|
AMPFormat,
|
|
|
|
HTMLFormat,
|
|
|
|
json,
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(formats)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(formats[0].Name, qt.Equals, "JSON")
|
|
|
|
c.Assert(formats[1].Name, qt.Equals, "HTML")
|
|
|
|
c.Assert(formats[2].Name, qt.Equals, "AMP")
|
2019-05-02 03:54:26 -04:00
|
|
|
}
|