-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations_test.go
More file actions
327 lines (307 loc) · 6.58 KB
/
operations_test.go
File metadata and controls
327 lines (307 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package fmt
import "testing"
func TestLastIndex(t *testing.T) {
tests := []struct {
name string
s string
substr string
want int
}{
// Casos básicos
{
name: "substring found at end",
s: "hello world",
substr: "world",
want: 6,
},
{
name: "substring found at beginning",
s: "hello world",
substr: "hello",
want: 0,
},
{
name: "substring found in middle",
s: "hello world hello",
substr: "hello",
want: 12, // última ocurrencia
},
{
name: "substring not found",
s: "hello world",
substr: "xyz",
want: -1,
},
{
name: "single character found",
s: "hello",
substr: "l",
want: 3, // última 'l'
},
{
name: "single character at end",
s: "hello",
substr: "o",
want: 4,
},
// Casos edge
{
name: "empty substring",
s: "hello",
substr: "",
want: 5, // len(s)
},
{
name: "empty string with empty substring",
s: "",
substr: "",
want: 0,
},
{
name: "substring longer than string",
s: "hi",
substr: "hello",
want: -1,
},
{
name: "identical strings",
s: "hello",
substr: "hello",
want: 0,
},
{
name: "substring not found in empty string",
s: "",
substr: "hello",
want: -1,
},
// Casos con múltiples ocurrencias
{
name: "multiple occurrences",
s: "abcabcabc",
substr: "abc",
want: 6, // última ocurrencia
},
{
name: "overlapping patterns",
s: "aaaa",
substr: "aa",
want: 2, // última posición donde empieza "aa"
},
{
name: "repeated single character",
s: "aaaa",
substr: "a",
want: 3, // última 'a'
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := LastIndex(tt.s, tt.substr)
if got != tt.want {
t.Errorf("LastIndex(%q, %q) = %d, want %d", tt.s, tt.substr, got, tt.want)
}
})
}
}
// TestLastIndexFileExtensions prueba el caso de uso específico de extracción de extensiones
// como se usa en DocPDF.RegisterImageOptions
func TestLastIndexFileExtensions(t *testing.T) {
tests := []struct {
name string
filename string
want string
wantPos int
hasError bool
}{
{
name: "simple jpg file",
filename: "image.jpg",
want: "jpg",
wantPos: 5,
hasError: false,
},
{
name: "simple png file",
filename: "photo.png",
want: "png",
wantPos: 5,
hasError: false,
},
{
name: "file with multiple dots",
filename: "backup.image.jpg",
want: "jpg",
wantPos: 12,
hasError: false,
},
{
name: "complex filename with version",
filename: "document.v2.backup.pdf",
want: "pdf",
wantPos: 18,
hasError: false,
},
{
name: "path with directories",
filename: "/home/user/docs/file.txt",
want: "txt",
wantPos: 20,
hasError: false,
},
{
name: "windows path",
filename: "C:\\Users\\docs\\image.gif",
want: "gif",
wantPos: 19,
hasError: false,
},
{
name: "file without extension",
filename: "README",
want: "",
wantPos: -1,
hasError: true,
},
{
name: "hidden file with extension",
filename: ".hidden.txt",
want: "txt",
wantPos: 7,
hasError: false,
},
{
name: "multiple extensions common case",
filename: "archive.tar.gz",
want: "gz",
wantPos: 11,
hasError: false,
},
{
name: "url-like filename",
filename: "https://example.com/image.png",
want: "png",
wantPos: 25,
hasError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Simular el comportamiento de DocPDF.RegisterImageOptions
pos := LastIndex(tt.filename, ".")
if tt.hasError {
if pos != -1 {
t.Errorf("Expected no dot found (pos = -1), got pos = %d", pos)
}
return
}
if pos != tt.wantPos {
t.Errorf("LastIndex(%q, \".\") = %d, want %d", tt.filename, pos, tt.wantPos)
return
}
if pos >= 0 {
extension := tt.filename[pos+1:]
if extension != tt.want {
t.Errorf("Extension extracted: %q, want %q", extension, tt.want)
}
}
})
}
}
// TestLastIndexImageTypeDetection simula exactamente el caso de uso de DocPDF
func TestLastIndexImageTypeDetection(t *testing.T) {
// Función que simula el comportamiento de RegisterImageOptions
getImageType := func(fileStr string) (string, error) {
pos := LastIndex(fileStr, ".")
if pos < 0 {
return "", Errf("image file has no extension and no type was specified: %s", fileStr)
}
return fileStr[pos+1:], nil
}
tests := []struct {
name string
filename string
expectedExt string
expectError bool
}{
{
name: "JPEG image",
filename: "photo.jpg",
expectedExt: "jpg",
expectError: false,
},
{
name: "PNG image",
filename: "screenshot.png",
expectedExt: "png",
expectError: false,
},
{
name: "GIF image",
filename: "animation.gif",
expectedExt: "gif",
expectError: false,
},
{
name: "Complex filename",
filename: "user.profile.backup.jpeg",
expectedExt: "jpeg",
expectError: false,
},
{
name: "Path with extension",
filename: "/var/www/images/logo.png",
expectedExt: "png",
expectError: false,
},
{
name: "File without extension should error",
filename: "imagefile",
expectedExt: "",
expectError: true,
},
{
name: "Hidden file with extension",
filename: ".DS_Store.backup.jpg",
expectedExt: "jpg",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ext, err := getImageType(tt.filename)
if tt.expectError {
if err == nil {
t.Errorf("Expected error for filename %q, but got none", tt.filename)
}
return
}
if err != nil {
t.Errorf("Unexpected error for filename %q: %v", tt.filename, err)
return
}
if ext != tt.expectedExt {
t.Errorf("getImageType(%q) = %q, want %q", tt.filename, ext, tt.expectedExt)
}
})
}
}
// Benchmark para comparar performance
func BenchmarkLastIndex(b *testing.B) {
s := "this is a very long string with multiple occurrences of the word test and more test words"
substr := "test"
b.ResetTimer()
for i := 0; i < b.N; i++ {
LastIndex(s, substr)
}
}
func BenchmarkLastIndexFileExtension(b *testing.B) {
filename := "/very/long/path/to/some/deeply/nested/directory/with/a/very/long/filename.extension"
b.ResetTimer()
for i := 0; i < b.N; i++ {
pos := LastIndex(filename, ".")
if pos >= 0 {
_ = filename[pos+1:]
}
}
}