github.com/gohugoio/hugo@v0.88.1/commands/import_jekyll_test.go (about)

     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 interface{}
    51  		postName string
    52  		postDate time.Time
    53  		draft    bool
    54  		expect   string
    55  	}{
    56  		{
    57  			map[interface{}]interface{}{},
    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[interface{}]interface{}{},
    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[interface{}]interface{}{"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[interface{}]interface{}{"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[interface{}]interface{}{"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[interface{}]interface{}{"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[interface{}]interface{}{"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 interface{}
   106  		content  string
   107  		expect   string
   108  	}{
   109  		{
   110  			map[interface{}]interface{}{},
   111  			"Test content\r\n<!-- more -->\npart2 content", "Test content\n<!--more-->\npart2 content",
   112  		},
   113  		{
   114  			map[interface{}]interface{}{},
   115  			"Test content\n<!-- More -->\npart2 content", "Test content\n<!--more-->\npart2 content",
   116  		},
   117  		{
   118  			map[interface{}]interface{}{"excerpt_separator": "<!--sep-->"},
   119  			"Test content\n<!--sep-->\npart2 content",
   120  			"---\nexcerpt_separator: <!--sep-->\n---\nTest content\n<!--more-->\npart2 content",
   121  		},
   122  		{map[interface{}]interface{}{}, "{% raw %}text{% endraw %}", "text"},
   123  		{map[interface{}]interface{}{}, "{%raw%} text2 {%endraw %}", "text2"},
   124  		{
   125  			map[interface{}]interface{}{},
   126  			"{% highlight go %}\nvar s int\n{% endhighlight %}",
   127  			"{{< highlight go >}}\nvar s int\n{{< / highlight >}}",
   128  		},
   129  		{
   130  			map[interface{}]interface{}{},
   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[interface{}]interface{}{},
   138  			"{% img http://placekitten.com/890/280 %}",
   139  			"{{< figure src=\"http://placekitten.com/890/280\" >}}",
   140  		},
   141  		{
   142  			map[interface{}]interface{}{},
   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[interface{}]interface{}{},
   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[interface{}]interface{}{},
   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[interface{}]interface{}{},
   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[interface{}]interface{}{},
   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[interface{}]interface{}{"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  }