@@ -22,39 +22,52 @@ import (
22
22
"time"
23
23
24
24
qt "github.com/frankban/quicktest"
25
+ "github.com/gohugoio/hugo/source"
25
26
)
26
27
27
28
// testdataPermalinks is used by a couple of tests; the expandsTo content is
28
29
// subject to the data in simplePageJSON.
29
30
var testdataPermalinks = []struct {
30
31
spec string
31
32
valid bool
33
+ withPage func (p * testPage )
32
34
expandsTo string
33
35
}{
34
- {":title" , true , "spf13-vim-3.0-release-and-new-website" },
35
- {"/:year-:month-:title" , true , "/2012-04-spf13-vim-3.0-release-and-new-website" },
36
- {"/:year/:yearday/:month/:monthname/:day/:weekday/:weekdayname/" , true , "/2012/97/04/April/06/5/Friday/" }, // Dates
37
- {"/:section/" , true , "/blue/" }, // Section
38
- {"/:title/" , true , "/spf13-vim-3.0-release-and-new-website/" }, // Title
39
- {"/:slug/" , true , "/the-slug/" }, // Slug
40
- {"/:slugorfilename/" , true , "/the-slug/" }, // Slug or filename
41
- {"/:filename/" , true , "/test-page/" }, // Filename
42
- {"/:06-:1-:2-:Monday" , true , "/12-4-6-Friday" }, // Dates with Go formatting
43
- {"/:2006_01_02_15_04_05.000" , true , "/2012_04_06_03_01_59.000" }, // Complicated custom date format
44
- {"/:sections/" , true , "/a/b/c/" }, // Sections
45
- {"/:sections[last]/" , true , "/c/" }, // Sections
46
- {"/:sections[0]/:sections[last]/" , true , "/a/c/" }, // Sections
47
- {"/\\ :filename" , true , "/:filename" }, // Escape sequence
48
- {"/special\\ ::slug/" , true , "/special:the-slug/" }, // Escape sequence
49
- {"/:contentbasename/" , true , "/index/" }, // Content base name
50
- {"/:contentbasenameorslug/" , true , "/index/" }, // Content base name or slug
51
-
36
+ {":title" , true , nil , "spf13-vim-3.0-release-and-new-website" },
37
+ {"/:year-:month-:title" , true , nil , "/2012-04-spf13-vim-3.0-release-and-new-website" },
38
+ {"/:year/:yearday/:month/:monthname/:day/:weekday/:weekdayname/" , true , nil , "/2012/97/04/April/06/5/Friday/" }, // Dates
39
+ {"/:section/" , true , nil , "/blue/" }, // Section
40
+ {"/:title/" , true , nil , "/spf13-vim-3.0-release-and-new-website/" }, // Title
41
+ {"/:slug/" , true , nil , "/the-slug/" }, // Slug
42
+ {"/:slugorfilename/" , true , nil , "/the-slug/" }, // Slug or filename
43
+ {"/:filename/" , true , nil , "/test-page/" }, // Filename
44
+ {"/:06-:1-:2-:Monday" , true , nil , "/12-4-6-Friday" }, // Dates with Go formatting
45
+ {"/:2006_01_02_15_04_05.000" , true , nil , "/2012_04_06_03_01_59.000" }, // Complicated custom date format
46
+ {"/:sections/" , true , nil , "/a/b/c/" }, // Sections
47
+ {"/:sections[last]/" , true , nil , "/c/" }, // Sections
48
+ {"/:sections[0]/:sections[last]/" , true , nil , "/a/c/" }, // Sections
49
+ {"/\\ :filename" , true , nil , "/:filename" }, // Escape sequence
50
+ {"/special\\ ::slug/" , true , nil , "/special:the-slug/" },
51
+ // contentbasename. // Escape sequence
52
+ {"/:contentbasename/" , true , nil , "/test-page/" },
53
+ // slug, contentbasename. // Content base name
54
+ {"/:slugorcontentbasename/" , true , func (p * testPage ) {
55
+ p .slug = ""
56
+ }, "/test-page/" },
57
+ {"/:slugorcontentbasename/" , true , func (p * testPage ) {
58
+ p .slug = "myslug"
59
+ }, "/myslug/" },
60
+ {"/:slugorcontentbasename/" , true , func (p * testPage ) {
61
+ p .slug = ""
62
+ p .title = "mytitle"
63
+ p .file = source .NewContentFileInfoFrom ("/" , "_index.md" )
64
+ }, "/test-page/" },
52
65
// Failures
53
- {"/blog/:fred" , false , "" },
54
- {"/:year//:title" , false , "" },
55
- {"/:TITLE" , false , "" }, // case is not normalized
56
- {"/:2017" , false , "" }, // invalid date format
57
- {"/:2006-01-02" , false , "" }, // valid date format but invalid attribute name
66
+ {"/blog/:fred" , false , nil , "" },
67
+ {"/:year//:title" , false , nil , "" },
68
+ {"/:TITLE" , false , nil , "" }, // case is not normalized
69
+ {"/:2017" , false , nil , "" }, // invalid date format
70
+ {"/:2006-01-02" , false , nil , "" }, // valid date format but invalid attribute name
58
71
}
59
72
60
73
func urlize (uri string ) string {
@@ -67,21 +80,30 @@ func TestPermalinkExpansion(t *testing.T) {
67
80
68
81
c := qt .New (t )
69
82
70
- page := newTestPageWithFile ("/test-page/index.md" )
71
- page .title = "Spf13 Vim 3.0 Release and new website"
72
- d , _ := time .Parse ("2006-01-02 15:04:05" , "2012-04-06 03:01:59" )
73
- page .date = d
74
- page .section = "blue"
75
- page .slug = "The Slug"
76
- page .kind = "page"
83
+ newPage := func () * testPage {
84
+ page := newTestPageWithFile ("/test-page/index.md" )
85
+ page .title = "Spf13 Vim 3.0 Release and new website"
86
+ d , _ := time .Parse ("2006-01-02 15:04:05" , "2012-04-06 03:01:59" )
87
+ page .date = d
88
+ page .section = "blue"
89
+ page .slug = "The Slug"
90
+ page .kind = "page"
91
+ // page.pathInfo
92
+ return page
93
+ }
77
94
78
- for _ , item := range testdataPermalinks {
95
+ for i , item := range testdataPermalinks {
79
96
if ! item .valid {
80
97
continue
81
98
}
82
99
100
+ page := newPage ()
101
+ if item .withPage != nil {
102
+ item .withPage (page )
103
+ }
104
+
83
105
specNameCleaner := regexp .MustCompile (`[\:\/\[\]]` )
84
- name := specNameCleaner .ReplaceAllString (item .spec , "" )
106
+ name := fmt . Sprintf ( "[%d] %s" , i , specNameCleaner .ReplaceAllString (item .spec , "_" ) )
85
107
86
108
c .Run (name , func (c * qt.C ) {
87
109
patterns := map [string ]map [string ]string {
0 commit comments