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