github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/parser/metadecoders/format_test.go (about)

     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  		{"toml", TOML},
    34  		{"config.toml", TOML},
    35  		{"tOMl", TOML},
    36  		{"org", ORG},
    37  		{"foo", ""},
    38  	} {
    39  		c.Assert(FormatFromString(test.s), qt.Equals, test.expect)
    40  	}
    41  }
    42  
    43  func TestFormatFromMediaType(t *testing.T) {
    44  	c := qt.New(t)
    45  	for _, test := range []struct {
    46  		m      media.Type
    47  		expect Format
    48  	}{
    49  		{media.JSONType, JSON},
    50  		{media.YAMLType, YAML},
    51  		{media.TOMLType, TOML},
    52  		{media.CalendarType, ""},
    53  	} {
    54  		c.Assert(FormatFromMediaType(test.m), qt.Equals, test.expect)
    55  	}
    56  }
    57  
    58  func TestFormatFromContentString(t *testing.T) {
    59  	t.Parallel()
    60  	c := qt.New(t)
    61  
    62  	for i, test := range []struct {
    63  		data   string
    64  		expect interface{}
    65  	}{
    66  		{`foo = "bar"`, TOML},
    67  		{`   foo = "bar"`, TOML},
    68  		{`foo="bar"`, TOML},
    69  		{`foo: "bar"`, YAML},
    70  		{`foo:"bar"`, YAML},
    71  		{`{ "foo": "bar"`, JSON},
    72  		{`a,b,c"`, CSV},
    73  		{`asdfasdf`, Format("")},
    74  		{``, Format("")},
    75  	} {
    76  		errMsg := qt.Commentf("[%d] %s", i, test.data)
    77  
    78  		result := Default.FormatFromContentString(test.data)
    79  
    80  		c.Assert(result, qt.Equals, test.expect, errMsg)
    81  	}
    82  }