| 250 | } |
| 251 | |
| 252 | func TestSetFunc(t *testing.T) { |
| 253 | s := store.Store[string, int]{} |
| 254 | |
| 255 | // non existing value |
| 256 | s.SetFunc("test", func(old int) int { |
| 257 | if old != 0 { |
| 258 | t.Fatalf("Expected old value %d, got %d", 0, old) |
| 259 | } |
| 260 | return old + 2 |
| 261 | }) |
| 262 | if v := s.Get("test"); v != 2 { |
| 263 | t.Fatalf("Expected the stored value to be %d, got %d", 2, v) |
| 264 | } |
| 265 | |
| 266 | // increment existing value |
| 267 | s.SetFunc("test", func(old int) int { |
| 268 | if old != 2 { |
| 269 | t.Fatalf("Expected old value %d, got %d", 2, old) |
| 270 | } |
| 271 | return old + 1 |
| 272 | }) |
| 273 | if v := s.Get("test"); v != 3 { |
| 274 | t.Fatalf("Expected the stored value to be %d, got %d", 3, v) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func TestGetOrSet(t *testing.T) { |
| 279 | s := store.New(map[string]int{ |