Implement the first generic JSON output testcase

This commit is contained in:
Bjørn Erik Pedersen
2017-03-08 13:45:33 +01:00
parent 3ec5fc3504
commit c8fff9501d
13 changed files with 188 additions and 56 deletions

View File

@@ -14,16 +14,40 @@
package output
import (
"fmt"
"strings"
"github.com/spf13/hugo/media"
)
var (
// An ordered list of built-in output formats
// See https://www.ampproject.org/learn/overview/
AMPType = Type{
Name: "AMP",
MediaType: media.HTMLType,
BaseName: "index",
}
CSSType = Type{
Name: "CSS",
MediaType: media.CSSType,
BaseName: "styles",
}
HTMLType = Type{
Name: "HTML",
MediaType: media.HTMLType,
BaseName: "index",
}
JSONType = Type{
Name: "JSON",
MediaType: media.HTMLType,
BaseName: "index",
IsPlainText: true,
}
RSSType = Type{
Name: "RSS",
MediaType: media.RSSType,
@@ -31,6 +55,14 @@ var (
}
)
var builtInTypes = map[string]Type{
strings.ToLower(AMPType.Name): AMPType,
strings.ToLower(CSSType.Name): CSSType,
strings.ToLower(HTMLType.Name): HTMLType,
strings.ToLower(JSONType.Name): JSONType,
strings.ToLower(RSSType.Name): RSSType,
}
type Types []Type
// Type represents an output represenation, usually to a file on disk.
@@ -57,3 +89,26 @@ type Type struct {
// Enable to ignore the global uglyURLs setting.
NoUgly bool
}
func GetType(key string) (Type, bool) {
found, ok := builtInTypes[key]
if !ok {
found, ok = builtInTypes[strings.ToLower(key)]
}
return found, ok
}
// TODO(bep) outputs rewamp on global config?
func GetTypes(keys ...string) (Types, error) {
var types []Type
for _, key := range keys {
tpe, ok := GetType(key)
if !ok {
return types, fmt.Errorf("OutputType with key %q not found", key)
}
types = append(types, tpe)
}
return types, nil
}

View File

@@ -31,3 +31,12 @@ func TestDefaultTypes(t *testing.T) {
require.Empty(t, RSSType.Path)
require.False(t, RSSType.IsPlainText)
}
func TestGetType(t *testing.T) {
tp, _ := GetType("html")
require.Equal(t, HTMLType, tp)
tp, _ = GetType("HTML")
require.Equal(t, HTMLType, tp)
_, found := GetType("FOO")
require.False(t, found)
}