page_matcher_test.go (3079B)
1 // Copyright 2020 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 page 15 16 import ( 17 "path/filepath" 18 "testing" 19 20 "github.com/gohugoio/hugo/common/hugo" 21 22 qt "github.com/frankban/quicktest" 23 ) 24 25 func TestPageMatcher(t *testing.T) { 26 c := qt.New(t) 27 developmentTestSite := testSite{h: hugo.NewInfo("development", nil)} 28 productionTestSite := testSite{h: hugo.NewInfo("production", nil)} 29 30 p1, p2, p3 := 31 &testPage{path: "/p1", kind: "section", lang: "en", site: developmentTestSite}, 32 &testPage{path: "p2", kind: "page", lang: "no", site: productionTestSite}, 33 &testPage{path: "p3", kind: "page", lang: "en"} 34 35 c.Run("Matches", func(c *qt.C) { 36 m := PageMatcher{Kind: "section"} 37 38 c.Assert(m.Matches(p1), qt.Equals, true) 39 c.Assert(m.Matches(p2), qt.Equals, false) 40 41 m = PageMatcher{Kind: "page"} 42 c.Assert(m.Matches(p1), qt.Equals, false) 43 c.Assert(m.Matches(p2), qt.Equals, true) 44 c.Assert(m.Matches(p3), qt.Equals, true) 45 46 m = PageMatcher{Kind: "page", Path: "/p2"} 47 c.Assert(m.Matches(p1), qt.Equals, false) 48 c.Assert(m.Matches(p2), qt.Equals, true) 49 c.Assert(m.Matches(p3), qt.Equals, false) 50 51 m = PageMatcher{Path: "/p*"} 52 c.Assert(m.Matches(p1), qt.Equals, true) 53 c.Assert(m.Matches(p2), qt.Equals, true) 54 c.Assert(m.Matches(p3), qt.Equals, true) 55 56 m = PageMatcher{Lang: "en"} 57 c.Assert(m.Matches(p1), qt.Equals, true) 58 c.Assert(m.Matches(p2), qt.Equals, false) 59 c.Assert(m.Matches(p3), qt.Equals, true) 60 61 m = PageMatcher{Environment: "development"} 62 c.Assert(m.Matches(p1), qt.Equals, true) 63 c.Assert(m.Matches(p2), qt.Equals, false) 64 c.Assert(m.Matches(p3), qt.Equals, false) 65 66 m = PageMatcher{Environment: "production"} 67 c.Assert(m.Matches(p1), qt.Equals, false) 68 c.Assert(m.Matches(p2), qt.Equals, true) 69 c.Assert(m.Matches(p3), qt.Equals, false) 70 }) 71 72 c.Run("Decode", func(c *qt.C) { 73 var v PageMatcher 74 c.Assert(DecodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil)) 75 c.Assert(DecodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil)) 76 c.Assert(DecodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil) 77 c.Assert(DecodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil) 78 c.Assert(DecodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil) 79 c.Assert(DecodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil) 80 c.Assert(DecodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil) 81 c.Assert(v, qt.Equals, PageMatcher{Kind: "home", Path: "/a/b/**"}) 82 }) 83 }