github.com/neohugo/neohugo@v0.123.8/resources/page/pagemeta/pagemeta_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 pagemeta 15 16 import ( 17 "fmt" 18 "testing" 19 "time" 20 21 "github.com/neohugo/neohugo/htesting/hqt" 22 23 "github.com/neohugo/neohugo/config" 24 25 qt "github.com/frankban/quicktest" 26 ) 27 28 func TestDecodeBuildConfig(t *testing.T) { 29 t.Parallel() 30 31 c := qt.New(t) 32 33 configTempl := ` 34 [_build] 35 render = %s 36 list = %s 37 publishResources = true` 38 39 for _, test := range []struct { 40 args []any 41 expect BuildConfig 42 }{ 43 { 44 []any{"true", "true"}, 45 BuildConfig{ 46 Render: Always, 47 List: Always, 48 PublishResources: true, 49 set: true, 50 }, 51 }, 52 {[]any{"true", "false"}, BuildConfig{ 53 Render: Always, 54 List: Never, 55 PublishResources: true, 56 set: true, 57 }}, 58 {[]any{`"always"`, `"always"`}, BuildConfig{ 59 Render: Always, 60 List: Always, 61 PublishResources: true, 62 set: true, 63 }}, 64 {[]any{`"never"`, `"never"`}, BuildConfig{ 65 Render: Never, 66 List: Never, 67 PublishResources: true, 68 set: true, 69 }}, 70 {[]any{`"link"`, `"local"`}, BuildConfig{ 71 Render: Link, 72 List: ListLocally, 73 PublishResources: true, 74 set: true, 75 }}, 76 {[]any{`"always"`, `"asdfadf"`}, BuildConfig{ 77 Render: Always, 78 List: Always, 79 PublishResources: true, 80 set: true, 81 }}, 82 } { 83 cfg, err := config.FromConfigString(fmt.Sprintf(configTempl, test.args...), "toml") 84 c.Assert(err, qt.IsNil) 85 bcfg, err := DecodeBuildConfig(cfg.Get("_build")) 86 c.Assert(err, qt.IsNil) 87 88 eq := qt.CmpEquals(hqt.DeepAllowUnexported(BuildConfig{})) 89 90 c.Assert(bcfg, eq, test.expect) 91 92 } 93 } 94 95 func TestDateAndSlugFromBaseFilename(t *testing.T) { 96 t.Parallel() 97 98 c := qt.New(t) 99 100 tests := []struct { 101 name string 102 date string 103 slug string 104 }{ 105 {"page.md", "0001-01-01", ""}, 106 {"2012-09-12-page.md", "2012-09-12", "page"}, 107 {"2018-02-28-page.md", "2018-02-28", "page"}, 108 {"2018-02-28_page.md", "2018-02-28", "page"}, 109 {"2018-02-28 page.md", "2018-02-28", "page"}, 110 {"2018-02-28page.md", "2018-02-28", "page"}, 111 {"2018-02-28-.md", "2018-02-28", ""}, 112 {"2018-02-28-.md", "2018-02-28", ""}, 113 {"2018-02-28.md", "2018-02-28", ""}, 114 {"2018-02-28-page", "2018-02-28", "page"}, 115 {"2012-9-12-page.md", "0001-01-01", ""}, 116 {"asdfasdf.md", "0001-01-01", ""}, 117 } 118 119 for _, test := range tests { 120 expecteFDate, err := time.Parse("2006-01-02", test.date) 121 c.Assert(err, qt.IsNil) 122 123 gotDate, gotSlug := dateAndSlugFromBaseFilename(time.UTC, test.name) 124 125 c.Assert(gotDate, qt.Equals, expecteFDate) 126 c.Assert(gotSlug, qt.Equals, test.slug) 127 128 } 129 } 130 131 func TestExpandDefaultValues(t *testing.T) { 132 c := qt.New(t) 133 c.Assert(expandDefaultValues([]string{"a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"a", "b", "c", "d"}) 134 c.Assert(expandDefaultValues([]string{"a", "b", "c"}, []string{"a", "b", "c"}), qt.DeepEquals, []string{"a", "b", "c"}) 135 c.Assert(expandDefaultValues([]string{":default", "a", ":default", "d"}, []string{"b", "c"}), qt.DeepEquals, []string{"b", "c", "a", "b", "c", "d"}) 136 }