-
Notifications
You must be signed in to change notification settings - Fork 2
/
readline_test.go
135 lines (123 loc) · 3.87 KB
/
readline_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
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
package specfile
import (
"fmt"
"io"
"math/rand"
"reflect"
"strings"
"testing"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func TestReadFull(t *testing.T) {
str := RandStringBytes(255)
var line Line
b, err := read(strings.NewReader(str), &line)
if !reflect.DeepEqual(b, []byte(str)) || err != nil || line.Offset != 255 {
t.Error("[readline]read full buf test failed")
}
}
func TestReadPartial(t *testing.T) {
str := RandStringBytes(100)
var line Line
b, err := read(strings.NewReader(str), &line)
if !reflect.DeepEqual(b, []byte(str)) || err != io.EOF || line.Offset != 100 {
t.Error("[readline]read partial buf test failed")
}
}
func TestReadFullWithBreak(t *testing.T) {
str := RandStringBytes(254)
str += "\n"
var line Line
b, err := read(strings.NewReader(str), &line)
if !reflect.DeepEqual(b, []byte(str)) || err != nil || line.Offset != 255 {
t.Error("[readline]read full buf test failed")
}
}
func TestReadLine(t *testing.T) {
str := RandStringBytes(10)
var line Line
var c Counter
err := readLine(strings.NewReader(str), &line, &c)
if err != io.EOF || line.Last != str || line.Len != 1 || !c.Valid() {
t.Error("[readline]readLine with single line test failed")
}
}
func TestReadLineWithMultipleLines(t *testing.T) {
str := RandStringBytes(10)
str += "\\\n"
str1 := RandStringBytes(10)
var line Line
var c Counter
err := readLine(strings.NewReader(str+str1), &line, &c)
if err != io.EOF || !reflect.DeepEqual(line.Lines, []string{str, str1}) || line.Last != str1 || line.Len != 2 || !c.Valid() {
t.Error("[readline]readLine with multple lines test failed")
}
}
func TestReadConditionalLine(t *testing.T) {
str := "%if 0%{?suse_version} > 1550\n" // 29
str += RandStringBytes(10)
str += "\n%endif\n"
line := NewLine(29, "%if 0%{?suse_version} > 1550\n")
var c Counter
err := readConditionalLine(strings.NewReader(str), &line, &c, 0)
if err != nil || line.Last != "%endif\n" || line.Len != 3 {
t.Error("[readline]readConditionalLine test failed")
}
}
func TestReadConditionalLineWithNoBreak(t *testing.T) {
str := "%if 0%{?suse_version} > 1550\n" // 29
str += RandStringBytes(10)
str += "\n%endif"
line := NewLine(29, "%if 0%{?suse_version} > 1550\n")
var c Counter
err := readConditionalLine(strings.NewReader(str), &line, &c, 0)
if err != io.EOF || line.Last != "%endif" || line.Len != 3 {
t.Error("[readline]readConditionalLine test failed")
}
}
func TestWalkFileWithContinue(t *testing.T) {
var str string
test := "this is the first line\nthis is the second line\nthis is the last line"
err := walkFile(strings.NewReader(test), false,
func(rd io.ReaderAt, line *Line) (error, int64) {
str1 := strings.Join(line.Lines, "")
str += str1
if strings.Contains(str1, "last") {
return fmt.Errorf("this is a test error"), line.Offset
}
return nil, line.Offset
})
if err != nil {
t.Errorf("[readline]walkFile should return nil but the error is %s", err)
}
if str != test {
t.Errorf("[readline]walkFile failed, expected read content %s, got %s", test, str)
}
}
func TestWalkFileWithBreak(t *testing.T) {
var str string
test := "this is the first line\nthis is the second line\nthis is the last line"
result := "this is the first line\nthis is the second line\n"
err := walkFile(strings.NewReader(test), true,
func(rd io.ReaderAt, line *Line) (error, int64) {
str1 := strings.Join(line.Lines, "")
if strings.Contains(str1, "last") {
return fmt.Errorf("this is a test error"), line.Offset
}
str += str1
return nil, line.Offset
})
if err != nil {
t.Errorf("[readline]walkFile should return nil but the error is %s", err)
}
if str != result {
t.Errorf("[readline]walkFile failed, expected read content '%s', got '%s'", result, str)
}
}