github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/output/outputFormat_test.go (about)

     1  // Copyright 2019 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 output
    15  
    16  import (
    17  	"sort"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  	"github.com/gohugoio/hugo/media"
    22  )
    23  
    24  func TestDefaultTypes(t *testing.T) {
    25  	c := qt.New(t)
    26  	c.Assert(CalendarFormat.Name, qt.Equals, "calendar")
    27  	c.Assert(CalendarFormat.MediaType, qt.Equals, media.Builtin.CalendarType)
    28  	c.Assert(CalendarFormat.Protocol, qt.Equals, "webcal://")
    29  	c.Assert(CalendarFormat.Path, qt.HasLen, 0)
    30  	c.Assert(CalendarFormat.IsPlainText, qt.Equals, true)
    31  	c.Assert(CalendarFormat.IsHTML, qt.Equals, false)
    32  
    33  	c.Assert(CSSFormat.Name, qt.Equals, "css")
    34  	c.Assert(CSSFormat.MediaType, qt.Equals, media.Builtin.CSSType)
    35  	c.Assert(CSSFormat.Path, qt.HasLen, 0)
    36  	c.Assert(CSSFormat.Protocol, qt.HasLen, 0) // Will inherit the BaseURL protocol.
    37  	c.Assert(CSSFormat.IsPlainText, qt.Equals, true)
    38  	c.Assert(CSSFormat.IsHTML, qt.Equals, false)
    39  
    40  	c.Assert(CSVFormat.Name, qt.Equals, "csv")
    41  	c.Assert(CSVFormat.MediaType, qt.Equals, media.Builtin.CSVType)
    42  	c.Assert(CSVFormat.Path, qt.HasLen, 0)
    43  	c.Assert(CSVFormat.Protocol, qt.HasLen, 0)
    44  	c.Assert(CSVFormat.IsPlainText, qt.Equals, true)
    45  	c.Assert(CSVFormat.IsHTML, qt.Equals, false)
    46  	c.Assert(CSVFormat.Permalinkable, qt.Equals, false)
    47  
    48  	c.Assert(HTMLFormat.Name, qt.Equals, "html")
    49  	c.Assert(HTMLFormat.MediaType, qt.Equals, media.Builtin.HTMLType)
    50  	c.Assert(HTMLFormat.Path, qt.HasLen, 0)
    51  	c.Assert(HTMLFormat.Protocol, qt.HasLen, 0)
    52  	c.Assert(HTMLFormat.IsPlainText, qt.Equals, false)
    53  	c.Assert(HTMLFormat.IsHTML, qt.Equals, true)
    54  	c.Assert(AMPFormat.Permalinkable, qt.Equals, true)
    55  
    56  	c.Assert(AMPFormat.Name, qt.Equals, "amp")
    57  	c.Assert(AMPFormat.MediaType, qt.Equals, media.Builtin.HTMLType)
    58  	c.Assert(AMPFormat.Path, qt.Equals, "amp")
    59  	c.Assert(AMPFormat.Protocol, qt.HasLen, 0)
    60  	c.Assert(AMPFormat.IsPlainText, qt.Equals, false)
    61  	c.Assert(AMPFormat.IsHTML, qt.Equals, true)
    62  	c.Assert(AMPFormat.Permalinkable, qt.Equals, true)
    63  
    64  	c.Assert(RSSFormat.Name, qt.Equals, "rss")
    65  	c.Assert(RSSFormat.MediaType, qt.Equals, media.Builtin.RSSType)
    66  	c.Assert(RSSFormat.Path, qt.HasLen, 0)
    67  	c.Assert(RSSFormat.IsPlainText, qt.Equals, false)
    68  	c.Assert(RSSFormat.NoUgly, qt.Equals, true)
    69  	c.Assert(CalendarFormat.IsHTML, qt.Equals, false)
    70  
    71  	c.Assert(len(DefaultFormats), qt.Equals, 11)
    72  
    73  }
    74  
    75  func TestGetFormatByName(t *testing.T) {
    76  	c := qt.New(t)
    77  	formats := Formats{AMPFormat, CalendarFormat}
    78  	tp, _ := formats.GetByName("AMp")
    79  	c.Assert(tp, qt.Equals, AMPFormat)
    80  	_, found := formats.GetByName("HTML")
    81  	c.Assert(found, qt.Equals, false)
    82  	_, found = formats.GetByName("FOO")
    83  	c.Assert(found, qt.Equals, false)
    84  }
    85  
    86  func TestGetFormatByExt(t *testing.T) {
    87  	c := qt.New(t)
    88  	formats1 := Formats{AMPFormat, CalendarFormat}
    89  	formats2 := Formats{AMPFormat, HTMLFormat, CalendarFormat}
    90  	tp, _ := formats1.GetBySuffix("html")
    91  	c.Assert(tp, qt.Equals, AMPFormat)
    92  	tp, _ = formats1.GetBySuffix("ics")
    93  	c.Assert(tp, qt.Equals, CalendarFormat)
    94  	_, found := formats1.GetBySuffix("not")
    95  	c.Assert(found, qt.Equals, false)
    96  
    97  	// ambiguous
    98  	_, found = formats2.GetBySuffix("html")
    99  	c.Assert(found, qt.Equals, false)
   100  }
   101  
   102  func TestGetFormatByFilename(t *testing.T) {
   103  	c := qt.New(t)
   104  	noExtNoDelimMediaType := media.Builtin.TextType
   105  	noExtNoDelimMediaType.Delimiter = ""
   106  
   107  	noExtMediaType := media.Builtin.TextType
   108  
   109  	var (
   110  		noExtDelimFormat = Format{
   111  			Name:      "NEM",
   112  			MediaType: noExtNoDelimMediaType,
   113  			BaseName:  "_redirects",
   114  		}
   115  		noExt = Format{
   116  			Name:      "NEX",
   117  			MediaType: noExtMediaType,
   118  			BaseName:  "next",
   119  		}
   120  	)
   121  
   122  	formats := Formats{AMPFormat, HTMLFormat, noExtDelimFormat, noExt, CalendarFormat}
   123  	f, found := formats.FromFilename("my.amp.html")
   124  	c.Assert(found, qt.Equals, true)
   125  	c.Assert(f, qt.Equals, AMPFormat)
   126  	_, found = formats.FromFilename("my.ics")
   127  	c.Assert(found, qt.Equals, true)
   128  	f, found = formats.FromFilename("my.html")
   129  	c.Assert(found, qt.Equals, true)
   130  	c.Assert(f, qt.Equals, HTMLFormat)
   131  	f, found = formats.FromFilename("my.nem")
   132  	c.Assert(found, qt.Equals, true)
   133  	c.Assert(f, qt.Equals, noExtDelimFormat)
   134  	f, found = formats.FromFilename("my.nex")
   135  	c.Assert(found, qt.Equals, true)
   136  	c.Assert(f, qt.Equals, noExt)
   137  	_, found = formats.FromFilename("my.css")
   138  	c.Assert(found, qt.Equals, false)
   139  }
   140  
   141  func TestSort(t *testing.T) {
   142  	c := qt.New(t)
   143  	c.Assert(DefaultFormats[0].Name, qt.Equals, "html")
   144  	c.Assert(DefaultFormats[1].Name, qt.Equals, "amp")
   145  
   146  	json := JSONFormat
   147  	json.Weight = 1
   148  
   149  	formats := Formats{
   150  		AMPFormat,
   151  		HTMLFormat,
   152  		json,
   153  	}
   154  
   155  	sort.Sort(formats)
   156  
   157  	c.Assert(formats[0].Name, qt.Equals, "json")
   158  	c.Assert(formats[1].Name, qt.Equals, "html")
   159  	c.Assert(formats[2].Name, qt.Equals, "amp")
   160  }