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