z_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package store
  2. import (
  3. "log"
  4. "testing"
  5. )
  6. func TestStore(t *testing.T) {
  7. st := New()
  8. st.Set("one", 10)
  9. log.Println(st.Get("one"))
  10. log.Println(st.Get("two"))
  11. log.Println(st.GetCreate("two", func(key any) (value any, created bool) {
  12. return 20, true
  13. }))
  14. st.Delete("two")
  15. log.Println(st.GetCreate("two", func(key any) (value any, created bool) {
  16. return 30, true
  17. }))
  18. log.Println(st.Map())
  19. st = FromMap(map[any]any{
  20. "one": 1,
  21. "two": 2,
  22. })
  23. log.Println(st.Map())
  24. }
  25. func TestStoreString(t *testing.T) {
  26. st := StringNew()
  27. st.Set("one", 10)
  28. log.Println(st.Get("one"))
  29. log.Println(st.Get("two"))
  30. log.Println(st.GetCreate("two", func(key string) (value any, created bool) {
  31. return 20, true
  32. }))
  33. st.Delete("two")
  34. log.Println(st.GetCreate("two", func(key string) (value any, created bool) {
  35. return 30, true
  36. }))
  37. log.Println(st.Map())
  38. st = StringFromMap(map[string]any{
  39. "one": 1,
  40. "two": 2,
  41. })
  42. log.Println(st.Map())
  43. val, check := st.GetCreateMulti("key1", func(string) (map[string]any, bool) {
  44. return map[string]any{
  45. "key100": 100,
  46. "key2": 200,
  47. }, true
  48. })
  49. log.Println(val, check)
  50. log.Println("==================")
  51. log.Println(st.items)
  52. }