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)
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(CalendarFormat.Name, qt.Equals, "calendar")
|
|
|
|
c.Assert(CalendarFormat.MediaType, qt.Equals, media.Builtin.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)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(CSSFormat.Name, qt.Equals, "css")
|
|
|
|
c.Assert(CSSFormat.MediaType, qt.Equals, media.Builtin.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)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(CSVFormat.Name, qt.Equals, "csv")
|
|
|
|
c.Assert(CSVFormat.MediaType, qt.Equals, media.Builtin.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)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(HTMLFormat.Name, qt.Equals, "html")
|
|
|
|
c.Assert(HTMLFormat.MediaType, qt.Equals, media.Builtin.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)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(AMPFormat.Name, qt.Equals, "amp")
|
|
|
|
c.Assert(AMPFormat.MediaType, qt.Equals, media.Builtin.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)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
c.Assert(RSSFormat.Name, qt.Equals, "rss")
|
|
|
|
c.Assert(RSSFormat.MediaType, qt.Equals, media.Builtin.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
|
|
|
|
2022-05-27 09:19:02 -04:00
|
|
|
c.Assert(len(DefaultFormats), qt.Equals, 11)
|
2021-06-18 07:15:00 -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")
|
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)
|
2023-01-04 12:24:36 -05:00
|
|
|
noExtNoDelimMediaType := media.Builtin.TextType
|
2017-06-20 11:20:08 -04:00
|
|
|
noExtNoDelimMediaType.Delimiter = ""
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
noExtMediaType := media.Builtin.TextType
|
2017-06-20 11:20:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
2023-01-04 12:24:36 -05:00
|
|
|
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)
|
|
|
|
|
2023-01-04 12:24:36 -05: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
|
|
|
}
|