| 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 FuzzParse(f *testing.F) { |
| 26 |
samples := []string{ |
| 27 |
`{{< foo >}}`, |
| 28 |
`{{% foo %}}`, |
| 29 |
`{{< foo >}} {{< bar >}}`, |
| 30 |
`--- |
| 31 |
title: "Front Matters" |
| 32 |
--- |
| 33 |
|
| 34 |
This is some summary. This is some summary. This is some summary. This is some summary. |
| 35 |
|
| 36 |
<!--more--> |
| 37 |
|
| 38 |
Foo bars. |
| 39 |
|
| 40 |
`, |
| 41 |
} |
| 42 |
for _, s := range samples { |
| 43 |
f.Add([]byte(s)) |
| 44 |
} |
| 45 |
|
| 46 |
f.Fuzz(func(t *testing.T, b []byte) { |
| 47 |
cfg := Config{} |
| 48 |
_, _ = parseBytes(b, cfg, lexIntroSection) |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
func BenchmarkParse(b *testing.B) { |
| 53 |
start := ` |
| 54 |
|
| 55 |
|
| 56 |
--- |
| 57 |
title: "Front Matters" |
| 58 |
description: "It really does" |
| 59 |
--- |
| 60 |
|
| 61 |
This is some summary. This is some summary. This is some summary. This is some summary. |
| 62 |
|
| 63 |
<!--more--> |
| 64 |
|
| 65 |
|
| 66 |
` |
| 67 |
input := []byte(start + strings.Repeat(strings.Repeat("this is text", 30)+"{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}", 10)) |
| 68 |
cfg := Config{} |
| 69 |
|
| 70 |
for b.Loop() { |
| 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, err := collectStringMain(input) |
| 98 |
c.Assert(err, qt.IsNil) |
| 99 |
|
| 100 |
c.Assert(IsProbablySourceOfItems([]byte(input), items), qt.IsTrue) |
| 101 |
c.Assert(IsProbablySourceOfItems(bytes.Repeat([]byte(" "), len(input)), items), qt.IsFalse) |
| 102 |
c.Assert(IsProbablySourceOfItems([]byte(`{{< foo >}} `), items), qt.IsFalse) |
| 103 |
c.Assert(IsProbablySourceOfItems([]byte(``), items), qt.IsFalse) |
| 104 |
} |
| 105 |
|
| 106 |
func TestHasShortcode(t *testing.T) { |
| 107 |
c := qt.New(t) |
| 108 |
|
| 109 |
c.Assert(HasShortcode("{{< foo >}}"), qt.IsTrue) |
| 110 |
c.Assert(HasShortcode("aSDasd SDasd aSD\n\nasdfadf{{% foo %}}\nasdf"), qt.IsTrue) |
| 111 |
c.Assert(HasShortcode("{{</* foo */>}}"), qt.IsFalse) |
| 112 |
c.Assert(HasShortcode("{{%/* foo */%}}"), qt.IsFalse) |
| 113 |
} |
| 114 |
|
| 115 |
func BenchmarkHasShortcode(b *testing.B) { |
| 116 |
withShortcode := strings.Repeat("this is text", 30) + "{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}" + strings.Repeat("this is text", 30) |
| 117 |
withoutShortcode := strings.Repeat("this is text", 30) + "This is some inner content." + strings.Repeat("this is text", 30) |
| 118 |
b.Run("Match", func(b *testing.B) { |
| 119 |
for b.Loop() { |
| 120 |
HasShortcode(withShortcode) |
| 121 |
} |
| 122 |
}) |
| 123 |
|
| 124 |
b.Run("NoMatch", func(b *testing.B) { |
| 125 |
for b.Loop() { |
| 126 |
HasShortcode(withoutShortcode) |
| 127 |
} |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
func TestSummaryDividerStartingFromMain(t *testing.T) { |
| 132 |
c := qt.New(t) |
| 133 |
|
| 134 |
input := `aaa <!--more--> bbb` |
| 135 |
items, err := collectStringMain(input) |
| 136 |
c.Assert(err, qt.IsNil) |
| 137 |
|
| 138 |
c.Assert(items, qt.HasLen, 4) |
| 139 |
c.Assert(items[1].Type, qt.Equals, TypeLeadSummaryDivider) |
| 140 |
} |
| 141 |
|
| 142 |
func TestIdeographicAfterSummaryDivider(t *testing.T) { |
| 143 |
c := qt.New(t) |
| 144 |
|
| 145 |
input := []byte(`aaa <!--more--> bbb`) |
| 146 |
items, err := collectStringMain(string(input)) |
| 147 |
c.Assert(err, qt.IsNil) |
| 148 |
c.Assert(items, qt.HasLen, 4) |
| 149 |
c.Assert(items[2].ValStr(input), qt.Equals, "\u3000bbb") |
| 150 |
} |