paste_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package fsext
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestParsePastedFiles(t *testing.T) {
  7. t.Run("WindowsTerminal", func(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. input string
  11. expected []string
  12. }{
  13. {
  14. name: "single path",
  15. input: `"C:\path\my-screenshot-one.png"`,
  16. expected: []string{`C:\path\my-screenshot-one.png`},
  17. },
  18. {
  19. name: "multiple paths no spaces",
  20. input: `"C:\path\my-screenshot-one.png" "C:\path\my-screenshot-two.png" "C:\path\my-screenshot-three.png"`,
  21. expected: []string{`C:\path\my-screenshot-one.png`, `C:\path\my-screenshot-two.png`, `C:\path\my-screenshot-three.png`},
  22. },
  23. {
  24. name: "single with spaces",
  25. input: `"C:\path\my screenshot one.png"`,
  26. expected: []string{`C:\path\my screenshot one.png`},
  27. },
  28. {
  29. name: "multiple paths with spaces",
  30. input: `"C:\path\my screenshot one.png" "C:\path\my screenshot two.png" "C:\path\my screenshot three.png"`,
  31. expected: []string{`C:\path\my screenshot one.png`, `C:\path\my screenshot two.png`, `C:\path\my screenshot three.png`},
  32. },
  33. {
  34. name: "empty string",
  35. input: "",
  36. expected: nil,
  37. },
  38. {
  39. name: "unclosed quotes",
  40. input: `"C:\path\file.png`,
  41. expected: nil,
  42. },
  43. {
  44. name: "text outside quotes",
  45. input: `"C:\path\file.png" some random text "C:\path\file2.png"`,
  46. expected: nil,
  47. },
  48. {
  49. name: "multiple spaces between paths",
  50. input: `"C:\path\file1.png" "C:\path\file2.png"`,
  51. expected: []string{`C:\path\file1.png`, `C:\path\file2.png`},
  52. },
  53. {
  54. name: "just whitespace",
  55. input: " ",
  56. expected: nil,
  57. },
  58. {
  59. name: "consecutive quoted sections",
  60. input: `"C:\path1""C:\path2"`,
  61. expected: []string{`C:\path1`, `C:\path2`},
  62. },
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. result := windowsTerminalParsePastedFiles(tt.input)
  67. require.Equal(t, tt.expected, result)
  68. })
  69. }
  70. })
  71. t.Run("Unix", func(t *testing.T) {
  72. tests := []struct {
  73. name string
  74. input string
  75. expected []string
  76. }{
  77. {
  78. name: "single path",
  79. input: `/path/my-screenshot.png`,
  80. expected: []string{"/path/my-screenshot.png"},
  81. },
  82. {
  83. name: "multiple paths no spaces",
  84. input: `/path/screenshot-one.png /path/screenshot-two.png /path/screenshot-three.png`,
  85. expected: []string{"/path/screenshot-one.png", "/path/screenshot-two.png", "/path/screenshot-three.png"},
  86. },
  87. {
  88. name: "sigle with spaces",
  89. input: `/path/my\ screenshot\ one.png`,
  90. expected: []string{"/path/my screenshot one.png"},
  91. },
  92. {
  93. name: "multiple paths with spaces",
  94. input: `/path/my\ screenshot\ one.png /path/my\ screenshot\ two.png /path/my\ screenshot\ three.png`,
  95. expected: []string{"/path/my screenshot one.png", "/path/my screenshot two.png", "/path/my screenshot three.png"},
  96. },
  97. {
  98. name: "empty string",
  99. input: "",
  100. expected: nil,
  101. },
  102. {
  103. name: "double backslash escapes",
  104. input: `/path/my\\file.png`,
  105. expected: []string{"/path/my\\file.png"},
  106. },
  107. {
  108. name: "trailing backslash",
  109. input: `/path/file\`,
  110. expected: []string{`/path/file\`},
  111. },
  112. {
  113. name: "multiple consecutive escaped spaces",
  114. input: `/path/file\ \ with\ \ many\ \ spaces.png`,
  115. expected: []string{"/path/file with many spaces.png"},
  116. },
  117. {
  118. name: "multiple unescaped spaces",
  119. input: `/path/file1.png /path/file2.png`,
  120. expected: []string{"/path/file1.png", "/path/file2.png"},
  121. },
  122. {
  123. name: "just whitespace",
  124. input: " ",
  125. expected: nil,
  126. },
  127. {
  128. name: "tab characters",
  129. input: "/path/file1.png\t/path/file2.png",
  130. expected: []string{"/path/file1.png\t/path/file2.png"},
  131. },
  132. {
  133. name: "newlines in input",
  134. input: "/path/file1.png\n/path/file2.png",
  135. expected: []string{"/path/file1.png\n/path/file2.png"},
  136. },
  137. }
  138. for _, tt := range tests {
  139. t.Run(tt.name, func(t *testing.T) {
  140. result := unixParsePastedFiles(tt.input)
  141. require.Equal(t, tt.expected, result)
  142. })
  143. }
  144. })
  145. }