Debian Codegraph @

common/collections/append_test.go

raw
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 } {
82
83 result, err := Append(test.start, test.addend...)
84
85 if b, ok := test.expected.(bool); ok && !b {
86
87 c.Assert(err, qt.Not(qt.IsNil))
88 continue
89 }
90
91 c.Assert(err, qt.IsNil)
92 c.Assert(result, qt.DeepEquals, test.expected, qt.Commentf("test: [%d] %v", i, test))
93 }
94 }
95
96 // #11093
97 func TestAppendToMultiDimensionalSlice(t *testing.T) {
98 t.Parallel()
99 c := qt.New(t)
100
101 for _, test := range []struct {
102 to any
103 from []any
104 expected any
105 }{
106 {
107 [][]string{{"a", "b"}},
108 []any{[]string{"c", "d"}},
109 [][]string{
110 {"a", "b"},
111 {"c", "d"},
112 },
113 },
114 {
115 [][]string{{"a", "b"}},
116 []any{[]string{"c", "d"}, []string{"e", "f"}},
117 [][]string{
118 {"a", "b"},
119 {"c", "d"},
120 {"e", "f"},
121 },
122 },
123 {
124 [][]string{{"a", "b"}},
125 []any{[]int{1, 2}},
126 false,
127 },
128 } {
129 result, err := Append(test.to, test.from...)
130 if b, ok := test.expected.(bool); ok && !b {
131 c.Assert(err, qt.Not(qt.IsNil))
132 } else {
133 c.Assert(err, qt.IsNil)
134 c.Assert(result, qt.DeepEquals, test.expected)
135 }
136 }
137 }
138
139 func TestAppendShouldMakeACopyOfTheInputSlice(t *testing.T) {
140 t.Parallel()
141 c := qt.New(t)
142 slice := make([]string, 0, 100)
143 slice = append(slice, "a", "b")
144 result, err := Append(slice, "c")
145 c.Assert(err, qt.IsNil)
146 slice[0] = "d"
147 c.Assert(result, qt.DeepEquals, []string{"a", "b", "c"})
148 c.Assert(slice, qt.DeepEquals, []string{"d", "b"})
149 }