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.

148 lines
2.2 KiB

  1. package kvrepl
  2. import "testing"
  3. func TestParse(t *testing.T) {
  4. var tests = []struct {
  5. cmds []string
  6. db *DB
  7. expected string
  8. }{
  9. {
  10. cmds: []string{
  11. "write a b",
  12. "start",
  13. "write a c",
  14. "abort",
  15. "read a",
  16. },
  17. db: &DB{
  18. KV1: map[string]string{},
  19. KV2: map[string]string{},
  20. PKV: true,
  21. },
  22. expected: "b",
  23. },
  24. {
  25. cmds: []string{
  26. "write a b",
  27. "start",
  28. "write a hello-again",
  29. "start",
  30. "delete a",
  31. "commit",
  32. "write a once-more",
  33. "abort",
  34. "read a",
  35. },
  36. db: &DB{
  37. KV1: map[string]string{},
  38. KV2: map[string]string{},
  39. PKV: true,
  40. },
  41. expected: "b",
  42. },
  43. {
  44. cmds: []string{
  45. "write a b",
  46. "start",
  47. "write a hello-again",
  48. "start",
  49. "delete a",
  50. "commit",
  51. "write a once-more",
  52. "start",
  53. "write a foo",
  54. "abort",
  55. "read a",
  56. },
  57. db: &DB{
  58. KV1: map[string]string{},
  59. KV2: map[string]string{},
  60. PKV: true,
  61. },
  62. expected: "b",
  63. },
  64. {
  65. cmds: []string{
  66. "write a b",
  67. "start",
  68. "write a c",
  69. "commit",
  70. "read a",
  71. },
  72. db: &DB{
  73. KV1: map[string]string{},
  74. KV2: map[string]string{},
  75. PKV: true,
  76. },
  77. expected: "c",
  78. },
  79. {
  80. cmds: []string{
  81. "write a b",
  82. "start",
  83. "write a c",
  84. "start",
  85. "delete a",
  86. "write a d",
  87. "commit",
  88. "read a",
  89. },
  90. db: &DB{
  91. KV1: map[string]string{},
  92. KV2: map[string]string{},
  93. PKV: true,
  94. },
  95. expected: "d",
  96. },
  97. {
  98. cmds: []string{
  99. "write a b",
  100. "start",
  101. "write a c",
  102. "start",
  103. "delete a",
  104. "write a d",
  105. "abort",
  106. "read a",
  107. },
  108. db: &DB{
  109. KV1: map[string]string{},
  110. KV2: map[string]string{},
  111. PKV: true,
  112. },
  113. expected: "b",
  114. },
  115. {
  116. cmds: []string{
  117. "write a b",
  118. "start",
  119. "delete a",
  120. "commit",
  121. "read a",
  122. },
  123. db: &DB{
  124. KV1: map[string]string{},
  125. KV2: map[string]string{},
  126. PKV: true,
  127. },
  128. expected: "",
  129. },
  130. }
  131. for _, rt := range tests {
  132. actual := ""
  133. for _, c := range rt.cmds {
  134. actual, _ = Parse(c, rt.db)
  135. }
  136. if actual != rt.expected {
  137. t.Errorf(
  138. "failed parse:\n\texpected: %v\n\t actual: %v",
  139. rt.expected,
  140. actual,
  141. )
  142. }
  143. }
  144. }