github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/resources/page/page_matcher_test.go (about) 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 qt "github.com/frankban/quicktest" 21 ) 22 23 func TestPageMatcher(t *testing.T) { 24 c := qt.New(t) 25 26 p1, p2, p3 := &testPage{path: "/p1", kind: "section", lang: "en"}, &testPage{path: "p2", kind: "page", lang: "no"}, &testPage{path: "p3", kind: "page", lang: "en"} 27 28 c.Run("Matches", func(c *qt.C) { 29 m := PageMatcher{Kind: "section"} 30 31 c.Assert(m.Matches(p1), qt.Equals, true) 32 c.Assert(m.Matches(p2), qt.Equals, false) 33 34 m = PageMatcher{Kind: "page"} 35 c.Assert(m.Matches(p1), qt.Equals, false) 36 c.Assert(m.Matches(p2), qt.Equals, true) 37 c.Assert(m.Matches(p3), qt.Equals, true) 38 39 m = PageMatcher{Kind: "page", Path: "/p2"} 40 c.Assert(m.Matches(p1), qt.Equals, false) 41 c.Assert(m.Matches(p2), qt.Equals, true) 42 c.Assert(m.Matches(p3), qt.Equals, false) 43 44 m = PageMatcher{Path: "/p*"} 45 c.Assert(m.Matches(p1), qt.Equals, true) 46 c.Assert(m.Matches(p2), qt.Equals, true) 47 c.Assert(m.Matches(p3), qt.Equals, true) 48 49 m = PageMatcher{Lang: "en"} 50 c.Assert(m.Matches(p1), qt.Equals, true) 51 c.Assert(m.Matches(p2), qt.Equals, false) 52 c.Assert(m.Matches(p3), qt.Equals, true) 53 }) 54 55 c.Run("Decode", func(c *qt.C) { 56 var v PageMatcher 57 c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "foo"}, &v), qt.Not((qt.IsNil))) 58 c.Assert(DecodePageMatcher(map[string]interface{}{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil) 59 c.Assert(v, qt.Equals, PageMatcher{Kind: "home", Path: "/a/b/**"}) 60 }) 61 }