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