github.com/SDLMoe/hugo@v0.47.1/resource/resource_metadata_test.go (about)

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