Debian Codegraph @

parser/pageparser/pageparser_intro_test.go

raw
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 pageparser
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 type lexerTest struct {
25 name string
26 input string
27 items []typeText
28 err error
29 }
30
31 type typeText struct {
32 typ ItemType
33 text string
34 }
35
36 func nti(tp ItemType, val string) typeText {
37 return typeText{typ: tp, text: val}
38 }
39
40 var (
41 tstJSON = `{ "a": { "b": "\"Hugo\"}" } }`
42 tstFrontMatterTOML = nti(TypeFrontMatterTOML, "foo = \"bar\"\n")
43 tstFrontMatterYAML = nti(TypeFrontMatterYAML, "foo: \"bar\"\n")
44 tstFrontMatterYAMLCRLF = nti(TypeFrontMatterYAML, "foo: \"bar\"\r\n")
45 tstFrontMatterJSON = nti(TypeFrontMatterJSON, tstJSON+"\r\n")
46 tstSomeText = nti(tText, "\nSome text.\n")
47 tstSummaryDivider = nti(TypeLeadSummaryDivider, "<!--more-->\n")
48 tstNewline = nti(tText, "\n")
49
50 tstORG = `
51 #+TITLE: T1
52 #+AUTHOR: A1
53 #+DESCRIPTION: D1
54 `
55 tstFrontMatterORG = nti(TypeFrontMatterORG, tstORG)
56 )
57
58 var crLfReplacer = strings.NewReplacer("\r", "#", "\n", "$")
59
60 // TODO(bep) a way to toggle ORG mode vs the rest.
61 var frontMatterTests = []lexerTest{
62 {"empty", "", []typeText{tstEOF}, nil},
63 {"No front matter", "\nSome text.\n", []typeText{tstSomeText, tstEOF}, nil},
64 {"YAML front matter", "---\nfoo: \"bar\"\n---\n\nSome text.\n", []typeText{tstFrontMatterYAML, tstSomeText, tstEOF}, nil},
65 {"YAML empty front matter", "---\n---\n\nSome text.\n", []typeText{nti(TypeFrontMatterYAML, ""), tstSomeText, tstEOF}, nil},
66 // Note that we keep all bytes as they are, but we need to handle CRLF
67 {"YAML front matter CRLF", "---\r\nfoo: \"bar\"\r\n---\n\nSome text.\n", []typeText{tstFrontMatterYAMLCRLF, tstSomeText, tstEOF}, nil},
68 {"TOML front matter", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n", []typeText{tstFrontMatterTOML, tstSomeText, tstEOF}, nil},
69 {"JSON front matter", tstJSON + "\r\n\nSome text.\n", []typeText{tstFrontMatterJSON, tstSomeText, tstEOF}, nil},
70 {"ORG front matter", tstORG + "\nSome text.\n", []typeText{tstFrontMatterORG, tstSomeText, tstEOF}, nil},
71 {"Summary divider ORG", tstORG + "\nSome text.\n# more\nSome text.\n", []typeText{tstFrontMatterORG, tstSomeText, nti(TypeLeadSummaryDivider, "# more\n"), nti(tText, "Some text.\n"), tstEOF}, nil},
72 {"Summary divider", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->\nSome text.\n", []typeText{tstFrontMatterTOML, tstSomeText, tstSummaryDivider, nti(tText, "Some text.\n"), tstEOF}, nil},
73 {"Summary divider same line", "+++\nfoo = \"bar\"\n+++\n\nSome text.<!--more-->Some text.\n", []typeText{tstFrontMatterTOML, nti(tText, "\nSome text."), nti(TypeLeadSummaryDivider, "<!--more-->"), nti(tText, "Some text.\n"), tstEOF}, nil},
74 // https://github.com/gohugoio/hugo/issues/5402
75 {"Summary and shortcode, no space", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->{{< sc1 >}}\nSome text.\n", []typeText{tstFrontMatterTOML, tstSomeText, nti(TypeLeadSummaryDivider, "<!--more-->"), tstLeftNoMD, tstSC1, tstRightNoMD, tstSomeText, tstEOF}, nil},
76 // https://github.com/gohugoio/hugo/issues/5464
77 {"Summary and shortcode only", "+++\nfoo = \"bar\"\n+++\n{{< sc1 >}}\n<!--more-->\n{{< sc2 >}}", []typeText{tstFrontMatterTOML, tstLeftNoMD, tstSC1, tstRightNoMD, tstNewline, tstSummaryDivider, tstLeftNoMD, tstSC2, tstRightNoMD, tstEOF}, nil},
78 }
79
80 func TestFrontMatter(t *testing.T) {
81 t.Parallel()
82 c := qt.New(t)
83 for i, test := range frontMatterTests {
84 items, err := collect([]byte(test.input), false, lexIntroSection)
85 if err != nil {
86 c.Assert(err, qt.Equals, test.err)
87 continue
88 } else {
89 c.Assert(test.err, qt.IsNil)
90 }
91 if !equal(test.input, items, test.items) {
92 got := itemsToString(items, []byte(test.input))
93 expected := testItemsToString(test.items)
94 c.Assert(got, qt.Equals, expected, qt.Commentf("Test %d: %s", i, test.name))
95 }
96 }
97 }
98
99 func itemsToString(items []Item, source []byte) string {
100 var sb strings.Builder
101 for i, item := range items {
102 var s string
103 if item.Err != nil {
104 s = item.Err.Error()
105 } else {
106 s = string(item.Val(source))
107 }
108 sb.WriteString(fmt.Sprintf("%s: %s\n", item.Type, s))
109
110 if i < len(items)-1 {
111 sb.WriteString("\n")
112 }
113 }
114 return crLfReplacer.Replace(sb.String())
115 }
116
117 func testItemsToString(items []typeText) string {
118 var sb strings.Builder
119 for i, item := range items {
120 sb.WriteString(fmt.Sprintf("%s: %s\n", item.typ, item.text))
121
122 if i < len(items)-1 {
123 sb.WriteString("\n")
124 }
125 }
126 return crLfReplacer.Replace(sb.String())
127 }
128
129 func collectWithConfig(input []byte, skipFrontMatter bool, stateStart stateFunc, cfg Config) (items []Item, err error) {
130 l := newPageLexer(input, stateStart, cfg)
131 l.run()
132 iter := NewIterator(l.items)
133
134 for {
135 if l.err != nil {
136 return nil, l.err
137 }
138 item := iter.Next()
139 items = append(items, item)
140 if item.Type == tEOF || item.Type == tError {
141 break
142 }
143 }
144 return
145 }
146
147 func collect(input []byte, skipFrontMatter bool, stateStart stateFunc) (items []Item, err error) {
148 var cfg Config
149
150 return collectWithConfig(input, skipFrontMatter, stateStart, cfg)
151 }
152
153 func collectStringMain(input string) ([]Item, error) {
154 return collect([]byte(input), true, lexMainSection)
155 }
156
157 // no positional checking, for now ...
158 func equal(source string, got []Item, expect []typeText) bool {
159 if len(got) != len(expect) {
160 return false
161 }
162 sourceb := []byte(source)
163 for k := range got {
164 g := got[k]
165 e := expect[k]
166 if g.Type != e.typ {
167 return false
168 }
169
170 var s string
171 if g.Err != nil {
172 s = g.Err.Error()
173 } else {
174 s = string(g.Val(sourceb))
175 }
176
177 if s != e.text {
178 return false
179 }
180
181 }
182 return true
183 }