| 1 |
package segments |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
qt "github.com/frankban/quicktest" |
| 7 |
"github.com/gohugoio/hugo/common/loggers" |
| 8 |
"github.com/gohugoio/hugo/common/predicate" |
| 9 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
testDims = sitesmatrix.NewTestingDimensions([]string{"no", "en"}, []string{"v1.0.0", "v1.2.0", "v2.0.0"}, []string{"guest", "member"}) |
| 14 |
no = sitesmatrix.Vector{0, 0, 0} |
| 15 |
en = sitesmatrix.Vector{1, 0, 0} |
| 16 |
sitesNoStar = sitesmatrix.Sites{ |
| 17 |
Matrix: sitesmatrix.StringSlices{ |
| 18 |
Languages: []string{"n*"}, |
| 19 |
}, |
| 20 |
} |
| 21 |
sitesNo = sitesmatrix.Sites{ |
| 22 |
Matrix: sitesmatrix.StringSlices{ |
| 23 |
Languages: []string{"no"}, |
| 24 |
}, |
| 25 |
} |
| 26 |
|
| 27 |
sitesNotNo = sitesmatrix.Sites{ |
| 28 |
Matrix: sitesmatrix.StringSlices{ |
| 29 |
Languages: []string{"! no"}, |
| 30 |
}, |
| 31 |
} |
| 32 |
) |
| 33 |
|
| 34 |
var compileSegments = func(c *qt.C, includes ...SegmentMatcherFields) predicate.P[SegmentQuery] { |
| 35 |
segments := Segments{ |
| 36 |
builder: &segmentsBuilder{ |
| 37 |
logger: loggers.NewDefault(), |
| 38 |
configuredDimensions: testDims, |
| 39 |
segmentsToRender: []string{"docs"}, |
| 40 |
segmentCfg: map[string]SegmentConfig{ |
| 41 |
"docs": { |
| 42 |
Includes: includes, |
| 43 |
}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
} |
| 47 |
c.Assert(segments.compile(), qt.IsNil) |
| 48 |
return segments.SegmentFilter.ShouldExcludeFine |
| 49 |
} |
| 50 |
|
| 51 |
func TestCompileSegments(t *testing.T) { |
| 52 |
c := qt.New(t) |
| 53 |
|
| 54 |
c.Run("variants1", func(c *qt.C) { |
| 55 |
fields := []SegmentMatcherFields{ |
| 56 |
{ |
| 57 |
Sites: sitesNoStar, |
| 58 |
Output: []string{"rss"}, |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
shouldExclude := compileSegments(c, fields...) |
| 63 |
|
| 64 |
check := func() { |
| 65 |
c.Assert(shouldExclude, qt.IsNotNil) |
| 66 |
c.Assert(shouldExclude(SegmentQuery{Site: no}), qt.Equals, true) |
| 67 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Kind: "page"}), qt.Equals, true) |
| 68 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "rss"}), qt.Equals, false) |
| 69 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "html"}), qt.Equals, true) |
| 70 |
c.Assert(shouldExclude(SegmentQuery{Kind: "page"}), qt.Equals, true) |
| 71 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "rss", Kind: "page"}), qt.Equals, false) |
| 72 |
} |
| 73 |
|
| 74 |
check() |
| 75 |
|
| 76 |
fields = []SegmentMatcherFields{ |
| 77 |
{ |
| 78 |
Path: []string{"/blog/**"}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
Sites: sitesNoStar, |
| 82 |
Output: []string{"rss"}, |
| 83 |
}, |
| 84 |
} |
| 85 |
|
| 86 |
shouldExclude = compileSegments(c, fields...) |
| 87 |
check() |
| 88 |
c.Assert(shouldExclude(SegmentQuery{Path: "/blog/foo"}), qt.Equals, false) |
| 89 |
}) |
| 90 |
|
| 91 |
c.Run("variants2", func(c *qt.C) { |
| 92 |
fields := []SegmentMatcherFields{ |
| 93 |
{ |
| 94 |
Path: []string{"/docs/**"}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
Sites: sitesNo, |
| 98 |
Output: []string{"rss", "json"}, |
| 99 |
}, |
| 100 |
} |
| 101 |
|
| 102 |
shouldExclude := compileSegments(c, fields...) |
| 103 |
c.Assert(shouldExclude, qt.IsNotNil) |
| 104 |
c.Assert(shouldExclude(SegmentQuery{Site: no}), qt.Equals, true) |
| 105 |
c.Assert(shouldExclude(SegmentQuery{Kind: "page"}), qt.Equals, true) |
| 106 |
c.Assert(shouldExclude(SegmentQuery{Kind: "page", Path: "/blog/foo"}), qt.Equals, true) |
| 107 |
c.Assert(shouldExclude(SegmentQuery{Site: en}), qt.Equals, true) |
| 108 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "rss"}), qt.Equals, false) |
| 109 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "json"}), qt.Equals, false) |
| 110 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "html"}), qt.Equals, true) |
| 111 |
c.Assert(shouldExclude(SegmentQuery{Kind: "page", Path: "/docs/foo"}), qt.Equals, false) |
| 112 |
}) |
| 113 |
} |
| 114 |
|
| 115 |
func TestCompileSegmentsNegate(t *testing.T) { |
| 116 |
c := qt.New(t) |
| 117 |
|
| 118 |
fields := []SegmentMatcherFields{ |
| 119 |
{ |
| 120 |
Sites: sitesNotNo, |
| 121 |
Output: []string{"! r**", "rem", "html"}, |
| 122 |
}, |
| 123 |
} |
| 124 |
|
| 125 |
shouldExclude := compileSegments(c, fields...) |
| 126 |
c.Assert(shouldExclude, qt.IsNotNil) |
| 127 |
c.Assert(shouldExclude(SegmentQuery{Site: no, Output: "html"}), qt.Equals, true) |
| 128 |
c.Assert(shouldExclude(SegmentQuery{Site: en, Output: "html"}), qt.Equals, false) |
| 129 |
c.Assert(shouldExclude(SegmentQuery{Site: en, Output: "rss"}), qt.Equals, true) |
| 130 |
c.Assert(shouldExclude(SegmentQuery{Site: en, Output: "rem"}), qt.Equals, true) |
| 131 |
} |
| 132 |
|
| 133 |
func BenchmarkSegmentsMatch(b *testing.B) { |
| 134 |
c := qt.New(b) |
| 135 |
fields := []SegmentMatcherFields{ |
| 136 |
{ |
| 137 |
Path: []string{"/docs/**"}, |
| 138 |
}, |
| 139 |
{ |
| 140 |
Sites: sitesNo, |
| 141 |
Output: []string{"rss"}, |
| 142 |
}, |
| 143 |
} |
| 144 |
|
| 145 |
match := compileSegments(c, fields...) |
| 146 |
|
| 147 |
for b.Loop() { |
| 148 |
match(SegmentQuery{Site: no, Output: "rss"}) |
| 149 |
} |
| 150 |
} |