github.com/dirkolbrich/hugo@v0.47.1/tpl/data/cache_test.go (about)

     1  // Copyright 2017 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 data
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/spf13/afero"
    21  	"github.com/spf13/viper"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestCache(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	fs := new(afero.MemMapFs)
    29  
    30  	for i, test := range []struct {
    31  		path    string
    32  		content []byte
    33  		ignore  bool
    34  	}{
    35  		{"http://Foo.Bar/foo_Bar-Foo", []byte(`T€st Content 123`), false},
    36  		{"fOO,bar:foo%bAR", []byte(`T€st Content 123 fOO,bar:foo%bAR`), false},
    37  		{"FOo/BaR.html", []byte(`FOo/BaR.html T€st Content 123`), false},
    38  		{"трям/трям", []byte(`T€st трям/трям Content 123`), false},
    39  		{"은행", []byte(`T€st C은행ontent 123`), false},
    40  		{"Банковский кассир", []byte(`Банковский кассир T€st Content 123`), false},
    41  		{"Банковский кассир", []byte(`Банковский кассир T€st Content 456`), true},
    42  	} {
    43  		msg := fmt.Sprintf("Test #%d: %v", i, test)
    44  
    45  		cfg := viper.New()
    46  
    47  		c, err := getCache(test.path, fs, cfg, test.ignore)
    48  		assert.NoError(t, err, msg)
    49  		assert.Nil(t, c, msg)
    50  
    51  		err = writeCache(test.path, test.content, fs, cfg, test.ignore)
    52  		assert.NoError(t, err, msg)
    53  
    54  		c, err = getCache(test.path, fs, cfg, test.ignore)
    55  		assert.NoError(t, err, msg)
    56  
    57  		if test.ignore {
    58  			assert.Nil(t, c, msg)
    59  		} else {
    60  			assert.Equal(t, string(test.content), string(c))
    61  		}
    62  	}
    63  }