format_test.go (2008B)
1 // Copyright 2018 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 metadecoders
15
16 import (
17 "testing"
18
19 "github.com/gohugoio/hugo/media"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestFormatFromString(t *testing.T) {
25 c := qt.New(t)
26 for _, test := range []struct {
27 s string
28 expect Format
29 }{
30 {"json", JSON},
31 {"yaml", YAML},
32 {"yml", YAML},
33 {"xml", XML},
34 {"toml", TOML},
35 {"config.toml", TOML},
36 {"tOMl", TOML},
37 {"org", ORG},
38 {"foo", ""},
39 } {
40 c.Assert(FormatFromString(test.s), qt.Equals, test.expect)
41 }
42 }
43
44 func TestFormatFromMediaType(t *testing.T) {
45 c := qt.New(t)
46 for _, test := range []struct {
47 m media.Type
48 expect Format
49 }{
50 {media.JSONType, JSON},
51 {media.YAMLType, YAML},
52 {media.XMLType, XML},
53 {media.RSSType, XML},
54 {media.TOMLType, TOML},
55 {media.CalendarType, ""},
56 } {
57 c.Assert(FormatFromMediaType(test.m), qt.Equals, test.expect)
58 }
59 }
60
61 func TestFormatFromContentString(t *testing.T) {
62 t.Parallel()
63 c := qt.New(t)
64
65 for i, test := range []struct {
66 data string
67 expect any
68 }{
69 {`foo = "bar"`, TOML},
70 {` foo = "bar"`, TOML},
71 {`foo="bar"`, TOML},
72 {`foo: "bar"`, YAML},
73 {`foo:"bar"`, YAML},
74 {`{ "foo": "bar"`, JSON},
75 {`a,b,c"`, CSV},
76 {`<foo>bar</foo>"`, XML},
77 {`asdfasdf`, Format("")},
78 {``, Format("")},
79 } {
80 errMsg := qt.Commentf("[%d] %s", i, test.data)
81
82 result := Default.FormatFromContentString(test.data)
83
84 c.Assert(result, qt.Equals, test.expect, errMsg)
85 }
86 }