2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testdataPermalinks is used by a couple of tests; the expandsTo content is
|
2018-09-21 15:03:17 -04:00
|
|
|
// subject to the data in simplePageJSON.
|
2013-11-18 04:35:56 -05:00
|
|
|
var testdataPermalinks = []struct {
|
|
|
|
spec string
|
|
|
|
valid bool
|
|
|
|
expandsTo string
|
|
|
|
}{
|
2018-09-21 15:03:17 -04:00
|
|
|
{":title", true, "spf13-vim-3.0-release-and-new-website"},
|
2014-10-29 00:37:59 -04:00
|
|
|
{"/:year-:month-:title", true, "/2012-04-spf13-vim-3.0-release-and-new-website"},
|
2018-09-21 15:03:17 -04:00
|
|
|
|
|
|
|
{"/:year/:yearday/:month/:monthname/:day/:weekday/:weekdayname/", true, "/2012/97/04/April/06/5/Friday/"}, // Dates
|
|
|
|
{"/:section/", true, "/blue/"}, // Section
|
|
|
|
{"/:title/", true, "/spf13-vim-3.0-release-and-new-website/"}, // Title
|
|
|
|
{"/:slug/", true, "/spf13-vim-3-0-release-and-new-website/"}, // Slug
|
|
|
|
{"/:filename/", true, "/test-page/"}, // Filename
|
|
|
|
// TODO(moorereason): need test scaffolding for this.
|
|
|
|
//{"/:sections/", false, "/blue/"}, // Sections
|
|
|
|
|
|
|
|
// Failures
|
|
|
|
{"/blog/:fred", false, ""},
|
|
|
|
{"/:year//:title", false, ""},
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermalinkValidation(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
2013-11-18 04:35:56 -05:00
|
|
|
for _, item := range testdataPermalinks {
|
2016-03-24 22:12:03 -04:00
|
|
|
pp := pathPattern(item.spec)
|
2013-11-18 04:35:56 -05:00
|
|
|
have := pp.validate()
|
|
|
|
if have == item.valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var howBad string
|
|
|
|
if have {
|
|
|
|
howBad = "validates but should not have"
|
|
|
|
} else {
|
|
|
|
howBad = "should have validated but did not"
|
|
|
|
}
|
|
|
|
t.Errorf("permlink spec %q %s", item.spec, howBad)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermalinkExpansion(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
s := newTestSite(t)
|
|
|
|
page, err := s.NewPageFrom(strings.NewReader(simplePageJSON), "blue/test-page.md")
|
|
|
|
|
2013-11-18 04:35:56 -05:00
|
|
|
if err != nil {
|
2018-09-21 15:03:17 -04:00
|
|
|
t.Fatalf("failed before we began, could not parse simplePageJSON: %s", err)
|
2013-11-18 04:35:56 -05:00
|
|
|
}
|
|
|
|
for _, item := range testdataPermalinks {
|
|
|
|
if !item.valid {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-24 22:12:03 -04:00
|
|
|
pp := pathPattern(item.spec)
|
2013-11-18 04:35:56 -05:00
|
|
|
result, err := pp.Expand(page)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to expand page: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result != item.expandsTo {
|
|
|
|
t.Errorf("expansion mismatch!\n\tExpected: %q\n\tReceived: %q", item.expandsTo, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|