gitee.com/mirrors/Hugo-Go@v0.47.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  	"github.com/stretchr/testify/assert"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestParseJekyllFilename(t *testing.T) {
    24  	filenameArray := []string{
    25  		"2015-01-02-test.md",
    26  		"2012-03-15-中文.markup",
    27  	}
    28  
    29  	expectResult := []struct {
    30  		postDate time.Time
    31  		postName string
    32  	}{
    33  		{time.Date(2015, time.January, 2, 0, 0, 0, 0, time.UTC), "test"},
    34  		{time.Date(2012, time.March, 15, 0, 0, 0, 0, time.UTC), "中文"},
    35  	}
    36  
    37  	for i, filename := range filenameArray {
    38  		postDate, postName, err := parseJekyllFilename(filename)
    39  		assert.Equal(t, err, nil)
    40  		assert.Equal(t, expectResult[i].postDate.Format("2006-01-02"), postDate.Format("2006-01-02"))
    41  		assert.Equal(t, expectResult[i].postName, postName)
    42  	}
    43  }
    44  
    45  func TestConvertJekyllMetadata(t *testing.T) {
    46  	testDataList := []struct {
    47  		metadata interface{}
    48  		postName string
    49  		postDate time.Time
    50  		draft    bool
    51  		expect   string
    52  	}{
    53  		{map[interface{}]interface{}{}, "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
    54  			`{"date":"2015-10-01T00:00:00Z"}`},
    55  		{map[interface{}]interface{}{}, "testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), true,
    56  			`{"date":"2015-10-01T00:00:00Z","draft":true}`},
    57  		{map[interface{}]interface{}{"Permalink": "/permalink.html", "layout": "post"},
    58  			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
    59  			`{"date":"2015-10-01T00:00:00Z","url":"/permalink.html"}`},
    60  		{map[interface{}]interface{}{"permalink": "/permalink.html"},
    61  			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
    62  			`{"date":"2015-10-01T00:00:00Z","url":"/permalink.html"}`},
    63  		{map[interface{}]interface{}{"category": nil, "permalink": 123},
    64  			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
    65  			`{"date":"2015-10-01T00:00:00Z"}`},
    66  		{map[interface{}]interface{}{"Excerpt_Separator": "sep"},
    67  			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
    68  			`{"date":"2015-10-01T00:00:00Z","excerpt_separator":"sep"}`},
    69  		{map[interface{}]interface{}{"category": "book", "layout": "post", "Others": "Goods", "Date": "2015-10-01 12:13:11"},
    70  			"testPost", time.Date(2015, 10, 1, 0, 0, 0, 0, time.UTC), false,
    71  			`{"Others":"Goods","categories":["book"],"date":"2015-10-01T12:13:11Z"}`},
    72  	}
    73  
    74  	for _, data := range testDataList {
    75  		result, err := convertJekyllMetaData(data.metadata, data.postName, data.postDate, data.draft)
    76  		assert.Equal(t, nil, err)
    77  		jsonResult, err := json.Marshal(result)
    78  		assert.Equal(t, nil, err)
    79  		assert.Equal(t, data.expect, string(jsonResult))
    80  	}
    81  }
    82  
    83  func TestConvertJekyllContent(t *testing.T) {
    84  	testDataList := []struct {
    85  		metadata interface{}
    86  		content  string
    87  		expect   string
    88  	}{
    89  		{map[interface{}]interface{}{},
    90  			`Test content\n<!-- more -->\npart2 content`, `Test content\n<!--more-->\npart2 content`},
    91  		{map[interface{}]interface{}{},
    92  			`Test content\n<!-- More -->\npart2 content`, `Test content\n<!--more-->\npart2 content`},
    93  		{map[interface{}]interface{}{"excerpt_separator": "<!--sep-->"},
    94  			`Test content\n<!--sep-->\npart2 content`, `Test content\n<!--more-->\npart2 content`},
    95  		{map[interface{}]interface{}{}, "{% raw %}text{% endraw %}", "text"},
    96  		{map[interface{}]interface{}{}, "{%raw%} text2 {%endraw %}", "text2"},
    97  		{map[interface{}]interface{}{},
    98  			"{% highlight go %}\nvar s int\n{% endhighlight %}",
    99  			"{{< highlight go >}}\nvar s int\n{{< / highlight >}}"},
   100  
   101  		// Octopress image tag
   102  		{map[interface{}]interface{}{},
   103  			"{% img http://placekitten.com/890/280 %}",
   104  			"{{< figure src=\"http://placekitten.com/890/280\" >}}"},
   105  		{map[interface{}]interface{}{},
   106  			"{% img left http://placekitten.com/320/250 Place Kitten #2 %}",
   107  			"{{< figure class=\"left\" src=\"http://placekitten.com/320/250\" title=\"Place Kitten #2\" >}}"},
   108  		{map[interface{}]interface{}{},
   109  			"{% img right http://placekitten.com/300/500 150 250 'Place Kitten #3' %}",
   110  			"{{< figure class=\"right\" src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #3\" >}}"},
   111  		{map[interface{}]interface{}{},
   112  			"{% img right http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
   113  			"{{< 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\" >}}"},
   114  		{map[interface{}]interface{}{},
   115  			"{% img http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}",
   116  			"{{< figure src=\"http://placekitten.com/300/500\" width=\"150\" height=\"250\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
   117  		{map[interface{}]interface{}{},
   118  			"{% img right /placekitten/300/500 'Place Kitten #4' 'An image of a very cute kitten' %}",
   119  			"{{< figure class=\"right\" src=\"/placekitten/300/500\" title=\"Place Kitten #4\" alt=\"An image of a very cute kitten\" >}}"},
   120  	}
   121  
   122  	for _, data := range testDataList {
   123  		result := convertJekyllContent(data.metadata, data.content)
   124  		assert.Equal(t, data.expect, result)
   125  	}
   126  }