-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdirectory_test.go
72 lines (66 loc) · 1.68 KB
/
directory_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
72
package detect
import (
"bufio"
"bytes"
"io"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_readUntilSafeBoundary(t *testing.T) {
// Arrange
cases := []struct {
name string
r io.Reader
expected string
}{
// Current split is fine, exit early.
{
name: "safe original split - LF",
r: strings.NewReader("abc\n\ndefghijklmnop\n\nqrstuvwxyz"),
expected: "abc\n\n",
},
{
name: "safe original split - CRLF",
r: strings.NewReader("a\r\n\r\nbcdefghijklmnop\n"),
expected: "a\r\n\r\n",
},
// Current split is bad, look for a better one.
{
name: "safe split - LF",
r: strings.NewReader("abcdefg\nhijklmnop\n\nqrstuvwxyz"),
expected: "abcdefg\nhijklmnop\n\n",
},
{
name: "safe split - CRLF",
r: strings.NewReader("abcdefg\r\nhijklmnop\r\n\r\nqrstuvwxyz"),
expected: "abcdefg\r\nhijklmnop\r\n\r\n",
},
{
name: "safe split - blank line",
r: strings.NewReader("abcdefg\nhijklmnop\n\t \t\nqrstuvwxyz"),
expected: "abcdefg\nhijklmnop\n\t \t\n",
},
// Current split is bad, exhaust options.
{
name: "no safe split",
r: strings.NewReader("abcdefg\nhijklmnopqrstuvwxyz"),
expected: "abcdefg\nhijklmnopqrstuvwx",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
buf := make([]byte, 5)
n, err := c.r.Read(buf)
require.NoError(t, err)
// Act
reader := bufio.NewReader(c.r)
peekBuf := bytes.NewBuffer(buf[:n])
err = readUntilSafeBoundary(reader, n, 20, peekBuf)
require.NoError(t, err)
// Assert
t.Logf(peekBuf.String())
require.Equal(t, c.expected, string(peekBuf.Bytes()))
})
}
}