summaryrefslogtreecommitdiffstats
path: root/hugolib/segments/segments_test.go
blob: 1a2dfb97bad3a26b3b15459315e2452af3d65025 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package segments

import (
	"testing"

	qt "github.com/frankban/quicktest"
)

func TestCompileSegments(t *testing.T) {
	c := qt.New(t)

	c.Run("excludes", func(c *qt.C) {
		fields := []SegmentMatcherFields{
			{
				Lang:   "n*",
				Output: "rss",
			},
		}

		match, err := compileSegments(fields)
		c.Assert(err, qt.IsNil)

		check := func() {
			c.Assert(match, qt.IsNotNil)
			c.Assert(match(SegmentMatcherFields{Lang: "no"}), qt.Equals, false)
			c.Assert(match(SegmentMatcherFields{Lang: "no", Kind: "page"}), qt.Equals, false)
			c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "rss"}), qt.Equals, true)
			c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "html"}), qt.Equals, false)
			c.Assert(match(SegmentMatcherFields{Kind: "page"}), qt.Equals, false)
			c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "rss", Kind: "page"}), qt.Equals, true)
		}

		check()

		fields = []SegmentMatcherFields{
			{
				Path: "/blog/**",
			},
			{
				Lang:   "n*",
				Output: "rss",
			},
		}

		match, err = compileSegments(fields)
		c.Assert(err, qt.IsNil)
		check()
		c.Assert(match(SegmentMatcherFields{Path: "/blog/foo"}), qt.Equals, true)
	})

	c.Run("includes", func(c *qt.C) {
		fields := []SegmentMatcherFields{
			{
				Path: "/docs/**",
			},
			{
				Lang:   "no",
				Output: "rss",
			},
		}

		match, err := compileSegments(fields)
		c.Assert(err, qt.IsNil)
		c.Assert(match, qt.IsNotNil)
		c.Assert(match(SegmentMatcherFields{Lang: "no"}), qt.Equals, false)
		c.Assert(match(SegmentMatcherFields{Kind: "page"}), qt.Equals, false)
		c.Assert(match(SegmentMatcherFields{Kind: "page", Path: "/blog/foo"}), qt.Equals, false)
		c.Assert(match(SegmentMatcherFields{Lang: "en"}), qt.Equals, false)
		c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "rss"}), qt.Equals, true)
		c.Assert(match(SegmentMatcherFields{Lang: "no", Output: "html"}), qt.Equals, false)
		c.Assert(match(SegmentMatcherFields{Kind: "page", Path: "/docs/foo"}), qt.Equals, true)
	})

	c.Run("includes variant1", func(c *qt.C) {
		c.Skip()

		fields := []SegmentMatcherFields{
			{
				Kind: "home",
			},
			{
				Path: "{/docs,/docs/**}",
			},
		}

		match, err := compileSegments(fields)
		c.Assert(err, qt.IsNil)
		c.Assert(match, qt.IsNotNil)
		c.Assert(match(SegmentMatcherFields{Path: "/blog/foo"}), qt.Equals, false)
		c.Assert(match(SegmentMatcherFields{Kind: "page", Path: "/docs/foo"}), qt.Equals, true)
		c.Assert(match(SegmentMatcherFields{Kind: "home", Path: "/"}), qt.Equals, true)
	})
}

func BenchmarkSegmentsMatch(b *testing.B) {
	fields := []SegmentMatcherFields{
		{
			Path: "/docs/**",
		},
		{
			Lang:   "no",
			Output: "rss",
		},
	}

	match, err := compileSegments(fields)
	if err != nil {
		b.Fatal(err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		match(SegmentMatcherFields{Lang: "no", Output: "rss"})
	}
}