shlex_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package shlex
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. )
  6. var (
  7. // one two "three four" "five \"six\"" seven#eight # nine # ten
  8. // eleven 'twelve\'
  9. testString = "one two \"three four\" \"five \\\"six\\\"\" seven#eight # nine # ten eleven 'twelve\\' thirteen=13 fourteen/14"
  10. )
  11. func TestLexer(t *testing.T) {
  12. testInput := testString
  13. expectedStrings := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "#", "nine", "#", "ten", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
  14. lexer := NewLexer(testInput)
  15. for i, want := range expectedStrings {
  16. got := lexer.Next()
  17. if got.Value != want {
  18. t.Errorf("Lexer.Next()[%v] of %q -> %v. Want: %v", i, testString, got, want)
  19. }
  20. }
  21. }
  22. type Tok struct {
  23. Pos int
  24. Val string
  25. }
  26. func TestSplit(t *testing.T) {
  27. want := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "#", "nine", "#", "ten", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
  28. got, err := Split(testString)
  29. if err != nil {
  30. t.Error(err)
  31. }
  32. if len(want) != len(got) {
  33. t.Errorf("Split(%q) -> %v. Want: %v", testString, got, want)
  34. }
  35. for i := range got {
  36. if got[i] != want[i] {
  37. t.Errorf("Split(%q)[%v] -> %v. Want: %v", testString, i, got[i], want[i])
  38. }
  39. }
  40. for _, x := range []string{
  41. `abc\`, `\`, `'abc`, `'`, `"`, `asd\`,
  42. } {
  43. _, err := Split(x)
  44. if err == nil {
  45. t.Fatalf("Failed to get an error for: %#v", x)
  46. }
  47. }
  48. s := func(q string) (ans []Tok) {
  49. l := NewLexer(q)
  50. for {
  51. w := l.Next()
  52. if w.Err != nil {
  53. t.Fatal(w.Err)
  54. }
  55. if w.Value == "" {
  56. break
  57. }
  58. ans = append(ans, Tok{w.Pos, w.Value})
  59. }
  60. return
  61. }
  62. for q, expected := range map[string][]Tok{
  63. `"ab"`: {{0, "ab"}},
  64. `x "ab"y \m`: {{0, `x`}, {2, `aby`}, {8, `m`}},
  65. `x'y"\z'1`: {{0, `xy"\z1`}},
  66. `\abc\ d`: {{0, `abc d`}},
  67. ``: nil,
  68. ` `: nil,
  69. " \tabc\n\t\r ": {{2, "abc"}},
  70. } {
  71. if diff := cmp.Diff(expected, s(q)); diff != "" {
  72. t.Fatalf("Failed for string: %#v\n%s", q, diff)
  73. }
  74. }
  75. }
  76. func TestSplitForCompletion(t *testing.T) {
  77. test := func(cmdline string, last_arg_pos int, expected ...string) {
  78. actual, actual_pos := SplitForCompletion(cmdline)
  79. if diff := cmp.Diff(expected, actual); diff != "" {
  80. t.Fatalf("Failed to split: %s\n%s", cmdline, diff)
  81. }
  82. if last_arg_pos != actual_pos {
  83. t.Fatalf("Failed to split: %#v\n Last arg pos: %d != %d", cmdline, last_arg_pos, actual_pos)
  84. }
  85. }
  86. test("a b", 2, "a", "b")
  87. test("a b ", 4, "a", "b", "")
  88. test("a b ", 5, "a", "b", "")
  89. test(`a "b c"`, 2, "a", "b c")
  90. test(`a "b c`, 2, "a", "b c")
  91. }
  92. func TestExpandANSICEscapes(t *testing.T) {
  93. var m = map[string]string{
  94. "abc": "abc",
  95. `a\ab`: "a\ab",
  96. `a\eb`: "a\x1bb",
  97. `a\r\nb`: "a\r\nb",
  98. `a\c b`: "a\000b",
  99. `a\c`: "a\\c",
  100. `a\x1bb`: "a\x1bb",
  101. `a\x1b`: "a\x1b",
  102. `a\x1`: "a\x01",
  103. `a\x1\\`: "a\x01\\",
  104. `a\x1g`: "a\x01g",
  105. `a\z\"`: "a\\z\"",
  106. `a\123b`: "a\123b",
  107. `a\128b`: "a\0128b",
  108. `a\u1234e`: "a\u1234e",
  109. `a\U1f1eez`: "a\U0001f1eez",
  110. }
  111. for q, expected := range m {
  112. actual := ExpandANSICEscapes(q)
  113. if diff := cmp.Diff(expected, actual); diff != "" {
  114. t.Fatalf("Failed to process: %#v\n%s", q, diff)
  115. }
  116. }
  117. }