summaryrefslogtreecommitdiffstats
path: root/commands/import_jekyll_test.go
blob: dbe4e25d010af382581efc376074c86a075b56d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
	"encoding/json"
	"testing"
	"time"

	qt "github.com/frankban/quicktest"
)

func TestParseJekyllFilename(t *testing.T) {
	c := qt.New(t)
	filenameArray := []string{
		"2015-01-02-test.md",
		"2012-03-15-中文.markup",
	}

	expectResult := []struct {
		postDate time.Time
		postName string
	}{
		{time.Date(2015, time.January, 2, 0, 0, 0, 0, time.UTC), "test"},
		{time.Date(2012, time.March, 15, 0, 0, 0, 0, time.UTC), "中文"},
	}

	for i, filename := range filenameArray {
		postDate, postName, err := parseJekyllFilename(filename)
		c.Assert(err, qt.IsNil)
		c.Assert(expectResult[i].postDate.Format("2006-01-02"), qt.Equals, postDate.Format("2006-01-02"))
		c.Assert(expectResult[i].postName, qt.Equals, postName)
	}
}

func TestConvertJekyllMetadata(t *testing.T) {
	c := qt.New(t)
	testDataList := []struct {
		metadata any
		postName string
		postDate time.Time
		draft    bool
		expect   string
	}{
		{
			map[any]any{},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
			`{"date":"2015-10-01T00:00:00Z"}`,
		},
		{
			map[any]any{},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), true,
			`{"date":"2015-10-01T00:00:00Z","draft":true}`,
		},
		{
			map[any]any{"Permalink": "/permalink.html", "layout": "post"},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
			`{"date":"2015-10-01T00:00:00Z","url":"/permalink.html"}`,
		},
		{
			map[any]any{"permalink": "/permalink.html"},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
			`{"date":"2015-10-01T00:00:00Z","url":"/permalink.html"}`,
		},
		{
			map[any]any{"category": nil, "permalink": 123},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
			`{"date":"2015-10-01T00:00:00Z"}`,
		},
		{
			map[any]any{"Excerpt_Separator": "sep"},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
			`{"date":"2015-10-01T00:00:00Z","excerpt_separator":"sep"}`,
		},
		{
			map[any]any{"category": "book", "layout": "post", "Others": "Goods", "Date": "2015-10-01 12:13:11"},
			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
			`{"Others":"Goods","categories":["book"],"date":"2015-10-01T12:13:11Z"}`,
		},
	}

	for _, data := range testDataList {
		result, err := convertJekyllMetaData(data.metadata, data.postName, data.postDate, data.draft)
		c.Assert(err, qt.IsNil)
		jsonResult, err := json.Marshal(result)
		c.Assert(err, qt.IsNil)
		c.Assert(string(jsonResult), qt.Equals, data.expect)
	}
}

func TestConvertJekyllContent(t *testing.T) {
	c := qt.New(t)
	testDataList := []struct {
		metadata any
		content  string
		expect   string
	}{
		{
			map[any]any{},
			"Test content\r\n<!-- more -->\npart2 content", "Test content\n<!--more-->\npart2 content",
		},
		{
			map[any]any{},
			"Test content\n<!-- More -->\npart2 content", "Test content\n<!--more-->\npart2 content",
		},
		{
			map[any]any{"excerpt_separator": "<!--sep-->"},
			"Test content\n<!--sep-->\npart2 content",
			"---\nexcerpt_separator: <!--sep-->\n---\nTest content\n<!--more-->\npart2 content",
		},
		{map[any]any{}, "{% raw %}text{% endraw %}", "text"},
		{map[any]any{}, "{%raw%} text2 {%endraw %}", "text2"},
		{
			map[any]any{},
			"{% highlight go %}\nvar s int\n{% endhighlight %}",
			"{{< highlight go >}}\nvar s int\n{{< / highlight >}}",
		},
		{
			map[any]any{},
			"{% highlight go linenos hl_lines=\"1 2\" %}\nvar s string\nvar i int\n{% endhighlight %}",
			"{{< highlight go \"linenos=table,hl_lines=1 2\" >}}\nvar s string\nvar i int\n{{< / highlight >}}",
		},

		// Octopress image tag
		{
			map[any]any{},
			"{% img http://placekitten.com/890/280 %}",
			"{{< figure src=\"http://placekitten.com/890/280\" >}}",
		},
		{
			map[any]any{},
			"{% img left http://placekitten.com/320/250 Place Kitten #2 %}",
			"{{< figure class=\"left\" src=\"http://placekitten.com/320/250\" title=\"Place Kitten #2\" >}}",
		},
		{
			map[any]any{},
			"{% img right http://placekitten.com/300/500 150 250 'Place Kitten #3' %}",
			"{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #3\" >}}",
		},
		{
			map[any]any{},
			"{% img right http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
			"{{< 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\" >}}",
		},
		{
			map[any]any{},
			"{% img http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
			"{{< figure src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}",
		},
		{
			map[any]any{},
			"{% img right /placekitten/300/500 'Place Kitten #4' 'An image of a very cute kitten' %}",
			"{{< figure class=\"right\" src=\"/placekitten/300/500\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}",
		},
		{
			map[any]any{"category": "book", "layout": "post", "Date": "2015-10-01 12:13:11"},
			"somecontent",
			"---\nDate: \"2015-10-01 12:13:11\"\ncategory: book\nlayout: post\n---\nsomecontent",
		},
	}
	for _, data := range testDataList {
		result, err := convertJekyllContent(data.metadata, data.content)
		c.Assert(result, qt.Equals, data.expect)
		c.Assert(err, qt.IsNil)
	}
}