diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-03 17:00:23 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-04 15:12:30 +0200 |
commit | c9aee467d387c4c3489c23f120a7ef2fed4d12df (patch) | |
tree | 4b1058db28f48f37aebf31e558292a8f5019045d /output/outputFormat_test.go | |
parent | d6e8b86f66d6d505fadc32bca601762a4aa90c5e (diff) |
output: Add output formats decoder
And clean up the output package.
Diffstat (limited to 'output/outputFormat_test.go')
-rw-r--r-- | output/outputFormat_test.go | 111 |
1 files changed, 98 insertions, 13 deletions
diff --git a/output/outputFormat_test.go b/output/outputFormat_test.go index b73e53f82..48937a8f1 100644 --- a/output/outputFormat_test.go +++ b/output/outputFormat_test.go @@ -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) + } + } +} |