mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
output: Add output formats decoder
And clean up the output package.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/hugo/media"
|
||||
@@ -65,18 +66,9 @@ func TestDefaultTypes(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestGetFormat(t *testing.T) {
|
||||
tp, _ := GetFormat("html")
|
||||
require.Equal(t, HTMLFormat, tp)
|
||||
tp, _ = GetFormat("HTML")
|
||||
require.Equal(t, HTMLFormat, tp)
|
||||
_, found := GetFormat("FOO")
|
||||
require.False(t, found)
|
||||
}
|
||||
|
||||
func TestGeGetFormatByName(t *testing.T) {
|
||||
func TestGetFormatByName(t *testing.T) {
|
||||
formats := Formats{AMPFormat, CalendarFormat}
|
||||
tp, _ := formats.GetByName("AMP")
|
||||
tp, _ := formats.GetByName("AMp")
|
||||
require.Equal(t, AMPFormat, tp)
|
||||
_, found := formats.GetByName("HTML")
|
||||
require.False(t, found)
|
||||
@@ -84,7 +76,7 @@ func TestGeGetFormatByName(t *testing.T) {
|
||||
require.False(t, found)
|
||||
}
|
||||
|
||||
func TestGeGetFormatByExt(t *testing.T) {
|
||||
func TestGetFormatByExt(t *testing.T) {
|
||||
formats1 := Formats{AMPFormat, CalendarFormat}
|
||||
formats2 := Formats{AMPFormat, HTMLFormat, CalendarFormat}
|
||||
tp, _ := formats1.GetBySuffix("html")
|
||||
@@ -95,6 +87,99 @@ func TestGeGetFormatByExt(t *testing.T) {
|
||||
require.False(t, found)
|
||||
|
||||
// ambiguous
|
||||
_, found = formats2.GetByName("html")
|
||||
_, found = formats2.GetBySuffix("html")
|
||||
require.False(t, found)
|
||||
}
|
||||
|
||||
func TestDecodeFormats(t *testing.T) {
|
||||
|
||||
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{}{
|
||||
map[string]interface{}{
|
||||
"JsON": map[string]interface{}{
|
||||
"baseName": "myindex",
|
||||
"isPlainText": "false"}}},
|
||||
false,
|
||||
func(t *testing.T, name string, f Formats) {
|
||||
require.Len(t, f, len(DefaultFormats), name)
|
||||
json, _ := f.GetByName("JSON")
|
||||
require.Equal(t, "myindex", json.BaseName)
|
||||
require.Equal(t, media.JSONType, json.MediaType)
|
||||
require.False(t, json.IsPlainText)
|
||||
|
||||
}},
|
||||
{
|
||||
"Add XML format with string as mediatype",
|
||||
[]map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"MYXMLFORMAT": map[string]interface{}{
|
||||
"baseName": "myxml",
|
||||
"mediaType": "application/xml",
|
||||
}}},
|
||||
false,
|
||||
func(t *testing.T, name string, f Formats) {
|
||||
require.Len(t, f, len(DefaultFormats)+1, name)
|
||||
xml, found := f.GetByName("MYXMLFORMAT")
|
||||
require.True(t, found)
|
||||
require.Equal(t, "myxml", xml.BaseName, fmt.Sprint(xml))
|
||||
require.Equal(t, media.XMLType, xml.MediaType)
|
||||
|
||||
// Verify that we haven't changed the DefaultFormats slice.
|
||||
json, _ := f.GetByName("JSON")
|
||||
require.Equal(t, "index", json.BaseName, name)
|
||||
|
||||
}},
|
||||
{
|
||||
"Add format unknown mediatype",
|
||||
[]map[string]interface{}{
|
||||
map[string]interface{}{
|
||||
"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{}{
|
||||
map[string]interface{}{
|
||||
"MYOTHERXMLFORMAT": map[string]interface{}{
|
||||
"baseName": "myotherxml",
|
||||
"mediaType": media.XMLType,
|
||||
}},
|
||||
map[string]interface{}{
|
||||
"MYOTHERXMLFORMAT": map[string]interface{}{
|
||||
"baseName": "myredefined",
|
||||
}},
|
||||
},
|
||||
false,
|
||||
func(t *testing.T, name string, f Formats) {
|
||||
require.Len(t, f, len(DefaultFormats)+1, name)
|
||||
xml, found := f.GetByName("MYOTHERXMLFORMAT")
|
||||
require.True(t, found)
|
||||
require.Equal(t, "myredefined", xml.BaseName, fmt.Sprint(xml))
|
||||
require.Equal(t, media.XMLType, xml.MediaType)
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result, err := DecodeOutputFormats(mediaTypes, test.maps...)
|
||||
if test.shouldError {
|
||||
require.Error(t, err, test.name)
|
||||
} else {
|
||||
require.NoError(t, err, test.name)
|
||||
test.assert(t, test.name, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user