-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeker_test.go
71 lines (63 loc) · 1.62 KB
/
seeker_test.go
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
// Copyright 2019 Yegor Myskin. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tailor
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestNewLineFinder(t *testing.T) {
var tests = []struct {
content string
offsetFromStart int64
res string
}{
{"", 0, ""},
{"\n", 0, "\n"},
{"\n\n", 1, "\n"},
{"\n\na\n", 3, "a\n"},
{"a", 0, "a"},
{"a\n", 0, "a\n"},
{"abc", 2, "abc"},
{"abc\n", 2, "abc\n"},
{"a\nb", 2, "b"},
{"a\nb\n", 2, "b\n"},
{"aaaaa\nbbbbbbbb\n", 4, "aaaaa\n"},
{"aaaaa\nbbbbbbbb\n", 10, "bbbbbbbb\n"},
{strings.Repeat("a", 300), 280, strings.Repeat("a", 300)},
{strings.Repeat("a", 300) + "\n", 280, strings.Repeat("a", 300) + "\n"},
{strings.Repeat("a", 100) + "\n" + strings.Repeat("a", 200), 280, strings.Repeat("a", 200)},
}
const file = "./tst"
defer os.Remove(file)
for i, data := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
err := ioutil.WriteFile(file, []byte(data.content), os.ModePerm)
if err != nil {
t.Error(err)
return
}
f := New(file)
err = f.openFile(data.offsetFromStart, io.SeekStart)
if err != nil {
t.Errorf("[%d] error executing: %s, data: %+v", i, err, data)
return
}
r := bufio.NewReader(f.file)
line, err := r.ReadString('\n')
if err != nil && err != io.EOF {
t.Errorf("[%d] error reading line: %s, data: %+v", i, err, data)
return
}
if line != data.res {
t.Errorf("[%d] actual: '%s', want: '%s', data: %+v", i, line, data.res, data)
return
}
})
}
}