-
Notifications
You must be signed in to change notification settings - Fork 35
/
scanner_test.go
77 lines (67 loc) · 1.46 KB
/
scanner_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
73
74
75
76
77
package gotenv
import (
"bufio"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestScanner(t *testing.T) {
type testCase struct {
name string
in string
exp []string
}
testCases := []testCase{
{
"regular LF split with trailing LF",
"aa\nbb\ncc\n",
[]string{"aa", "bb", "cc", ""},
},
{
"regular LF split with no trailing LF",
"aa\nbb\ncc",
[]string{"aa", "bb", "cc"},
},
{
"regular CR split with trailing CR",
"aa\rbb\rcc\r",
[]string{"aa", "bb", "cc", ""},
},
{
"regular CR split with no trailing CR",
"aa\rbb\rcc",
[]string{"aa", "bb", "cc"},
},
{
"regular CRLF split with trailing CRLF",
"aa\r\nbb\r\ncc\r\n",
[]string{"aa", "bb", "cc", ""},
},
{
"regular CRLF split with no trailing CRLF",
"aa\r\nbb\r\ncc",
[]string{"aa", "bb", "cc"},
},
{
"mix of possible line endings",
"aa\r\nbb\ncc\rdd",
[]string{"aa", "bb", "cc", "dd"},
},
}
for _, tc := range testCases {
s := bufio.NewScanner(strings.NewReader(tc.in))
s.Split(splitLines)
i := 0
for s.Scan() {
if i >= len(tc.exp) {
assert.Fail(t, "unexpected line", "testCase: %s - got extra line: %q", tc.name, s.Text())
} else {
got := s.Text()
assert.Equal(t, tc.exp[i], got, "testCase: %s - line %d", tc.name, i)
}
i++
}
assert.NoError(t, s.Err(), "testCase: %s", tc.name)
assert.Equal(t, len(tc.exp), i, "testCase: %s - expected to have the correct line count", tc.name)
}
}