import_jekyll_test.go (6001B)
1 // Copyright 2015 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 commands
15
16 import (
17 "encoding/json"
18 "testing"
19 "time"
20
21 qt "github.com/frankban/quicktest"
22 )
23
24 func TestParseJekyllFilename(t *testing.T) {
25 c := qt.New(t)
26 filenameArray := []string{
27 "2015-01-02-test.md",
28 "2012-03-15-中文.markup",
29 }
30
31 expectResult := []struct {
32 postDate time.Time
33 postName string
34 }{
35 {time.Date(2015, time.January, 2, 0, 0, 0, 0, time.UTC), "test"},
36 {time.Date(2012, time.March, 15, 0, 0, 0, 0, time.UTC), "中文"},
37 }
38
39 for i, filename := range filenameArray {
40 postDate, postName, err := parseJekyllFilename(filename)
41 c.Assert(err, qt.IsNil)
42 c.Assert(expectResult[i].postDate.Format("2006-01-02"), qt.Equals, postDate.Format("2006-01-02"))
43 c.Assert(expectResult[i].postName, qt.Equals, postName)
44 }
45 }
46
47 func TestConvertJekyllMetadata(t *testing.T) {
48 c := qt.New(t)
49 testDataList := []struct {
50 metadata any
51 postName string
52 postDate time.Time
53 draft bool
54 expect string
55 }{
56 {
57 map[any]any{},
58 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
59 `{"date":"2015-10-01T00:00:00Z"}`,
60 },
61 {
62 map[any]any{},
63 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), true,
64 `{"date":"2015-10-01T00:00:00Z","draft":true}`,
65 },
66 {
67 map[any]any{"Permalink": "/permalink.html", "layout": "post"},
68 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
69 `{"date":"2015-10-01T00:00:00Z","url":"/permalink.html"}`,
70 },
71 {
72 map[any]any{"permalink": "/permalink.html"},
73 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
74 `{"date":"2015-10-01T00:00:00Z","url":"/permalink.html"}`,
75 },
76 {
77 map[any]any{"category": nil, "permalink": 123},
78 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
79 `{"date":"2015-10-01T00:00:00Z"}`,
80 },
81 {
82 map[any]any{"Excerpt_Separator": "sep"},
83 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
84 `{"date":"2015-10-01T00:00:00Z","excerpt_separator":"sep"}`,
85 },
86 {
87 map[any]any{"category": "book", "layout": "post", "Others": "Goods", "Date": "2015-10-01 12:13:11"},
88 "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
89 `{"Others":"Goods","categories":["book"],"date":"2015-10-01T12:13:11Z"}`,
90 },
91 }
92
93 for _, data := range testDataList {
94 result, err := convertJekyllMetaData(data.metadata, data.postName, data.postDate, data.draft)
95 c.Assert(err, qt.IsNil)
96 jsonResult, err := json.Marshal(result)
97 c.Assert(err, qt.IsNil)
98 c.Assert(string(jsonResult), qt.Equals, data.expect)
99 }
100 }
101
102 func TestConvertJekyllContent(t *testing.T) {
103 c := qt.New(t)
104 testDataList := []struct {
105 metadata any
106 content string
107 expect string
108 }{
109 {
110 map[any]any{},
111 "Test content\r\n<!-- more -->\npart2 content", "Test content\n<!--more-->\npart2 content",
112 },
113 {
114 map[any]any{},
115 "Test content\n<!-- More -->\npart2 content", "Test content\n<!--more-->\npart2 content",
116 },
117 {
118 map[any]any{"excerpt_separator": "<!--sep-->"},
119 "Test content\n<!--sep-->\npart2 content",
120 "---\nexcerpt_separator: <!--sep-->\n---\nTest content\n<!--more-->\npart2 content",
121 },
122 {map[any]any{}, "{% raw %}text{% endraw %}", "text"},
123 {map[any]any{}, "{%raw%} text2 {%endraw %}", "text2"},
124 {
125 map[any]any{},
126 "{% highlight go %}\nvar s int\n{% endhighlight %}",
127 "{{< highlight go >}}\nvar s int\n{{< / highlight >}}",
128 },
129 {
130 map[any]any{},
131 "{% highlight go linenos hl_lines=\"1 2\" %}\nvar s string\nvar i int\n{% endhighlight %}",
132 "{{< highlight go \"linenos=table,hl_lines=1 2\" >}}\nvar s string\nvar i int\n{{< / highlight >}}",
133 },
134
135 // Octopress image tag
136 {
137 map[any]any{},
138 "{% img http://placekitten.com/890/280 %}",
139 "{{< figure src=\"http://placekitten.com/890/280\" >}}",
140 },
141 {
142 map[any]any{},
143 "{% img left http://placekitten.com/320/250 Place Kitten #2 %}",
144 "{{< figure class=\"left\" src=\"http://placekitten.com/320/250\" title=\"Place Kitten #2\" >}}",
145 },
146 {
147 map[any]any{},
148 "{% img right http://placekitten.com/300/500 150 250 'Place Kitten #3' %}",
149 "{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #3\" >}}",
150 },
151 {
152 map[any]any{},
153 "{% img right http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
154 "{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}",
155 },
156 {
157 map[any]any{},
158 "{% img http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
159 "{{< figure src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}",
160 },
161 {
162 map[any]any{},
163 "{% img right /placekitten/300/500 'Place Kitten #4' 'An image of a very cute kitten' %}",
164 "{{< figure class=\"right\" src=\"/placekitten/300/500\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}",
165 },
166 {
167 map[any]any{"category": "book", "layout": "post", "Date": "2015-10-01 12:13:11"},
168 "somecontent",
169 "---\nDate: \"2015-10-01 12:13:11\"\ncategory: book\nlayout: post\n---\nsomecontent",
170 },
171 }
172 for _, data := range testDataList {
173 result, err := convertJekyllContent(data.metadata, data.content)
174 c.Assert(result, qt.Equals, data.expect)
175 c.Assert(err, qt.IsNil)
176 }
177 }