github.com/pietrocarrara/hugo@v0.47.1/hugolib/permalinks_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 hugolib 15 16 import ( 17 "strings" 18 "testing" 19 ) 20 21 // testdataPermalinks is used by a couple of tests; the expandsTo content is 22 // subject to the data in SIMPLE_PAGE_JSON. 23 var testdataPermalinks = []struct { 24 spec string 25 valid bool 26 expandsTo string 27 }{ 28 //{"/:year/:month/:title/", true, "/2012/04/spf13-vim-3.0-release-and-new-website/"}, 29 //{"/:title", true, "/spf13-vim-3.0-release-and-new-website"}, 30 //{":title", true, "spf13-vim-3.0-release-and-new-website"}, 31 //{"/blog/:year/:yearday/:title", true, "/blog/2012/97/spf13-vim-3.0-release-and-new-website"}, 32 {"/:year-:month-:title", true, "/2012-04-spf13-vim-3.0-release-and-new-website"}, 33 {"/blog/:year-:month-:title", true, "/blog/2012-04-spf13-vim-3.0-release-and-new-website"}, 34 {"/blog-:year-:month-:title", true, "/blog-2012-04-spf13-vim-3.0-release-and-new-website"}, 35 //{"/blog/:fred", false, ""}, 36 //{"/:year//:title", false, ""}, 37 //{ 38 //"/:section/:year/:month/:day/:weekdayname/:yearday/:title", 39 //true, 40 //"/blue/2012/04/06/Friday/97/spf13-vim-3.0-release-and-new-website", 41 //}, 42 //{ 43 //"/:weekday/:weekdayname/:month/:monthname", 44 //true, 45 //"/5/Friday/04/April", 46 //}, 47 //{ 48 //"/:slug/:title", 49 //true, 50 //"/spf13-vim-3-0-release-and-new-website/spf13-vim-3.0-release-and-new-website", 51 //}, 52 } 53 54 func TestPermalinkValidation(t *testing.T) { 55 t.Parallel() 56 for _, item := range testdataPermalinks { 57 pp := pathPattern(item.spec) 58 have := pp.validate() 59 if have == item.valid { 60 continue 61 } 62 var howBad string 63 if have { 64 howBad = "validates but should not have" 65 } else { 66 howBad = "should have validated but did not" 67 } 68 t.Errorf("permlink spec %q %s", item.spec, howBad) 69 } 70 } 71 72 func TestPermalinkExpansion(t *testing.T) { 73 t.Parallel() 74 s := newTestSite(t) 75 page, err := s.NewPageFrom(strings.NewReader(simplePageJSON), "blue/test-page.md") 76 77 if err != nil { 78 t.Fatalf("failed before we began, could not parse SIMPLE_PAGE_JSON: %s", err) 79 } 80 for _, item := range testdataPermalinks { 81 if !item.valid { 82 continue 83 } 84 pp := pathPattern(item.spec) 85 result, err := pp.Expand(page) 86 if err != nil { 87 t.Errorf("failed to expand page: %s", err) 88 continue 89 } 90 if result != item.expandsTo { 91 t.Errorf("expansion mismatch!\n\tExpected: %q\n\tReceived: %q", item.expandsTo, result) 92 } 93 } 94 }