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"
|
2019-08-10 15:05:17 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var eq = qt.CmpEquals(
|
|
|
|
cmp.Comparer(func(m1, m2 media.Type) bool {
|
|
|
|
return m1.Type() == m2.Type()
|
|
|
|
}),
|
|
|
|
cmp.Comparer(func(o1, o2 Format) bool {
|
|
|
|
return o1.Name == o2.Name
|
|
|
|
}),
|
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")
|
|
|
|
c.Assert(CalendarFormat.MediaType, eq, media.CalendarType)
|
|
|
|
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")
|
|
|
|
c.Assert(CSSFormat.MediaType, eq, media.CSSType)
|
|
|
|
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")
|
|
|
|
c.Assert(CSVFormat.MediaType, eq, media.CSVType)
|
|
|
|
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")
|
|
|
|
c.Assert(HTMLFormat.MediaType, eq, media.HTMLType)
|
|
|
|
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")
|
|
|
|
c.Assert(AMPFormat.MediaType, eq, media.HTMLType)
|
|
|
|
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")
|
|
|
|
c.Assert(RSSFormat.MediaType, eq, media.RSSType)
|
|
|
|
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)
|
2017-03-24 06:25:25 -04:00
|
|
|
|
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")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(tp, eq, 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")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(tp, eq, AMPFormat)
|
2017-03-27 14:43:49 -04:00
|
|
|
tp, _ = formats1.GetBySuffix("ics")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(tp, eq, 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)
|
|
|
|
c.Assert(f, eq, 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)
|
|
|
|
c.Assert(f, eq, 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)
|
|
|
|
c.Assert(f, eq, 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)
|
|
|
|
c.Assert(f, eq, 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}
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
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",
|
|
|
|
"isPlainText": "false"}}},
|
|
|
|
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")
|
|
|
|
c.Assert(json.MediaType, eq, media.JSONType)
|
|
|
|
c.Assert(json.IsPlainText, qt.Equals, false)
|
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",
|
|
|
|
}}},
|
|
|
|
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")
|
|
|
|
c.Assert(xml.MediaType, eq, 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")
|
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",
|
|
|
|
}}},
|
|
|
|
true,
|
|
|
|
func(t *testing.T, name string, f Formats) {
|
|
|
|
|
|
|
|
}},
|
|
|
|
{
|
|
|
|
"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,
|
|
|
|
}},
|
2017-08-07 14:03:15 -04:00
|
|
|
{
|
2017-04-03 11:00:23 -04:00
|
|
|
"MYOTHERXMLFORMAT": map[string]interface{}{
|
|
|
|
"baseName": "myredefined",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
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")
|
|
|
|
c.Assert(xml.MediaType, eq, media.XMLType)
|
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
|
|
|
|
|
|
|
}
|