| 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 i, test := range []struct { |
| 28 |
start any |
| 29 |
addend []any |
| 30 |
expected any |
| 31 |
}{ |
| 32 |
{[]string{"a", "b"}, []any{"c"}, []string{"a", "b", "c"}}, |
| 33 |
{[]string{"a", "b"}, []any{"c", "d", "e"}, []string{"a", "b", "c", "d", "e"}}, |
| 34 |
{[]string{"a", "b"}, []any{[]string{"c", "d", "e"}}, []string{"a", "b", "c", "d", "e"}}, |
| 35 |
{[]string{"a"}, []any{"b", template.HTML("c")}, []any{"a", "b", template.HTML("c")}}, |
| 36 |
{nil, []any{"a", "b"}, []string{"a", "b"}}, |
| 37 |
{nil, []any{nil}, []any{nil}}, |
| 38 |
{[]any{}, []any{[]string{"c", "d", "e"}}, []string{"c", "d", "e"}}, |
| 39 |
{ |
| 40 |
tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}, |
| 41 |
[]any{&tstSlicer{"c"}}, |
| 42 |
tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}, &tstSlicer{"c"}}, |
| 43 |
}, |
| 44 |
{ |
| 45 |
&tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}, |
| 46 |
[]any{&tstSlicer{"c"}}, |
| 47 |
tstSlicers{ |
| 48 |
&tstSlicer{"a"}, |
| 49 |
&tstSlicer{"b"}, |
| 50 |
&tstSlicer{"c"}, |
| 51 |
}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn1{"b"}}, |
| 55 |
[]any{&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 |
[]any{tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}}, |
| 62 |
[]any{"a", "b", &tstSlicer{"a"}, &tstSlicer{"b"}}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
[]string{"a", "b"}, |
| 66 |
[]any{&tstSlicer{"a"}}, |
| 67 |
[]any{"a", "b", &tstSlicer{"a"}}, |
| 68 |
}, |
| 69 |
// Errors |
| 70 |
{"", []any{[]string{"a", "b"}}, false}, |
| 71 |
// No string concatenation. |
| 72 |
{ |
| 73 |
"ab", |
| 74 |
[]any{"c"}, |
| 75 |
false, |
| 76 |
}, |
| 77 |
{[]string{"a", "b"}, []any{nil}, []any{"a", "b", nil}}, |
| 78 |
{[]string{"a", "b"}, []any{nil, "d", nil}, []any{"a", "b", nil, "d", nil}}, |
| 79 |
{[]any{"a", nil, "c"}, []any{"d", nil, "f"}, []any{"a", nil, "c", "d", nil, "f"}}, |
| 80 |
{[]string{"a", "b"}, []any{}, []string{"a", "b"}}, |
| 81 |
{[]string{"a", "b"}, []any{[]any{}}, []string{"a", "b"}}, |
| 82 |
{[]int{1, 2}, []any{[]any(nil)}, []int{1, 2}}, |
| 83 |
} { |
| 84 |
|
| 85 |
result, err := Append(test.start, test.addend...) |
| 86 |
|
| 87 |
if b, ok := test.expected.(bool); ok && !b { |
| 88 |
|
| 89 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 90 |
continue |
| 91 |
} |
| 92 |
|
| 93 |
c.Assert(err, qt.IsNil) |
| 94 |
c.Assert(result, qt.DeepEquals, test.expected, qt.Commentf("test: [%d] %v", i, test)) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// #11093 |
| 99 |
func TestAppendToMultiDimensionalSlice(t *testing.T) { |
| 100 |
t.Parallel() |
| 101 |
c := qt.New(t) |
| 102 |
|
| 103 |
for _, test := range []struct { |
| 104 |
to any |
| 105 |
from []any |
| 106 |
expected any |
| 107 |
}{ |
| 108 |
{ |
| 109 |
[][]string{{"a", "b"}}, |
| 110 |
[]any{[]string{"c", "d"}}, |
| 111 |
[][]string{ |
| 112 |
{"a", "b"}, |
| 113 |
{"c", "d"}, |
| 114 |
}, |
| 115 |
}, |
| 116 |
{ |
| 117 |
[][]string{{"a", "b"}}, |
| 118 |
[]any{[]string{"c", "d"}, []string{"e", "f"}}, |
| 119 |
[][]string{ |
| 120 |
{"a", "b"}, |
| 121 |
{"c", "d"}, |
| 122 |
{"e", "f"}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
[][]string{{"a", "b"}}, |
| 127 |
[]any{[]int{1, 2}}, |
| 128 |
false, |
| 129 |
}, |
| 130 |
} { |
| 131 |
result, err := Append(test.to, test.from...) |
| 132 |
if b, ok := test.expected.(bool); ok && !b { |
| 133 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 134 |
} else { |
| 135 |
c.Assert(err, qt.IsNil) |
| 136 |
c.Assert(result, qt.DeepEquals, test.expected) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestAppendShouldMakeACopyOfTheInputSlice(t *testing.T) { |
| 142 |
t.Parallel() |
| 143 |
c := qt.New(t) |
| 144 |
slice := make([]string, 0, 100) |
| 145 |
slice = append(slice, "a", "b") |
| 146 |
result, err := Append(slice, "c") |
| 147 |
c.Assert(err, qt.IsNil) |
| 148 |
slice[0] = "d" |
| 149 |
c.Assert(result, qt.DeepEquals, []string{"a", "b", "c"}) |
| 150 |
c.Assert(slice, qt.DeepEquals, []string{"d", "b"}) |
| 151 |
} |