parser_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import "testing"
  3. func intRangesEqual(a, b intRanges) bool {
  4. if a == nil && b == nil {
  5. return true
  6. }
  7. if a == nil || b == nil {
  8. return false
  9. }
  10. if len(a) != len(b) {
  11. return false
  12. }
  13. for n := range a {
  14. r1 := a[n]
  15. r2 := b[n]
  16. if r1.min != r2.min || r1.max != r2.max {
  17. return false
  18. }
  19. }
  20. return true
  21. }
  22. func stringSetEqual(a, b stringSet) bool {
  23. if a == nil && b == nil {
  24. return true
  25. }
  26. if a == nil || b == nil {
  27. return false
  28. }
  29. if len(a) != len(b) {
  30. return false
  31. }
  32. for n := range a {
  33. if !b.get(n) {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. func TestParseNumberMenu(t *testing.T) {
  40. type result struct {
  41. Include intRanges
  42. Exclude intRanges
  43. OtherInclude stringSet
  44. OtherExclude stringSet
  45. }
  46. inputs := []string{
  47. "1 2 3 4 5",
  48. "1-10 5-15",
  49. "10-5 90-85",
  50. "1 ^2 ^10-5 99 ^40-38 ^123 60-62",
  51. "abort all none",
  52. "a-b ^a-b ^abort",
  53. "1\t2 3 4\t\t \t 5",
  54. "1 2,3, 4, 5,6 ,7 ,8",
  55. "",
  56. " \t ",
  57. "A B C D E",
  58. }
  59. expected := []result{
  60. {intRanges{makeIntRange(1, 1), makeIntRange(2, 2), makeIntRange(3, 3), makeIntRange(4, 4), makeIntRange(5, 5)}, intRanges{}, make(stringSet), make(stringSet)},
  61. {intRanges{makeIntRange(1, 10), makeIntRange(5, 15)}, intRanges{}, make(stringSet), make(stringSet)},
  62. {intRanges{makeIntRange(5, 10), makeIntRange(85, 90)}, intRanges{}, make(stringSet), make(stringSet)},
  63. {intRanges{makeIntRange(1, 1), makeIntRange(99, 99), makeIntRange(60, 62)}, intRanges{makeIntRange(2, 2), makeIntRange(5, 10), makeIntRange(38, 40), makeIntRange(123, 123)}, make(stringSet), make(stringSet)},
  64. {intRanges{}, intRanges{}, makeStringSet("abort", "all", "none"), make(stringSet)},
  65. {intRanges{}, intRanges{}, makeStringSet("a-b"), makeStringSet("abort", "a-b")},
  66. {intRanges{makeIntRange(1, 1), makeIntRange(2, 2), makeIntRange(3, 3), makeIntRange(4, 4), makeIntRange(5, 5)}, intRanges{}, make(stringSet), make(stringSet)},
  67. {intRanges{makeIntRange(1, 1), makeIntRange(2, 2), makeIntRange(3, 3), makeIntRange(4, 4), makeIntRange(5, 5), makeIntRange(6, 6), makeIntRange(7, 7), makeIntRange(8, 8)}, intRanges{}, make(stringSet), make(stringSet)},
  68. {intRanges{}, intRanges{}, make(stringSet), make(stringSet)},
  69. {intRanges{}, intRanges{}, make(stringSet), make(stringSet)},
  70. {intRanges{}, intRanges{}, makeStringSet("a", "b", "c", "d", "e"), make(stringSet)},
  71. }
  72. for n, in := range inputs {
  73. res := expected[n]
  74. include, exclude, otherInclude, otherExclude := parseNumberMenu(in)
  75. if !intRangesEqual(include, res.Include) ||
  76. !intRangesEqual(exclude, res.Exclude) ||
  77. !stringSetEqual(otherInclude, res.OtherInclude) ||
  78. !stringSetEqual(otherExclude, res.OtherExclude) {
  79. t.Fatalf("Test %d Failed: Expected: include=%+v exclude=%+v otherInclude=%+v otherExclude=%+v got include=%+v excluive=%+v otherInclude=%+v otherExclude=%+v",
  80. n+1, res.Include, res.Exclude, res.OtherInclude, res.OtherExclude, include, exclude, otherInclude, otherExclude)
  81. }
  82. }
  83. }