A simple command line REPL (read-eval-print loop) that drives a simple in-memory key/value storage system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
1.9 KiB

  1. package kvrepl
  2. import "testing"
  3. func TestRead(t *testing.T) {
  4. var tests = []struct {
  5. args []string
  6. kv map[string]string
  7. expected bool
  8. }{
  9. {[]string{}, nil, false},
  10. {[]string{"a", "b"}, nil, false},
  11. {[]string{"a"}, map[string]string{}, false},
  12. {[]string{"a"}, map[string]string{"a": "b"}, true},
  13. }
  14. for _, rt := range tests {
  15. _, actual := read(rt.args, rt.kv)
  16. if (actual == nil) != rt.expected {
  17. t.Errorf(
  18. "failed read:\n\texpected: %v\n\t actual: %v",
  19. rt.expected,
  20. (actual == nil),
  21. )
  22. }
  23. }
  24. }
  25. func TestWrite(t *testing.T) {
  26. var tests = []struct {
  27. args []string
  28. kv map[string]string
  29. expected bool
  30. }{
  31. {[]string{}, nil, false},
  32. {[]string{"a"}, nil, false},
  33. {[]string{"a", "b"}, map[string]string{}, true},
  34. }
  35. for _, rt := range tests {
  36. actual := write(rt.args, rt.kv)
  37. if (actual == nil) != rt.expected {
  38. t.Errorf(
  39. "failed write:\n\texpected: %v\n\t actual: %v",
  40. rt.expected,
  41. (actual == nil),
  42. )
  43. }
  44. }
  45. }
  46. func TestDel(t *testing.T) {
  47. var tests = []struct {
  48. args []string
  49. kv map[string]string
  50. expected bool
  51. }{
  52. {[]string{}, nil, false},
  53. {[]string{"a", "b"}, nil, false},
  54. {[]string{"a"}, map[string]string{}, false},
  55. {[]string{"a"}, map[string]string{"a": "b"}, true},
  56. }
  57. for _, rt := range tests {
  58. actual := del(rt.args, rt.kv)
  59. if (actual == nil) != rt.expected {
  60. t.Errorf(
  61. "failed del:\n\texpected: %v\n\t actual: %v",
  62. rt.expected,
  63. (actual == nil),
  64. )
  65. }
  66. }
  67. }
  68. func TestSeries(t *testing.T) {
  69. kv := make(map[string]string)
  70. err := write([]string{"a", "b"}, kv)
  71. if err != nil {
  72. t.Errorf(
  73. "failed series:\n\tfailed to write",
  74. )
  75. }
  76. v, _ := read([]string{"a"}, kv)
  77. if v != "b" {
  78. t.Errorf(
  79. "failed series:\n\texpected: %v\n\t actual: %v",
  80. "b",
  81. v,
  82. )
  83. }
  84. _ = del([]string{"a"}, kv)
  85. v, _ = read([]string{"a"}, kv)
  86. if v != "" {
  87. t.Errorf(
  88. "failed series:\n\texpected: %v\n\t actual: %v",
  89. "",
  90. v,
  91. )
  92. }
  93. }