| 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.Builtin.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.Builtin.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.Builtin.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.Builtin.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.Builtin.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.Builtin.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, 15) |
| 72 |
} |
| 73 |
|
| 74 |
func TestGetFormatByName(t *testing.T) { |
| 75 |
c := qt.New(t) |
| 76 |
formats := Formats{AMPFormat, CalendarFormat} |
| 77 |
tp, _ := formats.GetByName("AMp") |
| 78 |
c.Assert(tp, qt.Equals, AMPFormat) |
| 79 |
_, found := formats.GetByName("HTML") |
| 80 |
c.Assert(found, qt.Equals, false) |
| 81 |
_, found = formats.GetByName("FOO") |
| 82 |
c.Assert(found, qt.Equals, false) |
| 83 |
} |
| 84 |
|
| 85 |
func TestGetFormatByExt(t *testing.T) { |
| 86 |
c := qt.New(t) |
| 87 |
formats1 := Formats{AMPFormat, CalendarFormat} |
| 88 |
formats2 := Formats{AMPFormat, HTMLFormat, CalendarFormat} |
| 89 |
tp, _ := formats1.GetBySuffix("html") |
| 90 |
c.Assert(tp, qt.Equals, AMPFormat) |
| 91 |
tp, _ = formats1.GetBySuffix("ics") |
| 92 |
c.Assert(tp, qt.Equals, CalendarFormat) |
| 93 |
_, found := formats1.GetBySuffix("not") |
| 94 |
c.Assert(found, qt.Equals, false) |
| 95 |
|
| 96 |
// ambiguous |
| 97 |
_, found = formats2.GetBySuffix("html") |
| 98 |
c.Assert(found, qt.Equals, false) |
| 99 |
} |
| 100 |
|
| 101 |
func TestSort(t *testing.T) { |
| 102 |
c := qt.New(t) |
| 103 |
c.Assert(DefaultFormats[0].Name, qt.Equals, "html") |
| 104 |
c.Assert(DefaultFormats[1].Name, qt.Equals, "404") |
| 105 |
|
| 106 |
json := JSONFormat |
| 107 |
json.Weight = 1 |
| 108 |
|
| 109 |
formats := Formats{ |
| 110 |
AMPFormat, |
| 111 |
HTMLFormat, |
| 112 |
json, |
| 113 |
} |
| 114 |
|
| 115 |
sort.Sort(formats) |
| 116 |
|
| 117 |
c.Assert(formats[0].Name, qt.Equals, "json") |
| 118 |
c.Assert(formats[1].Name, qt.Equals, "html") |
| 119 |
c.Assert(formats[2].Name, qt.Equals, "amp") |
| 120 |
} |