github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/common/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  	"html/template"
    18  	"testing"
    19  
    20  	qt "github.com/frankban/quicktest"
    21  )
    22  
    23  func TestAppend(t *testing.T) {
    24  	t.Parallel()
    25  	c := qt.New(t)
    26  
    27  	for _, test := range []struct {
    28  		start    interface{}
    29  		addend   []interface{}
    30  		expected interface{}
    31  	}{
    32  		{[]string{"a", "b"}, []interface{}{"c"}, []string{"a", "b", "c"}},
    33  		{[]string{"a", "b"}, []interface{}{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
    34  		{[]string{"a", "b"}, []interface{}{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}},
    35  		{[]string{"a"}, []interface{}{"b", template.HTML("c")}, []interface{}{"a", "b", template.HTML("c")}},
    36  		{nil, []interface{}{"a", "b"}, []string{"a", "b"}},
    37  		{nil, []interface{}{nil}, []interface{}{nil}},
    38  		{[]interface{}{}, []interface{}{[]string{"c", "d", "e"}}, []string{"c", "d", "e"}},
    39  		{
    40  			tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}},
    41  			[]interface{}{&tstSlicer{"c"}},
    42  			tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}, &tstSlicer{"c"}},
    43  		},
    44  		{
    45  			&tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}},
    46  			[]interface{}{&tstSlicer{"c"}},
    47  			tstSlicers{
    48  				&tstSlicer{"a"},
    49  				&tstSlicer{"b"},
    50  				&tstSlicer{"c"},
    51  			},
    52  		},
    53  		{
    54  			testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}},
    55  			[]interface{}{&tstSlicerIn1{"c"}},
    56  			testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}, &tstSlicerIn1{"c"}},
    57  		},
    58  		//https://github.com/gohugoio/hugo/issues/5361
    59  		{
    60  			[]string{"a", "b"},
    61  			[]interface{}{tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}},
    62  			[]interface{}{"a", "b", &tstSlicer{"a"}, &tstSlicer{"b"}},
    63  		},
    64  		{
    65  			[]string{"a", "b"},
    66  			[]interface{}{&tstSlicer{"a"}},
    67  			[]interface{}{"a", "b", &tstSlicer{"a"}},
    68  		},
    69  		// Errors
    70  		{"", []interface{}{[]string{"a", "b"}}, false},
    71  		// No string concatenation.
    72  		{
    73  			"ab",
    74  			[]interface{}{"c"},
    75  			false,
    76  		},
    77  	} {
    78  
    79  		result, err := Append(test.start, test.addend...)
    80  
    81  		if b, ok := test.expected.(bool); ok && !b {
    82  
    83  			c.Assert(err, qt.Not(qt.IsNil))
    84  			continue
    85  		}
    86  
    87  		c.Assert(err, qt.IsNil)
    88  		c.Assert(result, qt.DeepEquals, test.expected)
    89  	}
    90  }