github.com/SuCicada/su-hugo@v1.0.0/parser/pageparser/pageparser_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 pageparser
    15  
    16  import (
    17  	"bytes"
    18  	"strings"
    19  	"testing"
    20  
    21  	qt "github.com/frankban/quicktest"
    22  	"github.com/gohugoio/hugo/parser/metadecoders"
    23  )
    24  
    25  func BenchmarkParse(b *testing.B) {
    26  	start := `
    27  	
    28  
    29  ---
    30  title: "Front Matters"
    31  description: "It really does"
    32  ---
    33  
    34  This is some summary. This is some summary. This is some summary. This is some summary.
    35  
    36   <!--more-->
    37  
    38  
    39  `
    40  	input := []byte(start + strings.Repeat(strings.Repeat("this is text", 30)+"{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}", 10))
    41  	cfg := Config{EnableEmoji: false}
    42  
    43  	b.ResetTimer()
    44  	for i := 0; i < b.N; i++ {
    45  		if _, err := parseBytes(input, cfg, lexIntroSection); err != nil {
    46  			b.Fatal(err)
    47  		}
    48  	}
    49  }
    50  
    51  func BenchmarkParseWithEmoji(b *testing.B) {
    52  	start := `
    53  	
    54  
    55  ---
    56  title: "Front Matters"
    57  description: "It really does"
    58  ---
    59  
    60  This is some summary. This is some summary. This is some summary. This is some summary.
    61  
    62   <!--more-->
    63  
    64  
    65  `
    66  	input := []byte(start + strings.Repeat("this is not emoji: ", 50) + strings.Repeat("some text ", 70) + strings.Repeat("this is not: ", 50) + strings.Repeat("but this is a :smile: ", 3) + strings.Repeat("some text ", 70))
    67  	cfg := Config{EnableEmoji: true}
    68  
    69  	b.ResetTimer()
    70  	for i := 0; i < b.N; i++ {
    71  		if _, err := parseBytes(input, cfg, lexIntroSection); err != nil {
    72  			b.Fatal(err)
    73  		}
    74  	}
    75  }
    76  
    77  func TestFormatFromFrontMatterType(t *testing.T) {
    78  	c := qt.New(t)
    79  	for _, test := range []struct {
    80  		typ    ItemType
    81  		expect metadecoders.Format
    82  	}{
    83  		{TypeFrontMatterJSON, metadecoders.JSON},
    84  		{TypeFrontMatterTOML, metadecoders.TOML},
    85  		{TypeFrontMatterYAML, metadecoders.YAML},
    86  		{TypeFrontMatterORG, metadecoders.ORG},
    87  		{TypeIgnore, ""},
    88  	} {
    89  		c.Assert(FormatFromFrontMatterType(test.typ), qt.Equals, test.expect)
    90  	}
    91  }
    92  
    93  func TestIsProbablyItemsSource(t *testing.T) {
    94  	c := qt.New(t)
    95  
    96  	input := ` {{< foo >}} `
    97  	items := collectStringMain(input)
    98  
    99  	c.Assert(IsProbablySourceOfItems([]byte(input), items), qt.IsTrue)
   100  	c.Assert(IsProbablySourceOfItems(bytes.Repeat([]byte(" "), len(input)), items), qt.IsFalse)
   101  	c.Assert(IsProbablySourceOfItems([]byte(`{{< foo >}}  `), items), qt.IsFalse)
   102  	c.Assert(IsProbablySourceOfItems([]byte(``), items), qt.IsFalse)
   103  }