github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/collections/append_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 collections
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  	"github.com/gohugoio/hugo/config"
    22  	"github.com/gohugoio/hugo/deps"
    23  	"github.com/gohugoio/hugo/langs"
    24  )
    25  
    26  // Also see tests in common/collection.
    27  func TestAppend(t *testing.T) {
    28  	t.Parallel()
    29  	c := qt.New(t)
    30  
    31  	ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())})
    32  
    33  	for i, test := range []struct {
    34  		start    any
    35  		addend   []any
    36  		expected any
    37  	}{
    38  		{[]string{"a", "b"}, []any{"c"}, []string{"a", "b", "c"}},
    39  		{[]string{"a", "b"}, []any{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
    40  		{[]string{"a", "b"}, []any{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}},
    41  		// Errors
    42  		{"", []any{[]string{"a", "b"}}, false},
    43  		{[]string{"a", "b"}, []any{}, false},
    44  		// No string concatenation.
    45  		{
    46  			"ab",
    47  			[]any{"c"},
    48  			false,
    49  		},
    50  	} {
    51  
    52  		errMsg := qt.Commentf("[%d]", i)
    53  
    54  		args := append(test.addend, test.start)
    55  
    56  		result, err := ns.Append(args...)
    57  
    58  		if b, ok := test.expected.(bool); ok && !b {
    59  			c.Assert(err, qt.Not(qt.IsNil), errMsg)
    60  			continue
    61  		}
    62  
    63  		c.Assert(err, qt.IsNil, errMsg)
    64  
    65  		if !reflect.DeepEqual(test.expected, result) {
    66  			t.Fatalf("%s got\n%T: %v\nexpected\n%T: %v", errMsg, result, result, test.expected, test.expected)
    67  		}
    68  	}
    69  }