-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines_test.go
More file actions
589 lines (512 loc) · 12.2 KB
/
Copy pathlines_test.go
File metadata and controls
589 lines (512 loc) · 12.2 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package lines_test
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"regexp"
"strings"
"testing"
"time"
_ "embed"
"mz.attahri.com/code/lines"
)
// memFile implements [io.ReadWriteSeeker]
// using a []byte buffer.
type memFile struct {
buf []byte
offset int64
}
// Truncate implements lines.ReWritable.
func (m *memFile) Truncate(n int64) error {
if n < 0 || n > int64(len(m.buf)) {
return errors.New("n must be 0 < n < len(m.buf)")
}
m.buf = m.buf[:n]
return nil
}
// Read implements [io.Reader].
func (m *memFile) Read(p []byte) (n int, err error) {
size := int64(len(m.buf))
if m.offset >= size {
err = io.EOF
return
}
end := m.offset + int64(len(p))
if end > size {
end = size
}
n = copy(p, m.buf[m.offset:end])
m.offset += int64(n)
if m.offset >= size {
err = io.EOF
}
return
}
// Seek implements [io.Seeker].
func (m *memFile) Seek(offset int64, whence int) (int64, error) {
size := int64(len(m.buf))
switch whence {
case io.SeekEnd:
m.offset = size - offset
case io.SeekCurrent:
m.offset += offset
case io.SeekStart:
m.offset = offset
default:
return 0, fmt.Errorf("invalid whence value: %v", whence)
}
if m.offset < 0 {
m.offset = 0
} else if m.offset > size {
m.offset = size
}
return m.offset, nil
}
// Write implements [io.Writer].
func (m *memFile) Write(p []byte) (n int, err error) {
if l, c := int64(len(m.buf)), (m.offset)+int64(len(p)); c > l {
m.buf = append(m.buf, make([]byte, c-l)...)
}
var (
a = m.offset
b = m.offset + int64(len(p))
)
n = copy(m.buf[a:b], p)
if n != len(p) {
err = io.ErrShortWrite
return
}
m.offset += int64(n)
return
}
func newMemFile(src []byte) *memFile {
b := make([]byte, len(src))
copy(b, src)
return &memFile{buf: b}
}
//go:embed test/file.txt
var fixtureRO []byte
//go:embed test/file.txt
var fixtureRW []byte
var fixtureLineCount = bytes.Count(fixtureRO, []byte("\n")) + 1
func TestContains(t *testing.T) {
tests := map[string]bool{
"GSV0U2DXCV9VLI3D,2009-09-05,Joellen,Lam,kristyn-sellers-reinhart612@unnecessary.com": true,
"something": false,
"else": false,
"7K8Y6XHA93Z7R1EV,2008-11-30,Glen,Kolb,kelli25969@yahoo.com": true,
}
for str, wanted := range tests {
got, err := lines.Contains(lines.All(bytes.NewBuffer(fixtureRO)), str)
if err != nil {
t.Fatal(err)
}
if got != wanted {
t.Fatalf("wanted: %v, got: %v", wanted, got)
}
}
}
func TestCount(t *testing.T) {
count, err := lines.Count(lines.All(bytes.NewBuffer(fixtureRO)), "GSV0U2DXCV9VLI3D,2009-09-05,Joellen,Lam,kristyn-sellers-reinhart612@unnecessary.com")
if err != nil {
t.Fatal(err)
}
const wanted = 1
if count != wanted {
t.Fatalf("wanted: %d, got: %d", wanted, count)
}
}
func TestCountFunc(t *testing.T) {
count, err := lines.CountFunc(lines.All(bytes.NewBuffer(fixtureRO)), func(line string) bool { return true })
if err != nil {
t.Fatal(err)
}
if count != fixtureLineCount {
t.Fatalf("wanted: %d, got: %d", fixtureLineCount, count)
}
}
func TestIndex(t *testing.T) {
tests := map[string]int{
"7K8Y6XHA93Z7R1EV,2008-11-30,Glen,Kolb,kelli25969@yahoo.com": 1,
"GSV0U2DXCV9VLI3D,2009-09-05,Joellen,Lam,kristyn-sellers-reinhart612@unnecessary.com": 22,
"hello": 0,
"world": 0,
}
for str, wanted := range tests {
got, err := lines.Index(lines.All(bytes.NewBuffer(fixtureRO)), str)
if err != nil {
t.Fatal(err)
}
if got != wanted {
t.Fatal("Wanted:", wanted, "Got:", got)
}
}
}
func TestAppend(t *testing.T) {
b := newMemFile(fixtureRW)
count, err := lines.CountFunc(lines.All(b), func(line string) bool { return true })
if err != nil {
t.Fatal(err)
}
if _, err := b.Seek(0, io.SeekEnd); err != nil {
t.Fatal(err)
}
line := time.Now().String()
if _, err := lines.Append(b, line); err != nil {
t.Fatal(err)
}
if _, err := b.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
l, lineno, err := lines.ContainsFunc(lines.All(b), func(a string) bool { return a == line })
if err != nil {
t.Fatal(err)
}
if l != line {
t.Log(l)
t.Log(line)
t.Fatal("expected content to match", l, line)
}
if lineno != (count + 1) {
t.Fatalf("line not appended at the bottom. Wanted: %d, Got: %d", count+1, lineno)
}
}
func TestHead(t *testing.T) {
var head []string
for line, err := range lines.Head(bytes.NewBuffer(fixtureRO), 10) {
if err != nil {
t.Fatal(err)
}
head = append(head, line.String())
}
if len(head) != 10 {
t.Fatal("Line count mismatch")
}
// n <= 0 returns no lines
head = nil
for line, err := range lines.Head(bytes.NewBuffer(fixtureRO), 0) {
if err != nil {
t.Fatal(err)
}
head = append(head, line.String())
}
if len(head) != 0 {
t.Fatal("Expected empty result for n=0")
}
}
func TestTail(t *testing.T) {
all := make([]string, 0, fixtureLineCount)
for line, err := range lines.Backward(bytes.NewReader(fixtureRO)) {
if err != nil {
t.Fatal(err)
}
all = append(all, line.String())
}
var tail []string
for line, err := range lines.Tail(bytes.NewReader(fixtureRO), 10) {
if err != nil {
t.Fatal(err)
}
tail = append(tail, line.String())
}
for i, l := range tail {
if l != all[i] {
t.Fatal("lines are not matching")
}
}
}
// The following example finds lines that
// match a specific regexp rule.
func ExampleContainsFunc() {
rule := regexp.MustCompile(`^[a-z]+\[\d+\]$`)
src := strings.NewReader("")
line, _, err := lines.ContainsFunc(lines.All(src), rule.MatchString)
if err != nil {
log.Fatal(err)
}
log.Printf("Found it: %#v", line)
}
func TestLineRead(t *testing.T) {
content := []byte("hello world")
line := &lines.Line{Content: content, Number: 1}
// Read in chunks
buf := make([]byte, 5)
n, err := line.Read(buf)
if err != nil {
t.Fatal(err)
}
if n != 5 || string(buf) != "hello" {
t.Fatalf("first read: expected 'hello', got %q", buf[:n])
}
n, err = line.Read(buf)
if err != nil {
t.Fatal(err)
}
if n != 5 || string(buf) != " worl" {
t.Fatalf("second read: expected ' worl', got %q", buf[:n])
}
n, err = line.Read(buf)
if err != nil {
t.Fatal(err)
}
if n != 1 || string(buf[:n]) != "d" {
t.Fatalf("third read: expected 'd', got %q", buf[:n])
}
// Next read should return EOF
_, err = line.Read(buf)
if err != io.EOF {
t.Fatalf("expected EOF, got %v", err)
}
}
func TestLineReadAt(t *testing.T) {
content := []byte("hello world")
line := &lines.Line{Content: content, Number: 1}
buf := make([]byte, 5)
// Read from offset 0
n, err := line.ReadAt(buf, 0)
if err != nil {
t.Fatal(err)
}
if n != 5 || string(buf) != "hello" {
t.Fatalf("expected 'hello', got %q", buf[:n])
}
// Read from offset 6 - "world" is exactly 5 bytes, so no EOF
n, err = line.ReadAt(buf, 6)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if string(buf[:n]) != "world" {
t.Fatalf("expected 'world', got %q", buf[:n])
}
// Read from offset 8 - only 3 bytes left, should return EOF
n, err = line.ReadAt(buf, 8)
if err != io.EOF {
t.Fatalf("expected EOF for short read, got %v", err)
}
if n != 3 || string(buf[:n]) != "rld" {
t.Fatalf("expected 'rld', got %q", buf[:n])
}
// Read past end
_, err = line.ReadAt(buf, 100)
if err != io.EOF {
t.Fatalf("expected EOF, got %v", err)
}
}
func TestLineWriteTo(t *testing.T) {
content := []byte("hello world")
line := &lines.Line{Content: content, Number: 1}
var buf bytes.Buffer
n, err := line.WriteTo(&buf)
if err != nil {
t.Fatal(err)
}
if n != int64(len(content)) {
t.Fatalf("expected %d bytes written, got %d", len(content), n)
}
if buf.String() != "hello world" {
t.Fatalf("expected 'hello world', got %q", buf.String())
}
}
func TestEmptyFile(t *testing.T) {
// Empty input
r := bytes.NewReader([]byte{})
count := 0
for _, err := range lines.All(r) {
if err != nil {
t.Fatal(err)
}
count++
}
if count != 0 {
t.Fatalf("expected 0 lines from empty file, got %d", count)
}
// Backward on empty input
r.Reset([]byte{})
count = 0
for _, err := range lines.Backward(r) {
if err != nil {
t.Fatal(err)
}
count++
}
if count != 0 {
t.Fatalf("expected 0 lines from empty file (backward), got %d", count)
}
// Contains on empty input
r.Reset([]byte{})
found, err := lines.Contains(lines.All(r), "anything")
if err != nil {
t.Fatal(err)
}
if found {
t.Fatal("expected not found in empty file")
}
}
func TestSingleLine(t *testing.T) {
// Single line without newline
data := []byte("hello world")
r := bytes.NewReader(data)
var collected []string
for line, err := range lines.All(r) {
if err != nil {
t.Fatal(err)
}
collected = append(collected, line.String())
if line.Number != 1 {
t.Fatalf("expected line number 1, got %d", line.Number)
}
}
if len(collected) != 1 {
t.Fatalf("expected 1 line, got %d", len(collected))
}
if collected[0] != "hello world" {
t.Fatalf("expected 'hello world', got %q", collected[0])
}
// Single line with newline
data = []byte("hello world\n")
r.Reset(data)
collected = nil
for line, err := range lines.All(r) {
if err != nil {
t.Fatal(err)
}
collected = append(collected, line.String())
}
if len(collected) != 1 {
t.Fatalf("expected 1 line, got %d", len(collected))
}
// Backward on single line
r.Reset([]byte("single line"))
collected = nil
for line, err := range lines.Backward(r) {
if err != nil {
t.Fatal(err)
}
collected = append(collected, line.String())
}
if len(collected) != 1 {
t.Fatalf("expected 1 line from backward, got %d", len(collected))
}
if collected[0] != "single line" {
t.Fatalf("expected 'single line', got %q", collected[0])
}
}
func TestCRLFLineEndings(t *testing.T) {
data := []byte("line1\r\nline2\r\nline3")
r := bytes.NewReader(data)
var collected []string
for line, err := range lines.All(r) {
if err != nil {
t.Fatal(err)
}
collected = append(collected, line.String())
}
if len(collected) != 3 {
t.Fatalf("expected 3 lines, got %d", len(collected))
}
expected := []string{"line1", "line2", "line3"}
for i, exp := range expected {
if collected[i] != exp {
t.Fatalf("line %d: expected %q, got %q", i+1, exp, collected[i])
}
}
}
func TestHeadMoreThanAvailable(t *testing.T) {
data := []byte("line1\nline2\nline3")
r := bytes.NewReader(data)
count := 0
for _, err := range lines.Head(r, 100) {
if err != nil {
t.Fatal(err)
}
count++
}
if count != 3 {
t.Fatalf("expected 3 lines when requesting 100 from 3-line file, got %d", count)
}
}
func TestTailMoreThanAvailable(t *testing.T) {
data := []byte("line1\nline2\nline3")
r := bytes.NewReader(data)
count := 0
for _, err := range lines.Tail(r, 100) {
if err != nil {
t.Fatal(err)
}
count++
}
if count != 3 {
t.Fatalf("expected 3 lines when requesting 100 from 3-line file, got %d", count)
}
}
func TestGetOutOfRange(t *testing.T) {
data := []byte("line1\nline2\nline3")
r := bytes.NewReader(data)
// Line 0 should return nil
line, err := lines.Get(lines.All(r), 0)
if err != nil {
t.Fatal(err)
}
if line != nil {
t.Fatal("expected nil for line 0")
}
// Line beyond range
r.Reset(data)
line, err = lines.Get(lines.All(r), 100)
if err != nil {
t.Fatal(err)
}
if line != nil {
t.Fatal("expected nil for line 100")
}
}
func TestLineEqual(t *testing.T) {
l1 := &lines.Line{Content: []byte("hello"), Number: 1}
l2 := &lines.Line{Content: []byte("hello"), Number: 1}
l3 := &lines.Line{Content: []byte("hello"), Number: 2}
l4 := &lines.Line{Content: []byte("world"), Number: 1}
if !l1.Equal(l2) {
t.Fatal("expected l1 == l2")
}
if l1.Equal(l3) {
t.Fatal("expected l1 != l3 (different number)")
}
if l1.Equal(l4) {
t.Fatal("expected l1 != l4 (different content)")
}
}
func TestJoin(t *testing.T) {
result := lines.Join("a", "b", "c")
if result != "a\nb\nc" {
t.Fatalf("expected 'a\\nb\\nc', got %q", result)
}
// Empty join
result = lines.Join[string]()
if result != "" {
t.Fatalf("expected empty string, got %q", result)
}
// Single element
result = lines.Join("single")
if result != "single" {
t.Fatalf("expected 'single', got %q", result)
}
}
func TestFilterWithNoMatches(t *testing.T) {
data := []byte("line1\nline2\nline3")
r := bytes.NewReader(data)
count := 0
for _, err := range lines.Filter(lines.All(r), "nonexistent") {
if err != nil {
t.Fatal(err)
}
count++
}
if count != 0 {
t.Fatalf("expected 0 matches, got %d", count)
}
}
func TestMain(m *testing.M) {
m.Run()
}