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