github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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  	"github.com/gohugoio/hugo/common/hugo"
    21  	"github.com/gohugoio/hugo/common/maps"
    22  
    23  	qt "github.com/frankban/quicktest"
    24  )
    25  
    26  func TestPageMatcher(t *testing.T) {
    27  	c := qt.New(t)
    28  	developmentTestSite := testSite{h: hugo.NewInfo(testConfig{environment: "development"}, nil)}
    29  	productionTestSite := testSite{h: hugo.NewInfo(testConfig{environment: "production"}, nil)}
    30  
    31  	p1, p2, p3 :=
    32  		&testPage{path: "/p1", kind: "section", lang: "en", site: developmentTestSite},
    33  		&testPage{path: "p2", kind: "page", lang: "no", site: productionTestSite},
    34  		&testPage{path: "p3", kind: "page", lang: "en"}
    35  
    36  	c.Run("Matches", func(c *qt.C) {
    37  		m := PageMatcher{Kind: "section"}
    38  
    39  		c.Assert(m.Matches(p1), qt.Equals, true)
    40  		c.Assert(m.Matches(p2), qt.Equals, false)
    41  
    42  		m = PageMatcher{Kind: "page"}
    43  		c.Assert(m.Matches(p1), qt.Equals, false)
    44  		c.Assert(m.Matches(p2), qt.Equals, true)
    45  		c.Assert(m.Matches(p3), qt.Equals, true)
    46  
    47  		m = PageMatcher{Kind: "page", Path: "/p2"}
    48  		c.Assert(m.Matches(p1), qt.Equals, false)
    49  		c.Assert(m.Matches(p2), qt.Equals, true)
    50  		c.Assert(m.Matches(p3), qt.Equals, false)
    51  
    52  		m = PageMatcher{Path: "/p*"}
    53  		c.Assert(m.Matches(p1), qt.Equals, true)
    54  		c.Assert(m.Matches(p2), qt.Equals, true)
    55  		c.Assert(m.Matches(p3), qt.Equals, true)
    56  
    57  		m = PageMatcher{Lang: "en"}
    58  		c.Assert(m.Matches(p1), qt.Equals, true)
    59  		c.Assert(m.Matches(p2), qt.Equals, false)
    60  		c.Assert(m.Matches(p3), qt.Equals, true)
    61  
    62  		m = PageMatcher{Environment: "development"}
    63  		c.Assert(m.Matches(p1), qt.Equals, true)
    64  		c.Assert(m.Matches(p2), qt.Equals, false)
    65  		c.Assert(m.Matches(p3), qt.Equals, false)
    66  
    67  		m = PageMatcher{Environment: "production"}
    68  		c.Assert(m.Matches(p1), qt.Equals, false)
    69  		c.Assert(m.Matches(p2), qt.Equals, true)
    70  		c.Assert(m.Matches(p3), qt.Equals, false)
    71  	})
    72  
    73  	c.Run("Decode", func(c *qt.C) {
    74  		var v PageMatcher
    75  		c.Assert(decodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil))
    76  		c.Assert(decodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
    77  		c.Assert(decodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil)
    78  		c.Assert(decodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
    79  		c.Assert(decodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
    80  		c.Assert(decodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil)
    81  		c.Assert(decodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
    82  		c.Assert(v, qt.Equals, PageMatcher{Kind: "home", Path: "/a/b/**"})
    83  	})
    84  
    85  	c.Run("mapToPageMatcherParamsConfig", func(c *qt.C) {
    86  		fn := func(m map[string]any) PageMatcherParamsConfig {
    87  			v, err := mapToPageMatcherParamsConfig(m)
    88  			c.Assert(err, qt.IsNil)
    89  			return v
    90  		}
    91  		// Legacy.
    92  		c.Assert(fn(map[string]any{"_target": map[string]any{"kind": "page"}, "foo": "bar"}), qt.DeepEquals, PageMatcherParamsConfig{
    93  			Params: maps.Params{
    94  				"foo": "bar",
    95  			},
    96  			Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
    97  		})
    98  
    99  		// Current format.
   100  		c.Assert(fn(map[string]any{"target": map[string]any{"kind": "page"}, "params": map[string]any{"foo": "bar"}}), qt.DeepEquals, PageMatcherParamsConfig{
   101  			Params: maps.Params{
   102  				"foo": "bar",
   103  			},
   104  			Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
   105  		})
   106  	})
   107  }
   108  
   109  func TestDecodeCascadeConfig(t *testing.T) {
   110  	c := qt.New(t)
   111  
   112  	in := []map[string]any{
   113  		{
   114  			"params": map[string]any{
   115  				"a": "av",
   116  			},
   117  			"target": map[string]any{
   118  				"kind":        "page",
   119  				"Environment": "production",
   120  			},
   121  		},
   122  		{
   123  			"params": map[string]any{
   124  				"b": "bv",
   125  			},
   126  			"target": map[string]any{
   127  				"kind": "page",
   128  			},
   129  		},
   130  	}
   131  
   132  	got, err := DecodeCascadeConfig(in)
   133  
   134  	c.Assert(err, qt.IsNil)
   135  	c.Assert(got, qt.IsNotNil)
   136  	c.Assert(got.Config, qt.DeepEquals,
   137  		map[PageMatcher]maps.Params{
   138  			{Path: "", Kind: "page", Lang: "", Environment: ""}: {
   139  				"b": "bv",
   140  			},
   141  			{Path: "", Kind: "page", Lang: "", Environment: "production"}: {
   142  				"a": "av",
   143  			},
   144  		},
   145  	)
   146  	c.Assert(got.SourceStructure, qt.DeepEquals, []PageMatcherParamsConfig{
   147  		{
   148  			Params: maps.Params{"a": string("av")},
   149  			Target: PageMatcher{Kind: "page", Environment: "production"},
   150  		},
   151  		{Params: maps.Params{"b": string("bv")}, Target: PageMatcher{Kind: "page"}},
   152  	})
   153  
   154  	got, err = DecodeCascadeConfig(nil)
   155  	c.Assert(err, qt.IsNil)
   156  	c.Assert(got, qt.IsNotNil)
   157  
   158  }
   159  
   160  type testConfig struct {
   161  	environment string
   162  	running     bool
   163  	workingDir  string
   164  }
   165  
   166  func (c testConfig) Environment() string {
   167  	return c.environment
   168  }
   169  
   170  func (c testConfig) Running() bool {
   171  	return c.running
   172  }
   173  
   174  func (c testConfig) WorkingDir() string {
   175  	return c.workingDir
   176  }