summaryrefslogtreecommitdiffstats
path: root/hugolib/permalinks_test.go
blob: ea3abdf41aa9e1685abea8e5b40fc468a2bbcc4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package hugolib

import (
	"strings"
	"testing"
)

// testdataPermalinks is used by a couple of tests; the expandsTo content is
// subject to the data in SIMPLE_PAGE_JSON.
var testdataPermalinks = []struct {
	spec      string
	valid     bool
	expandsTo string
}{
	{"/:year/:month/:title/", true, "/2012/04/spf13-vim-3.0-release-and-new-website/"},
	{"/:title", true, "/spf13-vim-3.0-release-and-new-website"},
	{":title", true, "spf13-vim-3.0-release-and-new-website"},
	{"/blog/:year/:yearday/:title", true, "/blog/2012/97/spf13-vim-3.0-release-and-new-website"},
	{"/blog/:fred", false, ""},
	{"/:year//:title", false, ""},
	{
		"/:section/:year/:month/:day/:weekdayname/:yearday/:title",
		true,
		"/blue/2012/04/06/Friday/97/spf13-vim-3.0-release-and-new-website",
	},
	{
		"/:weekday/:weekdayname/:month/:monthname",
		true,
		"/5/Friday/04/April",
	},
	{
		"/:slug/:title",
		true,
		"/spf13-vim-3-0-release-and-new-website/spf13-vim-3.0-release-and-new-website",
	},
}

func TestPermalinkValidation(t *testing.T) {
	for _, item := range testdataPermalinks {
		pp := PathPattern(item.spec)
		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) {
	page, err := NewPageFrom(strings.NewReader(SIMPLE_PAGE_JSON), "blue/test-page.md")
	if err != nil {
		t.Fatalf("failed before we began, could not parse SIMPLE_PAGE_JSON: %s", err)
	}
	for _, item := range testdataPermalinks {
		if !item.valid {
			continue
		}
		pp := PathPattern(item.spec)
		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)
		}
	}
}