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