| 276 | } |
| 277 | |
| 278 | func TestGetOrSet(t *testing.T) { |
| 279 | s := store.New(map[string]int{ |
| 280 | "test1": 0, |
| 281 | "test2": 1, |
| 282 | "test3": 3, |
| 283 | }) |
| 284 | |
| 285 | scenarios := []struct { |
| 286 | key string |
| 287 | value int |
| 288 | expected int |
| 289 | }{ |
| 290 | {"test2", 20, 1}, |
| 291 | {"test3", 2, 3}, |
| 292 | {"test_new", 20, 20}, |
| 293 | {"test_new", 50, 20}, // should return the previously inserted value |
| 294 | } |
| 295 | |
| 296 | for _, scenario := range scenarios { |
| 297 | t.Run(scenario.key, func(t *testing.T) { |
| 298 | result := s.GetOrSet(scenario.key, func() int { |
| 299 | return scenario.value |
| 300 | }) |
| 301 | |
| 302 | if result != scenario.expected { |
| 303 | t.Fatalf("Expected %v, got %v", scenario.expected, result) |
| 304 | } |
| 305 | }) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func TestSetIfLessThanLimit(t *testing.T) { |
| 310 | s := store.Store[string, int]{} |