github.com/neohugo/neohugo@v0.123.8/common/maps/scratch_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 maps 15 16 import ( 17 "reflect" 18 "sync" 19 "testing" 20 21 qt "github.com/frankban/quicktest" 22 ) 23 24 func TestScratchAdd(t *testing.T) { 25 t.Parallel() 26 c := qt.New(t) 27 28 scratch := NewScratch() 29 30 _, err := scratch.Add("int1", 10) 31 c.Assert(err, qt.IsNil) 32 _, err = scratch.Add("int1", 20) 33 c.Assert(err, qt.IsNil) 34 _, err = scratch.Add("int2", 20) 35 c.Assert(err, qt.IsNil) 36 37 c.Assert(scratch.Get("int1"), qt.Equals, int64(30)) 38 c.Assert(scratch.Get("int2"), qt.Equals, 20) 39 40 _, err = scratch.Add("float1", float64(10.5)) 41 c.Assert(err, qt.IsNil) 42 _, err = scratch.Add("float1", float64(20.1)) 43 c.Assert(err, qt.IsNil) 44 45 c.Assert(scratch.Get("float1"), qt.Equals, float64(30.6)) 46 47 _, err = scratch.Add("string1", "Hello ") 48 c.Assert(err, qt.IsNil) 49 _, err = scratch.Add("string1", "big ") 50 c.Assert(err, qt.IsNil) 51 _, err = scratch.Add("string1", "World!") 52 c.Assert(err, qt.IsNil) 53 54 c.Assert(scratch.Get("string1"), qt.Equals, "Hello big World!") 55 56 _, err = scratch.Add("scratch", scratch) 57 c.Assert(err, qt.IsNil) 58 _, err = scratch.Add("scratch", scratch) 59 c.Assert(err, qt.Not(qt.IsNil)) 60 61 m := scratch.Values() 62 c.Assert(m, qt.HasLen, 5) 63 64 if err == nil { 65 t.Errorf("Expected error from invalid arithmetic") 66 } 67 } 68 69 func TestScratchAddSlice(t *testing.T) { 70 t.Parallel() 71 c := qt.New(t) 72 73 scratch := NewScratch() 74 75 _, err := scratch.Add("intSlice", []int{1, 2}) 76 c.Assert(err, qt.IsNil) 77 _, err = scratch.Add("intSlice", 3) 78 c.Assert(err, qt.IsNil) 79 80 sl := scratch.Get("intSlice") 81 expected := []int{1, 2, 3} 82 83 if !reflect.DeepEqual(expected, sl) { 84 t.Errorf("Slice difference, go %q expected %q", sl, expected) 85 } 86 _, err = scratch.Add("intSlice", []int{4, 5}) 87 88 c.Assert(err, qt.IsNil) 89 90 sl = scratch.Get("intSlice") 91 expected = []int{1, 2, 3, 4, 5} 92 93 if !reflect.DeepEqual(expected, sl) { 94 t.Errorf("Slice difference, go %q expected %q", sl, expected) 95 } 96 } 97 98 // https://github.com/gohugoio/hugo/issues/5275 99 func TestScratchAddTypedSliceToInterfaceSlice(t *testing.T) { 100 t.Parallel() 101 c := qt.New(t) 102 103 scratch := NewScratch() 104 scratch.Set("slice", []any{}) 105 106 _, err := scratch.Add("slice", []int{1, 2}) 107 c.Assert(err, qt.IsNil) 108 c.Assert(scratch.Get("slice"), qt.DeepEquals, []int{1, 2}) 109 } 110 111 // https://github.com/gohugoio/hugo/issues/5361 112 func TestScratchAddDifferentTypedSliceToInterfaceSlice(t *testing.T) { 113 t.Parallel() 114 c := qt.New(t) 115 116 scratch := NewScratch() 117 scratch.Set("slice", []string{"foo"}) 118 119 _, err := scratch.Add("slice", []int{1, 2}) 120 c.Assert(err, qt.IsNil) 121 c.Assert(scratch.Get("slice"), qt.DeepEquals, []any{"foo", 1, 2}) 122 } 123 124 func TestScratchSet(t *testing.T) { 125 t.Parallel() 126 c := qt.New(t) 127 128 scratch := NewScratch() 129 scratch.Set("key", "val") 130 c.Assert(scratch.Get("key"), qt.Equals, "val") 131 } 132 133 func TestScratchDelete(t *testing.T) { 134 t.Parallel() 135 c := qt.New(t) 136 137 scratch := NewScratch() 138 scratch.Set("key", "val") 139 scratch.Delete("key") 140 _, err := scratch.Add("key", "Lucy Parsons") 141 c.Assert(err, qt.IsNil) 142 c.Assert(scratch.Get("key"), qt.Equals, "Lucy Parsons") 143 } 144 145 // Issue #2005 146 func TestScratchInParallel(t *testing.T) { 147 var wg sync.WaitGroup 148 scratch := NewScratch() 149 150 key := "counter" 151 scratch.Set(key, int64(1)) 152 for i := 1; i <= 10; i++ { 153 wg.Add(1) 154 go func(j int) { 155 for k := 0; k < 10; k++ { 156 newVal := int64(k + j) 157 158 _, err := scratch.Add(key, newVal) 159 if err != nil { 160 t.Errorf("Got err %s", err) 161 } 162 163 scratch.Set(key, newVal) 164 165 val := scratch.Get(key) 166 167 if counter, ok := val.(int64); ok { 168 if counter < 1 { 169 t.Errorf("Got %d", counter) 170 } 171 } else { 172 t.Errorf("Got %T", val) 173 } 174 } 175 wg.Done() 176 }(i) 177 } 178 wg.Wait() 179 } 180 181 func TestScratchGet(t *testing.T) { 182 t.Parallel() 183 scratch := NewScratch() 184 nothing := scratch.Get("nothing") 185 if nothing != nil { 186 t.Errorf("Should not return anything, but got %v", nothing) 187 } 188 } 189 190 func TestScratchSetInMap(t *testing.T) { 191 t.Parallel() 192 c := qt.New(t) 193 194 scratch := NewScratch() 195 scratch.SetInMap("key", "lux", "Lux") 196 scratch.SetInMap("key", "abc", "Abc") 197 scratch.SetInMap("key", "zyx", "Zyx") 198 scratch.SetInMap("key", "abc", "Abc (updated)") 199 scratch.SetInMap("key", "def", "Def") 200 c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []any{0: "Abc (updated)", 1: "Def", 2: "Lux", 3: "Zyx"}) 201 } 202 203 func TestScratchDeleteInMap(t *testing.T) { 204 t.Parallel() 205 c := qt.New(t) 206 207 scratch := NewScratch() 208 scratch.SetInMap("key", "lux", "Lux") 209 scratch.SetInMap("key", "abc", "Abc") 210 scratch.SetInMap("key", "zyx", "Zyx") 211 scratch.DeleteInMap("key", "abc") 212 scratch.SetInMap("key", "def", "Def") 213 scratch.DeleteInMap("key", "lmn") // Do nothing 214 c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []any{0: "Def", 1: "Lux", 2: "Zyx"}) 215 } 216 217 func TestScratchGetSortedMapValues(t *testing.T) { 218 t.Parallel() 219 scratch := NewScratch() 220 nothing := scratch.GetSortedMapValues("nothing") 221 if nothing != nil { 222 t.Errorf("Should not return anything, but got %v", nothing) 223 } 224 } 225 226 func BenchmarkScratchGet(b *testing.B) { 227 c := qt.New(b) 228 scratch := NewScratch() 229 _, err := scratch.Add("A", 1) 230 c.Assert(err, qt.IsNil) 231 b.ResetTimer() 232 for i := 0; i < b.N; i++ { 233 scratch.Get("A") 234 } 235 }