resource_metadata_test.go (6119B)
1 // Copyright 2019 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 resources 15 16 import ( 17 "testing" 18 19 "github.com/gohugoio/hugo/media" 20 "github.com/gohugoio/hugo/resources/resource" 21 22 qt "github.com/frankban/quicktest" 23 ) 24 25 func TestAssignMetadata(t *testing.T) { 26 c := qt.New(t) 27 spec := newTestResourceSpec(specDescriptor{c: c}) 28 29 var foo1, foo2, foo3, logo1, logo2, logo3 resource.Resource 30 var resources resource.Resources 31 32 for _, this := range []struct { 33 metaData []map[string]any 34 assertFunc func(err error) 35 }{ 36 {[]map[string]any{ 37 { 38 "title": "My Resource", 39 "name": "My Name", 40 "src": "*", 41 }, 42 }, func(err error) { 43 c.Assert(logo1.Title(), qt.Equals, "My Resource") 44 c.Assert(logo1.Name(), qt.Equals, "My Name") 45 c.Assert(foo2.Name(), qt.Equals, "My Name") 46 }}, 47 {[]map[string]any{ 48 { 49 "title": "My Logo", 50 "src": "*loGo*", 51 }, 52 { 53 "title": "My Resource", 54 "name": "My Name", 55 "src": "*", 56 }, 57 }, func(err error) { 58 c.Assert(logo1.Title(), qt.Equals, "My Logo") 59 c.Assert(logo2.Title(), qt.Equals, "My Logo") 60 c.Assert(logo1.Name(), qt.Equals, "My Name") 61 c.Assert(foo2.Name(), qt.Equals, "My Name") 62 c.Assert(foo3.Name(), qt.Equals, "My Name") 63 c.Assert(foo3.Title(), qt.Equals, "My Resource") 64 }}, 65 {[]map[string]any{ 66 { 67 "title": "My Logo", 68 "src": "*loGo*", 69 "params": map[string]any{ 70 "Param1": true, 71 "icon": "logo", 72 }, 73 }, 74 { 75 "title": "My Resource", 76 "src": "*", 77 "params": map[string]any{ 78 "Param2": true, 79 "icon": "resource", 80 }, 81 }, 82 }, func(err error) { 83 c.Assert(err, qt.IsNil) 84 c.Assert(logo1.Title(), qt.Equals, "My Logo") 85 c.Assert(foo3.Title(), qt.Equals, "My Resource") 86 _, p1 := logo2.Params()["param1"] 87 _, p2 := foo2.Params()["param2"] 88 _, p1_2 := foo2.Params()["param1"] 89 _, p2_2 := logo2.Params()["param2"] 90 91 icon1 := logo2.Params()["icon"] 92 icon2 := foo2.Params()["icon"] 93 94 c.Assert(p1, qt.Equals, true) 95 c.Assert(p2, qt.Equals, true) 96 97 // Check merge 98 c.Assert(p2_2, qt.Equals, true) 99 c.Assert(p1_2, qt.Equals, false) 100 101 c.Assert(icon1, qt.Equals, "logo") 102 c.Assert(icon2, qt.Equals, "resource") 103 }}, 104 {[]map[string]any{ 105 { 106 "name": "Logo Name #:counter", 107 "src": "*logo*", 108 }, 109 { 110 "title": "Resource #:counter", 111 "name": "Name #:counter", 112 "src": "*", 113 }, 114 }, func(err error) { 115 c.Assert(err, qt.IsNil) 116 c.Assert(logo2.Title(), qt.Equals, "Resource #2") 117 c.Assert(logo2.Name(), qt.Equals, "Logo Name #1") 118 c.Assert(logo1.Title(), qt.Equals, "Resource #4") 119 c.Assert(logo1.Name(), qt.Equals, "Logo Name #2") 120 c.Assert(foo2.Title(), qt.Equals, "Resource #1") 121 c.Assert(foo1.Title(), qt.Equals, "Resource #3") 122 c.Assert(foo1.Name(), qt.Equals, "Name #2") 123 c.Assert(foo3.Title(), qt.Equals, "Resource #5") 124 125 c.Assert(resources.GetMatch("logo name #1*"), qt.Equals, logo2) 126 }}, 127 {[]map[string]any{ 128 { 129 "title": "Third Logo #:counter", 130 "src": "logo3.png", 131 }, 132 { 133 "title": "Other Logo #:counter", 134 "name": "Name #:counter", 135 "src": "logo*", 136 }, 137 }, func(err error) { 138 c.Assert(err, qt.IsNil) 139 c.Assert(logo3.Title(), qt.Equals, "Third Logo #1") 140 c.Assert(logo3.Name(), qt.Equals, "Name #3") 141 c.Assert(logo2.Title(), qt.Equals, "Other Logo #1") 142 c.Assert(logo2.Name(), qt.Equals, "Name #1") 143 c.Assert(logo1.Title(), qt.Equals, "Other Logo #2") 144 c.Assert(logo1.Name(), qt.Equals, "Name #2") 145 }}, 146 {[]map[string]any{ 147 { 148 "title": "Third Logo", 149 "src": "logo3.png", 150 }, 151 { 152 "title": "Other Logo #:counter", 153 "name": "Name #:counter", 154 "src": "logo*", 155 }, 156 }, func(err error) { 157 c.Assert(err, qt.IsNil) 158 c.Assert(logo3.Title(), qt.Equals, "Third Logo") 159 c.Assert(logo3.Name(), qt.Equals, "Name #3") 160 c.Assert(logo2.Title(), qt.Equals, "Other Logo #1") 161 c.Assert(logo2.Name(), qt.Equals, "Name #1") 162 c.Assert(logo1.Title(), qt.Equals, "Other Logo #2") 163 c.Assert(logo1.Name(), qt.Equals, "Name #2") 164 }}, 165 {[]map[string]any{ 166 { 167 "name": "third-logo", 168 "src": "logo3.png", 169 }, 170 { 171 "title": "Logo #:counter", 172 "name": "Name #:counter", 173 "src": "logo*", 174 }, 175 }, func(err error) { 176 c.Assert(err, qt.IsNil) 177 c.Assert(logo3.Title(), qt.Equals, "Logo #3") 178 c.Assert(logo3.Name(), qt.Equals, "third-logo") 179 c.Assert(logo2.Title(), qt.Equals, "Logo #1") 180 c.Assert(logo2.Name(), qt.Equals, "Name #1") 181 c.Assert(logo1.Title(), qt.Equals, "Logo #2") 182 c.Assert(logo1.Name(), qt.Equals, "Name #2") 183 }}, 184 {[]map[string]any{ 185 { 186 "title": "Third Logo #:counter", 187 }, 188 }, func(err error) { 189 // Missing src 190 c.Assert(err, qt.Not(qt.IsNil)) 191 }}, 192 {[]map[string]any{ 193 { 194 "title": "Title", 195 "src": "[]", 196 }, 197 }, func(err error) { 198 // Invalid pattern 199 c.Assert(err, qt.Not(qt.IsNil)) 200 }}, 201 } { 202 203 foo2 = spec.newGenericResource(nil, nil, nil, "/b/foo2.css", "foo2.css", media.CSSType) 204 logo2 = spec.newGenericResource(nil, nil, nil, "/b/Logo2.png", "Logo2.png", pngType) 205 foo1 = spec.newGenericResource(nil, nil, nil, "/a/foo1.css", "foo1.css", media.CSSType) 206 logo1 = spec.newGenericResource(nil, nil, nil, "/a/logo1.png", "logo1.png", pngType) 207 foo3 = spec.newGenericResource(nil, nil, nil, "/b/foo3.css", "foo3.css", media.CSSType) 208 logo3 = spec.newGenericResource(nil, nil, nil, "/b/logo3.png", "logo3.png", pngType) 209 210 resources = resource.Resources{ 211 foo2, 212 logo2, 213 foo1, 214 logo1, 215 foo3, 216 logo3, 217 } 218 219 this.assertFunc(AssignMetadata(this.metaData, resources...)) 220 } 221 }