@@ -22,39 +22,52 @@ import (
2222 "time"
2323
2424 qt "github.com/frankban/quicktest"
25+ "github.com/gohugoio/hugo/source"
2526)
2627
2728// testdataPermalinks is used by a couple of tests; the expandsTo content is
2829// subject to the data in simplePageJSON.
2930var testdataPermalinks = []struct {
3031 spec string
3132 valid bool
33+ withPage func (p * testPage )
3234 expandsTo string
3335}{
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/" },
5265 // 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
5871}
5972
6073func urlize (uri string ) string {
@@ -67,21 +80,30 @@ func TestPermalinkExpansion(t *testing.T) {
6780
6881 c := qt .New (t )
6982
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+ }
7794
78- for _ , item := range testdataPermalinks {
95+ for i , item := range testdataPermalinks {
7996 if ! item .valid {
8097 continue
8198 }
8299
100+ page := newPage ()
101+ if item .withPage != nil {
102+ item .withPage (page )
103+ }
104+
83105 specNameCleaner := regexp .MustCompile (`[\:\/\[\]]` )
84- name := specNameCleaner .ReplaceAllString (item .spec , "" )
106+ name := fmt . Sprintf ( "[%d] %s" , i , specNameCleaner .ReplaceAllString (item .spec , "_" ) )
85107
86108 c .Run (name , func (c * qt.C ) {
87109 patterns := map [string ]map [string ]string {
0 commit comments